xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision 4d154b1ca580fd724f384a7042a5f24611809476)
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 */
20765f571c61SHawking Zhang 	if (gc_ver == IP_VERSION(9, 4, 3) ||
20775f571c61SHawking Zhang 	    gc_ver == IP_VERSION(9, 4, 4)) {
207898a936c3SYang Wang 		if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
207998a936c3SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
208098a936c3SYang Wang 		return 0;
208198a936c3SYang Wang 	}
208298a936c3SYang Wang 
208398a936c3SYang Wang 	if (!(attr->flags & mask))
208498a936c3SYang Wang 		*states = ATTR_STATE_UNSUPPORTED;
208598a936c3SYang Wang 
208698a936c3SYang Wang 	return 0;
208798a936c3SYang Wang }
208898a936c3SYang Wang 
2089190145f6SYang Wang static int pp_dpm_dcefclk_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2090190145f6SYang Wang 				      uint32_t mask, enum amdgpu_device_attr_states *states)
2091190145f6SYang Wang {
2092190145f6SYang Wang 	struct device_attribute *dev_attr = &attr->dev_attr;
2093190145f6SYang Wang 	uint32_t gc_ver;
2094190145f6SYang Wang 
2095190145f6SYang Wang 	*states = ATTR_STATE_SUPPORTED;
2096190145f6SYang Wang 
2097190145f6SYang Wang 	if (!(attr->flags & mask)) {
2098190145f6SYang Wang 		*states = ATTR_STATE_UNSUPPORTED;
2099190145f6SYang Wang 		return 0;
2100190145f6SYang Wang 	}
2101190145f6SYang Wang 
2102190145f6SYang Wang 	gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2103190145f6SYang Wang 	/* dcefclk node is not available on gfx 11.0.3 sriov */
2104190145f6SYang Wang 	if ((gc_ver == IP_VERSION(11, 0, 3) && amdgpu_sriov_is_pp_one_vf(adev)) ||
2105190145f6SYang Wang 	    gc_ver < IP_VERSION(9, 0, 0) ||
2106190145f6SYang Wang 	    !amdgpu_device_has_display_hardware(adev))
2107190145f6SYang Wang 		*states = ATTR_STATE_UNSUPPORTED;
2108190145f6SYang Wang 
2109190145f6SYang Wang 	/* SMU MP1 does not support dcefclk level setting,
2110190145f6SYang Wang 	 * setting should not be allowed from VF if not in one VF mode.
2111190145f6SYang Wang 	 */
2112190145f6SYang Wang 	if (gc_ver >= IP_VERSION(10, 0, 0) ||
2113190145f6SYang Wang 	    (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))) {
2114190145f6SYang Wang 		dev_attr->attr.mode &= ~S_IWUGO;
2115190145f6SYang Wang 		dev_attr->store = NULL;
2116190145f6SYang Wang 	}
2117190145f6SYang Wang 
2118190145f6SYang Wang 	return 0;
2119190145f6SYang Wang }
2120190145f6SYang Wang 
2121166a3c73SYang Wang static int pp_dpm_clk_default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2122166a3c73SYang Wang 					  uint32_t mask, enum amdgpu_device_attr_states *states)
2123166a3c73SYang Wang {
2124166a3c73SYang Wang 	struct device_attribute *dev_attr = &attr->dev_attr;
2125166a3c73SYang Wang 	enum amdgpu_device_attr_id attr_id = attr->attr_id;
2126166a3c73SYang Wang 	uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
2127166a3c73SYang Wang 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2128166a3c73SYang Wang 
2129166a3c73SYang Wang 	*states = ATTR_STATE_SUPPORTED;
2130166a3c73SYang Wang 
2131166a3c73SYang Wang 	if (!(attr->flags & mask)) {
2132166a3c73SYang Wang 		*states = ATTR_STATE_UNSUPPORTED;
2133166a3c73SYang Wang 		return 0;
2134166a3c73SYang Wang 	}
2135166a3c73SYang Wang 
2136166a3c73SYang Wang 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2137166a3c73SYang Wang 		if (gc_ver < IP_VERSION(9, 0, 0))
2138166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2139166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2140166a3c73SYang Wang 		if (mp1_ver < IP_VERSION(10, 0, 0))
2141166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2142166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
2143166a3c73SYang Wang 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2144166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 3) ||
2145166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 6) ||
2146166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 7) ||
2147166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 0) ||
2148166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 1, 2) ||
2149166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 0) ||
2150166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 1) ||
2151166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 4) ||
2152166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 5, 0) ||
2153166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 2) ||
2154166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 3) ||
21555f571c61SHawking Zhang 		      gc_ver == IP_VERSION(9, 4, 3) ||
21565f571c61SHawking Zhang 		      gc_ver == IP_VERSION(9, 4, 4)))
2157166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2158166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
2159166a3c73SYang Wang 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2160166a3c73SYang Wang 		       gc_ver == IP_VERSION(10, 3, 0) ||
2161166a3c73SYang Wang 		       gc_ver == IP_VERSION(11, 0, 2) ||
2162166a3c73SYang Wang 		       gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2163166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2164166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
2165166a3c73SYang Wang 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2166166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 3) ||
2167166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 6) ||
2168166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 7) ||
2169166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 3, 0) ||
2170166a3c73SYang Wang 		      gc_ver == IP_VERSION(10, 1, 2) ||
2171166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 0) ||
2172166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 1) ||
2173166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 4) ||
2174166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 5, 0) ||
2175166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 2) ||
2176166a3c73SYang Wang 		      gc_ver == IP_VERSION(11, 0, 3) ||
21775f571c61SHawking Zhang 		      gc_ver == IP_VERSION(9, 4, 3) ||
21785f571c61SHawking Zhang 		      gc_ver == IP_VERSION(9, 4, 4)))
2179166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2180166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
2181166a3c73SYang Wang 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2182166a3c73SYang Wang 		       gc_ver == IP_VERSION(10, 3, 0) ||
2183166a3c73SYang Wang 		       gc_ver == IP_VERSION(11, 0, 2) ||
2184166a3c73SYang Wang 		       gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2185166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2186166a3c73SYang Wang 	} else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
2187166a3c73SYang Wang 		if (gc_ver == IP_VERSION(9, 4, 2) ||
21885f571c61SHawking Zhang 		    gc_ver == IP_VERSION(9, 4, 3) ||
21895f571c61SHawking Zhang 		    gc_ver == IP_VERSION(9, 4, 4))
2190166a3c73SYang Wang 			*states = ATTR_STATE_UNSUPPORTED;
2191166a3c73SYang Wang 	}
2192166a3c73SYang Wang 
2193166a3c73SYang Wang 	switch (gc_ver) {
2194166a3c73SYang Wang 	case IP_VERSION(9, 4, 1):
2195166a3c73SYang Wang 	case IP_VERSION(9, 4, 2):
2196166a3c73SYang Wang 		/* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2197166a3c73SYang Wang 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2198166a3c73SYang Wang 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2199166a3c73SYang Wang 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2200166a3c73SYang Wang 			dev_attr->attr.mode &= ~S_IWUGO;
2201166a3c73SYang Wang 			dev_attr->store = NULL;
2202166a3c73SYang Wang 		}
2203166a3c73SYang Wang 		break;
2204166a3c73SYang Wang 	default:
2205166a3c73SYang Wang 		break;
2206166a3c73SYang Wang 	}
2207166a3c73SYang Wang 
2208166a3c73SYang Wang 	/* setting should not be allowed from VF if not in one VF mode */
2209166a3c73SYang Wang 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_is_pp_one_vf(adev)) {
2210166a3c73SYang Wang 		dev_attr->attr.mode &= ~S_IWUGO;
2211166a3c73SYang Wang 		dev_attr->store = NULL;
2212166a3c73SYang Wang 	}
2213166a3c73SYang Wang 
2214166a3c73SYang Wang 	return 0;
2215166a3c73SYang Wang }
2216166a3c73SYang Wang 
221721e43386SLe Ma /* Following items will be read out to indicate current plpd policy:
221821e43386SLe Ma  *  - -1: none
221921e43386SLe Ma  *  - 0: disallow
222021e43386SLe Ma  *  - 1: default
222121e43386SLe Ma  *  - 2: optimized
222221e43386SLe Ma  */
222321e43386SLe Ma static ssize_t amdgpu_get_xgmi_plpd_policy(struct device *dev,
222421e43386SLe Ma 					   struct device_attribute *attr,
222521e43386SLe Ma 					   char *buf)
222621e43386SLe Ma {
222721e43386SLe Ma 	struct drm_device *ddev = dev_get_drvdata(dev);
222821e43386SLe Ma 	struct amdgpu_device *adev = drm_to_adev(ddev);
222921e43386SLe Ma 	char *mode_desc = "none";
223021e43386SLe Ma 	int mode;
223121e43386SLe Ma 
223221e43386SLe Ma 	if (amdgpu_in_reset(adev))
223321e43386SLe Ma 		return -EPERM;
223421e43386SLe Ma 	if (adev->in_suspend && !adev->in_runpm)
223521e43386SLe Ma 		return -EPERM;
223621e43386SLe Ma 
223721e43386SLe Ma 	mode = amdgpu_dpm_get_xgmi_plpd_mode(adev, &mode_desc);
223821e43386SLe Ma 
223921e43386SLe Ma 	return sysfs_emit(buf, "%d: %s\n", mode, mode_desc);
224021e43386SLe Ma }
224121e43386SLe Ma 
224221e43386SLe Ma /* Following argument value is expected from user to change plpd policy
224321e43386SLe Ma  *  - arg 0: disallow plpd
224421e43386SLe Ma  *  - arg 1: default policy
224521e43386SLe Ma  *  - arg 2: optimized policy
224621e43386SLe Ma  */
224721e43386SLe Ma static ssize_t amdgpu_set_xgmi_plpd_policy(struct device *dev,
224821e43386SLe Ma 					   struct device_attribute *attr,
224921e43386SLe Ma 					   const char *buf, size_t count)
225021e43386SLe Ma {
225121e43386SLe Ma 	struct drm_device *ddev = dev_get_drvdata(dev);
225221e43386SLe Ma 	struct amdgpu_device *adev = drm_to_adev(ddev);
225321e43386SLe Ma 	int mode, ret;
225421e43386SLe Ma 
225521e43386SLe Ma 	if (amdgpu_in_reset(adev))
225621e43386SLe Ma 		return -EPERM;
225721e43386SLe Ma 	if (adev->in_suspend && !adev->in_runpm)
225821e43386SLe Ma 		return -EPERM;
225921e43386SLe Ma 
226021e43386SLe Ma 	ret = kstrtos32(buf, 0, &mode);
226121e43386SLe Ma 	if (ret)
226221e43386SLe Ma 		return -EINVAL;
226321e43386SLe Ma 
226421e43386SLe Ma 	ret = pm_runtime_get_sync(ddev->dev);
226521e43386SLe Ma 	if (ret < 0) {
226621e43386SLe Ma 		pm_runtime_put_autosuspend(ddev->dev);
226721e43386SLe Ma 		return ret;
226821e43386SLe Ma 	}
226921e43386SLe Ma 
227021e43386SLe Ma 	ret = amdgpu_dpm_set_xgmi_plpd_mode(adev, mode);
227121e43386SLe Ma 
227221e43386SLe Ma 	pm_runtime_mark_last_busy(ddev->dev);
227321e43386SLe Ma 	pm_runtime_put_autosuspend(ddev->dev);
227421e43386SLe Ma 
227521e43386SLe Ma 	if (ret)
227621e43386SLe Ma 		return ret;
227721e43386SLe Ma 
227821e43386SLe Ma 	return count;
227921e43386SLe Ma }
228021e43386SLe Ma 
2281*4d154b1cSLijo Lazar /* pm policy attributes */
2282*4d154b1cSLijo Lazar struct amdgpu_pm_policy_attr {
2283*4d154b1cSLijo Lazar 	struct device_attribute dev_attr;
2284*4d154b1cSLijo Lazar 	enum pp_pm_policy id;
2285*4d154b1cSLijo Lazar };
2286*4d154b1cSLijo Lazar 
2287*4d154b1cSLijo Lazar static ssize_t amdgpu_get_pm_policy_attr(struct device *dev,
2288*4d154b1cSLijo Lazar 					 struct device_attribute *attr,
2289*4d154b1cSLijo Lazar 					 char *buf)
2290*4d154b1cSLijo Lazar {
2291*4d154b1cSLijo Lazar 	struct drm_device *ddev = dev_get_drvdata(dev);
2292*4d154b1cSLijo Lazar 	struct amdgpu_device *adev = drm_to_adev(ddev);
2293*4d154b1cSLijo Lazar 	struct amdgpu_pm_policy_attr *policy_attr;
2294*4d154b1cSLijo Lazar 
2295*4d154b1cSLijo Lazar 	policy_attr =
2296*4d154b1cSLijo Lazar 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
2297*4d154b1cSLijo Lazar 
2298*4d154b1cSLijo Lazar 	if (amdgpu_in_reset(adev))
2299*4d154b1cSLijo Lazar 		return -EPERM;
2300*4d154b1cSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
2301*4d154b1cSLijo Lazar 		return -EPERM;
2302*4d154b1cSLijo Lazar 
2303*4d154b1cSLijo Lazar 	return amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, buf);
2304*4d154b1cSLijo Lazar }
2305*4d154b1cSLijo Lazar 
2306*4d154b1cSLijo Lazar static ssize_t amdgpu_set_pm_policy_attr(struct device *dev,
2307*4d154b1cSLijo Lazar 					 struct device_attribute *attr,
2308*4d154b1cSLijo Lazar 					 const char *buf, size_t count)
2309*4d154b1cSLijo Lazar {
2310*4d154b1cSLijo Lazar 	struct drm_device *ddev = dev_get_drvdata(dev);
2311*4d154b1cSLijo Lazar 	struct amdgpu_device *adev = drm_to_adev(ddev);
2312*4d154b1cSLijo Lazar 	struct amdgpu_pm_policy_attr *policy_attr;
2313*4d154b1cSLijo Lazar 	int ret, num_params = 0;
2314*4d154b1cSLijo Lazar 	char delimiter[] = " \n\t";
2315*4d154b1cSLijo Lazar 	char tmp_buf[128];
2316*4d154b1cSLijo Lazar 	char *tmp, *param;
2317*4d154b1cSLijo Lazar 	long val;
2318*4d154b1cSLijo Lazar 
2319*4d154b1cSLijo Lazar 	if (amdgpu_in_reset(adev))
2320*4d154b1cSLijo Lazar 		return -EPERM;
2321*4d154b1cSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
2322*4d154b1cSLijo Lazar 		return -EPERM;
2323*4d154b1cSLijo Lazar 
2324*4d154b1cSLijo Lazar 	count = min(count, sizeof(tmp_buf));
2325*4d154b1cSLijo Lazar 	memcpy(tmp_buf, buf, count);
2326*4d154b1cSLijo Lazar 	tmp_buf[count - 1] = '\0';
2327*4d154b1cSLijo Lazar 	tmp = tmp_buf;
2328*4d154b1cSLijo Lazar 
2329*4d154b1cSLijo Lazar 	tmp = skip_spaces(tmp);
2330*4d154b1cSLijo Lazar 	while ((param = strsep(&tmp, delimiter))) {
2331*4d154b1cSLijo Lazar 		if (!strlen(param)) {
2332*4d154b1cSLijo Lazar 			tmp = skip_spaces(tmp);
2333*4d154b1cSLijo Lazar 			continue;
2334*4d154b1cSLijo Lazar 		}
2335*4d154b1cSLijo Lazar 		ret = kstrtol(param, 0, &val);
2336*4d154b1cSLijo Lazar 		if (ret)
2337*4d154b1cSLijo Lazar 			return -EINVAL;
2338*4d154b1cSLijo Lazar 		num_params++;
2339*4d154b1cSLijo Lazar 		if (num_params > 1)
2340*4d154b1cSLijo Lazar 			return -EINVAL;
2341*4d154b1cSLijo Lazar 	}
2342*4d154b1cSLijo Lazar 
2343*4d154b1cSLijo Lazar 	if (num_params != 1)
2344*4d154b1cSLijo Lazar 		return -EINVAL;
2345*4d154b1cSLijo Lazar 
2346*4d154b1cSLijo Lazar 	policy_attr =
2347*4d154b1cSLijo Lazar 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
2348*4d154b1cSLijo Lazar 
2349*4d154b1cSLijo Lazar 	ret = pm_runtime_get_sync(ddev->dev);
2350*4d154b1cSLijo Lazar 	if (ret < 0) {
2351*4d154b1cSLijo Lazar 		pm_runtime_put_autosuspend(ddev->dev);
2352*4d154b1cSLijo Lazar 		return ret;
2353*4d154b1cSLijo Lazar 	}
2354*4d154b1cSLijo Lazar 
2355*4d154b1cSLijo Lazar 	ret = amdgpu_dpm_set_pm_policy(adev, policy_attr->id, val);
2356*4d154b1cSLijo Lazar 
2357*4d154b1cSLijo Lazar 	pm_runtime_mark_last_busy(ddev->dev);
2358*4d154b1cSLijo Lazar 	pm_runtime_put_autosuspend(ddev->dev);
2359*4d154b1cSLijo Lazar 
2360*4d154b1cSLijo Lazar 	if (ret)
2361*4d154b1cSLijo Lazar 		return ret;
2362*4d154b1cSLijo Lazar 
2363*4d154b1cSLijo Lazar 	return count;
2364*4d154b1cSLijo Lazar }
2365*4d154b1cSLijo Lazar 
2366*4d154b1cSLijo Lazar #define AMDGPU_PM_POLICY_ATTR(_name, _id)                                  \
2367*4d154b1cSLijo Lazar 	static struct amdgpu_pm_policy_attr pm_policy_attr_##_name = {     \
2368*4d154b1cSLijo Lazar 		.dev_attr = __ATTR(_name, 0644, amdgpu_get_pm_policy_attr, \
2369*4d154b1cSLijo Lazar 				   amdgpu_set_pm_policy_attr),             \
2370*4d154b1cSLijo Lazar 		.id = PP_PM_POLICY_##_id,                                  \
2371*4d154b1cSLijo Lazar 	};
2372*4d154b1cSLijo Lazar 
2373*4d154b1cSLijo Lazar #define AMDGPU_PM_POLICY_ATTR_VAR(_name) pm_policy_attr_##_name.dev_attr.attr
2374*4d154b1cSLijo Lazar 
2375*4d154b1cSLijo Lazar AMDGPU_PM_POLICY_ATTR(soc_pstate, SOC_PSTATE)
2376*4d154b1cSLijo Lazar 
2377*4d154b1cSLijo Lazar static struct attribute *pm_policy_attrs[] = {
2378*4d154b1cSLijo Lazar 	&AMDGPU_PM_POLICY_ATTR_VAR(soc_pstate),
2379*4d154b1cSLijo Lazar 	NULL
2380*4d154b1cSLijo Lazar };
2381*4d154b1cSLijo Lazar 
2382*4d154b1cSLijo Lazar static umode_t amdgpu_pm_policy_attr_visible(struct kobject *kobj,
2383*4d154b1cSLijo Lazar 					     struct attribute *attr, int n)
2384*4d154b1cSLijo Lazar {
2385*4d154b1cSLijo Lazar 	struct device *dev = kobj_to_dev(kobj);
2386*4d154b1cSLijo Lazar 	struct drm_device *ddev = dev_get_drvdata(dev);
2387*4d154b1cSLijo Lazar 	struct amdgpu_device *adev = drm_to_adev(ddev);
2388*4d154b1cSLijo Lazar 	struct amdgpu_pm_policy_attr *policy_attr;
2389*4d154b1cSLijo Lazar 
2390*4d154b1cSLijo Lazar 	policy_attr =
2391*4d154b1cSLijo Lazar 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr.attr);
2392*4d154b1cSLijo Lazar 
2393*4d154b1cSLijo Lazar 	if (amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, NULL) ==
2394*4d154b1cSLijo Lazar 	    -ENOENT)
2395*4d154b1cSLijo Lazar 		return 0;
2396*4d154b1cSLijo Lazar 
2397*4d154b1cSLijo Lazar 	return attr->mode;
2398*4d154b1cSLijo Lazar }
2399*4d154b1cSLijo Lazar 
2400*4d154b1cSLijo Lazar const struct attribute_group amdgpu_pm_policy_attr_group = {
2401*4d154b1cSLijo Lazar 	.name = "pm_policy",
2402*4d154b1cSLijo Lazar 	.attrs = pm_policy_attrs,
2403*4d154b1cSLijo Lazar 	.is_visible = amdgpu_pm_policy_attr_visible,
2404*4d154b1cSLijo Lazar };
2405*4d154b1cSLijo Lazar 
2406e098bc96SEvan Quan static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2407e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
24084215a119SHorace Chen 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
24097884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
24107884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
24117884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
24127884d0e9SJiawei Gu 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2413166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2414166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2415166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2416166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2417166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2418166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2419166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2420166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2421166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2422166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2423166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2424166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2425166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2426166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2427166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2428166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2429190145f6SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2430190145f6SYang Wang 			      .attr_update = pp_dpm_dcefclk_attr_update),
2431166a3c73SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2432166a3c73SYang Wang 			      .attr_update = pp_dpm_clk_default_attr_update),
2433e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2434e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2435ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
243698a936c3SYang Wang 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC,
243798a936c3SYang Wang 			      .attr_update = pp_od_clk_voltage_attr_update),
2438ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2439ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2440d1b2703cSXiaojian Du 	AMDGPU_DEVICE_ATTR_RO(vcn_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2441e098bc96SEvan Quan 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2442ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2443ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2444ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2445c3ed0e72SKun Liu 	AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2446ac82902dSVignesh Chander 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2447a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
2448a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
2449a7673a1cSSathishkumar S 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
2450a7673a1cSSathishkumar S 			      .attr_update = ss_power_attr_update),
245130d95a37SSathishkumar S 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
245230d95a37SSathishkumar S 			      .attr_update = ss_bias_attr_update),
245321e43386SLe Ma 	AMDGPU_DEVICE_ATTR_RW(xgmi_plpd_policy,				ATTR_FLAG_BASIC),
2454223aad1bSLijo Lazar 	AMDGPU_DEVICE_ATTR_RO(pm_metrics,				ATTR_FLAG_BASIC,
2455223aad1bSLijo Lazar 			      .attr_update = amdgpu_pm_metrics_attr_update),
2456e098bc96SEvan Quan };
2457e098bc96SEvan Quan 
2458e098bc96SEvan Quan static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2459e098bc96SEvan Quan 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2460e098bc96SEvan Quan {
2461e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
24622ea6f4d9SYang Wang 	enum amdgpu_device_attr_id attr_id = attr->attr_id;
24634e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2464e098bc96SEvan Quan 
2465e098bc96SEvan Quan 	if (!(attr->flags & mask)) {
2466e098bc96SEvan Quan 		*states = ATTR_STATE_UNSUPPORTED;
2467e098bc96SEvan Quan 		return 0;
2468e098bc96SEvan Quan 	}
2469e098bc96SEvan Quan 
2470166a3c73SYang Wang 	if (DEVICE_ATTR_IS(mem_busy_percent)) {
24715df0f0b3SAsad Kamal 		if ((adev->flags & AMD_IS_APU &&
24725df0f0b3SAsad Kamal 		     gc_ver != IP_VERSION(9, 4, 3)) ||
24735df0f0b3SAsad Kamal 		    gc_ver == IP_VERSION(9, 0, 1))
2474e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2475d1b2703cSXiaojian Du 	} else if (DEVICE_ATTR_IS(vcn_busy_percent)) {
2476d1b2703cSXiaojian Du 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2477d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(10, 3, 3) ||
2478d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(10, 3, 6) ||
2479d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(10, 3, 7) ||
2480d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(11, 0, 1) ||
2481d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(11, 0, 4) ||
2482d1b2703cSXiaojian Du 			  gc_ver == IP_VERSION(11, 5, 0)))
2483d1b2703cSXiaojian Du 			*states = ATTR_STATE_UNSUPPORTED;
2484e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2485e098bc96SEvan Quan 		/* PCIe Perf counters won't work on APU nodes */
24865fe4a8d3SAsad Kamal 		if (adev->flags & AMD_IS_APU ||
24875fe4a8d3SAsad Kamal 		    !adev->asic_funcs->get_pcie_usage)
2488e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2489e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(unique_id)) {
249060044748SKent Russell 		switch (gc_ver) {
249160044748SKent Russell 		case IP_VERSION(9, 0, 1):
249260044748SKent Russell 		case IP_VERSION(9, 4, 0):
249360044748SKent Russell 		case IP_VERSION(9, 4, 1):
249460044748SKent Russell 		case IP_VERSION(9, 4, 2):
2495baf65745SLijo Lazar 		case IP_VERSION(9, 4, 3):
24965f571c61SHawking Zhang 		case IP_VERSION(9, 4, 4):
2497ebd9c071SKent Russell 		case IP_VERSION(10, 3, 0):
2498276c03a0SEvan Quan 		case IP_VERSION(11, 0, 0):
249935e67ca6SKent Russell 		case IP_VERSION(11, 0, 1):
250035e67ca6SKent Russell 		case IP_VERSION(11, 0, 2):
2501d82758adSKenneth Feng 		case IP_VERSION(11, 0, 3):
250260044748SKent Russell 			*states = ATTR_STATE_SUPPORTED;
250360044748SKent Russell 			break;
250460044748SKent Russell 		default:
2505e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
250660044748SKent Russell 		}
2507e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(pp_features)) {
2508fc8e84a2SLijo Lazar 		if ((adev->flags & AMD_IS_APU &&
2509fc8e84a2SLijo Lazar 		     gc_ver != IP_VERSION(9, 4, 3)) ||
2510fc8e84a2SLijo Lazar 		    gc_ver < IP_VERSION(9, 0, 0))
2511e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2512e098bc96SEvan Quan 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
25138ecad8d6SLijo Lazar 		if (gc_ver < IP_VERSION(9, 1, 0))
2514e098bc96SEvan Quan 			*states = ATTR_STATE_UNSUPPORTED;
2515a7505591SMario Limonciello 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
251679c65f3fSEvan Quan 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2517a7505591SMario Limonciello 			*states = ATTR_STATE_UNSUPPORTED;
2518b57c4f1cSVictor Zhao 		else if ((gc_ver == IP_VERSION(10, 3, 0) ||
2519b57c4f1cSVictor Zhao 			  gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev))
25201b852572SDanijel Slivka 			*states = ATTR_STATE_UNSUPPORTED;
252121e43386SLe Ma 	} else if (DEVICE_ATTR_IS(xgmi_plpd_policy)) {
252221e43386SLe Ma 		if (amdgpu_dpm_get_xgmi_plpd_mode(adev, NULL) == XGMI_PLPD_NONE)
252321e43386SLe Ma 			*states = ATTR_STATE_UNSUPPORTED;
2524df2a5f74SDmitrii Galantsev 	} else if (DEVICE_ATTR_IS(pp_mclk_od)) {
25258cfd6a05SLijo Lazar 		if (amdgpu_dpm_get_mclk_od(adev) == -EOPNOTSUPP)
25268cfd6a05SLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
2527df2a5f74SDmitrii Galantsev 	} else if (DEVICE_ATTR_IS(pp_sclk_od)) {
25288cfd6a05SLijo Lazar 		if (amdgpu_dpm_get_sclk_od(adev) == -EOPNOTSUPP)
25298cfd6a05SLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
25308cfd6a05SLijo Lazar 	} else if (DEVICE_ATTR_IS(apu_thermal_cap)) {
25318cfd6a05SLijo Lazar 		u32 limit;
25328cfd6a05SLijo Lazar 
25338cfd6a05SLijo Lazar 		if (amdgpu_dpm_get_apu_thermal_limit(adev, &limit) ==
25348cfd6a05SLijo Lazar 		    -EOPNOTSUPP)
25358cfd6a05SLijo Lazar 			*states = ATTR_STATE_UNSUPPORTED;
2536e098bc96SEvan Quan 	}
2537e098bc96SEvan Quan 
25388ecad8d6SLijo Lazar 	switch (gc_ver) {
25391b852572SDanijel Slivka 	case IP_VERSION(10, 3, 0):
25401b852572SDanijel Slivka 		if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
25411b852572SDanijel Slivka 		    amdgpu_sriov_vf(adev)) {
25421b852572SDanijel Slivka 			dev_attr->attr.mode &= ~0222;
25431b852572SDanijel Slivka 			dev_attr->store = NULL;
25441b852572SDanijel Slivka 		}
25451b852572SDanijel Slivka 		break;
25461d0e622fSKevin Wang 	default:
25471d0e622fSKevin Wang 		break;
2548e098bc96SEvan Quan 	}
2549e098bc96SEvan Quan 
2550e098bc96SEvan Quan 	return 0;
2551e098bc96SEvan Quan }
2552e098bc96SEvan Quan 
2553e098bc96SEvan Quan 
2554e098bc96SEvan Quan static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2555e098bc96SEvan Quan 				     struct amdgpu_device_attr *attr,
2556e098bc96SEvan Quan 				     uint32_t mask, struct list_head *attr_list)
2557e098bc96SEvan Quan {
2558e098bc96SEvan Quan 	int ret = 0;
2559e098bc96SEvan Quan 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2560e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *attr_entry;
256125e6373aSYang Wang 	struct device_attribute *dev_attr;
256225e6373aSYang Wang 	const char *name;
2563e098bc96SEvan Quan 
2564e098bc96SEvan Quan 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2565e098bc96SEvan Quan 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2566e098bc96SEvan Quan 
256725e6373aSYang Wang 	if (!attr)
256825e6373aSYang Wang 		return -EINVAL;
256925e6373aSYang Wang 
257025e6373aSYang Wang 	dev_attr = &attr->dev_attr;
257125e6373aSYang Wang 	name = dev_attr->attr.name;
2572e098bc96SEvan Quan 
25738a81028bSSathishkumar S 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2574e098bc96SEvan Quan 
2575e098bc96SEvan Quan 	ret = attr_update(adev, attr, mask, &attr_states);
2576e098bc96SEvan Quan 	if (ret) {
2577e098bc96SEvan Quan 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2578e098bc96SEvan Quan 			name, ret);
2579e098bc96SEvan Quan 		return ret;
2580e098bc96SEvan Quan 	}
2581e098bc96SEvan Quan 
2582e098bc96SEvan Quan 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2583e098bc96SEvan Quan 		return 0;
2584e098bc96SEvan Quan 
2585e098bc96SEvan Quan 	ret = device_create_file(adev->dev, dev_attr);
2586e098bc96SEvan Quan 	if (ret) {
2587e098bc96SEvan Quan 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2588e098bc96SEvan Quan 			name, ret);
2589e098bc96SEvan Quan 	}
2590e098bc96SEvan Quan 
2591e098bc96SEvan Quan 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2592e098bc96SEvan Quan 	if (!attr_entry)
2593e098bc96SEvan Quan 		return -ENOMEM;
2594e098bc96SEvan Quan 
2595e098bc96SEvan Quan 	attr_entry->attr = attr;
2596e098bc96SEvan Quan 	INIT_LIST_HEAD(&attr_entry->entry);
2597e098bc96SEvan Quan 
2598e098bc96SEvan Quan 	list_add_tail(&attr_entry->entry, attr_list);
2599e098bc96SEvan Quan 
2600e098bc96SEvan Quan 	return ret;
2601e098bc96SEvan Quan }
2602e098bc96SEvan Quan 
2603e098bc96SEvan Quan static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2604e098bc96SEvan Quan {
2605e098bc96SEvan Quan 	struct device_attribute *dev_attr = &attr->dev_attr;
2606e098bc96SEvan Quan 
2607e098bc96SEvan Quan 	device_remove_file(adev->dev, dev_attr);
2608e098bc96SEvan Quan }
2609e098bc96SEvan Quan 
2610e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2611e098bc96SEvan Quan 					     struct list_head *attr_list);
2612e098bc96SEvan Quan 
2613e098bc96SEvan Quan static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2614e098bc96SEvan Quan 					    struct amdgpu_device_attr *attrs,
2615e098bc96SEvan Quan 					    uint32_t counts,
2616e098bc96SEvan Quan 					    uint32_t mask,
2617e098bc96SEvan Quan 					    struct list_head *attr_list)
2618e098bc96SEvan Quan {
2619e098bc96SEvan Quan 	int ret = 0;
2620e098bc96SEvan Quan 	uint32_t i = 0;
2621e098bc96SEvan Quan 
2622e098bc96SEvan Quan 	for (i = 0; i < counts; i++) {
2623e098bc96SEvan Quan 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2624e098bc96SEvan Quan 		if (ret)
2625e098bc96SEvan Quan 			goto failed;
2626e098bc96SEvan Quan 	}
2627e098bc96SEvan Quan 
2628e098bc96SEvan Quan 	return 0;
2629e098bc96SEvan Quan 
2630e098bc96SEvan Quan failed:
2631e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, attr_list);
2632e098bc96SEvan Quan 
2633e098bc96SEvan Quan 	return ret;
2634e098bc96SEvan Quan }
2635e098bc96SEvan Quan 
2636e098bc96SEvan Quan static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2637e098bc96SEvan Quan 					     struct list_head *attr_list)
2638e098bc96SEvan Quan {
2639e098bc96SEvan Quan 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2640e098bc96SEvan Quan 
2641e098bc96SEvan Quan 	if (list_empty(attr_list))
2642e098bc96SEvan Quan 		return ;
2643e098bc96SEvan Quan 
2644e098bc96SEvan Quan 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2645e098bc96SEvan Quan 		amdgpu_device_attr_remove(adev, entry->attr);
2646e098bc96SEvan Quan 		list_del(&entry->entry);
2647e098bc96SEvan Quan 		kfree(entry);
2648e098bc96SEvan Quan 	}
2649e098bc96SEvan Quan }
2650e098bc96SEvan Quan 
2651e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2652e098bc96SEvan Quan 				      struct device_attribute *attr,
2653e098bc96SEvan Quan 				      char *buf)
2654e098bc96SEvan Quan {
2655e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2656e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2657d78c227fSMario Limonciello 	int r, temp = 0;
2658e098bc96SEvan Quan 
2659e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2660e098bc96SEvan Quan 		return -EINVAL;
2661e098bc96SEvan Quan 
2662e098bc96SEvan Quan 	switch (channel) {
2663e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2664e098bc96SEvan Quan 		/* get current junction temperature */
2665d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2666d78c227fSMario Limonciello 					   (void *)&temp);
2667e098bc96SEvan Quan 		break;
2668e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2669e098bc96SEvan Quan 		/* get current edge temperature */
2670d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2671d78c227fSMario Limonciello 					   (void *)&temp);
2672e098bc96SEvan Quan 		break;
2673e098bc96SEvan Quan 	case PP_TEMP_MEM:
2674e098bc96SEvan Quan 		/* get current memory temperature */
2675d78c227fSMario Limonciello 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2676d78c227fSMario Limonciello 					   (void *)&temp);
2677e098bc96SEvan Quan 		break;
2678e098bc96SEvan Quan 	default:
2679e098bc96SEvan Quan 		r = -EINVAL;
2680e098bc96SEvan Quan 		break;
2681e098bc96SEvan Quan 	}
2682e098bc96SEvan Quan 
2683e098bc96SEvan Quan 	if (r)
2684e098bc96SEvan Quan 		return r;
2685e098bc96SEvan Quan 
2686a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2687e098bc96SEvan Quan }
2688e098bc96SEvan Quan 
2689e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2690e098bc96SEvan Quan 					     struct device_attribute *attr,
2691e098bc96SEvan Quan 					     char *buf)
2692e098bc96SEvan Quan {
2693e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2694e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2695e098bc96SEvan Quan 	int temp;
2696e098bc96SEvan Quan 
2697e098bc96SEvan Quan 	if (hyst)
2698e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_temp;
2699e098bc96SEvan Quan 	else
2700e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_temp;
2701e098bc96SEvan Quan 
2702a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2703e098bc96SEvan Quan }
2704e098bc96SEvan Quan 
2705e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2706e098bc96SEvan Quan 					     struct device_attribute *attr,
2707e098bc96SEvan Quan 					     char *buf)
2708e098bc96SEvan Quan {
2709e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2710e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2711e098bc96SEvan Quan 	int temp;
2712e098bc96SEvan Quan 
2713e098bc96SEvan Quan 	if (hyst)
2714e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2715e098bc96SEvan Quan 	else
2716e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2717e098bc96SEvan Quan 
2718a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2719e098bc96SEvan Quan }
2720e098bc96SEvan Quan 
2721e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2722e098bc96SEvan Quan 					     struct device_attribute *attr,
2723e098bc96SEvan Quan 					     char *buf)
2724e098bc96SEvan Quan {
2725e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2726e098bc96SEvan Quan 	int hyst = to_sensor_dev_attr(attr)->index;
2727e098bc96SEvan Quan 	int temp;
2728e098bc96SEvan Quan 
2729e098bc96SEvan Quan 	if (hyst)
2730e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.min_mem_temp;
2731e098bc96SEvan Quan 	else
2732e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2733e098bc96SEvan Quan 
2734a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2735e098bc96SEvan Quan }
2736e098bc96SEvan Quan 
2737e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2738e098bc96SEvan Quan 					     struct device_attribute *attr,
2739e098bc96SEvan Quan 					     char *buf)
2740e098bc96SEvan Quan {
2741e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2742e098bc96SEvan Quan 
2743e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2744e098bc96SEvan Quan 		return -EINVAL;
2745e098bc96SEvan Quan 
2746a9ca9bb3STian Tao 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2747e098bc96SEvan Quan }
2748e098bc96SEvan Quan 
2749e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2750e098bc96SEvan Quan 					     struct device_attribute *attr,
2751e098bc96SEvan Quan 					     char *buf)
2752e098bc96SEvan Quan {
2753e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2754e098bc96SEvan Quan 	int channel = to_sensor_dev_attr(attr)->index;
2755e098bc96SEvan Quan 	int temp = 0;
2756e098bc96SEvan Quan 
2757e098bc96SEvan Quan 	if (channel >= PP_TEMP_MAX)
2758e098bc96SEvan Quan 		return -EINVAL;
2759e098bc96SEvan Quan 
2760e098bc96SEvan Quan 	switch (channel) {
2761e098bc96SEvan Quan 	case PP_TEMP_JUNCTION:
2762e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2763e098bc96SEvan Quan 		break;
2764e098bc96SEvan Quan 	case PP_TEMP_EDGE:
2765e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2766e098bc96SEvan Quan 		break;
2767e098bc96SEvan Quan 	case PP_TEMP_MEM:
2768e098bc96SEvan Quan 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2769e098bc96SEvan Quan 		break;
2770e098bc96SEvan Quan 	}
2771e098bc96SEvan Quan 
2772a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", temp);
2773e098bc96SEvan Quan }
2774e098bc96SEvan Quan 
2775e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2776e098bc96SEvan Quan 					    struct device_attribute *attr,
2777e098bc96SEvan Quan 					    char *buf)
2778e098bc96SEvan Quan {
2779e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2780e098bc96SEvan Quan 	u32 pwm_mode = 0;
2781e098bc96SEvan Quan 	int ret;
2782e098bc96SEvan Quan 
278353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2784e098bc96SEvan Quan 		return -EPERM;
2785d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2786d2ae842dSAlex Deucher 		return -EPERM;
2787e098bc96SEvan Quan 
27884a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2789e098bc96SEvan Quan 	if (ret < 0) {
27904a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2791e098bc96SEvan Quan 		return ret;
2792e098bc96SEvan Quan 	}
2793e098bc96SEvan Quan 
279479c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
279579c65f3fSEvan Quan 
27964a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
27974a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
279879c65f3fSEvan Quan 
279979c65f3fSEvan Quan 	if (ret)
2800e098bc96SEvan Quan 		return -EINVAL;
2801e098bc96SEvan Quan 
2802fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%u\n", pwm_mode);
2803e098bc96SEvan Quan }
2804e098bc96SEvan Quan 
2805e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2806e098bc96SEvan Quan 					    struct device_attribute *attr,
2807e098bc96SEvan Quan 					    const char *buf,
2808e098bc96SEvan Quan 					    size_t count)
2809e098bc96SEvan Quan {
2810e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2811e098bc96SEvan Quan 	int err, ret;
2812f317c5e5SMa Jun 	u32 pwm_mode;
2813e098bc96SEvan Quan 	int value;
2814e098bc96SEvan Quan 
281553b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2816e098bc96SEvan Quan 		return -EPERM;
2817d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2818d2ae842dSAlex Deucher 		return -EPERM;
2819e098bc96SEvan Quan 
2820e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
2821e098bc96SEvan Quan 	if (err)
2822e098bc96SEvan Quan 		return err;
2823e098bc96SEvan Quan 
2824f317c5e5SMa Jun 	if (value == 0)
2825f317c5e5SMa Jun 		pwm_mode = AMD_FAN_CTRL_NONE;
2826f317c5e5SMa Jun 	else if (value == 1)
2827f317c5e5SMa Jun 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2828f317c5e5SMa Jun 	else if (value == 2)
2829f317c5e5SMa Jun 		pwm_mode = AMD_FAN_CTRL_AUTO;
2830f317c5e5SMa Jun 	else
2831f317c5e5SMa Jun 		return -EINVAL;
2832f317c5e5SMa Jun 
28334a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2834e098bc96SEvan Quan 	if (ret < 0) {
28354a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2836e098bc96SEvan Quan 		return ret;
2837e098bc96SEvan Quan 	}
2838e098bc96SEvan Quan 
2839f317c5e5SMa Jun 	ret = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
284079c65f3fSEvan Quan 
28414a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
28424a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
284379c65f3fSEvan Quan 
284479c65f3fSEvan Quan 	if (ret)
2845e098bc96SEvan Quan 		return -EINVAL;
2846e098bc96SEvan Quan 
2847e098bc96SEvan Quan 	return count;
2848e098bc96SEvan Quan }
2849e098bc96SEvan Quan 
2850e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2851e098bc96SEvan Quan 					 struct device_attribute *attr,
2852e098bc96SEvan Quan 					 char *buf)
2853e098bc96SEvan Quan {
2854fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 0);
2855e098bc96SEvan Quan }
2856e098bc96SEvan Quan 
2857e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2858e098bc96SEvan Quan 					 struct device_attribute *attr,
2859e098bc96SEvan Quan 					 char *buf)
2860e098bc96SEvan Quan {
2861fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", 255);
2862e098bc96SEvan Quan }
2863e098bc96SEvan Quan 
2864e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2865e098bc96SEvan Quan 				     struct device_attribute *attr,
2866e098bc96SEvan Quan 				     const char *buf, size_t count)
2867e098bc96SEvan Quan {
2868e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2869e098bc96SEvan Quan 	int err;
2870e098bc96SEvan Quan 	u32 value;
2871e098bc96SEvan Quan 	u32 pwm_mode;
2872e098bc96SEvan Quan 
287353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2874e098bc96SEvan Quan 		return -EPERM;
2875d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2876d2ae842dSAlex Deucher 		return -EPERM;
2877e098bc96SEvan Quan 
287879c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
287979c65f3fSEvan Quan 	if (err)
288079c65f3fSEvan Quan 		return err;
288179c65f3fSEvan Quan 
28824a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2883e098bc96SEvan Quan 	if (err < 0) {
28844a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2885e098bc96SEvan Quan 		return err;
2886e098bc96SEvan Quan 	}
2887e098bc96SEvan Quan 
288879c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
288979c65f3fSEvan Quan 	if (err)
289079c65f3fSEvan Quan 		goto out;
289179c65f3fSEvan Quan 
2892e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2893e098bc96SEvan Quan 		pr_info("manual fan speed control should be enabled first\n");
2894e098bc96SEvan Quan 		err = -EINVAL;
289579c65f3fSEvan Quan 		goto out;
289679c65f3fSEvan Quan 	}
2897e098bc96SEvan Quan 
289879c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
289979c65f3fSEvan Quan 
290079c65f3fSEvan Quan out:
29014a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29024a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2903e098bc96SEvan Quan 
2904e098bc96SEvan Quan 	if (err)
2905e098bc96SEvan Quan 		return err;
2906e098bc96SEvan Quan 
2907e098bc96SEvan Quan 	return count;
2908e098bc96SEvan Quan }
2909e098bc96SEvan Quan 
2910e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2911e098bc96SEvan Quan 				     struct device_attribute *attr,
2912e098bc96SEvan Quan 				     char *buf)
2913e098bc96SEvan Quan {
2914e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2915e098bc96SEvan Quan 	int err;
2916e098bc96SEvan Quan 	u32 speed = 0;
2917e098bc96SEvan Quan 
291853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2919e098bc96SEvan Quan 		return -EPERM;
2920d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2921d2ae842dSAlex Deucher 		return -EPERM;
2922e098bc96SEvan Quan 
29234a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2924e098bc96SEvan Quan 	if (err < 0) {
29254a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2926e098bc96SEvan Quan 		return err;
2927e098bc96SEvan Quan 	}
2928e098bc96SEvan Quan 
29290d8318e1SEvan Quan 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2930e098bc96SEvan Quan 
29314a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29324a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2933e098bc96SEvan Quan 
2934e098bc96SEvan Quan 	if (err)
2935e098bc96SEvan Quan 		return err;
2936e098bc96SEvan Quan 
2937fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2938e098bc96SEvan Quan }
2939e098bc96SEvan Quan 
2940e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2941e098bc96SEvan Quan 					   struct device_attribute *attr,
2942e098bc96SEvan Quan 					   char *buf)
2943e098bc96SEvan Quan {
2944e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2945e098bc96SEvan Quan 	int err;
2946e098bc96SEvan Quan 	u32 speed = 0;
2947e098bc96SEvan Quan 
294853b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
2949e098bc96SEvan Quan 		return -EPERM;
2950d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
2951d2ae842dSAlex Deucher 		return -EPERM;
2952e098bc96SEvan Quan 
29534a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2954e098bc96SEvan Quan 	if (err < 0) {
29554a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2956e098bc96SEvan Quan 		return err;
2957e098bc96SEvan Quan 	}
2958e098bc96SEvan Quan 
2959e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2960e098bc96SEvan Quan 
29614a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
29624a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2963e098bc96SEvan Quan 
2964e098bc96SEvan Quan 	if (err)
2965e098bc96SEvan Quan 		return err;
2966e098bc96SEvan Quan 
2967fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", speed);
2968e098bc96SEvan Quan }
2969e098bc96SEvan Quan 
2970e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2971e098bc96SEvan Quan 					 struct device_attribute *attr,
2972e098bc96SEvan Quan 					 char *buf)
2973e098bc96SEvan Quan {
2974e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2975e098bc96SEvan Quan 	u32 min_rpm = 0;
2976e098bc96SEvan Quan 	int r;
2977e098bc96SEvan Quan 
2978d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2979d78c227fSMario Limonciello 				   (void *)&min_rpm);
2980e098bc96SEvan Quan 
2981e098bc96SEvan Quan 	if (r)
2982e098bc96SEvan Quan 		return r;
2983e098bc96SEvan Quan 
2984a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", min_rpm);
2985e098bc96SEvan Quan }
2986e098bc96SEvan Quan 
2987e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2988e098bc96SEvan Quan 					 struct device_attribute *attr,
2989e098bc96SEvan Quan 					 char *buf)
2990e098bc96SEvan Quan {
2991e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2992e098bc96SEvan Quan 	u32 max_rpm = 0;
2993e098bc96SEvan Quan 	int r;
2994e098bc96SEvan Quan 
2995d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2996d78c227fSMario Limonciello 				   (void *)&max_rpm);
2997e098bc96SEvan Quan 
2998e098bc96SEvan Quan 	if (r)
2999e098bc96SEvan Quan 		return r;
3000e098bc96SEvan Quan 
3001a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", max_rpm);
3002e098bc96SEvan Quan }
3003e098bc96SEvan Quan 
3004e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
3005e098bc96SEvan Quan 					   struct device_attribute *attr,
3006e098bc96SEvan Quan 					   char *buf)
3007e098bc96SEvan Quan {
3008e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3009e098bc96SEvan Quan 	int err;
3010e098bc96SEvan Quan 	u32 rpm = 0;
3011e098bc96SEvan Quan 
301253b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3013e098bc96SEvan Quan 		return -EPERM;
3014d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3015d2ae842dSAlex Deucher 		return -EPERM;
3016e098bc96SEvan Quan 
30174a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3018e098bc96SEvan Quan 	if (err < 0) {
30194a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3020e098bc96SEvan Quan 		return err;
3021e098bc96SEvan Quan 	}
3022e098bc96SEvan Quan 
3023e098bc96SEvan Quan 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
3024e098bc96SEvan Quan 
30254a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30264a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3027e098bc96SEvan Quan 
3028e098bc96SEvan Quan 	if (err)
3029e098bc96SEvan Quan 		return err;
3030e098bc96SEvan Quan 
3031fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", rpm);
3032e098bc96SEvan Quan }
3033e098bc96SEvan Quan 
3034e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
3035e098bc96SEvan Quan 				     struct device_attribute *attr,
3036e098bc96SEvan Quan 				     const char *buf, size_t count)
3037e098bc96SEvan Quan {
3038e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3039e098bc96SEvan Quan 	int err;
3040e098bc96SEvan Quan 	u32 value;
3041e098bc96SEvan Quan 	u32 pwm_mode;
3042e098bc96SEvan Quan 
304353b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3044e098bc96SEvan Quan 		return -EPERM;
3045d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3046d2ae842dSAlex Deucher 		return -EPERM;
3047e098bc96SEvan Quan 
304879c65f3fSEvan Quan 	err = kstrtou32(buf, 10, &value);
304979c65f3fSEvan Quan 	if (err)
305079c65f3fSEvan Quan 		return err;
305179c65f3fSEvan Quan 
30524a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3053e098bc96SEvan Quan 	if (err < 0) {
30544a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3055e098bc96SEvan Quan 		return err;
3056e098bc96SEvan Quan 	}
3057e098bc96SEvan Quan 
305879c65f3fSEvan Quan 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
305979c65f3fSEvan Quan 	if (err)
306079c65f3fSEvan Quan 		goto out;
3061e098bc96SEvan Quan 
3062e098bc96SEvan Quan 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
306379c65f3fSEvan Quan 		err = -ENODATA;
306479c65f3fSEvan Quan 		goto out;
3065e098bc96SEvan Quan 	}
3066e098bc96SEvan Quan 
3067e098bc96SEvan Quan 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
3068e098bc96SEvan Quan 
306979c65f3fSEvan Quan out:
30704a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
30714a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3072e098bc96SEvan Quan 
3073e098bc96SEvan Quan 	if (err)
3074e098bc96SEvan Quan 		return err;
3075e098bc96SEvan Quan 
3076e098bc96SEvan Quan 	return count;
3077e098bc96SEvan Quan }
3078e098bc96SEvan Quan 
3079e098bc96SEvan Quan static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
3080e098bc96SEvan Quan 					    struct device_attribute *attr,
3081e098bc96SEvan Quan 					    char *buf)
3082e098bc96SEvan Quan {
3083e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3084e098bc96SEvan Quan 	u32 pwm_mode = 0;
3085e098bc96SEvan Quan 	int ret;
3086e098bc96SEvan Quan 
308753b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3088e098bc96SEvan Quan 		return -EPERM;
3089d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3090d2ae842dSAlex Deucher 		return -EPERM;
3091e098bc96SEvan Quan 
30924a580877SLuben Tuikov 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3093e098bc96SEvan Quan 	if (ret < 0) {
30944a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3095e098bc96SEvan Quan 		return ret;
3096e098bc96SEvan Quan 	}
3097e098bc96SEvan Quan 
309879c65f3fSEvan Quan 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
309979c65f3fSEvan Quan 
31004a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
31014a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
310279c65f3fSEvan Quan 
310379c65f3fSEvan Quan 	if (ret)
3104e098bc96SEvan Quan 		return -EINVAL;
3105e098bc96SEvan Quan 
3106fdf8eea5SDarren Powell 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
3107e098bc96SEvan Quan }
3108e098bc96SEvan Quan 
3109e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
3110e098bc96SEvan Quan 					    struct device_attribute *attr,
3111e098bc96SEvan Quan 					    const char *buf,
3112e098bc96SEvan Quan 					    size_t count)
3113e098bc96SEvan Quan {
3114e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3115e098bc96SEvan Quan 	int err;
3116e098bc96SEvan Quan 	int value;
3117e098bc96SEvan Quan 	u32 pwm_mode;
3118e098bc96SEvan Quan 
311953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3120e098bc96SEvan Quan 		return -EPERM;
3121d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3122d2ae842dSAlex Deucher 		return -EPERM;
3123e098bc96SEvan Quan 
3124e098bc96SEvan Quan 	err = kstrtoint(buf, 10, &value);
3125e098bc96SEvan Quan 	if (err)
3126e098bc96SEvan Quan 		return err;
3127e098bc96SEvan Quan 
3128e098bc96SEvan Quan 	if (value == 0)
3129e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_AUTO;
3130e098bc96SEvan Quan 	else if (value == 1)
3131e098bc96SEvan Quan 		pwm_mode = AMD_FAN_CTRL_MANUAL;
3132e098bc96SEvan Quan 	else
3133e098bc96SEvan Quan 		return -EINVAL;
3134e098bc96SEvan Quan 
31354a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3136e098bc96SEvan Quan 	if (err < 0) {
31374a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3138e098bc96SEvan Quan 		return err;
3139e098bc96SEvan Quan 	}
3140e098bc96SEvan Quan 
314179c65f3fSEvan Quan 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
3142e098bc96SEvan Quan 
31434a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
31444a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3145e098bc96SEvan Quan 
314679c65f3fSEvan Quan 	if (err)
314779c65f3fSEvan Quan 		return -EINVAL;
314879c65f3fSEvan Quan 
3149e098bc96SEvan Quan 	return count;
3150e098bc96SEvan Quan }
3151e098bc96SEvan Quan 
3152e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
3153e098bc96SEvan Quan 					struct device_attribute *attr,
3154e098bc96SEvan Quan 					char *buf)
3155e098bc96SEvan Quan {
3156e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3157e098bc96SEvan Quan 	u32 vddgfx;
3158d78c227fSMario Limonciello 	int r;
3159e098bc96SEvan Quan 
3160e098bc96SEvan Quan 	/* get the voltage */
3161d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
3162d78c227fSMario Limonciello 				   (void *)&vddgfx);
3163e098bc96SEvan Quan 	if (r)
3164e098bc96SEvan Quan 		return r;
3165e098bc96SEvan Quan 
3166a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddgfx);
3167e098bc96SEvan Quan }
3168e098bc96SEvan Quan 
3169e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
3170e098bc96SEvan Quan 					      struct device_attribute *attr,
3171e098bc96SEvan Quan 					      char *buf)
3172e098bc96SEvan Quan {
3173a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddgfx\n");
3174e098bc96SEvan Quan }
3175e098bc96SEvan Quan 
3176e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
3177e098bc96SEvan Quan 				       struct device_attribute *attr,
3178e098bc96SEvan Quan 				       char *buf)
3179e098bc96SEvan Quan {
3180e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3181e098bc96SEvan Quan 	u32 vddnb;
3182d78c227fSMario Limonciello 	int r;
3183e098bc96SEvan Quan 
3184e098bc96SEvan Quan 	/* only APUs have vddnb */
3185e098bc96SEvan Quan 	if  (!(adev->flags & AMD_IS_APU))
3186e098bc96SEvan Quan 		return -EINVAL;
3187e098bc96SEvan Quan 
3188e098bc96SEvan Quan 	/* get the voltage */
3189d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
3190d78c227fSMario Limonciello 				   (void *)&vddnb);
3191e098bc96SEvan Quan 	if (r)
3192e098bc96SEvan Quan 		return r;
3193e098bc96SEvan Quan 
3194a9ca9bb3STian Tao 	return sysfs_emit(buf, "%d\n", vddnb);
3195e098bc96SEvan Quan }
3196e098bc96SEvan Quan 
3197e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
3198e098bc96SEvan Quan 					      struct device_attribute *attr,
3199e098bc96SEvan Quan 					      char *buf)
3200e098bc96SEvan Quan {
3201a9ca9bb3STian Tao 	return sysfs_emit(buf, "vddnb\n");
3202e098bc96SEvan Quan }
3203e098bc96SEvan Quan 
3204a5600853SAlex Deucher static int amdgpu_hwmon_get_power(struct device *dev,
3205d78c227fSMario Limonciello 				  enum amd_pp_sensors sensor)
3206e098bc96SEvan Quan {
3207e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3208d78c227fSMario Limonciello 	unsigned int uw;
3209e098bc96SEvan Quan 	u32 query = 0;
3210d78c227fSMario Limonciello 	int r;
3211e098bc96SEvan Quan 
3212d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
3213e098bc96SEvan Quan 	if (r)
3214e098bc96SEvan Quan 		return r;
3215e098bc96SEvan Quan 
3216e098bc96SEvan Quan 	/* convert to microwatts */
3217e098bc96SEvan Quan 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
3218e098bc96SEvan Quan 
3219d78c227fSMario Limonciello 	return uw;
3220d78c227fSMario Limonciello }
3221d78c227fSMario Limonciello 
3222d78c227fSMario Limonciello static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
3223d78c227fSMario Limonciello 					   struct device_attribute *attr,
3224d78c227fSMario Limonciello 					   char *buf)
3225d78c227fSMario Limonciello {
3226d1090194SSrinivasan Shanmugam 	ssize_t val;
3227d78c227fSMario Limonciello 
32289366c2e8SMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
3229d78c227fSMario Limonciello 	if (val < 0)
3230d78c227fSMario Limonciello 		return val;
3231d78c227fSMario Limonciello 
3232d1090194SSrinivasan Shanmugam 	return sysfs_emit(buf, "%zd\n", val);
3233e098bc96SEvan Quan }
3234e098bc96SEvan Quan 
3235bb9f7b68SMario Limonciello static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
3236bb9f7b68SMario Limonciello 					     struct device_attribute *attr,
3237bb9f7b68SMario Limonciello 					     char *buf)
3238bb9f7b68SMario Limonciello {
3239d1090194SSrinivasan Shanmugam 	ssize_t val;
3240bb9f7b68SMario Limonciello 
324147f1724dSMario Limonciello 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
3242bb9f7b68SMario Limonciello 	if (val < 0)
3243bb9f7b68SMario Limonciello 		return val;
3244bb9f7b68SMario Limonciello 
3245d1090194SSrinivasan Shanmugam 	return sysfs_emit(buf, "%zd\n", val);
3246bb9f7b68SMario Limonciello }
3247bb9f7b68SMario Limonciello 
324891161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
3249e098bc96SEvan Quan 					struct device_attribute *attr,
325091161b06SDarren Powell 					char *buf,
325191161b06SDarren Powell 					enum pp_power_limit_level pp_limit_level)
3252e098bc96SEvan Quan {
3253e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3254a40a020dSDarren Powell 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
3255a40a020dSDarren Powell 	uint32_t limit;
3256e098bc96SEvan Quan 	ssize_t size;
3257e098bc96SEvan Quan 	int r;
3258e098bc96SEvan Quan 
325953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3260e098bc96SEvan Quan 		return -EPERM;
3261d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3262d2ae842dSAlex Deucher 		return -EPERM;
3263e098bc96SEvan Quan 
32644a580877SLuben Tuikov 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3265e098bc96SEvan Quan 	if (r < 0) {
32664a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3267e098bc96SEvan Quan 		return r;
3268e098bc96SEvan Quan 	}
3269e098bc96SEvan Quan 
327079c65f3fSEvan Quan 	r = amdgpu_dpm_get_power_limit(adev, &limit,
327104bec521SDarren Powell 				      pp_limit_level, power_type);
3272dc2a8240SDarren Powell 
3273dc2a8240SDarren Powell 	if (!r)
327409b6744cSDarren Powell 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
3275dc2a8240SDarren Powell 	else
327609b6744cSDarren Powell 		size = sysfs_emit(buf, "\n");
3277e098bc96SEvan Quan 
32784a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
32794a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3280e098bc96SEvan Quan 
3281e098bc96SEvan Quan 	return size;
3282e098bc96SEvan Quan }
3283e098bc96SEvan Quan 
328419589468SMa Jun static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
328519589468SMa Jun 					 struct device_attribute *attr,
328619589468SMa Jun 					 char *buf)
328719589468SMa Jun {
328819589468SMa Jun 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MIN);
328919589468SMa Jun }
329091161b06SDarren Powell 
329191161b06SDarren Powell static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
329291161b06SDarren Powell 					 struct device_attribute *attr,
329391161b06SDarren Powell 					 char *buf)
329491161b06SDarren Powell {
329591161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
329691161b06SDarren Powell 
329791161b06SDarren Powell }
329891161b06SDarren Powell 
3299e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
3300e098bc96SEvan Quan 					 struct device_attribute *attr,
3301e098bc96SEvan Quan 					 char *buf)
3302e098bc96SEvan Quan {
330391161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
3304e098bc96SEvan Quan 
3305e098bc96SEvan Quan }
3306e098bc96SEvan Quan 
33076e58941cSEric Huang static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
33086e58941cSEric Huang 					 struct device_attribute *attr,
33096e58941cSEric Huang 					 char *buf)
33106e58941cSEric Huang {
331191161b06SDarren Powell 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
33126e58941cSEric Huang 
33136e58941cSEric Huang }
33146e58941cSEric Huang 
3315ae07970aSXiaomeng Hou static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
3316ae07970aSXiaomeng Hou 					 struct device_attribute *attr,
3317ae07970aSXiaomeng Hou 					 char *buf)
3318ae07970aSXiaomeng Hou {
33193b99e8e3SYang Wang 	struct amdgpu_device *adev = dev_get_drvdata(dev);
33204e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
3321ae07970aSXiaomeng Hou 
33228ecad8d6SLijo Lazar 	if (gc_ver == IP_VERSION(10, 3, 1))
3323a9ca9bb3STian Tao 		return sysfs_emit(buf, "%s\n",
33243b99e8e3SYang Wang 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
33253b99e8e3SYang Wang 				  "fastPPT" : "slowPPT");
33263b99e8e3SYang Wang 	else
33273b99e8e3SYang Wang 		return sysfs_emit(buf, "PPT\n");
3328ae07970aSXiaomeng Hou }
3329e098bc96SEvan Quan 
3330e098bc96SEvan Quan static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3331e098bc96SEvan Quan 		struct device_attribute *attr,
3332e098bc96SEvan Quan 		const char *buf,
3333e098bc96SEvan Quan 		size_t count)
3334e098bc96SEvan Quan {
3335e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3336ae07970aSXiaomeng Hou 	int limit_type = to_sensor_dev_attr(attr)->index;
3337e098bc96SEvan Quan 	int err;
3338e098bc96SEvan Quan 	u32 value;
3339e098bc96SEvan Quan 
334053b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
3341e098bc96SEvan Quan 		return -EPERM;
3342d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
3343d2ae842dSAlex Deucher 		return -EPERM;
3344e098bc96SEvan Quan 
3345e098bc96SEvan Quan 	if (amdgpu_sriov_vf(adev))
3346e098bc96SEvan Quan 		return -EINVAL;
3347e098bc96SEvan Quan 
3348e098bc96SEvan Quan 	err = kstrtou32(buf, 10, &value);
3349e098bc96SEvan Quan 	if (err)
3350e098bc96SEvan Quan 		return err;
3351e098bc96SEvan Quan 
3352e098bc96SEvan Quan 	value = value / 1000000; /* convert to Watt */
3353ae07970aSXiaomeng Hou 	value |= limit_type << 24;
3354e098bc96SEvan Quan 
33554a580877SLuben Tuikov 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3356e098bc96SEvan Quan 	if (err < 0) {
33574a580877SLuben Tuikov 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3358e098bc96SEvan Quan 		return err;
3359e098bc96SEvan Quan 	}
3360e098bc96SEvan Quan 
336179c65f3fSEvan Quan 	err = amdgpu_dpm_set_power_limit(adev, value);
3362e098bc96SEvan Quan 
33634a580877SLuben Tuikov 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
33644a580877SLuben Tuikov 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3365e098bc96SEvan Quan 
3366e098bc96SEvan Quan 	if (err)
3367e098bc96SEvan Quan 		return err;
3368e098bc96SEvan Quan 
3369e098bc96SEvan Quan 	return count;
3370e098bc96SEvan Quan }
3371e098bc96SEvan Quan 
3372e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3373e098bc96SEvan Quan 				      struct device_attribute *attr,
3374e098bc96SEvan Quan 				      char *buf)
3375e098bc96SEvan Quan {
3376e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3377e098bc96SEvan Quan 	uint32_t sclk;
3378d78c227fSMario Limonciello 	int r;
3379e098bc96SEvan Quan 
3380e098bc96SEvan Quan 	/* get the sclk */
3381d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3382d78c227fSMario Limonciello 				   (void *)&sclk);
3383e098bc96SEvan Quan 	if (r)
3384e098bc96SEvan Quan 		return r;
3385e098bc96SEvan Quan 
3386a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
3387e098bc96SEvan Quan }
3388e098bc96SEvan Quan 
3389e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3390e098bc96SEvan Quan 					    struct device_attribute *attr,
3391e098bc96SEvan Quan 					    char *buf)
3392e098bc96SEvan Quan {
3393a9ca9bb3STian Tao 	return sysfs_emit(buf, "sclk\n");
3394e098bc96SEvan Quan }
3395e098bc96SEvan Quan 
3396e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3397e098bc96SEvan Quan 				      struct device_attribute *attr,
3398e098bc96SEvan Quan 				      char *buf)
3399e098bc96SEvan Quan {
3400e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3401e098bc96SEvan Quan 	uint32_t mclk;
3402d78c227fSMario Limonciello 	int r;
3403e098bc96SEvan Quan 
3404e098bc96SEvan Quan 	/* get the sclk */
3405d78c227fSMario Limonciello 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3406d78c227fSMario Limonciello 				   (void *)&mclk);
3407e098bc96SEvan Quan 	if (r)
3408e098bc96SEvan Quan 		return r;
3409e098bc96SEvan Quan 
3410a9ca9bb3STian Tao 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3411e098bc96SEvan Quan }
3412e098bc96SEvan Quan 
3413e098bc96SEvan Quan static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3414e098bc96SEvan Quan 					    struct device_attribute *attr,
3415e098bc96SEvan Quan 					    char *buf)
3416e098bc96SEvan Quan {
3417a9ca9bb3STian Tao 	return sysfs_emit(buf, "mclk\n");
3418e098bc96SEvan Quan }
3419e098bc96SEvan Quan 
3420e098bc96SEvan Quan /**
3421e098bc96SEvan Quan  * DOC: hwmon
3422e098bc96SEvan Quan  *
3423e098bc96SEvan Quan  * The amdgpu driver exposes the following sensor interfaces:
3424e098bc96SEvan Quan  *
3425e098bc96SEvan Quan  * - GPU temperature (via the on-die sensor)
3426e098bc96SEvan Quan  *
3427e098bc96SEvan Quan  * - GPU voltage
3428e098bc96SEvan Quan  *
3429e098bc96SEvan Quan  * - Northbridge voltage (APUs only)
3430e098bc96SEvan Quan  *
3431e098bc96SEvan Quan  * - GPU power
3432e098bc96SEvan Quan  *
3433e098bc96SEvan Quan  * - GPU fan
3434e098bc96SEvan Quan  *
3435e098bc96SEvan Quan  * - GPU gfx/compute engine clock
3436e098bc96SEvan Quan  *
3437e098bc96SEvan Quan  * - GPU memory clock (dGPU only)
3438e098bc96SEvan Quan  *
3439e098bc96SEvan Quan  * hwmon interfaces for GPU temperature:
3440e098bc96SEvan Quan  *
3441e098bc96SEvan Quan  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3442e098bc96SEvan Quan  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3443e098bc96SEvan Quan  *
3444e098bc96SEvan Quan  * - temp[1-3]_label: temperature channel label
3445e098bc96SEvan Quan  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3446e098bc96SEvan Quan  *
3447e098bc96SEvan Quan  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3448e098bc96SEvan Quan  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3449e098bc96SEvan Quan  *
3450e098bc96SEvan Quan  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3451e098bc96SEvan Quan  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3452e098bc96SEvan Quan  *
3453e098bc96SEvan Quan  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3454e098bc96SEvan Quan  *   - these are supported on SOC15 dGPUs only
3455e098bc96SEvan Quan  *
3456e098bc96SEvan Quan  * hwmon interfaces for GPU voltage:
3457e098bc96SEvan Quan  *
3458e098bc96SEvan Quan  * - in0_input: the voltage on the GPU in millivolts
3459e098bc96SEvan Quan  *
3460e098bc96SEvan Quan  * - in1_input: the voltage on the Northbridge in millivolts
3461e098bc96SEvan Quan  *
3462e098bc96SEvan Quan  * hwmon interfaces for GPU power:
3463e098bc96SEvan Quan  *
346429f5be8dSAlex Deucher  * - power1_average: average power used by the SoC in microWatts.  On APUs this includes the CPU.
3465e098bc96SEvan Quan  *
3466bb9f7b68SMario Limonciello  * - power1_input: instantaneous power used by the SoC in microWatts.  On APUs this includes the CPU.
3467bb9f7b68SMario Limonciello  *
3468e098bc96SEvan Quan  * - power1_cap_min: minimum cap supported in microWatts
3469e098bc96SEvan Quan  *
3470e098bc96SEvan Quan  * - power1_cap_max: maximum cap supported in microWatts
3471e098bc96SEvan Quan  *
3472e098bc96SEvan Quan  * - power1_cap: selected power cap in microWatts
3473e098bc96SEvan Quan  *
3474e098bc96SEvan Quan  * hwmon interfaces for GPU fan:
3475e098bc96SEvan Quan  *
3476e098bc96SEvan Quan  * - pwm1: pulse width modulation fan level (0-255)
3477e098bc96SEvan Quan  *
3478e098bc96SEvan 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)
3479e098bc96SEvan Quan  *
3480e098bc96SEvan Quan  * - pwm1_min: pulse width modulation fan control minimum level (0)
3481e098bc96SEvan Quan  *
3482e098bc96SEvan Quan  * - pwm1_max: pulse width modulation fan control maximum level (255)
3483e098bc96SEvan Quan  *
3484e5527d8cSBhaskar Chowdhury  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3485e098bc96SEvan Quan  *
3486e5527d8cSBhaskar Chowdhury  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3487e098bc96SEvan Quan  *
3488e098bc96SEvan Quan  * - fan1_input: fan speed in RPM
3489e098bc96SEvan Quan  *
3490e098bc96SEvan Quan  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3491e098bc96SEvan Quan  *
3492e098bc96SEvan Quan  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3493e098bc96SEvan Quan  *
349496401f7cSEvan Quan  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
349596401f7cSEvan Quan  *       That will get the former one overridden.
349696401f7cSEvan Quan  *
3497e098bc96SEvan Quan  * hwmon interfaces for GPU clocks:
3498e098bc96SEvan Quan  *
3499e098bc96SEvan Quan  * - freq1_input: the gfx/compute clock in hertz
3500e098bc96SEvan Quan  *
3501e098bc96SEvan Quan  * - freq2_input: the memory clock in hertz
3502e098bc96SEvan Quan  *
3503e098bc96SEvan Quan  * You can use hwmon tools like sensors to view this information on your system.
3504e098bc96SEvan Quan  *
3505e098bc96SEvan Quan  */
3506e098bc96SEvan Quan 
3507e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3508e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3509e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3510e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3511e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3512e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3513e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3514e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3515e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3516e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3517e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3518e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3519e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3520e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3521e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3522e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3523e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3524e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3525e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3526e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3527e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3528e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3529e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3530e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3531e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3532e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3533e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3534e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3535e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3536bb9f7b68SMario Limonciello static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
3537e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3538e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3539e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
35406e58941cSEric Huang static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3541ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3542ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3543ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3544ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3545ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
35466e58941cSEric Huang static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3547ae07970aSXiaomeng Hou static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3548e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3549e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3550e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3551e098bc96SEvan Quan static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3552e098bc96SEvan Quan 
3553e098bc96SEvan Quan static struct attribute *hwmon_attributes[] = {
3554e098bc96SEvan Quan 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3555e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3556e098bc96SEvan Quan 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3557e098bc96SEvan Quan 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3558e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3559e098bc96SEvan Quan 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3560e098bc96SEvan Quan 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3561e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3562e098bc96SEvan Quan 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3563e098bc96SEvan Quan 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3564e098bc96SEvan Quan 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3565e098bc96SEvan Quan 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3566e098bc96SEvan Quan 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3567e098bc96SEvan Quan 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3568e098bc96SEvan Quan 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3569e098bc96SEvan Quan 	&sensor_dev_attr_pwm1.dev_attr.attr,
3570e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3571e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3572e098bc96SEvan Quan 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3573e098bc96SEvan Quan 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3574e098bc96SEvan Quan 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3575e098bc96SEvan Quan 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3576e098bc96SEvan Quan 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3577e098bc96SEvan Quan 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3578e098bc96SEvan Quan 	&sensor_dev_attr_in0_input.dev_attr.attr,
3579e098bc96SEvan Quan 	&sensor_dev_attr_in0_label.dev_attr.attr,
3580e098bc96SEvan Quan 	&sensor_dev_attr_in1_input.dev_attr.attr,
3581e098bc96SEvan Quan 	&sensor_dev_attr_in1_label.dev_attr.attr,
3582e098bc96SEvan Quan 	&sensor_dev_attr_power1_average.dev_attr.attr,
3583bb9f7b68SMario Limonciello 	&sensor_dev_attr_power1_input.dev_attr.attr,
3584e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3585e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3586e098bc96SEvan Quan 	&sensor_dev_attr_power1_cap.dev_attr.attr,
35876e58941cSEric Huang 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3588ae07970aSXiaomeng Hou 	&sensor_dev_attr_power1_label.dev_attr.attr,
3589ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_average.dev_attr.attr,
3590ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3591ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3592ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_cap.dev_attr.attr,
35936e58941cSEric Huang 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3594ae07970aSXiaomeng Hou 	&sensor_dev_attr_power2_label.dev_attr.attr,
3595e098bc96SEvan Quan 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3596e098bc96SEvan Quan 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3597e098bc96SEvan Quan 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3598e098bc96SEvan Quan 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3599e098bc96SEvan Quan 	NULL
3600e098bc96SEvan Quan };
3601e098bc96SEvan Quan 
3602e098bc96SEvan Quan static umode_t hwmon_attributes_visible(struct kobject *kobj,
3603e098bc96SEvan Quan 					struct attribute *attr, int index)
3604e098bc96SEvan Quan {
3605e098bc96SEvan Quan 	struct device *dev = kobj_to_dev(kobj);
3606e098bc96SEvan Quan 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3607e098bc96SEvan Quan 	umode_t effective_mode = attr->mode;
36084e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
360915419813SMario Limonciello 	uint32_t tmp;
3610e098bc96SEvan Quan 
36114f0f1b58SDanijel Slivka 	/* under pp one vf mode manage of hwmon attributes is not supported */
36124f0f1b58SDanijel Slivka 	if (amdgpu_sriov_is_pp_one_vf(adev))
36134f0f1b58SDanijel Slivka 		effective_mode &= ~S_IWUSR;
36144f0f1b58SDanijel Slivka 
3615e098bc96SEvan Quan 	/* Skip fan attributes if fan is not present */
3616e098bc96SEvan Quan 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3617e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3618e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3619e098bc96SEvan Quan 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3620e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3621e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3622e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3623e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3624e098bc96SEvan Quan 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3625e098bc96SEvan Quan 		return 0;
3626e098bc96SEvan Quan 
3627e098bc96SEvan Quan 	/* Skip fan attributes on APU */
3628e098bc96SEvan Quan 	if ((adev->flags & AMD_IS_APU) &&
3629e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3630e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3631e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3632e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3633e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3634e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3635e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3636e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3637e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3638e098bc96SEvan Quan 		return 0;
3639e098bc96SEvan Quan 
3640e098bc96SEvan Quan 	/* Skip crit temp on APU */
36418572fa2aSAsad Kamal 	if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
36425f571c61SHawking Zhang 	    (gc_ver == IP_VERSION(9, 4, 3) || gc_ver == IP_VERSION(9, 4, 4))) &&
3643e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3644e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3645e098bc96SEvan Quan 		return 0;
3646e098bc96SEvan Quan 
3647e098bc96SEvan Quan 	/* Skip limit attributes if DPM is not enabled */
3648e098bc96SEvan Quan 	if (!adev->pm.dpm_enabled &&
3649e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3650e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3651e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3652e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3653e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3654e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3655e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3656e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3657e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3658e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3659e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3660e098bc96SEvan Quan 		return 0;
3661e098bc96SEvan Quan 
3662e098bc96SEvan Quan 	/* mask fan attributes if we have no bindings for this asic to expose */
3663685fae24SEvan Quan 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3664e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3665685fae24SEvan Quan 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3666e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3667e098bc96SEvan Quan 		effective_mode &= ~S_IRUGO;
3668e098bc96SEvan Quan 
3669685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3670e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3671685fae24SEvan Quan 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3672e098bc96SEvan Quan 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3673e098bc96SEvan Quan 		effective_mode &= ~S_IWUSR;
3674e098bc96SEvan Quan 
36758572fa2aSAsad Kamal 	/* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
3676ae07970aSXiaomeng Hou 	if (((adev->family == AMDGPU_FAMILY_SI) ||
36778572fa2aSAsad Kamal 	     ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)) &&
36785f571c61SHawking Zhang 	      (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4)))) &&
3679367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3680e098bc96SEvan Quan 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
36816e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
36826e58941cSEric Huang 	     attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3683e098bc96SEvan Quan 		return 0;
3684e098bc96SEvan Quan 
368589317d42SGuilherme G. Piccoli 	/* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
3686367deb67SAlex Deucher 	if (((adev->family == AMDGPU_FAMILY_SI) ||
36878ecad8d6SLijo Lazar 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3688367deb67SAlex Deucher 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3689367deb67SAlex Deucher 		return 0;
3690367deb67SAlex Deucher 
369115419813SMario Limonciello 	/* not all products support both average and instantaneous */
369215419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
369315419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
369415419813SMario Limonciello 		return 0;
369515419813SMario Limonciello 	if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
369615419813SMario Limonciello 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
369715419813SMario Limonciello 		return 0;
369815419813SMario Limonciello 
3699e098bc96SEvan Quan 	/* hide max/min values if we can't both query and manage the fan */
3700685fae24SEvan Quan 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3701685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3702685fae24SEvan Quan 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3703685fae24SEvan Quan 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3704e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3705e098bc96SEvan Quan 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3706e098bc96SEvan Quan 		return 0;
3707e098bc96SEvan Quan 
3708685fae24SEvan Quan 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3709685fae24SEvan Quan 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3710e098bc96SEvan Quan 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3711e098bc96SEvan Quan 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3712e098bc96SEvan Quan 		return 0;
3713e098bc96SEvan Quan 
3714e098bc96SEvan Quan 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
37158572fa2aSAsad Kamal 	     adev->family == AMDGPU_FAMILY_KV ||	/* not implemented yet */
37165f571c61SHawking Zhang 	     (gc_ver == IP_VERSION(9, 4, 3) ||
37175f571c61SHawking Zhang 	      gc_ver == IP_VERSION(9, 4, 4))) &&
3718e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3719e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3720e098bc96SEvan Quan 		return 0;
3721e098bc96SEvan Quan 
37228572fa2aSAsad Kamal 	/* only APUs other than gc 9,4,3 have vddnb */
37235f571c61SHawking Zhang 	if ((!(adev->flags & AMD_IS_APU) ||
37245f571c61SHawking Zhang 	     (gc_ver == IP_VERSION(9, 4, 3) ||
37255f571c61SHawking Zhang 	      gc_ver == IP_VERSION(9, 4, 4))) &&
3726e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3727e098bc96SEvan Quan 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3728e098bc96SEvan Quan 		return 0;
3729e098bc96SEvan Quan 
37308572fa2aSAsad Kamal 	/* no mclk on APUs other than gc 9,4,3*/
37318572fa2aSAsad Kamal 	if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
3732e098bc96SEvan Quan 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3733e098bc96SEvan Quan 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3734e098bc96SEvan Quan 		return 0;
3735e098bc96SEvan Quan 
37368ecad8d6SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
37375f571c61SHawking Zhang 	    (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4)) &&
37388572fa2aSAsad Kamal 	    (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3739bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
374007864911SAsad Kamal 	     attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3741bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
374207864911SAsad Kamal 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
374307864911SAsad Kamal 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
37448572fa2aSAsad Kamal 		return 0;
37458572fa2aSAsad Kamal 
3746bfb4fd20SAsad Kamal 	/* hotspot temperature for gc 9,4,3*/
37475f571c61SHawking Zhang 	if (gc_ver == IP_VERSION(9, 4, 3) ||
37485f571c61SHawking Zhang 	    gc_ver == IP_VERSION(9, 4, 4)) {
37499cff0879SLijo Lazar 		if (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
37509cff0879SLijo Lazar 		    attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
37519cff0879SLijo Lazar 		    attr == &sensor_dev_attr_temp1_label.dev_attr.attr)
37528572fa2aSAsad Kamal 			return 0;
37538572fa2aSAsad Kamal 
37549cff0879SLijo Lazar 		if (attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
37559cff0879SLijo Lazar 		    attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)
37569cff0879SLijo Lazar 			return attr->mode;
37579cff0879SLijo Lazar 	}
37589cff0879SLijo Lazar 
37598572fa2aSAsad Kamal 	/* only SOC15 dGPUs support hotspot and mem temperatures */
37609cff0879SLijo Lazar 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
376107864911SAsad Kamal 	    (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3762e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3763e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3764e098bc96SEvan Quan 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3765bfb4fd20SAsad Kamal 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
3766e098bc96SEvan Quan 		return 0;
3767e098bc96SEvan Quan 
3768ae07970aSXiaomeng Hou 	/* only Vangogh has fast PPT limit and power labels */
37698ecad8d6SLijo Lazar 	if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3770ae07970aSXiaomeng Hou 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3771ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3772ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3773ae07970aSXiaomeng Hou 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
37746e58941cSEric Huang 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3775de7fbd02SYang Wang 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3776ae07970aSXiaomeng Hou 		return 0;
3777ae07970aSXiaomeng Hou 
3778e098bc96SEvan Quan 	return effective_mode;
3779e098bc96SEvan Quan }
3780e098bc96SEvan Quan 
3781e098bc96SEvan Quan static const struct attribute_group hwmon_attrgroup = {
3782e098bc96SEvan Quan 	.attrs = hwmon_attributes,
3783e098bc96SEvan Quan 	.is_visible = hwmon_attributes_visible,
3784e098bc96SEvan Quan };
3785e098bc96SEvan Quan 
3786e098bc96SEvan Quan static const struct attribute_group *hwmon_groups[] = {
3787e098bc96SEvan Quan 	&hwmon_attrgroup,
3788e098bc96SEvan Quan 	NULL
3789e098bc96SEvan Quan };
3790e098bc96SEvan Quan 
3791d7bf1b55SEvan Quan static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
3792d7bf1b55SEvan Quan 				       enum pp_clock_type od_type,
3793d7bf1b55SEvan Quan 				       char *buf)
3794d7bf1b55SEvan Quan {
3795d7bf1b55SEvan Quan 	int size = 0;
3796d7bf1b55SEvan Quan 	int ret;
3797d7bf1b55SEvan Quan 
3798d7bf1b55SEvan Quan 	if (amdgpu_in_reset(adev))
3799d7bf1b55SEvan Quan 		return -EPERM;
3800d7bf1b55SEvan Quan 	if (adev->in_suspend && !adev->in_runpm)
3801d7bf1b55SEvan Quan 		return -EPERM;
3802d7bf1b55SEvan Quan 
3803d7bf1b55SEvan Quan 	ret = pm_runtime_get_sync(adev->dev);
3804d7bf1b55SEvan Quan 	if (ret < 0) {
3805d7bf1b55SEvan Quan 		pm_runtime_put_autosuspend(adev->dev);
3806d7bf1b55SEvan Quan 		return ret;
3807d7bf1b55SEvan Quan 	}
3808d7bf1b55SEvan Quan 
3809d7bf1b55SEvan Quan 	size = amdgpu_dpm_print_clock_levels(adev, od_type, buf);
3810d7bf1b55SEvan Quan 	if (size == 0)
3811d7bf1b55SEvan Quan 		size = sysfs_emit(buf, "\n");
3812d7bf1b55SEvan Quan 
3813d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3814d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3815d7bf1b55SEvan Quan 
3816d7bf1b55SEvan Quan 	return size;
3817d7bf1b55SEvan Quan }
3818d7bf1b55SEvan Quan 
3819d7bf1b55SEvan Quan static int parse_input_od_command_lines(const char *buf,
3820d7bf1b55SEvan Quan 					size_t count,
3821d7bf1b55SEvan Quan 					u32 *type,
3822d7bf1b55SEvan Quan 					long *params,
3823d7bf1b55SEvan Quan 					uint32_t *num_of_params)
3824d7bf1b55SEvan Quan {
3825d7bf1b55SEvan Quan 	const char delimiter[3] = {' ', '\n', '\0'};
3826d7bf1b55SEvan Quan 	uint32_t parameter_size = 0;
3827d7bf1b55SEvan Quan 	char buf_cpy[128] = {0};
3828d7bf1b55SEvan Quan 	char *tmp_str, *sub_str;
3829d7bf1b55SEvan Quan 	int ret;
3830d7bf1b55SEvan Quan 
3831d7bf1b55SEvan Quan 	if (count > sizeof(buf_cpy) - 1)
3832d7bf1b55SEvan Quan 		return -EINVAL;
3833d7bf1b55SEvan Quan 
3834d7bf1b55SEvan Quan 	memcpy(buf_cpy, buf, count);
3835d7bf1b55SEvan Quan 	tmp_str = buf_cpy;
3836d7bf1b55SEvan Quan 
3837d7bf1b55SEvan Quan 	/* skip heading spaces */
3838d7bf1b55SEvan Quan 	while (isspace(*tmp_str))
3839d7bf1b55SEvan Quan 		tmp_str++;
3840d7bf1b55SEvan Quan 
3841d7bf1b55SEvan Quan 	switch (*tmp_str) {
3842d7bf1b55SEvan Quan 	case 'c':
3843d7bf1b55SEvan Quan 		*type = PP_OD_COMMIT_DPM_TABLE;
3844d7bf1b55SEvan Quan 		return 0;
3845f7f9e48fSMa Jun 	case 'r':
3846f7f9e48fSMa Jun 		params[parameter_size] = *type;
3847f7f9e48fSMa Jun 		*num_of_params = 1;
3848f7f9e48fSMa Jun 		*type = PP_OD_RESTORE_DEFAULT_TABLE;
3849f7f9e48fSMa Jun 		return 0;
3850d7bf1b55SEvan Quan 	default:
3851d7bf1b55SEvan Quan 		break;
3852d7bf1b55SEvan Quan 	}
3853d7bf1b55SEvan Quan 
3854d7bf1b55SEvan Quan 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
3855d7bf1b55SEvan Quan 		if (strlen(sub_str) == 0)
3856d7bf1b55SEvan Quan 			continue;
3857d7bf1b55SEvan Quan 
3858d7bf1b55SEvan Quan 		ret = kstrtol(sub_str, 0, &params[parameter_size]);
3859d7bf1b55SEvan Quan 		if (ret)
3860d7bf1b55SEvan Quan 			return -EINVAL;
3861d7bf1b55SEvan Quan 		parameter_size++;
3862d7bf1b55SEvan Quan 
3863d7bf1b55SEvan Quan 		while (isspace(*tmp_str))
3864d7bf1b55SEvan Quan 			tmp_str++;
3865d7bf1b55SEvan Quan 	}
3866d7bf1b55SEvan Quan 
3867d7bf1b55SEvan Quan 	*num_of_params = parameter_size;
3868d7bf1b55SEvan Quan 
3869d7bf1b55SEvan Quan 	return 0;
3870d7bf1b55SEvan Quan }
3871d7bf1b55SEvan Quan 
3872d7bf1b55SEvan Quan static int
3873d7bf1b55SEvan Quan amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
3874d7bf1b55SEvan Quan 				     enum PP_OD_DPM_TABLE_COMMAND cmd_type,
3875d7bf1b55SEvan Quan 				     const char *in_buf,
3876d7bf1b55SEvan Quan 				     size_t count)
3877d7bf1b55SEvan Quan {
3878d7bf1b55SEvan Quan 	uint32_t parameter_size = 0;
3879d7bf1b55SEvan Quan 	long parameter[64];
3880d7bf1b55SEvan Quan 	int ret;
3881d7bf1b55SEvan Quan 
3882d7bf1b55SEvan Quan 	if (amdgpu_in_reset(adev))
3883d7bf1b55SEvan Quan 		return -EPERM;
3884d7bf1b55SEvan Quan 	if (adev->in_suspend && !adev->in_runpm)
3885d7bf1b55SEvan Quan 		return -EPERM;
3886d7bf1b55SEvan Quan 
3887d7bf1b55SEvan Quan 	ret = parse_input_od_command_lines(in_buf,
3888d7bf1b55SEvan Quan 					   count,
3889d7bf1b55SEvan Quan 					   &cmd_type,
3890d7bf1b55SEvan Quan 					   parameter,
3891d7bf1b55SEvan Quan 					   &parameter_size);
3892d7bf1b55SEvan Quan 	if (ret)
3893d7bf1b55SEvan Quan 		return ret;
3894d7bf1b55SEvan Quan 
3895d7bf1b55SEvan Quan 	ret = pm_runtime_get_sync(adev->dev);
3896d7bf1b55SEvan Quan 	if (ret < 0)
3897d7bf1b55SEvan Quan 		goto err_out0;
3898d7bf1b55SEvan Quan 
3899d7bf1b55SEvan Quan 	ret = amdgpu_dpm_odn_edit_dpm_table(adev,
3900d7bf1b55SEvan Quan 					    cmd_type,
3901d7bf1b55SEvan Quan 					    parameter,
3902d7bf1b55SEvan Quan 					    parameter_size);
3903d7bf1b55SEvan Quan 	if (ret)
3904d7bf1b55SEvan Quan 		goto err_out1;
3905d7bf1b55SEvan Quan 
3906d7bf1b55SEvan Quan 	if (cmd_type == PP_OD_COMMIT_DPM_TABLE) {
3907d7bf1b55SEvan Quan 		ret = amdgpu_dpm_dispatch_task(adev,
3908d7bf1b55SEvan Quan 					       AMD_PP_TASK_READJUST_POWER_STATE,
3909d7bf1b55SEvan Quan 					       NULL);
3910d7bf1b55SEvan Quan 		if (ret)
3911d7bf1b55SEvan Quan 			goto err_out1;
3912d7bf1b55SEvan Quan 	}
3913d7bf1b55SEvan Quan 
3914d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3915d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3916d7bf1b55SEvan Quan 
3917d7bf1b55SEvan Quan 	return count;
3918d7bf1b55SEvan Quan 
3919d7bf1b55SEvan Quan err_out1:
3920d7bf1b55SEvan Quan 	pm_runtime_mark_last_busy(adev->dev);
3921d7bf1b55SEvan Quan err_out0:
3922d7bf1b55SEvan Quan 	pm_runtime_put_autosuspend(adev->dev);
3923d7bf1b55SEvan Quan 
3924d7bf1b55SEvan Quan 	return ret;
3925d7bf1b55SEvan Quan }
3926d7bf1b55SEvan Quan 
3927d7bf1b55SEvan Quan /**
3928d7bf1b55SEvan Quan  * DOC: fan_curve
3929d7bf1b55SEvan Quan  *
3930d7bf1b55SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the fan
3931d7bf1b55SEvan Quan  * control curve line.
3932d7bf1b55SEvan Quan  *
3933d7bf1b55SEvan Quan  * Reading back the file shows you the current settings(temperature in Celsius
3934d7bf1b55SEvan Quan  * degree and fan speed in pwm) applied to every anchor point of the curve line
3935d7bf1b55SEvan Quan  * and their permitted ranges if changable.
3936d7bf1b55SEvan Quan  *
3937d7bf1b55SEvan Quan  * Writing a desired string(with the format like "anchor_point_index temperature
3938d7bf1b55SEvan Quan  * fan_speed_in_pwm") to the file, change the settings for the specific anchor
3939d7bf1b55SEvan Quan  * point accordingly.
3940d7bf1b55SEvan Quan  *
3941d7bf1b55SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
3942d7bf1b55SEvan Quan  * your changes.
3943d7bf1b55SEvan Quan  *
3944f7f9e48fSMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
3945f7f9e48fSMa Jun  * reset them
3946f7f9e48fSMa Jun  *
3947d7bf1b55SEvan Quan  * There are two fan control modes supported: auto and manual. With auto mode,
3948d7bf1b55SEvan Quan  * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature).
3949d7bf1b55SEvan Quan  * While with manual mode, users can set their own fan curve line as what
3950d7bf1b55SEvan Quan  * described here. Normally the ASIC is booted up with auto mode. Any
3951d7bf1b55SEvan Quan  * settings via this interface will switch the fan control to manual mode
3952d7bf1b55SEvan Quan  * implicitly.
3953d7bf1b55SEvan Quan  */
3954d7bf1b55SEvan Quan static ssize_t fan_curve_show(struct kobject *kobj,
3955d7bf1b55SEvan Quan 			      struct kobj_attribute *attr,
3956d7bf1b55SEvan Quan 			      char *buf)
3957d7bf1b55SEvan Quan {
3958d7bf1b55SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3959d7bf1b55SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3960d7bf1b55SEvan Quan 
3961d7bf1b55SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf);
3962d7bf1b55SEvan Quan }
3963d7bf1b55SEvan Quan 
3964d7bf1b55SEvan Quan static ssize_t fan_curve_store(struct kobject *kobj,
3965d7bf1b55SEvan Quan 			       struct kobj_attribute *attr,
3966d7bf1b55SEvan Quan 			       const char *buf,
3967d7bf1b55SEvan Quan 			       size_t count)
3968d7bf1b55SEvan Quan {
3969d7bf1b55SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3970d7bf1b55SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3971d7bf1b55SEvan Quan 
3972d7bf1b55SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3973d7bf1b55SEvan Quan 							     PP_OD_EDIT_FAN_CURVE,
3974d7bf1b55SEvan Quan 							     buf,
3975d7bf1b55SEvan Quan 							     count);
3976d7bf1b55SEvan Quan }
3977d7bf1b55SEvan Quan 
3978d7bf1b55SEvan Quan static umode_t fan_curve_visible(struct amdgpu_device *adev)
3979d7bf1b55SEvan Quan {
3980d7bf1b55SEvan Quan 	umode_t umode = 0000;
3981d7bf1b55SEvan Quan 
3982d7bf1b55SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE)
3983d7bf1b55SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3984d7bf1b55SEvan Quan 
3985d7bf1b55SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET)
3986d7bf1b55SEvan Quan 		umode |= S_IWUSR;
3987d7bf1b55SEvan Quan 
3988d7bf1b55SEvan Quan 	return umode;
3989d7bf1b55SEvan Quan }
3990d7bf1b55SEvan Quan 
3991548009adSEvan Quan /**
3992548009adSEvan Quan  * DOC: acoustic_limit_rpm_threshold
3993548009adSEvan Quan  *
3994548009adSEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
3995548009adSEvan Quan  * acoustic limit in RPM for fan control.
3996548009adSEvan Quan  *
3997548009adSEvan Quan  * Reading back the file shows you the current setting and the permitted
3998548009adSEvan Quan  * ranges if changable.
3999548009adSEvan Quan  *
4000548009adSEvan Quan  * Writing an integer to the file, change the setting accordingly.
4001548009adSEvan Quan  *
4002548009adSEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
4003548009adSEvan Quan  * your changes.
4004548009adSEvan Quan  *
40051007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
40061007bc36SMa Jun  * reset them
40071007bc36SMa Jun  *
4008548009adSEvan Quan  * This setting works under auto fan control mode only. It adjusts the PMFW's
4009548009adSEvan Quan  * behavior about the maximum speed in RPM the fan can spin. Setting via this
4010548009adSEvan Quan  * interface will switch the fan control to auto mode implicitly.
4011548009adSEvan Quan  */
4012548009adSEvan Quan static ssize_t acoustic_limit_threshold_show(struct kobject *kobj,
4013548009adSEvan Quan 					     struct kobj_attribute *attr,
4014548009adSEvan Quan 					     char *buf)
4015548009adSEvan Quan {
4016548009adSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4017548009adSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4018548009adSEvan Quan 
4019548009adSEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf);
4020548009adSEvan Quan }
4021548009adSEvan Quan 
4022548009adSEvan Quan static ssize_t acoustic_limit_threshold_store(struct kobject *kobj,
4023548009adSEvan Quan 					      struct kobj_attribute *attr,
4024548009adSEvan Quan 					      const char *buf,
4025548009adSEvan Quan 					      size_t count)
4026548009adSEvan Quan {
4027548009adSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4028548009adSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4029548009adSEvan Quan 
4030548009adSEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4031548009adSEvan Quan 							     PP_OD_EDIT_ACOUSTIC_LIMIT,
4032548009adSEvan Quan 							     buf,
4033548009adSEvan Quan 							     count);
4034548009adSEvan Quan }
4035548009adSEvan Quan 
4036548009adSEvan Quan static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev)
4037548009adSEvan Quan {
4038548009adSEvan Quan 	umode_t umode = 0000;
4039548009adSEvan Quan 
4040548009adSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE)
4041548009adSEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4042548009adSEvan Quan 
4043548009adSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET)
4044548009adSEvan Quan 		umode |= S_IWUSR;
4045548009adSEvan Quan 
4046548009adSEvan Quan 	return umode;
4047548009adSEvan Quan }
4048548009adSEvan Quan 
404947cf6fcbSEvan Quan /**
405047cf6fcbSEvan Quan  * DOC: acoustic_target_rpm_threshold
405147cf6fcbSEvan Quan  *
405247cf6fcbSEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
405347cf6fcbSEvan Quan  * acoustic target in RPM for fan control.
405447cf6fcbSEvan Quan  *
405547cf6fcbSEvan Quan  * Reading back the file shows you the current setting and the permitted
405647cf6fcbSEvan Quan  * ranges if changable.
405747cf6fcbSEvan Quan  *
405847cf6fcbSEvan Quan  * Writing an integer to the file, change the setting accordingly.
405947cf6fcbSEvan Quan  *
406047cf6fcbSEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
406147cf6fcbSEvan Quan  * your changes.
406247cf6fcbSEvan Quan  *
40631007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
40641007bc36SMa Jun  * reset them
40651007bc36SMa Jun  *
406647cf6fcbSEvan Quan  * This setting works under auto fan control mode only. It can co-exist with
406747cf6fcbSEvan Quan  * other settings which can work also under auto mode. It adjusts the PMFW's
406847cf6fcbSEvan Quan  * behavior about the maximum speed in RPM the fan can spin when ASIC
406947cf6fcbSEvan Quan  * temperature is not greater than target temperature. Setting via this
407047cf6fcbSEvan Quan  * interface will switch the fan control to auto mode implicitly.
407147cf6fcbSEvan Quan  */
407247cf6fcbSEvan Quan static ssize_t acoustic_target_threshold_show(struct kobject *kobj,
407347cf6fcbSEvan Quan 					      struct kobj_attribute *attr,
407447cf6fcbSEvan Quan 					      char *buf)
407547cf6fcbSEvan Quan {
407647cf6fcbSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
407747cf6fcbSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
407847cf6fcbSEvan Quan 
407947cf6fcbSEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf);
408047cf6fcbSEvan Quan }
408147cf6fcbSEvan Quan 
408247cf6fcbSEvan Quan static ssize_t acoustic_target_threshold_store(struct kobject *kobj,
408347cf6fcbSEvan Quan 					       struct kobj_attribute *attr,
408447cf6fcbSEvan Quan 					       const char *buf,
408547cf6fcbSEvan Quan 					       size_t count)
408647cf6fcbSEvan Quan {
408747cf6fcbSEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
408847cf6fcbSEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
408947cf6fcbSEvan Quan 
409047cf6fcbSEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
409147cf6fcbSEvan Quan 							     PP_OD_EDIT_ACOUSTIC_TARGET,
409247cf6fcbSEvan Quan 							     buf,
409347cf6fcbSEvan Quan 							     count);
409447cf6fcbSEvan Quan }
409547cf6fcbSEvan Quan 
409647cf6fcbSEvan Quan static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev)
409747cf6fcbSEvan Quan {
409847cf6fcbSEvan Quan 	umode_t umode = 0000;
409947cf6fcbSEvan Quan 
410047cf6fcbSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE)
410147cf6fcbSEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
410247cf6fcbSEvan Quan 
410347cf6fcbSEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET)
410447cf6fcbSEvan Quan 		umode |= S_IWUSR;
410547cf6fcbSEvan Quan 
410647cf6fcbSEvan Quan 	return umode;
410747cf6fcbSEvan Quan }
410847cf6fcbSEvan Quan 
4109eedd5a34SEvan Quan /**
4110eedd5a34SEvan Quan  * DOC: fan_target_temperature
4111eedd5a34SEvan Quan  *
4112eedd5a34SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
4113eedd5a34SEvan Quan  * target tempeature in Celsius degree for fan control.
4114eedd5a34SEvan Quan  *
4115eedd5a34SEvan Quan  * Reading back the file shows you the current setting and the permitted
4116eedd5a34SEvan Quan  * ranges if changable.
4117eedd5a34SEvan Quan  *
4118eedd5a34SEvan Quan  * Writing an integer to the file, change the setting accordingly.
4119eedd5a34SEvan Quan  *
4120eedd5a34SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
4121eedd5a34SEvan Quan  * your changes.
4122eedd5a34SEvan Quan  *
41231007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
41241007bc36SMa Jun  * reset them
41251007bc36SMa Jun  *
4126eedd5a34SEvan Quan  * This setting works under auto fan control mode only. It can co-exist with
4127eedd5a34SEvan Quan  * other settings which can work also under auto mode. Paring with the
4128eedd5a34SEvan Quan  * acoustic_target_rpm_threshold setting, they define the maximum speed in
4129eedd5a34SEvan Quan  * RPM the fan can spin when ASIC temperature is not greater than target
4130eedd5a34SEvan Quan  * temperature. Setting via this interface will switch the fan control to
4131eedd5a34SEvan Quan  * auto mode implicitly.
4132eedd5a34SEvan Quan  */
4133eedd5a34SEvan Quan static ssize_t fan_target_temperature_show(struct kobject *kobj,
4134eedd5a34SEvan Quan 					   struct kobj_attribute *attr,
4135eedd5a34SEvan Quan 					   char *buf)
4136eedd5a34SEvan Quan {
4137eedd5a34SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4138eedd5a34SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4139eedd5a34SEvan Quan 
4140eedd5a34SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf);
4141eedd5a34SEvan Quan }
4142eedd5a34SEvan Quan 
4143eedd5a34SEvan Quan static ssize_t fan_target_temperature_store(struct kobject *kobj,
4144eedd5a34SEvan Quan 					    struct kobj_attribute *attr,
4145eedd5a34SEvan Quan 					    const char *buf,
4146eedd5a34SEvan Quan 					    size_t count)
4147eedd5a34SEvan Quan {
4148eedd5a34SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4149eedd5a34SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4150eedd5a34SEvan Quan 
4151eedd5a34SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4152eedd5a34SEvan Quan 							     PP_OD_EDIT_FAN_TARGET_TEMPERATURE,
4153eedd5a34SEvan Quan 							     buf,
4154eedd5a34SEvan Quan 							     count);
4155eedd5a34SEvan Quan }
4156eedd5a34SEvan Quan 
4157eedd5a34SEvan Quan static umode_t fan_target_temperature_visible(struct amdgpu_device *adev)
4158eedd5a34SEvan Quan {
4159eedd5a34SEvan Quan 	umode_t umode = 0000;
4160eedd5a34SEvan Quan 
4161eedd5a34SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE)
4162eedd5a34SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4163eedd5a34SEvan Quan 
4164eedd5a34SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET)
4165eedd5a34SEvan Quan 		umode |= S_IWUSR;
4166eedd5a34SEvan Quan 
4167eedd5a34SEvan Quan 	return umode;
4168eedd5a34SEvan Quan }
4169eedd5a34SEvan Quan 
41709df5d008SEvan Quan /**
41719df5d008SEvan Quan  * DOC: fan_minimum_pwm
41729df5d008SEvan Quan  *
41739df5d008SEvan Quan  * The amdgpu driver provides a sysfs API for checking and adjusting the
41749df5d008SEvan Quan  * minimum fan speed in PWM.
41759df5d008SEvan Quan  *
41769df5d008SEvan Quan  * Reading back the file shows you the current setting and the permitted
41779df5d008SEvan Quan  * ranges if changable.
41789df5d008SEvan Quan  *
41799df5d008SEvan Quan  * Writing an integer to the file, change the setting accordingly.
41809df5d008SEvan Quan  *
41819df5d008SEvan Quan  * When you have finished the editing, write "c" (commit) to the file to commit
41829df5d008SEvan Quan  * your changes.
41839df5d008SEvan Quan  *
41841007bc36SMa Jun  * If you want to reset to the default value, write "r" (reset) to the file to
41851007bc36SMa Jun  * reset them
41861007bc36SMa Jun  *
41879df5d008SEvan Quan  * This setting works under auto fan control mode only. It can co-exist with
41889df5d008SEvan Quan  * other settings which can work also under auto mode. It adjusts the PMFW's
41899df5d008SEvan Quan  * behavior about the minimum fan speed in PWM the fan should spin. Setting
41909df5d008SEvan Quan  * via this interface will switch the fan control to auto mode implicitly.
41919df5d008SEvan Quan  */
41929df5d008SEvan Quan static ssize_t fan_minimum_pwm_show(struct kobject *kobj,
41939df5d008SEvan Quan 				    struct kobj_attribute *attr,
41949df5d008SEvan Quan 				    char *buf)
41959df5d008SEvan Quan {
41969df5d008SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
41979df5d008SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
41989df5d008SEvan Quan 
41999df5d008SEvan Quan 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf);
42009df5d008SEvan Quan }
42019df5d008SEvan Quan 
42029df5d008SEvan Quan static ssize_t fan_minimum_pwm_store(struct kobject *kobj,
42039df5d008SEvan Quan 				     struct kobj_attribute *attr,
42049df5d008SEvan Quan 				     const char *buf,
42059df5d008SEvan Quan 				     size_t count)
42069df5d008SEvan Quan {
42079df5d008SEvan Quan 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
42089df5d008SEvan Quan 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
42099df5d008SEvan Quan 
42109df5d008SEvan Quan 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
42119df5d008SEvan Quan 							     PP_OD_EDIT_FAN_MINIMUM_PWM,
42129df5d008SEvan Quan 							     buf,
42139df5d008SEvan Quan 							     count);
42149df5d008SEvan Quan }
42159df5d008SEvan Quan 
42169df5d008SEvan Quan static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev)
42179df5d008SEvan Quan {
42189df5d008SEvan Quan 	umode_t umode = 0000;
42199df5d008SEvan Quan 
42209df5d008SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE)
42219df5d008SEvan Quan 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
42229df5d008SEvan Quan 
42239df5d008SEvan Quan 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET)
42249df5d008SEvan Quan 		umode |= S_IWUSR;
42259df5d008SEvan Quan 
42269df5d008SEvan Quan 	return umode;
42279df5d008SEvan Quan }
42289df5d008SEvan Quan 
4229d7bf1b55SEvan Quan static struct od_feature_set amdgpu_od_set = {
4230d7bf1b55SEvan Quan 	.containers = {
4231d7bf1b55SEvan Quan 		[0] = {
4232d7bf1b55SEvan Quan 			.name = "fan_ctrl",
4233d7bf1b55SEvan Quan 			.sub_feature = {
4234d7bf1b55SEvan Quan 				[0] = {
4235d7bf1b55SEvan Quan 					.name = "fan_curve",
4236d7bf1b55SEvan Quan 					.ops = {
4237d7bf1b55SEvan Quan 						.is_visible = fan_curve_visible,
4238d7bf1b55SEvan Quan 						.show = fan_curve_show,
4239d7bf1b55SEvan Quan 						.store = fan_curve_store,
4240d7bf1b55SEvan Quan 					},
4241d7bf1b55SEvan Quan 				},
4242548009adSEvan Quan 				[1] = {
4243548009adSEvan Quan 					.name = "acoustic_limit_rpm_threshold",
4244548009adSEvan Quan 					.ops = {
4245548009adSEvan Quan 						.is_visible = acoustic_limit_threshold_visible,
4246548009adSEvan Quan 						.show = acoustic_limit_threshold_show,
4247548009adSEvan Quan 						.store = acoustic_limit_threshold_store,
4248548009adSEvan Quan 					},
4249548009adSEvan Quan 				},
425047cf6fcbSEvan Quan 				[2] = {
425147cf6fcbSEvan Quan 					.name = "acoustic_target_rpm_threshold",
425247cf6fcbSEvan Quan 					.ops = {
425347cf6fcbSEvan Quan 						.is_visible = acoustic_target_threshold_visible,
425447cf6fcbSEvan Quan 						.show = acoustic_target_threshold_show,
425547cf6fcbSEvan Quan 						.store = acoustic_target_threshold_store,
425647cf6fcbSEvan Quan 					},
425747cf6fcbSEvan Quan 				},
4258eedd5a34SEvan Quan 				[3] = {
4259eedd5a34SEvan Quan 					.name = "fan_target_temperature",
4260eedd5a34SEvan Quan 					.ops = {
4261eedd5a34SEvan Quan 						.is_visible = fan_target_temperature_visible,
4262eedd5a34SEvan Quan 						.show = fan_target_temperature_show,
4263eedd5a34SEvan Quan 						.store = fan_target_temperature_store,
4264eedd5a34SEvan Quan 					},
4265eedd5a34SEvan Quan 				},
42669df5d008SEvan Quan 				[4] = {
42679df5d008SEvan Quan 					.name = "fan_minimum_pwm",
42689df5d008SEvan Quan 					.ops = {
42699df5d008SEvan Quan 						.is_visible = fan_minimum_pwm_visible,
42709df5d008SEvan Quan 						.show = fan_minimum_pwm_show,
42719df5d008SEvan Quan 						.store = fan_minimum_pwm_store,
42729df5d008SEvan Quan 					},
42739df5d008SEvan Quan 				},
4274d7bf1b55SEvan Quan 			},
4275d7bf1b55SEvan Quan 		},
4276d7bf1b55SEvan Quan 	},
4277d7bf1b55SEvan Quan };
42783e38b634SEvan Quan 
42793e38b634SEvan Quan static void od_kobj_release(struct kobject *kobj)
42803e38b634SEvan Quan {
42813e38b634SEvan Quan 	struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj);
42823e38b634SEvan Quan 
42833e38b634SEvan Quan 	kfree(od_kobj);
42843e38b634SEvan Quan }
42853e38b634SEvan Quan 
42863e38b634SEvan Quan static const struct kobj_type od_ktype = {
42873e38b634SEvan Quan 	.release	= od_kobj_release,
42883e38b634SEvan Quan 	.sysfs_ops	= &kobj_sysfs_ops,
42893e38b634SEvan Quan };
42903e38b634SEvan Quan 
42913e38b634SEvan Quan static void amdgpu_od_set_fini(struct amdgpu_device *adev)
42923e38b634SEvan Quan {
42933e38b634SEvan Quan 	struct od_kobj *container, *container_next;
42943e38b634SEvan Quan 	struct od_attribute *attribute, *attribute_next;
42953e38b634SEvan Quan 
42963e38b634SEvan Quan 	if (list_empty(&adev->pm.od_kobj_list))
42973e38b634SEvan Quan 		return;
42983e38b634SEvan Quan 
42993e38b634SEvan Quan 	list_for_each_entry_safe(container, container_next,
43003e38b634SEvan Quan 				 &adev->pm.od_kobj_list, entry) {
43013e38b634SEvan Quan 		list_del(&container->entry);
43023e38b634SEvan Quan 
43033e38b634SEvan Quan 		list_for_each_entry_safe(attribute, attribute_next,
43043e38b634SEvan Quan 					 &container->attribute, entry) {
43053e38b634SEvan Quan 			list_del(&attribute->entry);
43063e38b634SEvan Quan 			sysfs_remove_file(&container->kobj,
43073e38b634SEvan Quan 					  &attribute->attribute.attr);
43083e38b634SEvan Quan 			kfree(attribute);
43093e38b634SEvan Quan 		}
43103e38b634SEvan Quan 
43113e38b634SEvan Quan 		kobject_put(&container->kobj);
43123e38b634SEvan Quan 	}
43133e38b634SEvan Quan }
43143e38b634SEvan Quan 
43153e38b634SEvan Quan static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev,
43163e38b634SEvan Quan 					   struct od_feature_ops *feature_ops)
43173e38b634SEvan Quan {
43183e38b634SEvan Quan 	umode_t mode;
43193e38b634SEvan Quan 
43203e38b634SEvan Quan 	if (!feature_ops->is_visible)
43213e38b634SEvan Quan 		return false;
43223e38b634SEvan Quan 
43233e38b634SEvan Quan 	/*
43243e38b634SEvan Quan 	 * If the feature has no user read and write mode set,
43253e38b634SEvan Quan 	 * we can assume the feature is actually not supported.(?)
43263e38b634SEvan Quan 	 * And the revelant sysfs interface should not be exposed.
43273e38b634SEvan Quan 	 */
43283e38b634SEvan Quan 	mode = feature_ops->is_visible(adev);
43293e38b634SEvan Quan 	if (mode & (S_IRUSR | S_IWUSR))
43303e38b634SEvan Quan 		return true;
43313e38b634SEvan Quan 
43323e38b634SEvan Quan 	return false;
43333e38b634SEvan Quan }
43343e38b634SEvan Quan 
43353e38b634SEvan Quan static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev,
43363e38b634SEvan Quan 					struct od_feature_container *container)
43373e38b634SEvan Quan {
43383e38b634SEvan Quan 	int i;
43393e38b634SEvan Quan 
43403e38b634SEvan Quan 	/*
43413e38b634SEvan Quan 	 * If there is no valid entry within the container, the container
43423e38b634SEvan Quan 	 * is recognized as a self contained container. And the valid entry
43433e38b634SEvan Quan 	 * here means it has a valid naming and it is visible/supported by
43443e38b634SEvan Quan 	 * the ASIC.
43453e38b634SEvan Quan 	 */
43463e38b634SEvan Quan 	for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) {
43473e38b634SEvan Quan 		if (container->sub_feature[i].name &&
43483e38b634SEvan Quan 		    amdgpu_is_od_feature_supported(adev,
43493e38b634SEvan Quan 			&container->sub_feature[i].ops))
43503e38b634SEvan Quan 			return false;
43513e38b634SEvan Quan 	}
43523e38b634SEvan Quan 
43533e38b634SEvan Quan 	return true;
43543e38b634SEvan Quan }
43553e38b634SEvan Quan 
43563e38b634SEvan Quan static int amdgpu_od_set_init(struct amdgpu_device *adev)
43573e38b634SEvan Quan {
43583e38b634SEvan Quan 	struct od_kobj *top_set, *sub_set;
43593e38b634SEvan Quan 	struct od_attribute *attribute;
43603e38b634SEvan Quan 	struct od_feature_container *container;
43613e38b634SEvan Quan 	struct od_feature_item *feature;
43623e38b634SEvan Quan 	int i, j;
43633e38b634SEvan Quan 	int ret;
43643e38b634SEvan Quan 
43653e38b634SEvan Quan 	/* Setup the top `gpu_od` directory which holds all other OD interfaces */
43663e38b634SEvan Quan 	top_set = kzalloc(sizeof(*top_set), GFP_KERNEL);
43673e38b634SEvan Quan 	if (!top_set)
43683e38b634SEvan Quan 		return -ENOMEM;
43693e38b634SEvan Quan 	list_add(&top_set->entry, &adev->pm.od_kobj_list);
43703e38b634SEvan Quan 
43713e38b634SEvan Quan 	ret = kobject_init_and_add(&top_set->kobj,
43723e38b634SEvan Quan 				   &od_ktype,
43733e38b634SEvan Quan 				   &adev->dev->kobj,
43743e38b634SEvan Quan 				   "%s",
43753e38b634SEvan Quan 				   "gpu_od");
43763e38b634SEvan Quan 	if (ret)
43773e38b634SEvan Quan 		goto err_out;
43783e38b634SEvan Quan 	INIT_LIST_HEAD(&top_set->attribute);
43793e38b634SEvan Quan 	top_set->priv = adev;
43803e38b634SEvan Quan 
43813e38b634SEvan Quan 	for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) {
43823e38b634SEvan Quan 		container = &amdgpu_od_set.containers[i];
43833e38b634SEvan Quan 
43843e38b634SEvan Quan 		if (!container->name)
43853e38b634SEvan Quan 			continue;
43863e38b634SEvan Quan 
43873e38b634SEvan Quan 		/*
43883e38b634SEvan Quan 		 * If there is valid entries within the container, the container
43893e38b634SEvan Quan 		 * will be presented as a sub directory and all its holding entries
43903e38b634SEvan Quan 		 * will be presented as plain files under it.
43913e38b634SEvan Quan 		 * While if there is no valid entry within the container, the container
43923e38b634SEvan Quan 		 * itself will be presented as a plain file under top `gpu_od` directory.
43933e38b634SEvan Quan 		 */
43943e38b634SEvan Quan 		if (amdgpu_od_is_self_contained(adev, container)) {
43953e38b634SEvan Quan 			if (!amdgpu_is_od_feature_supported(adev,
43963e38b634SEvan Quan 			     &container->ops))
43973e38b634SEvan Quan 				continue;
43983e38b634SEvan Quan 
43993e38b634SEvan Quan 			/*
44003e38b634SEvan Quan 			 * The container is presented as a plain file under top `gpu_od`
44013e38b634SEvan Quan 			 * directory.
44023e38b634SEvan Quan 			 */
44033e38b634SEvan Quan 			attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
44043e38b634SEvan Quan 			if (!attribute) {
44053e38b634SEvan Quan 				ret = -ENOMEM;
44063e38b634SEvan Quan 				goto err_out;
44073e38b634SEvan Quan 			}
44083e38b634SEvan Quan 			list_add(&attribute->entry, &top_set->attribute);
44093e38b634SEvan Quan 
44103e38b634SEvan Quan 			attribute->attribute.attr.mode =
44113e38b634SEvan Quan 					container->ops.is_visible(adev);
44123e38b634SEvan Quan 			attribute->attribute.attr.name = container->name;
44133e38b634SEvan Quan 			attribute->attribute.show =
44143e38b634SEvan Quan 					container->ops.show;
44153e38b634SEvan Quan 			attribute->attribute.store =
44163e38b634SEvan Quan 					container->ops.store;
44173e38b634SEvan Quan 			ret = sysfs_create_file(&top_set->kobj,
44183e38b634SEvan Quan 						&attribute->attribute.attr);
44193e38b634SEvan Quan 			if (ret)
44203e38b634SEvan Quan 				goto err_out;
44213e38b634SEvan Quan 		} else {
44223e38b634SEvan Quan 			/* The container is presented as a sub directory. */
44233e38b634SEvan Quan 			sub_set = kzalloc(sizeof(*sub_set), GFP_KERNEL);
44243e38b634SEvan Quan 			if (!sub_set) {
44253e38b634SEvan Quan 				ret = -ENOMEM;
44263e38b634SEvan Quan 				goto err_out;
44273e38b634SEvan Quan 			}
44283e38b634SEvan Quan 			list_add(&sub_set->entry, &adev->pm.od_kobj_list);
44293e38b634SEvan Quan 
44303e38b634SEvan Quan 			ret = kobject_init_and_add(&sub_set->kobj,
44313e38b634SEvan Quan 						   &od_ktype,
44323e38b634SEvan Quan 						   &top_set->kobj,
44333e38b634SEvan Quan 						   "%s",
44343e38b634SEvan Quan 						   container->name);
44353e38b634SEvan Quan 			if (ret)
44363e38b634SEvan Quan 				goto err_out;
44373e38b634SEvan Quan 			INIT_LIST_HEAD(&sub_set->attribute);
44383e38b634SEvan Quan 			sub_set->priv = adev;
44393e38b634SEvan Quan 
44403e38b634SEvan Quan 			for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) {
44413e38b634SEvan Quan 				feature = &container->sub_feature[j];
44423e38b634SEvan Quan 				if (!feature->name)
44433e38b634SEvan Quan 					continue;
44443e38b634SEvan Quan 
44453e38b634SEvan Quan 				if (!amdgpu_is_od_feature_supported(adev,
44463e38b634SEvan Quan 				     &feature->ops))
44473e38b634SEvan Quan 					continue;
44483e38b634SEvan Quan 
44493e38b634SEvan Quan 				/*
44503e38b634SEvan Quan 				 * With the container presented as a sub directory, the entry within
44513e38b634SEvan Quan 				 * it is presented as a plain file under the sub directory.
44523e38b634SEvan Quan 				 */
44533e38b634SEvan Quan 				attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
44543e38b634SEvan Quan 				if (!attribute) {
44553e38b634SEvan Quan 					ret = -ENOMEM;
44563e38b634SEvan Quan 					goto err_out;
44573e38b634SEvan Quan 				}
44583e38b634SEvan Quan 				list_add(&attribute->entry, &sub_set->attribute);
44593e38b634SEvan Quan 
44603e38b634SEvan Quan 				attribute->attribute.attr.mode =
44613e38b634SEvan Quan 						feature->ops.is_visible(adev);
44623e38b634SEvan Quan 				attribute->attribute.attr.name = feature->name;
44633e38b634SEvan Quan 				attribute->attribute.show =
44643e38b634SEvan Quan 						feature->ops.show;
44653e38b634SEvan Quan 				attribute->attribute.store =
44663e38b634SEvan Quan 						feature->ops.store;
44673e38b634SEvan Quan 				ret = sysfs_create_file(&sub_set->kobj,
44683e38b634SEvan Quan 							&attribute->attribute.attr);
44693e38b634SEvan Quan 				if (ret)
44703e38b634SEvan Quan 					goto err_out;
44713e38b634SEvan Quan 			}
44723e38b634SEvan Quan 		}
44733e38b634SEvan Quan 	}
44743e38b634SEvan Quan 
447569bc7a8aSMa Jun 	/*
447669bc7a8aSMa Jun 	 * If gpu_od is the only member in the list, that means gpu_od is an
447769bc7a8aSMa Jun 	 * empty directory, so remove it.
447869bc7a8aSMa Jun 	 */
447969bc7a8aSMa Jun 	if (list_is_singular(&adev->pm.od_kobj_list))
448069bc7a8aSMa Jun 		goto err_out;
448169bc7a8aSMa Jun 
44823e38b634SEvan Quan 	return 0;
44833e38b634SEvan Quan 
44843e38b634SEvan Quan err_out:
44853e38b634SEvan Quan 	amdgpu_od_set_fini(adev);
44863e38b634SEvan Quan 
44873e38b634SEvan Quan 	return ret;
44883e38b634SEvan Quan }
44893e38b634SEvan Quan 
4490e098bc96SEvan Quan int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
4491e098bc96SEvan Quan {
449288e5c8f8SMa Jun 	enum amdgpu_sriov_vf_mode mode;
4493e098bc96SEvan Quan 	uint32_t mask = 0;
44943e38b634SEvan Quan 	int ret;
4495e098bc96SEvan Quan 
4496e098bc96SEvan Quan 	if (adev->pm.sysfs_initialized)
4497e098bc96SEvan Quan 		return 0;
4498e098bc96SEvan Quan 
44995fa99373SZhenGuo Yin 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
45005fa99373SZhenGuo Yin 
4501e098bc96SEvan Quan 	if (adev->pm.dpm_enabled == 0)
4502e098bc96SEvan Quan 		return 0;
4503e098bc96SEvan Quan 
450488e5c8f8SMa Jun 	mode = amdgpu_virt_get_sriov_vf_mode(adev);
450588e5c8f8SMa Jun 
450688e5c8f8SMa Jun 	/* under multi-vf mode, the hwmon attributes are all not supported */
450788e5c8f8SMa Jun 	if (mode != SRIOV_VF_MODE_MULTI_VF) {
4508e098bc96SEvan Quan 		adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
4509e098bc96SEvan Quan 									DRIVER_NAME, adev,
4510e098bc96SEvan Quan 									hwmon_groups);
4511e098bc96SEvan Quan 		if (IS_ERR(adev->pm.int_hwmon_dev)) {
4512e098bc96SEvan Quan 			ret = PTR_ERR(adev->pm.int_hwmon_dev);
451388e5c8f8SMa Jun 			dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret);
4514e098bc96SEvan Quan 			return ret;
4515e098bc96SEvan Quan 		}
451688e5c8f8SMa Jun 	}
4517e098bc96SEvan Quan 
451888e5c8f8SMa Jun 	switch (mode) {
4519e098bc96SEvan Quan 	case SRIOV_VF_MODE_ONE_VF:
4520e098bc96SEvan Quan 		mask = ATTR_FLAG_ONEVF;
4521e098bc96SEvan Quan 		break;
4522e098bc96SEvan Quan 	case SRIOV_VF_MODE_MULTI_VF:
4523e098bc96SEvan Quan 		mask = 0;
4524e098bc96SEvan Quan 		break;
4525e098bc96SEvan Quan 	case SRIOV_VF_MODE_BARE_METAL:
4526e098bc96SEvan Quan 	default:
4527e098bc96SEvan Quan 		mask = ATTR_FLAG_MASK_ALL;
4528e098bc96SEvan Quan 		break;
4529e098bc96SEvan Quan 	}
4530e098bc96SEvan Quan 
4531e098bc96SEvan Quan 	ret = amdgpu_device_attr_create_groups(adev,
4532e098bc96SEvan Quan 					       amdgpu_device_attrs,
4533e098bc96SEvan Quan 					       ARRAY_SIZE(amdgpu_device_attrs),
4534e098bc96SEvan Quan 					       mask,
4535e098bc96SEvan Quan 					       &adev->pm.pm_attr_list);
4536e098bc96SEvan Quan 	if (ret)
45373e38b634SEvan Quan 		goto err_out0;
45383e38b634SEvan Quan 
45393e38b634SEvan Quan 	if (amdgpu_dpm_is_overdrive_supported(adev)) {
45403e38b634SEvan Quan 		ret = amdgpu_od_set_init(adev);
45413e38b634SEvan Quan 		if (ret)
45423e38b634SEvan Quan 			goto err_out1;
4543e6f1a194SMa Jun 	} else if (adev->pm.pp_feature & PP_OVERDRIVE_MASK) {
4544e6f1a194SMa Jun 		dev_info(adev->dev, "overdrive feature is not supported\n");
45453e38b634SEvan Quan 	}
4546e098bc96SEvan Quan 
4547*4d154b1cSLijo Lazar 	if (amdgpu_dpm_get_pm_policy_info(adev, PP_PM_POLICY_NONE, NULL) !=
4548*4d154b1cSLijo Lazar 	    -EOPNOTSUPP) {
4549*4d154b1cSLijo Lazar 		ret = devm_device_add_group(adev->dev,
4550*4d154b1cSLijo Lazar 					    &amdgpu_pm_policy_attr_group);
4551*4d154b1cSLijo Lazar 		if (ret)
4552*4d154b1cSLijo Lazar 			goto err_out0;
4553*4d154b1cSLijo Lazar 	}
4554*4d154b1cSLijo Lazar 
4555e098bc96SEvan Quan 	adev->pm.sysfs_initialized = true;
4556e098bc96SEvan Quan 
4557e098bc96SEvan Quan 	return 0;
45583e38b634SEvan Quan 
45593e38b634SEvan Quan err_out1:
45603e38b634SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
45613e38b634SEvan Quan err_out0:
45623e38b634SEvan Quan 	if (adev->pm.int_hwmon_dev)
45633e38b634SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
45643e38b634SEvan Quan 
45653e38b634SEvan Quan 	return ret;
4566e098bc96SEvan Quan }
4567e098bc96SEvan Quan 
4568e098bc96SEvan Quan void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
4569e098bc96SEvan Quan {
45703e38b634SEvan Quan 	amdgpu_od_set_fini(adev);
45713e38b634SEvan Quan 
4572e098bc96SEvan Quan 	if (adev->pm.int_hwmon_dev)
4573e098bc96SEvan Quan 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
4574e098bc96SEvan Quan 
4575e098bc96SEvan Quan 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4576e098bc96SEvan Quan }
4577e098bc96SEvan Quan 
4578e098bc96SEvan Quan /*
4579e098bc96SEvan Quan  * Debugfs info
4580e098bc96SEvan Quan  */
4581e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
4582e098bc96SEvan Quan 
4583517cb957SHuang Rui static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
4584e1b3bcaaSRan Sun 					   struct amdgpu_device *adev)
4585e1b3bcaaSRan Sun {
4586517cb957SHuang Rui 	uint16_t *p_val;
4587517cb957SHuang Rui 	uint32_t size;
4588517cb957SHuang Rui 	int i;
458979c65f3fSEvan Quan 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
4590517cb957SHuang Rui 
459179c65f3fSEvan Quan 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
459279c65f3fSEvan Quan 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
4593517cb957SHuang Rui 				GFP_KERNEL);
4594517cb957SHuang Rui 
4595517cb957SHuang Rui 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
4596517cb957SHuang Rui 					    (void *)p_val, &size)) {
459779c65f3fSEvan Quan 			for (i = 0; i < num_cpu_cores; i++)
4598517cb957SHuang Rui 				seq_printf(m, "\t%u MHz (CPU%d)\n",
4599517cb957SHuang Rui 					   *(p_val + i), i);
4600517cb957SHuang Rui 		}
4601517cb957SHuang Rui 
4602517cb957SHuang Rui 		kfree(p_val);
4603517cb957SHuang Rui 	}
4604517cb957SHuang Rui }
4605517cb957SHuang Rui 
4606e098bc96SEvan Quan static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
4607e098bc96SEvan Quan {
46084e8303cfSLijo Lazar 	uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
46094e8303cfSLijo Lazar 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
4610e098bc96SEvan Quan 	uint32_t value;
4611800c53d6SXiaojian Du 	uint64_t value64 = 0;
4612e098bc96SEvan Quan 	uint32_t query = 0;
4613e098bc96SEvan Quan 	int size;
4614e098bc96SEvan Quan 
4615e098bc96SEvan Quan 	/* GPU Clocks */
4616e098bc96SEvan Quan 	size = sizeof(value);
4617e098bc96SEvan Quan 	seq_printf(m, "GFX Clocks and Power:\n");
4618517cb957SHuang Rui 
4619517cb957SHuang Rui 	amdgpu_debugfs_prints_cpu_info(m, adev);
4620517cb957SHuang Rui 
4621e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
4622e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
4623e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
4624e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
4625e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
4626e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
4627e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
4628e098bc96SEvan Quan 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
4629e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
4630e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
4631e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
4632e098bc96SEvan Quan 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
4633e098bc96SEvan Quan 	size = sizeof(uint32_t);
46346127d7dfSAlex Deucher 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size)) {
46356127d7dfSAlex Deucher 		if (adev->flags & AMD_IS_APU)
46366127d7dfSAlex Deucher 			seq_printf(m, "\t%u.%02u W (average SoC including CPU)\n", query >> 8, query & 0xff);
46376127d7dfSAlex Deucher 		else
46386127d7dfSAlex Deucher 			seq_printf(m, "\t%u.%02u W (average SoC)\n", query >> 8, query & 0xff);
46396127d7dfSAlex Deucher 	}
4640e0e1764aSAlex Deucher 	size = sizeof(uint32_t);
46416127d7dfSAlex Deucher 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size)) {
46426127d7dfSAlex Deucher 		if (adev->flags & AMD_IS_APU)
46436127d7dfSAlex Deucher 			seq_printf(m, "\t%u.%02u W (current SoC including CPU)\n", query >> 8, query & 0xff);
46446127d7dfSAlex Deucher 		else
46456127d7dfSAlex Deucher 			seq_printf(m, "\t%u.%02u W (current SoC)\n", query >> 8, query & 0xff);
46466127d7dfSAlex Deucher 	}
4647e098bc96SEvan Quan 	size = sizeof(value);
4648e098bc96SEvan Quan 	seq_printf(m, "\n");
4649e098bc96SEvan Quan 
4650e098bc96SEvan Quan 	/* GPU Temp */
4651e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
4652e098bc96SEvan Quan 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
4653e098bc96SEvan Quan 
4654e098bc96SEvan Quan 	/* GPU Load */
4655e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
4656e098bc96SEvan Quan 		seq_printf(m, "GPU Load: %u %%\n", value);
4657e098bc96SEvan Quan 	/* MEM Load */
4658e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
4659e098bc96SEvan Quan 		seq_printf(m, "MEM Load: %u %%\n", value);
4660d1b2703cSXiaojian Du 	/* VCN Load */
4661d1b2703cSXiaojian Du 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_LOAD, (void *)&value, &size))
4662d1b2703cSXiaojian Du 		seq_printf(m, "VCN Load: %u %%\n", value);
4663e098bc96SEvan Quan 
4664e098bc96SEvan Quan 	seq_printf(m, "\n");
4665e098bc96SEvan Quan 
4666e098bc96SEvan Quan 	/* SMC feature mask */
4667e098bc96SEvan Quan 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
4668e098bc96SEvan Quan 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
4669e098bc96SEvan Quan 
46708ecad8d6SLijo Lazar 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
46718ecad8d6SLijo Lazar 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
4672e098bc96SEvan Quan 		/* VCN clocks */
4673e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
4674e098bc96SEvan Quan 			if (!value) {
46756127d7dfSAlex Deucher 				seq_printf(m, "VCN: Powered down\n");
4676e098bc96SEvan Quan 			} else {
46776127d7dfSAlex Deucher 				seq_printf(m, "VCN: Powered up\n");
4678e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4679e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4680e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4681e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4682e098bc96SEvan Quan 			}
4683e098bc96SEvan Quan 		}
4684e098bc96SEvan Quan 		seq_printf(m, "\n");
4685e098bc96SEvan Quan 	} else {
4686e098bc96SEvan Quan 		/* UVD clocks */
4687e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
4688e098bc96SEvan Quan 			if (!value) {
46896127d7dfSAlex Deucher 				seq_printf(m, "UVD: Powered down\n");
4690e098bc96SEvan Quan 			} else {
46916127d7dfSAlex Deucher 				seq_printf(m, "UVD: Powered up\n");
4692e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4693e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4694e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4695e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4696e098bc96SEvan Quan 			}
4697e098bc96SEvan Quan 		}
4698e098bc96SEvan Quan 		seq_printf(m, "\n");
4699e098bc96SEvan Quan 
4700e098bc96SEvan Quan 		/* VCE clocks */
4701e098bc96SEvan Quan 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
4702e098bc96SEvan Quan 			if (!value) {
47036127d7dfSAlex Deucher 				seq_printf(m, "VCE: Powered down\n");
4704e098bc96SEvan Quan 			} else {
47056127d7dfSAlex Deucher 				seq_printf(m, "VCE: Powered up\n");
4706e098bc96SEvan Quan 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
4707e098bc96SEvan Quan 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
4708e098bc96SEvan Quan 			}
4709e098bc96SEvan Quan 		}
4710e098bc96SEvan Quan 	}
4711e098bc96SEvan Quan 
4712e098bc96SEvan Quan 	return 0;
4713e098bc96SEvan Quan }
4714e098bc96SEvan Quan 
471544762718SNathan Chancellor static const struct cg_flag_name clocks[] = {
471644762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
471744762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
471844762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
471944762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
472044762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
472144762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
472244762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
472344762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
472444762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
472544762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
472644762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
472744762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
472844762718SNathan Chancellor 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
472944762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
473044762718SNathan Chancellor 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
473144762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
473244762718SNathan Chancellor 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
473344762718SNathan Chancellor 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
473444762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
473544762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
473644762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
473744762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
473844762718SNathan Chancellor 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
473944762718SNathan Chancellor 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
474044762718SNathan Chancellor 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
474144762718SNathan Chancellor 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
474244762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
474344762718SNathan Chancellor 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
474444762718SNathan Chancellor 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
474544762718SNathan Chancellor 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
474644762718SNathan Chancellor 	{AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
474744762718SNathan Chancellor 	{AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
474844762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
474944762718SNathan Chancellor 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
475044762718SNathan Chancellor 	{0, NULL},
475144762718SNathan Chancellor };
475244762718SNathan Chancellor 
475325faeddcSEvan Quan static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
4754e098bc96SEvan Quan {
4755e098bc96SEvan Quan 	int i;
4756e098bc96SEvan Quan 
4757e098bc96SEvan Quan 	for (i = 0; clocks[i].flag; i++)
4758e098bc96SEvan Quan 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
4759e098bc96SEvan Quan 			   (flags & clocks[i].flag) ? "On" : "Off");
4760e098bc96SEvan Quan }
4761e098bc96SEvan Quan 
4762373720f7SNirmoy Das static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
4763e098bc96SEvan Quan {
4764373720f7SNirmoy Das 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
4765373720f7SNirmoy Das 	struct drm_device *dev = adev_to_drm(adev);
476625faeddcSEvan Quan 	u64 flags = 0;
4767e098bc96SEvan Quan 	int r;
4768e098bc96SEvan Quan 
476953b3f8f4SDennis Li 	if (amdgpu_in_reset(adev))
4770e098bc96SEvan Quan 		return -EPERM;
4771d2ae842dSAlex Deucher 	if (adev->in_suspend && !adev->in_runpm)
4772d2ae842dSAlex Deucher 		return -EPERM;
4773e098bc96SEvan Quan 
4774e098bc96SEvan Quan 	r = pm_runtime_get_sync(dev->dev);
4775e098bc96SEvan Quan 	if (r < 0) {
4776e098bc96SEvan Quan 		pm_runtime_put_autosuspend(dev->dev);
4777e098bc96SEvan Quan 		return r;
4778e098bc96SEvan Quan 	}
4779e098bc96SEvan Quan 
478079c65f3fSEvan Quan 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
4781e098bc96SEvan Quan 		r = amdgpu_debugfs_pm_info_pp(m, adev);
4782e098bc96SEvan Quan 		if (r)
4783e098bc96SEvan Quan 			goto out;
478479c65f3fSEvan Quan 	}
4785e098bc96SEvan Quan 
4786e098bc96SEvan Quan 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
4787e098bc96SEvan Quan 
478825faeddcSEvan Quan 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
4789e098bc96SEvan Quan 	amdgpu_parse_cg_state(m, flags);
4790e098bc96SEvan Quan 	seq_printf(m, "\n");
4791e098bc96SEvan Quan 
4792e098bc96SEvan Quan out:
4793e098bc96SEvan Quan 	pm_runtime_mark_last_busy(dev->dev);
4794e098bc96SEvan Quan 	pm_runtime_put_autosuspend(dev->dev);
4795e098bc96SEvan Quan 
4796e098bc96SEvan Quan 	return r;
4797e098bc96SEvan Quan }
4798e098bc96SEvan Quan 
4799373720f7SNirmoy Das DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
4800373720f7SNirmoy Das 
480127ebf21fSLijo Lazar /*
480227ebf21fSLijo Lazar  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
480327ebf21fSLijo Lazar  *
480427ebf21fSLijo Lazar  * Reads debug memory region allocated to PMFW
480527ebf21fSLijo Lazar  */
480627ebf21fSLijo Lazar static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
480727ebf21fSLijo Lazar 					 size_t size, loff_t *pos)
480827ebf21fSLijo Lazar {
480927ebf21fSLijo Lazar 	struct amdgpu_device *adev = file_inode(f)->i_private;
481027ebf21fSLijo Lazar 	size_t smu_prv_buf_size;
481127ebf21fSLijo Lazar 	void *smu_prv_buf;
481279c65f3fSEvan Quan 	int ret = 0;
481327ebf21fSLijo Lazar 
481427ebf21fSLijo Lazar 	if (amdgpu_in_reset(adev))
481527ebf21fSLijo Lazar 		return -EPERM;
481627ebf21fSLijo Lazar 	if (adev->in_suspend && !adev->in_runpm)
481727ebf21fSLijo Lazar 		return -EPERM;
481827ebf21fSLijo Lazar 
481979c65f3fSEvan Quan 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
482079c65f3fSEvan Quan 	if (ret)
482179c65f3fSEvan Quan 		return ret;
482227ebf21fSLijo Lazar 
482327ebf21fSLijo Lazar 	if (!smu_prv_buf || !smu_prv_buf_size)
482427ebf21fSLijo Lazar 		return -EINVAL;
482527ebf21fSLijo Lazar 
482627ebf21fSLijo Lazar 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
482727ebf21fSLijo Lazar 				       smu_prv_buf_size);
482827ebf21fSLijo Lazar }
482927ebf21fSLijo Lazar 
483027ebf21fSLijo Lazar static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
483127ebf21fSLijo Lazar 	.owner = THIS_MODULE,
483227ebf21fSLijo Lazar 	.open = simple_open,
483327ebf21fSLijo Lazar 	.read = amdgpu_pm_prv_buffer_read,
483427ebf21fSLijo Lazar 	.llseek = default_llseek,
483527ebf21fSLijo Lazar };
483627ebf21fSLijo Lazar 
4837e098bc96SEvan Quan #endif
4838e098bc96SEvan Quan 
4839373720f7SNirmoy Das void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
4840e098bc96SEvan Quan {
4841e098bc96SEvan Quan #if defined(CONFIG_DEBUG_FS)
4842373720f7SNirmoy Das 	struct drm_minor *minor = adev_to_drm(adev)->primary;
4843373720f7SNirmoy Das 	struct dentry *root = minor->debugfs_root;
4844373720f7SNirmoy Das 
48451613f346SFlora Cui 	if (!adev->pm.dpm_enabled)
48461613f346SFlora Cui 		return;
48471613f346SFlora Cui 
4848373720f7SNirmoy Das 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
4849373720f7SNirmoy Das 			    &amdgpu_debugfs_pm_info_fops);
4850373720f7SNirmoy Das 
485127ebf21fSLijo Lazar 	if (adev->pm.smu_prv_buffer_size > 0)
485227ebf21fSLijo Lazar 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
485327ebf21fSLijo Lazar 					 adev,
485427ebf21fSLijo Lazar 					 &amdgpu_debugfs_pm_prv_buffer_fops,
485527ebf21fSLijo Lazar 					 adev->pm.smu_prv_buffer_size);
48561f5fc7a5SAndrey Grodzovsky 
485779c65f3fSEvan Quan 	amdgpu_dpm_stb_debug_fs_init(adev);
4858e098bc96SEvan Quan #endif
4859e098bc96SEvan Quan }
4860