xref: /openbsd/sys/dev/pci/drm/amd/pm/amdgpu_pm.c (revision 73471bf0)
1 /*
2  * Copyright 2017 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Rafał Miłecki <zajec5@gmail.com>
23  *          Alex Deucher <alexdeucher@gmail.com>
24  */
25 
26 #include <drm/drm_debugfs.h>
27 
28 #include "amdgpu.h"
29 #include "amdgpu_drv.h"
30 #include "amdgpu_pm.h"
31 #include "amdgpu_dpm.h"
32 #include "amdgpu_smu.h"
33 #include "atom.h"
34 #include <linux/pci.h>
35 #include <linux/hwmon.h>
36 #include <linux/hwmon-sysfs.h>
37 #include <linux/nospec.h>
38 #include <linux/pm_runtime.h>
39 #include "hwmgr.h"
40 
41 static const struct cg_flag_name clocks[] = {
42 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
43 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
44 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
45 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
46 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
47 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
48 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
49 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
50 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
51 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
52 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
53 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
54 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
55 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
56 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
57 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
58 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
59 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
60 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
61 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
62 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
63 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
64 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
65 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
66 
67 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
68 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
69 	{0, NULL},
70 };
71 
72 static const struct hwmon_temp_label {
73 	enum PP_HWMON_TEMP channel;
74 	const char *label;
75 } temp_label[] = {
76 	{PP_TEMP_EDGE, "edge"},
77 	{PP_TEMP_JUNCTION, "junction"},
78 	{PP_TEMP_MEM, "mem"},
79 };
80 
81 #ifdef __linux__
82 
83 /**
84  * DOC: power_dpm_state
85  *
86  * The power_dpm_state file is a legacy interface and is only provided for
87  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
88  * certain power related parameters.  The file power_dpm_state is used for this.
89  * It accepts the following arguments:
90  *
91  * - battery
92  *
93  * - balanced
94  *
95  * - performance
96  *
97  * battery
98  *
99  * On older GPUs, the vbios provided a special power state for battery
100  * operation.  Selecting battery switched to this state.  This is no
101  * longer provided on newer GPUs so the option does nothing in that case.
102  *
103  * balanced
104  *
105  * On older GPUs, the vbios provided a special power state for balanced
106  * operation.  Selecting balanced switched to this state.  This is no
107  * longer provided on newer GPUs so the option does nothing in that case.
108  *
109  * performance
110  *
111  * On older GPUs, the vbios provided a special power state for performance
112  * operation.  Selecting performance switched to this state.  This is no
113  * longer provided on newer GPUs so the option does nothing in that case.
114  *
115  */
116 
117 static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
118 					  struct device_attribute *attr,
119 					  char *buf)
120 {
121 	struct drm_device *ddev = dev_get_drvdata(dev);
122 	struct amdgpu_device *adev = drm_to_adev(ddev);
123 	enum amd_pm_state_type pm;
124 	int ret;
125 
126 	if (amdgpu_in_reset(adev))
127 		return -EPERM;
128 
129 	ret = pm_runtime_get_sync(ddev->dev);
130 	if (ret < 0) {
131 		pm_runtime_put_autosuspend(ddev->dev);
132 		return ret;
133 	}
134 
135 	if (is_support_sw_smu(adev)) {
136 		if (adev->smu.ppt_funcs->get_current_power_state)
137 			pm = smu_get_current_power_state(&adev->smu);
138 		else
139 			pm = adev->pm.dpm.user_state;
140 	} else if (adev->powerplay.pp_funcs->get_current_power_state) {
141 		pm = amdgpu_dpm_get_current_power_state(adev);
142 	} else {
143 		pm = adev->pm.dpm.user_state;
144 	}
145 
146 	pm_runtime_mark_last_busy(ddev->dev);
147 	pm_runtime_put_autosuspend(ddev->dev);
148 
149 	return snprintf(buf, PAGE_SIZE, "%s\n",
150 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
151 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
152 }
153 
154 static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
155 					  struct device_attribute *attr,
156 					  const char *buf,
157 					  size_t count)
158 {
159 	struct drm_device *ddev = dev_get_drvdata(dev);
160 	struct amdgpu_device *adev = drm_to_adev(ddev);
161 	enum amd_pm_state_type  state;
162 	int ret;
163 
164 	if (amdgpu_in_reset(adev))
165 		return -EPERM;
166 
167 	if (strncmp("battery", buf, strlen("battery")) == 0)
168 		state = POWER_STATE_TYPE_BATTERY;
169 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
170 		state = POWER_STATE_TYPE_BALANCED;
171 	else if (strncmp("performance", buf, strlen("performance")) == 0)
172 		state = POWER_STATE_TYPE_PERFORMANCE;
173 	else
174 		return -EINVAL;
175 
176 	ret = pm_runtime_get_sync(ddev->dev);
177 	if (ret < 0) {
178 		pm_runtime_put_autosuspend(ddev->dev);
179 		return ret;
180 	}
181 
182 	if (is_support_sw_smu(adev)) {
183 		mutex_lock(&adev->pm.mutex);
184 		adev->pm.dpm.user_state = state;
185 		mutex_unlock(&adev->pm.mutex);
186 	} else if (adev->powerplay.pp_funcs->dispatch_tasks) {
187 		amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
188 	} else {
189 		mutex_lock(&adev->pm.mutex);
190 		adev->pm.dpm.user_state = state;
191 		mutex_unlock(&adev->pm.mutex);
192 
193 		amdgpu_pm_compute_clocks(adev);
194 	}
195 	pm_runtime_mark_last_busy(ddev->dev);
196 	pm_runtime_put_autosuspend(ddev->dev);
197 
198 	return count;
199 }
200 
201 
202 /**
203  * DOC: power_dpm_force_performance_level
204  *
205  * The amdgpu driver provides a sysfs API for adjusting certain power
206  * related parameters.  The file power_dpm_force_performance_level is
207  * used for this.  It accepts the following arguments:
208  *
209  * - auto
210  *
211  * - low
212  *
213  * - high
214  *
215  * - manual
216  *
217  * - profile_standard
218  *
219  * - profile_min_sclk
220  *
221  * - profile_min_mclk
222  *
223  * - profile_peak
224  *
225  * auto
226  *
227  * When auto is selected, the driver will attempt to dynamically select
228  * the optimal power profile for current conditions in the driver.
229  *
230  * low
231  *
232  * When low is selected, the clocks are forced to the lowest power state.
233  *
234  * high
235  *
236  * When high is selected, the clocks are forced to the highest power state.
237  *
238  * manual
239  *
240  * When manual is selected, the user can manually adjust which power states
241  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
242  * and pp_dpm_pcie files and adjust the power state transition heuristics
243  * via the pp_power_profile_mode sysfs file.
244  *
245  * profile_standard
246  * profile_min_sclk
247  * profile_min_mclk
248  * profile_peak
249  *
250  * When the profiling modes are selected, clock and power gating are
251  * disabled and the clocks are set for different profiling cases. This
252  * mode is recommended for profiling specific work loads where you do
253  * not want clock or power gating for clock fluctuation to interfere
254  * with your results. profile_standard sets the clocks to a fixed clock
255  * level which varies from asic to asic.  profile_min_sclk forces the sclk
256  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
257  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
258  *
259  */
260 
261 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
262 							    struct device_attribute *attr,
263 							    char *buf)
264 {
265 	struct drm_device *ddev = dev_get_drvdata(dev);
266 	struct amdgpu_device *adev = drm_to_adev(ddev);
267 	enum amd_dpm_forced_level level = 0xff;
268 	int ret;
269 
270 	if (amdgpu_in_reset(adev))
271 		return -EPERM;
272 
273 	ret = pm_runtime_get_sync(ddev->dev);
274 	if (ret < 0) {
275 		pm_runtime_put_autosuspend(ddev->dev);
276 		return ret;
277 	}
278 
279 	if (is_support_sw_smu(adev))
280 		level = smu_get_performance_level(&adev->smu);
281 	else if (adev->powerplay.pp_funcs->get_performance_level)
282 		level = amdgpu_dpm_get_performance_level(adev);
283 	else
284 		level = adev->pm.dpm.forced_level;
285 
286 	pm_runtime_mark_last_busy(ddev->dev);
287 	pm_runtime_put_autosuspend(ddev->dev);
288 
289 	return snprintf(buf, PAGE_SIZE, "%s\n",
290 			(level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
291 			(level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
292 			(level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
293 			(level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
294 			(level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
295 			(level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
296 			(level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
297 			(level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
298 			"unknown");
299 }
300 
301 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
302 							    struct device_attribute *attr,
303 							    const char *buf,
304 							    size_t count)
305 {
306 	struct drm_device *ddev = dev_get_drvdata(dev);
307 	struct amdgpu_device *adev = drm_to_adev(ddev);
308 	enum amd_dpm_forced_level level;
309 	enum amd_dpm_forced_level current_level = 0xff;
310 	int ret = 0;
311 
312 	if (amdgpu_in_reset(adev))
313 		return -EPERM;
314 
315 	if (strncmp("low", buf, strlen("low")) == 0) {
316 		level = AMD_DPM_FORCED_LEVEL_LOW;
317 	} else if (strncmp("high", buf, strlen("high")) == 0) {
318 		level = AMD_DPM_FORCED_LEVEL_HIGH;
319 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
320 		level = AMD_DPM_FORCED_LEVEL_AUTO;
321 	} else if (strncmp("manual", buf, strlen("manual")) == 0) {
322 		level = AMD_DPM_FORCED_LEVEL_MANUAL;
323 	} else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
324 		level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
325 	} else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
326 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
327 	} else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
328 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
329 	} else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
330 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
331 	} else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
332 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
333 	}  else {
334 		return -EINVAL;
335 	}
336 
337 	ret = pm_runtime_get_sync(ddev->dev);
338 	if (ret < 0) {
339 		pm_runtime_put_autosuspend(ddev->dev);
340 		return ret;
341 	}
342 
343 	if (is_support_sw_smu(adev))
344 		current_level = smu_get_performance_level(&adev->smu);
345 	else if (adev->powerplay.pp_funcs->get_performance_level)
346 		current_level = amdgpu_dpm_get_performance_level(adev);
347 
348 	if (current_level == level) {
349 		pm_runtime_mark_last_busy(ddev->dev);
350 		pm_runtime_put_autosuspend(ddev->dev);
351 		return count;
352 	}
353 
354 	if (adev->asic_type == CHIP_RAVEN) {
355 		if (!(adev->apu_flags & AMD_APU_IS_RAVEN2)) {
356 			if (current_level != AMD_DPM_FORCED_LEVEL_MANUAL && level == AMD_DPM_FORCED_LEVEL_MANUAL)
357 				amdgpu_gfx_off_ctrl(adev, false);
358 			else if (current_level == AMD_DPM_FORCED_LEVEL_MANUAL && level != AMD_DPM_FORCED_LEVEL_MANUAL)
359 				amdgpu_gfx_off_ctrl(adev, true);
360 		}
361 	}
362 
363 	/* profile_exit setting is valid only when current mode is in profile mode */
364 	if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
365 	    AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
366 	    AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
367 	    AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
368 	    (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
369 		pr_err("Currently not in any profile mode!\n");
370 		pm_runtime_mark_last_busy(ddev->dev);
371 		pm_runtime_put_autosuspend(ddev->dev);
372 		return -EINVAL;
373 	}
374 
375 	if (is_support_sw_smu(adev)) {
376 		ret = smu_force_performance_level(&adev->smu, level);
377 		if (ret) {
378 			pm_runtime_mark_last_busy(ddev->dev);
379 			pm_runtime_put_autosuspend(ddev->dev);
380 			return -EINVAL;
381 		}
382 	} else if (adev->powerplay.pp_funcs->force_performance_level) {
383 		mutex_lock(&adev->pm.mutex);
384 		if (adev->pm.dpm.thermal_active) {
385 			mutex_unlock(&adev->pm.mutex);
386 			pm_runtime_mark_last_busy(ddev->dev);
387 			pm_runtime_put_autosuspend(ddev->dev);
388 			return -EINVAL;
389 		}
390 		ret = amdgpu_dpm_force_performance_level(adev, level);
391 		if (ret) {
392 			mutex_unlock(&adev->pm.mutex);
393 			pm_runtime_mark_last_busy(ddev->dev);
394 			pm_runtime_put_autosuspend(ddev->dev);
395 			return -EINVAL;
396 		} else {
397 			adev->pm.dpm.forced_level = level;
398 		}
399 		mutex_unlock(&adev->pm.mutex);
400 	}
401 	pm_runtime_mark_last_busy(ddev->dev);
402 	pm_runtime_put_autosuspend(ddev->dev);
403 
404 	return count;
405 }
406 
407 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
408 		struct device_attribute *attr,
409 		char *buf)
410 {
411 	struct drm_device *ddev = dev_get_drvdata(dev);
412 	struct amdgpu_device *adev = drm_to_adev(ddev);
413 	struct pp_states_info data;
414 	int i, buf_len, ret;
415 
416 	if (amdgpu_in_reset(adev))
417 		return -EPERM;
418 
419 	ret = pm_runtime_get_sync(ddev->dev);
420 	if (ret < 0) {
421 		pm_runtime_put_autosuspend(ddev->dev);
422 		return ret;
423 	}
424 
425 	if (is_support_sw_smu(adev)) {
426 		ret = smu_get_power_num_states(&adev->smu, &data);
427 		if (ret)
428 			return ret;
429 	} else if (adev->powerplay.pp_funcs->get_pp_num_states) {
430 		amdgpu_dpm_get_pp_num_states(adev, &data);
431 	} else {
432 		memset(&data, 0, sizeof(data));
433 	}
434 
435 	pm_runtime_mark_last_busy(ddev->dev);
436 	pm_runtime_put_autosuspend(ddev->dev);
437 
438 	buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
439 	for (i = 0; i < data.nums; i++)
440 		buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
441 				(data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
442 				(data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
443 				(data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
444 				(data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
445 
446 	return buf_len;
447 }
448 
449 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
450 		struct device_attribute *attr,
451 		char *buf)
452 {
453 	struct drm_device *ddev = dev_get_drvdata(dev);
454 	struct amdgpu_device *adev = drm_to_adev(ddev);
455 	struct pp_states_info data;
456 	struct smu_context *smu = &adev->smu;
457 	enum amd_pm_state_type pm = 0;
458 	int i = 0, ret = 0;
459 
460 	if (amdgpu_in_reset(adev))
461 		return -EPERM;
462 
463 	ret = pm_runtime_get_sync(ddev->dev);
464 	if (ret < 0) {
465 		pm_runtime_put_autosuspend(ddev->dev);
466 		return ret;
467 	}
468 
469 	if (is_support_sw_smu(adev)) {
470 		pm = smu_get_current_power_state(smu);
471 		ret = smu_get_power_num_states(smu, &data);
472 		if (ret)
473 			return ret;
474 	} else if (adev->powerplay.pp_funcs->get_current_power_state
475 		 && adev->powerplay.pp_funcs->get_pp_num_states) {
476 		pm = amdgpu_dpm_get_current_power_state(adev);
477 		amdgpu_dpm_get_pp_num_states(adev, &data);
478 	}
479 
480 	pm_runtime_mark_last_busy(ddev->dev);
481 	pm_runtime_put_autosuspend(ddev->dev);
482 
483 	for (i = 0; i < data.nums; i++) {
484 		if (pm == data.states[i])
485 			break;
486 	}
487 
488 	if (i == data.nums)
489 		i = -EINVAL;
490 
491 	return snprintf(buf, PAGE_SIZE, "%d\n", i);
492 }
493 
494 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
495 		struct device_attribute *attr,
496 		char *buf)
497 {
498 	struct drm_device *ddev = dev_get_drvdata(dev);
499 	struct amdgpu_device *adev = drm_to_adev(ddev);
500 
501 	if (amdgpu_in_reset(adev))
502 		return -EPERM;
503 
504 	if (adev->pp_force_state_enabled)
505 		return amdgpu_get_pp_cur_state(dev, attr, buf);
506 	else
507 		return snprintf(buf, PAGE_SIZE, "\n");
508 }
509 
510 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
511 		struct device_attribute *attr,
512 		const char *buf,
513 		size_t count)
514 {
515 	struct drm_device *ddev = dev_get_drvdata(dev);
516 	struct amdgpu_device *adev = drm_to_adev(ddev);
517 	enum amd_pm_state_type state = 0;
518 	unsigned long idx;
519 	int ret;
520 
521 	if (amdgpu_in_reset(adev))
522 		return -EPERM;
523 
524 	if (strlen(buf) == 1)
525 		adev->pp_force_state_enabled = false;
526 	else if (is_support_sw_smu(adev))
527 		adev->pp_force_state_enabled = false;
528 	else if (adev->powerplay.pp_funcs->dispatch_tasks &&
529 			adev->powerplay.pp_funcs->get_pp_num_states) {
530 		struct pp_states_info data;
531 
532 		ret = kstrtoul(buf, 0, &idx);
533 		if (ret || idx >= ARRAY_SIZE(data.states))
534 			return -EINVAL;
535 
536 		idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
537 
538 		amdgpu_dpm_get_pp_num_states(adev, &data);
539 		state = data.states[idx];
540 
541 		ret = pm_runtime_get_sync(ddev->dev);
542 		if (ret < 0) {
543 			pm_runtime_put_autosuspend(ddev->dev);
544 			return ret;
545 		}
546 
547 		/* only set user selected power states */
548 		if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
549 		    state != POWER_STATE_TYPE_DEFAULT) {
550 			amdgpu_dpm_dispatch_task(adev,
551 					AMD_PP_TASK_ENABLE_USER_STATE, &state);
552 			adev->pp_force_state_enabled = true;
553 		}
554 		pm_runtime_mark_last_busy(ddev->dev);
555 		pm_runtime_put_autosuspend(ddev->dev);
556 	}
557 
558 	return count;
559 }
560 
561 /**
562  * DOC: pp_table
563  *
564  * The amdgpu driver provides a sysfs API for uploading new powerplay
565  * tables.  The file pp_table is used for this.  Reading the file
566  * will dump the current power play table.  Writing to the file
567  * will attempt to upload a new powerplay table and re-initialize
568  * powerplay using that new table.
569  *
570  */
571 
572 static ssize_t amdgpu_get_pp_table(struct device *dev,
573 		struct device_attribute *attr,
574 		char *buf)
575 {
576 	struct drm_device *ddev = dev_get_drvdata(dev);
577 	struct amdgpu_device *adev = drm_to_adev(ddev);
578 	char *table = NULL;
579 	int size, ret;
580 
581 	if (amdgpu_in_reset(adev))
582 		return -EPERM;
583 
584 	ret = pm_runtime_get_sync(ddev->dev);
585 	if (ret < 0) {
586 		pm_runtime_put_autosuspend(ddev->dev);
587 		return ret;
588 	}
589 
590 	if (is_support_sw_smu(adev)) {
591 		size = smu_sys_get_pp_table(&adev->smu, (void **)&table);
592 		pm_runtime_mark_last_busy(ddev->dev);
593 		pm_runtime_put_autosuspend(ddev->dev);
594 		if (size < 0)
595 			return size;
596 	} else if (adev->powerplay.pp_funcs->get_pp_table) {
597 		size = amdgpu_dpm_get_pp_table(adev, &table);
598 		pm_runtime_mark_last_busy(ddev->dev);
599 		pm_runtime_put_autosuspend(ddev->dev);
600 		if (size < 0)
601 			return size;
602 	} else {
603 		pm_runtime_mark_last_busy(ddev->dev);
604 		pm_runtime_put_autosuspend(ddev->dev);
605 		return 0;
606 	}
607 
608 	if (size >= PAGE_SIZE)
609 		size = PAGE_SIZE - 1;
610 
611 	memcpy(buf, table, size);
612 
613 	return size;
614 }
615 
616 static ssize_t amdgpu_set_pp_table(struct device *dev,
617 		struct device_attribute *attr,
618 		const char *buf,
619 		size_t count)
620 {
621 	struct drm_device *ddev = dev_get_drvdata(dev);
622 	struct amdgpu_device *adev = drm_to_adev(ddev);
623 	int ret = 0;
624 
625 	if (amdgpu_in_reset(adev))
626 		return -EPERM;
627 
628 	ret = pm_runtime_get_sync(ddev->dev);
629 	if (ret < 0) {
630 		pm_runtime_put_autosuspend(ddev->dev);
631 		return ret;
632 	}
633 
634 	if (is_support_sw_smu(adev)) {
635 		ret = smu_sys_set_pp_table(&adev->smu, (void *)buf, count);
636 		if (ret) {
637 			pm_runtime_mark_last_busy(ddev->dev);
638 			pm_runtime_put_autosuspend(ddev->dev);
639 			return ret;
640 		}
641 	} else if (adev->powerplay.pp_funcs->set_pp_table)
642 		amdgpu_dpm_set_pp_table(adev, buf, count);
643 
644 	pm_runtime_mark_last_busy(ddev->dev);
645 	pm_runtime_put_autosuspend(ddev->dev);
646 
647 	return count;
648 }
649 
650 /**
651  * DOC: pp_od_clk_voltage
652  *
653  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
654  * in each power level within a power state.  The pp_od_clk_voltage is used for
655  * this.
656  *
657  * Note that the actual memory controller clock rate are exposed, not
658  * the effective memory clock of the DRAMs. To translate it, use the
659  * following formula:
660  *
661  * Clock conversion (Mhz):
662  *
663  * HBM: effective_memory_clock = memory_controller_clock * 1
664  *
665  * G5: effective_memory_clock = memory_controller_clock * 1
666  *
667  * G6: effective_memory_clock = memory_controller_clock * 2
668  *
669  * DRAM data rate (MT/s):
670  *
671  * HBM: effective_memory_clock * 2 = data_rate
672  *
673  * G5: effective_memory_clock * 4 = data_rate
674  *
675  * G6: effective_memory_clock * 8 = data_rate
676  *
677  * Bandwidth (MB/s):
678  *
679  * data_rate * vram_bit_width / 8 = memory_bandwidth
680  *
681  * Some examples:
682  *
683  * G5 on RX460:
684  *
685  * memory_controller_clock = 1750 Mhz
686  *
687  * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
688  *
689  * data rate = 1750 * 4 = 7000 MT/s
690  *
691  * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
692  *
693  * G6 on RX5700:
694  *
695  * memory_controller_clock = 875 Mhz
696  *
697  * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
698  *
699  * data rate = 1750 * 8 = 14000 MT/s
700  *
701  * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
702  *
703  * < For Vega10 and previous ASICs >
704  *
705  * Reading the file will display:
706  *
707  * - a list of engine clock levels and voltages labeled OD_SCLK
708  *
709  * - a list of memory clock levels and voltages labeled OD_MCLK
710  *
711  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
712  *
713  * To manually adjust these settings, first select manual using
714  * power_dpm_force_performance_level. Enter a new value for each
715  * level by writing a string that contains "s/m level clock voltage" to
716  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
717  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
718  * 810 mV.  When you have edited all of the states as needed, write
719  * "c" (commit) to the file to commit your changes.  If you want to reset to the
720  * default power levels, write "r" (reset) to the file to reset them.
721  *
722  *
723  * < For Vega20 and newer ASICs >
724  *
725  * Reading the file will display:
726  *
727  * - minimum and maximum engine clock labeled OD_SCLK
728  *
729  * - maximum memory clock labeled OD_MCLK
730  *
731  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
732  *   They can be used to calibrate the sclk voltage curve.
733  *
734  * - a list of valid ranges for sclk, mclk, and voltage curve points
735  *   labeled OD_RANGE
736  *
737  * To manually adjust these settings:
738  *
739  * - First select manual using power_dpm_force_performance_level
740  *
741  * - For clock frequency setting, enter a new value by writing a
742  *   string that contains "s/m index clock" to the file. The index
743  *   should be 0 if to set minimum clock. And 1 if to set maximum
744  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
745  *   "m 1 800" will update maximum mclk to be 800Mhz.
746  *
747  *   For sclk voltage curve, enter the new values by writing a
748  *   string that contains "vc point clock voltage" to the file. The
749  *   points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
750  *   update point1 with clock set as 300Mhz and voltage as
751  *   600mV. "vc 2 1000 1000" will update point3 with clock set
752  *   as 1000Mhz and voltage 1000mV.
753  *
754  * - When you have edited all of the states as needed, write "c" (commit)
755  *   to the file to commit your changes
756  *
757  * - If you want to reset to the default power levels, write "r" (reset)
758  *   to the file to reset them
759  *
760  */
761 
762 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
763 		struct device_attribute *attr,
764 		const char *buf,
765 		size_t count)
766 {
767 	struct drm_device *ddev = dev_get_drvdata(dev);
768 	struct amdgpu_device *adev = drm_to_adev(ddev);
769 	int ret;
770 	uint32_t parameter_size = 0;
771 	long parameter[64];
772 	char buf_cpy[128];
773 	char *tmp_str;
774 	char *sub_str;
775 	const char delimiter[3] = {' ', '\n', '\0'};
776 	uint32_t type;
777 
778 	if (amdgpu_in_reset(adev))
779 		return -EPERM;
780 
781 	if (count > 127)
782 		return -EINVAL;
783 
784 	if (*buf == 's')
785 		type = PP_OD_EDIT_SCLK_VDDC_TABLE;
786 	else if (*buf == 'm')
787 		type = PP_OD_EDIT_MCLK_VDDC_TABLE;
788 	else if(*buf == 'r')
789 		type = PP_OD_RESTORE_DEFAULT_TABLE;
790 	else if (*buf == 'c')
791 		type = PP_OD_COMMIT_DPM_TABLE;
792 	else if (!strncmp(buf, "vc", 2))
793 		type = PP_OD_EDIT_VDDC_CURVE;
794 	else
795 		return -EINVAL;
796 
797 	memcpy(buf_cpy, buf, count+1);
798 
799 	tmp_str = buf_cpy;
800 
801 	if (type == PP_OD_EDIT_VDDC_CURVE)
802 		tmp_str++;
803 	while (isspace(*++tmp_str));
804 
805 	while (tmp_str[0]) {
806 		sub_str = strsep(&tmp_str, delimiter);
807 		ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
808 		if (ret)
809 			return -EINVAL;
810 		parameter_size++;
811 
812 		while (isspace(*tmp_str))
813 			tmp_str++;
814 	}
815 
816 	ret = pm_runtime_get_sync(ddev->dev);
817 	if (ret < 0) {
818 		pm_runtime_put_autosuspend(ddev->dev);
819 		return ret;
820 	}
821 
822 	if (is_support_sw_smu(adev)) {
823 		ret = smu_od_edit_dpm_table(&adev->smu, type,
824 					    parameter, parameter_size);
825 
826 		if (ret) {
827 			pm_runtime_mark_last_busy(ddev->dev);
828 			pm_runtime_put_autosuspend(ddev->dev);
829 			return -EINVAL;
830 		}
831 	} else {
832 
833 		if (adev->powerplay.pp_funcs->set_fine_grain_clk_vol) {
834 			ret = amdgpu_dpm_set_fine_grain_clk_vol(adev, type,
835 								parameter,
836 								parameter_size);
837 			if (ret) {
838 				pm_runtime_mark_last_busy(ddev->dev);
839 				pm_runtime_put_autosuspend(ddev->dev);
840 				return -EINVAL;
841 			}
842 		}
843 
844 		if (adev->powerplay.pp_funcs->odn_edit_dpm_table) {
845 			ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
846 						parameter, parameter_size);
847 			if (ret) {
848 				pm_runtime_mark_last_busy(ddev->dev);
849 				pm_runtime_put_autosuspend(ddev->dev);
850 				return -EINVAL;
851 			}
852 		}
853 
854 		if (type == PP_OD_COMMIT_DPM_TABLE) {
855 			if (adev->powerplay.pp_funcs->dispatch_tasks) {
856 				amdgpu_dpm_dispatch_task(adev,
857 						AMD_PP_TASK_READJUST_POWER_STATE,
858 						NULL);
859 				pm_runtime_mark_last_busy(ddev->dev);
860 				pm_runtime_put_autosuspend(ddev->dev);
861 				return count;
862 			} else {
863 				pm_runtime_mark_last_busy(ddev->dev);
864 				pm_runtime_put_autosuspend(ddev->dev);
865 				return -EINVAL;
866 			}
867 		}
868 	}
869 	pm_runtime_mark_last_busy(ddev->dev);
870 	pm_runtime_put_autosuspend(ddev->dev);
871 
872 	return count;
873 }
874 
875 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
876 		struct device_attribute *attr,
877 		char *buf)
878 {
879 	struct drm_device *ddev = dev_get_drvdata(dev);
880 	struct amdgpu_device *adev = drm_to_adev(ddev);
881 	ssize_t size;
882 	int ret;
883 
884 	if (amdgpu_in_reset(adev))
885 		return -EPERM;
886 
887 	ret = pm_runtime_get_sync(ddev->dev);
888 	if (ret < 0) {
889 		pm_runtime_put_autosuspend(ddev->dev);
890 		return ret;
891 	}
892 
893 	if (is_support_sw_smu(adev)) {
894 		size = smu_print_clk_levels(&adev->smu, SMU_OD_SCLK, buf);
895 		size += smu_print_clk_levels(&adev->smu, SMU_OD_MCLK, buf+size);
896 		size += smu_print_clk_levels(&adev->smu, SMU_OD_VDDC_CURVE, buf+size);
897 		size += smu_print_clk_levels(&adev->smu, SMU_OD_RANGE, buf+size);
898 	} else if (adev->powerplay.pp_funcs->print_clock_levels) {
899 		size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
900 		size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
901 		size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
902 		size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
903 	} else {
904 		size = snprintf(buf, PAGE_SIZE, "\n");
905 	}
906 	pm_runtime_mark_last_busy(ddev->dev);
907 	pm_runtime_put_autosuspend(ddev->dev);
908 
909 	return size;
910 }
911 
912 /**
913  * DOC: pp_features
914  *
915  * The amdgpu driver provides a sysfs API for adjusting what powerplay
916  * features to be enabled. The file pp_features is used for this. And
917  * this is only available for Vega10 and later dGPUs.
918  *
919  * Reading back the file will show you the followings:
920  * - Current ppfeature masks
921  * - List of the all supported powerplay features with their naming,
922  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
923  *
924  * To manually enable or disable a specific feature, just set or clear
925  * the corresponding bit from original ppfeature masks and input the
926  * new ppfeature masks.
927  */
928 static ssize_t amdgpu_set_pp_features(struct device *dev,
929 				      struct device_attribute *attr,
930 				      const char *buf,
931 				      size_t count)
932 {
933 	struct drm_device *ddev = dev_get_drvdata(dev);
934 	struct amdgpu_device *adev = drm_to_adev(ddev);
935 	uint64_t featuremask;
936 	int ret;
937 
938 	if (amdgpu_in_reset(adev))
939 		return -EPERM;
940 
941 	ret = kstrtou64(buf, 0, &featuremask);
942 	if (ret)
943 		return -EINVAL;
944 
945 	pr_debug("featuremask = 0x%llx\n", featuremask);
946 
947 	ret = pm_runtime_get_sync(ddev->dev);
948 	if (ret < 0) {
949 		pm_runtime_put_autosuspend(ddev->dev);
950 		return ret;
951 	}
952 
953 	if (is_support_sw_smu(adev)) {
954 		ret = smu_sys_set_pp_feature_mask(&adev->smu, featuremask);
955 		if (ret) {
956 			pm_runtime_mark_last_busy(ddev->dev);
957 			pm_runtime_put_autosuspend(ddev->dev);
958 			return -EINVAL;
959 		}
960 	} else if (adev->powerplay.pp_funcs->set_ppfeature_status) {
961 		ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
962 		if (ret) {
963 			pm_runtime_mark_last_busy(ddev->dev);
964 			pm_runtime_put_autosuspend(ddev->dev);
965 			return -EINVAL;
966 		}
967 	}
968 	pm_runtime_mark_last_busy(ddev->dev);
969 	pm_runtime_put_autosuspend(ddev->dev);
970 
971 	return count;
972 }
973 
974 static ssize_t amdgpu_get_pp_features(struct device *dev,
975 				      struct device_attribute *attr,
976 				      char *buf)
977 {
978 	struct drm_device *ddev = dev_get_drvdata(dev);
979 	struct amdgpu_device *adev = drm_to_adev(ddev);
980 	ssize_t size;
981 	int ret;
982 
983 	if (amdgpu_in_reset(adev))
984 		return -EPERM;
985 
986 	ret = pm_runtime_get_sync(ddev->dev);
987 	if (ret < 0) {
988 		pm_runtime_put_autosuspend(ddev->dev);
989 		return ret;
990 	}
991 
992 	if (is_support_sw_smu(adev))
993 		size = smu_sys_get_pp_feature_mask(&adev->smu, buf);
994 	else if (adev->powerplay.pp_funcs->get_ppfeature_status)
995 		size = amdgpu_dpm_get_ppfeature_status(adev, buf);
996 	else
997 		size = snprintf(buf, PAGE_SIZE, "\n");
998 
999 	pm_runtime_mark_last_busy(ddev->dev);
1000 	pm_runtime_put_autosuspend(ddev->dev);
1001 
1002 	return size;
1003 }
1004 
1005 /**
1006  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
1007  *
1008  * The amdgpu driver provides a sysfs API for adjusting what power levels
1009  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
1010  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
1011  * this.
1012  *
1013  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
1014  * Vega10 and later ASICs.
1015  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
1016  *
1017  * Reading back the files will show you the available power levels within
1018  * the power state and the clock information for those levels.
1019  *
1020  * To manually adjust these states, first select manual using
1021  * power_dpm_force_performance_level.
1022  * Secondly, enter a new value for each level by inputing a string that
1023  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1024  * E.g.,
1025  *
1026  * .. code-block:: bash
1027  *
1028  *	echo "4 5 6" > pp_dpm_sclk
1029  *
1030  * will enable sclk levels 4, 5, and 6.
1031  *
1032  * NOTE: change to the dcefclk max dpm level is not supported now
1033  */
1034 
1035 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
1036 		struct device_attribute *attr,
1037 		char *buf)
1038 {
1039 	struct drm_device *ddev = dev_get_drvdata(dev);
1040 	struct amdgpu_device *adev = drm_to_adev(ddev);
1041 	ssize_t size;
1042 	int ret;
1043 
1044 	if (amdgpu_in_reset(adev))
1045 		return -EPERM;
1046 
1047 	ret = pm_runtime_get_sync(ddev->dev);
1048 	if (ret < 0) {
1049 		pm_runtime_put_autosuspend(ddev->dev);
1050 		return ret;
1051 	}
1052 
1053 	if (is_support_sw_smu(adev))
1054 		size = smu_print_clk_levels(&adev->smu, SMU_SCLK, buf);
1055 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1056 		size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
1057 	else
1058 		size = snprintf(buf, PAGE_SIZE, "\n");
1059 
1060 	pm_runtime_mark_last_busy(ddev->dev);
1061 	pm_runtime_put_autosuspend(ddev->dev);
1062 
1063 	return size;
1064 }
1065 
1066 /*
1067  * Worst case: 32 bits individually specified, in octal at 12 characters
1068  * per line (+1 for \n).
1069  */
1070 #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1071 
1072 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1073 {
1074 	int ret;
1075 	unsigned long level;
1076 	char *sub_str = NULL;
1077 	char *tmp;
1078 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1079 	const char delimiter[3] = {' ', '\n', '\0'};
1080 	size_t bytes;
1081 
1082 	*mask = 0;
1083 
1084 	bytes = min(count, sizeof(buf_cpy) - 1);
1085 	memcpy(buf_cpy, buf, bytes);
1086 	buf_cpy[bytes] = '\0';
1087 	tmp = buf_cpy;
1088 	while (tmp[0]) {
1089 		sub_str = strsep(&tmp, delimiter);
1090 		if (strlen(sub_str)) {
1091 			ret = kstrtoul(sub_str, 0, &level);
1092 			if (ret || level > 31)
1093 				return -EINVAL;
1094 			*mask |= 1 << level;
1095 		} else
1096 			break;
1097 	}
1098 
1099 	return 0;
1100 }
1101 
1102 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1103 		struct device_attribute *attr,
1104 		const char *buf,
1105 		size_t count)
1106 {
1107 	struct drm_device *ddev = dev_get_drvdata(dev);
1108 	struct amdgpu_device *adev = drm_to_adev(ddev);
1109 	int ret;
1110 	uint32_t mask = 0;
1111 
1112 	if (amdgpu_in_reset(adev))
1113 		return -EPERM;
1114 
1115 	ret = amdgpu_read_mask(buf, count, &mask);
1116 	if (ret)
1117 		return ret;
1118 
1119 	ret = pm_runtime_get_sync(ddev->dev);
1120 	if (ret < 0) {
1121 		pm_runtime_put_autosuspend(ddev->dev);
1122 		return ret;
1123 	}
1124 
1125 	if (is_support_sw_smu(adev))
1126 		ret = smu_force_clk_levels(&adev->smu, SMU_SCLK, mask);
1127 	else if (adev->powerplay.pp_funcs->force_clock_level)
1128 		ret = amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
1129 
1130 	pm_runtime_mark_last_busy(ddev->dev);
1131 	pm_runtime_put_autosuspend(ddev->dev);
1132 
1133 	if (ret)
1134 		return -EINVAL;
1135 
1136 	return count;
1137 }
1138 
1139 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1140 		struct device_attribute *attr,
1141 		char *buf)
1142 {
1143 	struct drm_device *ddev = dev_get_drvdata(dev);
1144 	struct amdgpu_device *adev = drm_to_adev(ddev);
1145 	ssize_t size;
1146 	int ret;
1147 
1148 	if (amdgpu_in_reset(adev))
1149 		return -EPERM;
1150 
1151 	ret = pm_runtime_get_sync(ddev->dev);
1152 	if (ret < 0) {
1153 		pm_runtime_put_autosuspend(ddev->dev);
1154 		return ret;
1155 	}
1156 
1157 	if (is_support_sw_smu(adev))
1158 		size = smu_print_clk_levels(&adev->smu, SMU_MCLK, buf);
1159 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1160 		size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
1161 	else
1162 		size = snprintf(buf, PAGE_SIZE, "\n");
1163 
1164 	pm_runtime_mark_last_busy(ddev->dev);
1165 	pm_runtime_put_autosuspend(ddev->dev);
1166 
1167 	return size;
1168 }
1169 
1170 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1171 		struct device_attribute *attr,
1172 		const char *buf,
1173 		size_t count)
1174 {
1175 	struct drm_device *ddev = dev_get_drvdata(dev);
1176 	struct amdgpu_device *adev = drm_to_adev(ddev);
1177 	uint32_t mask = 0;
1178 	int ret;
1179 
1180 	if (amdgpu_in_reset(adev))
1181 		return -EPERM;
1182 
1183 	ret = amdgpu_read_mask(buf, count, &mask);
1184 	if (ret)
1185 		return ret;
1186 
1187 	ret = pm_runtime_get_sync(ddev->dev);
1188 	if (ret < 0) {
1189 		pm_runtime_put_autosuspend(ddev->dev);
1190 		return ret;
1191 	}
1192 
1193 	if (is_support_sw_smu(adev))
1194 		ret = smu_force_clk_levels(&adev->smu, SMU_MCLK, mask);
1195 	else if (adev->powerplay.pp_funcs->force_clock_level)
1196 		ret = amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
1197 
1198 	pm_runtime_mark_last_busy(ddev->dev);
1199 	pm_runtime_put_autosuspend(ddev->dev);
1200 
1201 	if (ret)
1202 		return -EINVAL;
1203 
1204 	return count;
1205 }
1206 
1207 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1208 		struct device_attribute *attr,
1209 		char *buf)
1210 {
1211 	struct drm_device *ddev = dev_get_drvdata(dev);
1212 	struct amdgpu_device *adev = drm_to_adev(ddev);
1213 	ssize_t size;
1214 	int ret;
1215 
1216 	if (amdgpu_in_reset(adev))
1217 		return -EPERM;
1218 
1219 	ret = pm_runtime_get_sync(ddev->dev);
1220 	if (ret < 0) {
1221 		pm_runtime_put_autosuspend(ddev->dev);
1222 		return ret;
1223 	}
1224 
1225 	if (is_support_sw_smu(adev))
1226 		size = smu_print_clk_levels(&adev->smu, SMU_SOCCLK, buf);
1227 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1228 		size = amdgpu_dpm_print_clock_levels(adev, PP_SOCCLK, buf);
1229 	else
1230 		size = snprintf(buf, PAGE_SIZE, "\n");
1231 
1232 	pm_runtime_mark_last_busy(ddev->dev);
1233 	pm_runtime_put_autosuspend(ddev->dev);
1234 
1235 	return size;
1236 }
1237 
1238 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1239 		struct device_attribute *attr,
1240 		const char *buf,
1241 		size_t count)
1242 {
1243 	struct drm_device *ddev = dev_get_drvdata(dev);
1244 	struct amdgpu_device *adev = drm_to_adev(ddev);
1245 	int ret;
1246 	uint32_t mask = 0;
1247 
1248 	if (amdgpu_in_reset(adev))
1249 		return -EPERM;
1250 
1251 	ret = amdgpu_read_mask(buf, count, &mask);
1252 	if (ret)
1253 		return ret;
1254 
1255 	ret = pm_runtime_get_sync(ddev->dev);
1256 	if (ret < 0) {
1257 		pm_runtime_put_autosuspend(ddev->dev);
1258 		return ret;
1259 	}
1260 
1261 	if (is_support_sw_smu(adev))
1262 		ret = smu_force_clk_levels(&adev->smu, SMU_SOCCLK, mask);
1263 	else if (adev->powerplay.pp_funcs->force_clock_level)
1264 		ret = amdgpu_dpm_force_clock_level(adev, PP_SOCCLK, mask);
1265 	else
1266 		ret = 0;
1267 
1268 	pm_runtime_mark_last_busy(ddev->dev);
1269 	pm_runtime_put_autosuspend(ddev->dev);
1270 
1271 	if (ret)
1272 		return -EINVAL;
1273 
1274 	return count;
1275 }
1276 
1277 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1278 		struct device_attribute *attr,
1279 		char *buf)
1280 {
1281 	struct drm_device *ddev = dev_get_drvdata(dev);
1282 	struct amdgpu_device *adev = drm_to_adev(ddev);
1283 	ssize_t size;
1284 	int ret;
1285 
1286 	if (amdgpu_in_reset(adev))
1287 		return -EPERM;
1288 
1289 	ret = pm_runtime_get_sync(ddev->dev);
1290 	if (ret < 0) {
1291 		pm_runtime_put_autosuspend(ddev->dev);
1292 		return ret;
1293 	}
1294 
1295 	if (is_support_sw_smu(adev))
1296 		size = smu_print_clk_levels(&adev->smu, SMU_FCLK, buf);
1297 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1298 		size = amdgpu_dpm_print_clock_levels(adev, PP_FCLK, buf);
1299 	else
1300 		size = snprintf(buf, PAGE_SIZE, "\n");
1301 
1302 	pm_runtime_mark_last_busy(ddev->dev);
1303 	pm_runtime_put_autosuspend(ddev->dev);
1304 
1305 	return size;
1306 }
1307 
1308 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1309 		struct device_attribute *attr,
1310 		const char *buf,
1311 		size_t count)
1312 {
1313 	struct drm_device *ddev = dev_get_drvdata(dev);
1314 	struct amdgpu_device *adev = drm_to_adev(ddev);
1315 	int ret;
1316 	uint32_t mask = 0;
1317 
1318 	if (amdgpu_in_reset(adev))
1319 		return -EPERM;
1320 
1321 	ret = amdgpu_read_mask(buf, count, &mask);
1322 	if (ret)
1323 		return ret;
1324 
1325 	ret = pm_runtime_get_sync(ddev->dev);
1326 	if (ret < 0) {
1327 		pm_runtime_put_autosuspend(ddev->dev);
1328 		return ret;
1329 	}
1330 
1331 	if (is_support_sw_smu(adev))
1332 		ret = smu_force_clk_levels(&adev->smu, SMU_FCLK, mask);
1333 	else if (adev->powerplay.pp_funcs->force_clock_level)
1334 		ret = amdgpu_dpm_force_clock_level(adev, PP_FCLK, mask);
1335 	else
1336 		ret = 0;
1337 
1338 	pm_runtime_mark_last_busy(ddev->dev);
1339 	pm_runtime_put_autosuspend(ddev->dev);
1340 
1341 	if (ret)
1342 		return -EINVAL;
1343 
1344 	return count;
1345 }
1346 
1347 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1348 		struct device_attribute *attr,
1349 		char *buf)
1350 {
1351 	struct drm_device *ddev = dev_get_drvdata(dev);
1352 	struct amdgpu_device *adev = drm_to_adev(ddev);
1353 	ssize_t size;
1354 	int ret;
1355 
1356 	if (amdgpu_in_reset(adev))
1357 		return -EPERM;
1358 
1359 	ret = pm_runtime_get_sync(ddev->dev);
1360 	if (ret < 0) {
1361 		pm_runtime_put_autosuspend(ddev->dev);
1362 		return ret;
1363 	}
1364 
1365 	if (is_support_sw_smu(adev))
1366 		size = smu_print_clk_levels(&adev->smu, SMU_DCEFCLK, buf);
1367 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1368 		size = amdgpu_dpm_print_clock_levels(adev, PP_DCEFCLK, buf);
1369 	else
1370 		size = snprintf(buf, PAGE_SIZE, "\n");
1371 
1372 	pm_runtime_mark_last_busy(ddev->dev);
1373 	pm_runtime_put_autosuspend(ddev->dev);
1374 
1375 	return size;
1376 }
1377 
1378 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1379 		struct device_attribute *attr,
1380 		const char *buf,
1381 		size_t count)
1382 {
1383 	struct drm_device *ddev = dev_get_drvdata(dev);
1384 	struct amdgpu_device *adev = drm_to_adev(ddev);
1385 	int ret;
1386 	uint32_t mask = 0;
1387 
1388 	if (amdgpu_in_reset(adev))
1389 		return -EPERM;
1390 
1391 	ret = amdgpu_read_mask(buf, count, &mask);
1392 	if (ret)
1393 		return ret;
1394 
1395 	ret = pm_runtime_get_sync(ddev->dev);
1396 	if (ret < 0) {
1397 		pm_runtime_put_autosuspend(ddev->dev);
1398 		return ret;
1399 	}
1400 
1401 	if (is_support_sw_smu(adev))
1402 		ret = smu_force_clk_levels(&adev->smu, SMU_DCEFCLK, mask);
1403 	else if (adev->powerplay.pp_funcs->force_clock_level)
1404 		ret = amdgpu_dpm_force_clock_level(adev, PP_DCEFCLK, mask);
1405 	else
1406 		ret = 0;
1407 
1408 	pm_runtime_mark_last_busy(ddev->dev);
1409 	pm_runtime_put_autosuspend(ddev->dev);
1410 
1411 	if (ret)
1412 		return -EINVAL;
1413 
1414 	return count;
1415 }
1416 
1417 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1418 		struct device_attribute *attr,
1419 		char *buf)
1420 {
1421 	struct drm_device *ddev = dev_get_drvdata(dev);
1422 	struct amdgpu_device *adev = drm_to_adev(ddev);
1423 	ssize_t size;
1424 	int ret;
1425 
1426 	if (amdgpu_in_reset(adev))
1427 		return -EPERM;
1428 
1429 	ret = pm_runtime_get_sync(ddev->dev);
1430 	if (ret < 0) {
1431 		pm_runtime_put_autosuspend(ddev->dev);
1432 		return ret;
1433 	}
1434 
1435 	if (is_support_sw_smu(adev))
1436 		size = smu_print_clk_levels(&adev->smu, SMU_PCIE, buf);
1437 	else if (adev->powerplay.pp_funcs->print_clock_levels)
1438 		size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
1439 	else
1440 		size = snprintf(buf, PAGE_SIZE, "\n");
1441 
1442 	pm_runtime_mark_last_busy(ddev->dev);
1443 	pm_runtime_put_autosuspend(ddev->dev);
1444 
1445 	return size;
1446 }
1447 
1448 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1449 		struct device_attribute *attr,
1450 		const char *buf,
1451 		size_t count)
1452 {
1453 	struct drm_device *ddev = dev_get_drvdata(dev);
1454 	struct amdgpu_device *adev = drm_to_adev(ddev);
1455 	int ret;
1456 	uint32_t mask = 0;
1457 
1458 	if (amdgpu_in_reset(adev))
1459 		return -EPERM;
1460 
1461 	ret = amdgpu_read_mask(buf, count, &mask);
1462 	if (ret)
1463 		return ret;
1464 
1465 	ret = pm_runtime_get_sync(ddev->dev);
1466 	if (ret < 0) {
1467 		pm_runtime_put_autosuspend(ddev->dev);
1468 		return ret;
1469 	}
1470 
1471 	if (is_support_sw_smu(adev))
1472 		ret = smu_force_clk_levels(&adev->smu, SMU_PCIE, mask);
1473 	else if (adev->powerplay.pp_funcs->force_clock_level)
1474 		ret = amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
1475 	else
1476 		ret = 0;
1477 
1478 	pm_runtime_mark_last_busy(ddev->dev);
1479 	pm_runtime_put_autosuspend(ddev->dev);
1480 
1481 	if (ret)
1482 		return -EINVAL;
1483 
1484 	return count;
1485 }
1486 
1487 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1488 		struct device_attribute *attr,
1489 		char *buf)
1490 {
1491 	struct drm_device *ddev = dev_get_drvdata(dev);
1492 	struct amdgpu_device *adev = drm_to_adev(ddev);
1493 	uint32_t value = 0;
1494 	int ret;
1495 
1496 	if (amdgpu_in_reset(adev))
1497 		return -EPERM;
1498 
1499 	ret = pm_runtime_get_sync(ddev->dev);
1500 	if (ret < 0) {
1501 		pm_runtime_put_autosuspend(ddev->dev);
1502 		return ret;
1503 	}
1504 
1505 	if (is_support_sw_smu(adev))
1506 		value = smu_get_od_percentage(&(adev->smu), SMU_OD_SCLK);
1507 	else if (adev->powerplay.pp_funcs->get_sclk_od)
1508 		value = amdgpu_dpm_get_sclk_od(adev);
1509 
1510 	pm_runtime_mark_last_busy(ddev->dev);
1511 	pm_runtime_put_autosuspend(ddev->dev);
1512 
1513 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
1514 }
1515 
1516 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1517 		struct device_attribute *attr,
1518 		const char *buf,
1519 		size_t count)
1520 {
1521 	struct drm_device *ddev = dev_get_drvdata(dev);
1522 	struct amdgpu_device *adev = drm_to_adev(ddev);
1523 	int ret;
1524 	long int value;
1525 
1526 	if (amdgpu_in_reset(adev))
1527 		return -EPERM;
1528 
1529 	ret = kstrtol(buf, 0, &value);
1530 
1531 	if (ret)
1532 		return -EINVAL;
1533 
1534 	ret = pm_runtime_get_sync(ddev->dev);
1535 	if (ret < 0) {
1536 		pm_runtime_put_autosuspend(ddev->dev);
1537 		return ret;
1538 	}
1539 
1540 	if (is_support_sw_smu(adev)) {
1541 		value = smu_set_od_percentage(&(adev->smu), SMU_OD_SCLK, (uint32_t)value);
1542 	} else {
1543 		if (adev->powerplay.pp_funcs->set_sclk_od)
1544 			amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1545 
1546 		if (adev->powerplay.pp_funcs->dispatch_tasks) {
1547 			amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1548 		} else {
1549 			adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1550 			amdgpu_pm_compute_clocks(adev);
1551 		}
1552 	}
1553 
1554 	pm_runtime_mark_last_busy(ddev->dev);
1555 	pm_runtime_put_autosuspend(ddev->dev);
1556 
1557 	return count;
1558 }
1559 
1560 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1561 		struct device_attribute *attr,
1562 		char *buf)
1563 {
1564 	struct drm_device *ddev = dev_get_drvdata(dev);
1565 	struct amdgpu_device *adev = drm_to_adev(ddev);
1566 	uint32_t value = 0;
1567 	int ret;
1568 
1569 	if (amdgpu_in_reset(adev))
1570 		return -EPERM;
1571 
1572 	ret = pm_runtime_get_sync(ddev->dev);
1573 	if (ret < 0) {
1574 		pm_runtime_put_autosuspend(ddev->dev);
1575 		return ret;
1576 	}
1577 
1578 	if (is_support_sw_smu(adev))
1579 		value = smu_get_od_percentage(&(adev->smu), SMU_OD_MCLK);
1580 	else if (adev->powerplay.pp_funcs->get_mclk_od)
1581 		value = amdgpu_dpm_get_mclk_od(adev);
1582 
1583 	pm_runtime_mark_last_busy(ddev->dev);
1584 	pm_runtime_put_autosuspend(ddev->dev);
1585 
1586 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
1587 }
1588 
1589 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1590 		struct device_attribute *attr,
1591 		const char *buf,
1592 		size_t count)
1593 {
1594 	struct drm_device *ddev = dev_get_drvdata(dev);
1595 	struct amdgpu_device *adev = drm_to_adev(ddev);
1596 	int ret;
1597 	long int value;
1598 
1599 	if (amdgpu_in_reset(adev))
1600 		return -EPERM;
1601 
1602 	ret = kstrtol(buf, 0, &value);
1603 
1604 	if (ret)
1605 		return -EINVAL;
1606 
1607 	ret = pm_runtime_get_sync(ddev->dev);
1608 	if (ret < 0) {
1609 		pm_runtime_put_autosuspend(ddev->dev);
1610 		return ret;
1611 	}
1612 
1613 	if (is_support_sw_smu(adev)) {
1614 		value = smu_set_od_percentage(&(adev->smu), SMU_OD_MCLK, (uint32_t)value);
1615 	} else {
1616 		if (adev->powerplay.pp_funcs->set_mclk_od)
1617 			amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1618 
1619 		if (adev->powerplay.pp_funcs->dispatch_tasks) {
1620 			amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1621 		} else {
1622 			adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1623 			amdgpu_pm_compute_clocks(adev);
1624 		}
1625 	}
1626 
1627 	pm_runtime_mark_last_busy(ddev->dev);
1628 	pm_runtime_put_autosuspend(ddev->dev);
1629 
1630 	return count;
1631 }
1632 
1633 /**
1634  * DOC: pp_power_profile_mode
1635  *
1636  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1637  * related to switching between power levels in a power state.  The file
1638  * pp_power_profile_mode is used for this.
1639  *
1640  * Reading this file outputs a list of all of the predefined power profiles
1641  * and the relevant heuristics settings for that profile.
1642  *
1643  * To select a profile or create a custom profile, first select manual using
1644  * power_dpm_force_performance_level.  Writing the number of a predefined
1645  * profile to pp_power_profile_mode will enable those heuristics.  To
1646  * create a custom set of heuristics, write a string of numbers to the file
1647  * starting with the number of the custom profile along with a setting
1648  * for each heuristic parameter.  Due to differences across asic families
1649  * the heuristic parameters vary from family to family.
1650  *
1651  */
1652 
1653 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1654 		struct device_attribute *attr,
1655 		char *buf)
1656 {
1657 	struct drm_device *ddev = dev_get_drvdata(dev);
1658 	struct amdgpu_device *adev = drm_to_adev(ddev);
1659 	ssize_t size;
1660 	int ret;
1661 
1662 	if (amdgpu_in_reset(adev))
1663 		return -EPERM;
1664 
1665 	ret = pm_runtime_get_sync(ddev->dev);
1666 	if (ret < 0) {
1667 		pm_runtime_put_autosuspend(ddev->dev);
1668 		return ret;
1669 	}
1670 
1671 	if (is_support_sw_smu(adev))
1672 		size = smu_get_power_profile_mode(&adev->smu, buf);
1673 	else if (adev->powerplay.pp_funcs->get_power_profile_mode)
1674 		size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1675 	else
1676 		size = snprintf(buf, PAGE_SIZE, "\n");
1677 
1678 	pm_runtime_mark_last_busy(ddev->dev);
1679 	pm_runtime_put_autosuspend(ddev->dev);
1680 
1681 	return size;
1682 }
1683 
1684 
1685 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1686 		struct device_attribute *attr,
1687 		const char *buf,
1688 		size_t count)
1689 {
1690 	int ret;
1691 	struct drm_device *ddev = dev_get_drvdata(dev);
1692 	struct amdgpu_device *adev = drm_to_adev(ddev);
1693 	uint32_t parameter_size = 0;
1694 	long parameter[64];
1695 	char *sub_str, buf_cpy[128];
1696 	char *tmp_str;
1697 	uint32_t i = 0;
1698 	char tmp[2];
1699 	long int profile_mode = 0;
1700 	const char delimiter[3] = {' ', '\n', '\0'};
1701 
1702 	if (amdgpu_in_reset(adev))
1703 		return -EPERM;
1704 
1705 	tmp[0] = *(buf);
1706 	tmp[1] = '\0';
1707 	ret = kstrtol(tmp, 0, &profile_mode);
1708 	if (ret)
1709 		return -EINVAL;
1710 
1711 	if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1712 		if (count < 2 || count > 127)
1713 			return -EINVAL;
1714 		while (isspace(*++buf))
1715 			i++;
1716 		memcpy(buf_cpy, buf, count-i);
1717 		tmp_str = buf_cpy;
1718 		while (tmp_str[0]) {
1719 			sub_str = strsep(&tmp_str, delimiter);
1720 			ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
1721 			if (ret)
1722 				return -EINVAL;
1723 			parameter_size++;
1724 			while (isspace(*tmp_str))
1725 				tmp_str++;
1726 		}
1727 	}
1728 	parameter[parameter_size] = profile_mode;
1729 
1730 	ret = pm_runtime_get_sync(ddev->dev);
1731 	if (ret < 0) {
1732 		pm_runtime_put_autosuspend(ddev->dev);
1733 		return ret;
1734 	}
1735 
1736 	if (is_support_sw_smu(adev))
1737 		ret = smu_set_power_profile_mode(&adev->smu, parameter, parameter_size, true);
1738 	else if (adev->powerplay.pp_funcs->set_power_profile_mode)
1739 		ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1740 
1741 	pm_runtime_mark_last_busy(ddev->dev);
1742 	pm_runtime_put_autosuspend(ddev->dev);
1743 
1744 	if (!ret)
1745 		return count;
1746 
1747 	return -EINVAL;
1748 }
1749 
1750 /**
1751  * DOC: gpu_busy_percent
1752  *
1753  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1754  * is as a percentage.  The file gpu_busy_percent is used for this.
1755  * The SMU firmware computes a percentage of load based on the
1756  * aggregate activity level in the IP cores.
1757  */
1758 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1759 					   struct device_attribute *attr,
1760 					   char *buf)
1761 {
1762 	struct drm_device *ddev = dev_get_drvdata(dev);
1763 	struct amdgpu_device *adev = drm_to_adev(ddev);
1764 	int r, value, size = sizeof(value);
1765 
1766 	if (amdgpu_in_reset(adev))
1767 		return -EPERM;
1768 
1769 	r = pm_runtime_get_sync(ddev->dev);
1770 	if (r < 0) {
1771 		pm_runtime_put_autosuspend(ddev->dev);
1772 		return r;
1773 	}
1774 
1775 	/* read the IP busy sensor */
1776 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1777 				   (void *)&value, &size);
1778 
1779 	pm_runtime_mark_last_busy(ddev->dev);
1780 	pm_runtime_put_autosuspend(ddev->dev);
1781 
1782 	if (r)
1783 		return r;
1784 
1785 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
1786 }
1787 
1788 /**
1789  * DOC: mem_busy_percent
1790  *
1791  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1792  * is as a percentage.  The file mem_busy_percent is used for this.
1793  * The SMU firmware computes a percentage of load based on the
1794  * aggregate activity level in the IP cores.
1795  */
1796 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1797 					   struct device_attribute *attr,
1798 					   char *buf)
1799 {
1800 	struct drm_device *ddev = dev_get_drvdata(dev);
1801 	struct amdgpu_device *adev = drm_to_adev(ddev);
1802 	int r, value, size = sizeof(value);
1803 
1804 	if (amdgpu_in_reset(adev))
1805 		return -EPERM;
1806 
1807 	r = pm_runtime_get_sync(ddev->dev);
1808 	if (r < 0) {
1809 		pm_runtime_put_autosuspend(ddev->dev);
1810 		return r;
1811 	}
1812 
1813 	/* read the IP busy sensor */
1814 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1815 				   (void *)&value, &size);
1816 
1817 	pm_runtime_mark_last_busy(ddev->dev);
1818 	pm_runtime_put_autosuspend(ddev->dev);
1819 
1820 	if (r)
1821 		return r;
1822 
1823 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
1824 }
1825 
1826 /**
1827  * DOC: pcie_bw
1828  *
1829  * The amdgpu driver provides a sysfs API for estimating how much data
1830  * has been received and sent by the GPU in the last second through PCIe.
1831  * The file pcie_bw is used for this.
1832  * The Perf counters count the number of received and sent messages and return
1833  * those values, as well as the maximum payload size of a PCIe packet (mps).
1834  * Note that it is not possible to easily and quickly obtain the size of each
1835  * packet transmitted, so we output the max payload size (mps) to allow for
1836  * quick estimation of the PCIe bandwidth usage
1837  */
1838 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1839 		struct device_attribute *attr,
1840 		char *buf)
1841 {
1842 	struct drm_device *ddev = dev_get_drvdata(dev);
1843 	struct amdgpu_device *adev = drm_to_adev(ddev);
1844 	uint64_t count0 = 0, count1 = 0;
1845 	int ret;
1846 
1847 	if (amdgpu_in_reset(adev))
1848 		return -EPERM;
1849 
1850 	if (adev->flags & AMD_IS_APU)
1851 		return -ENODATA;
1852 
1853 	if (!adev->asic_funcs->get_pcie_usage)
1854 		return -ENODATA;
1855 
1856 	ret = pm_runtime_get_sync(ddev->dev);
1857 	if (ret < 0) {
1858 		pm_runtime_put_autosuspend(ddev->dev);
1859 		return ret;
1860 	}
1861 
1862 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1863 
1864 	pm_runtime_mark_last_busy(ddev->dev);
1865 	pm_runtime_put_autosuspend(ddev->dev);
1866 
1867 	return snprintf(buf, PAGE_SIZE,	"%llu %llu %i\n",
1868 			count0, count1, pcie_get_mps(adev->pdev));
1869 }
1870 
1871 /**
1872  * DOC: unique_id
1873  *
1874  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1875  * The file unique_id is used for this.
1876  * This will provide a Unique ID that will persist from machine to machine
1877  *
1878  * NOTE: This will only work for GFX9 and newer. This file will be absent
1879  * on unsupported ASICs (GFX8 and older)
1880  */
1881 static ssize_t amdgpu_get_unique_id(struct device *dev,
1882 		struct device_attribute *attr,
1883 		char *buf)
1884 {
1885 	struct drm_device *ddev = dev_get_drvdata(dev);
1886 	struct amdgpu_device *adev = drm_to_adev(ddev);
1887 
1888 	if (amdgpu_in_reset(adev))
1889 		return -EPERM;
1890 
1891 	if (adev->unique_id)
1892 		return snprintf(buf, PAGE_SIZE, "%016llx\n", adev->unique_id);
1893 
1894 	return 0;
1895 }
1896 
1897 /**
1898  * DOC: thermal_throttling_logging
1899  *
1900  * Thermal throttling pulls down the clock frequency and thus the performance.
1901  * It's an useful mechanism to protect the chip from overheating. Since it
1902  * impacts performance, the user controls whether it is enabled and if so,
1903  * the log frequency.
1904  *
1905  * Reading back the file shows you the status(enabled or disabled) and
1906  * the interval(in seconds) between each thermal logging.
1907  *
1908  * Writing an integer to the file, sets a new logging interval, in seconds.
1909  * The value should be between 1 and 3600. If the value is less than 1,
1910  * thermal logging is disabled. Values greater than 3600 are ignored.
1911  */
1912 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1913 						     struct device_attribute *attr,
1914 						     char *buf)
1915 {
1916 	struct drm_device *ddev = dev_get_drvdata(dev);
1917 	struct amdgpu_device *adev = drm_to_adev(ddev);
1918 
1919 	return snprintf(buf, PAGE_SIZE, "%s: thermal throttling logging %s, with interval %d seconds\n",
1920 			adev_to_drm(adev)->unique,
1921 			atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1922 			adev->throttling_logging_rs.interval / HZ + 1);
1923 }
1924 
1925 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1926 						     struct device_attribute *attr,
1927 						     const char *buf,
1928 						     size_t count)
1929 {
1930 	struct drm_device *ddev = dev_get_drvdata(dev);
1931 	struct amdgpu_device *adev = drm_to_adev(ddev);
1932 	long throttling_logging_interval;
1933 	unsigned long flags;
1934 	int ret = 0;
1935 
1936 	ret = kstrtol(buf, 0, &throttling_logging_interval);
1937 	if (ret)
1938 		return ret;
1939 
1940 	if (throttling_logging_interval > 3600)
1941 		return -EINVAL;
1942 
1943 	if (throttling_logging_interval > 0) {
1944 		raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1945 		/*
1946 		 * Reset the ratelimit timer internals.
1947 		 * This can effectively restart the timer.
1948 		 */
1949 		adev->throttling_logging_rs.interval =
1950 			(throttling_logging_interval - 1) * HZ;
1951 		adev->throttling_logging_rs.begin = 0;
1952 		adev->throttling_logging_rs.printed = 0;
1953 		adev->throttling_logging_rs.missed = 0;
1954 		raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1955 
1956 		atomic_set(&adev->throttling_logging_enabled, 1);
1957 	} else {
1958 		atomic_set(&adev->throttling_logging_enabled, 0);
1959 	}
1960 
1961 	return count;
1962 }
1963 
1964 /**
1965  * DOC: gpu_metrics
1966  *
1967  * The amdgpu driver provides a sysfs API for retrieving current gpu
1968  * metrics data. The file gpu_metrics is used for this. Reading the
1969  * file will dump all the current gpu metrics data.
1970  *
1971  * These data include temperature, frequency, engines utilization,
1972  * power consume, throttler status, fan speed and cpu core statistics(
1973  * available for APU only). That's it will give a snapshot of all sensors
1974  * at the same time.
1975  */
1976 static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1977 				      struct device_attribute *attr,
1978 				      char *buf)
1979 {
1980 	struct drm_device *ddev = dev_get_drvdata(dev);
1981 	struct amdgpu_device *adev = drm_to_adev(ddev);
1982 	void *gpu_metrics;
1983 	ssize_t size = 0;
1984 	int ret;
1985 
1986 	if (amdgpu_in_reset(adev))
1987 		return -EPERM;
1988 
1989 	ret = pm_runtime_get_sync(ddev->dev);
1990 	if (ret < 0) {
1991 		pm_runtime_put_autosuspend(ddev->dev);
1992 		return ret;
1993 	}
1994 
1995 	if (is_support_sw_smu(adev))
1996 		size = smu_sys_get_gpu_metrics(&adev->smu, &gpu_metrics);
1997 	else if (adev->powerplay.pp_funcs->get_gpu_metrics)
1998 		size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1999 
2000 	if (size <= 0)
2001 		goto out;
2002 
2003 	if (size >= PAGE_SIZE)
2004 		size = PAGE_SIZE - 1;
2005 
2006 	memcpy(buf, gpu_metrics, size);
2007 
2008 out:
2009 	pm_runtime_mark_last_busy(ddev->dev);
2010 	pm_runtime_put_autosuspend(ddev->dev);
2011 
2012 	return size;
2013 }
2014 
2015 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2016 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2017 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC),
2018 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC),
2019 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC),
2020 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC),
2021 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC),
2022 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2023 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2024 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2025 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2026 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC),
2027 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC),
2028 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2029 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2030 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC),
2031 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC),
2032 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC),
2033 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC),
2034 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2035 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC),
2036 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC),
2037 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC),
2038 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC),
2039 };
2040 
2041 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2042 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2043 {
2044 	struct device_attribute *dev_attr = &attr->dev_attr;
2045 	const char *attr_name = dev_attr->attr.name;
2046 	struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2047 	enum amd_asic_type asic_type = adev->asic_type;
2048 
2049 	if (!(attr->flags & mask)) {
2050 		*states = ATTR_STATE_UNSUPPORTED;
2051 		return 0;
2052 	}
2053 
2054 #define DEVICE_ATTR_IS(_name)	(!strcmp(attr_name, #_name))
2055 
2056 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2057 		if (asic_type < CHIP_VEGA10)
2058 			*states = ATTR_STATE_UNSUPPORTED;
2059 	} else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2060 		if (asic_type < CHIP_VEGA10 || asic_type == CHIP_ARCTURUS)
2061 			*states = ATTR_STATE_UNSUPPORTED;
2062 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2063 		if (asic_type < CHIP_VEGA20)
2064 			*states = ATTR_STATE_UNSUPPORTED;
2065 	} else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
2066 		if (asic_type == CHIP_ARCTURUS)
2067 			*states = ATTR_STATE_UNSUPPORTED;
2068 	} else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2069 		*states = ATTR_STATE_UNSUPPORTED;
2070 		if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2071 		    (!is_support_sw_smu(adev) && hwmgr->od_enabled))
2072 			*states = ATTR_STATE_SUPPORTED;
2073 	} else if (DEVICE_ATTR_IS(mem_busy_percent)) {
2074 		if (adev->flags & AMD_IS_APU || asic_type == CHIP_VEGA10)
2075 			*states = ATTR_STATE_UNSUPPORTED;
2076 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2077 		/* PCIe Perf counters won't work on APU nodes */
2078 		if (adev->flags & AMD_IS_APU)
2079 			*states = ATTR_STATE_UNSUPPORTED;
2080 	} else if (DEVICE_ATTR_IS(unique_id)) {
2081 		if (asic_type != CHIP_VEGA10 &&
2082 		    asic_type != CHIP_VEGA20 &&
2083 		    asic_type != CHIP_ARCTURUS)
2084 			*states = ATTR_STATE_UNSUPPORTED;
2085 	} else if (DEVICE_ATTR_IS(pp_features)) {
2086 		if (adev->flags & AMD_IS_APU || asic_type < CHIP_VEGA10)
2087 			*states = ATTR_STATE_UNSUPPORTED;
2088 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
2089 		if (asic_type < CHIP_VEGA12)
2090 			*states = ATTR_STATE_UNSUPPORTED;
2091 	}
2092 
2093 	if (asic_type == CHIP_ARCTURUS) {
2094 		/* Arcturus does not support standalone mclk/socclk/fclk level setting */
2095 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2096 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2097 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2098 			dev_attr->attr.mode &= ~S_IWUGO;
2099 			dev_attr->store = NULL;
2100 		}
2101 	}
2102 
2103 #undef DEVICE_ATTR_IS
2104 
2105 	return 0;
2106 }
2107 
2108 
2109 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2110 				     struct amdgpu_device_attr *attr,
2111 				     uint32_t mask, struct list_head *attr_list)
2112 {
2113 	int ret = 0;
2114 	struct device_attribute *dev_attr = &attr->dev_attr;
2115 	const char *name = dev_attr->attr.name;
2116 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2117 	struct amdgpu_device_attr_entry *attr_entry;
2118 
2119 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2120 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2121 
2122 	BUG_ON(!attr);
2123 
2124 	attr_update = attr->attr_update ? attr_update : default_attr_update;
2125 
2126 	ret = attr_update(adev, attr, mask, &attr_states);
2127 	if (ret) {
2128 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2129 			name, ret);
2130 		return ret;
2131 	}
2132 
2133 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2134 		return 0;
2135 
2136 	ret = device_create_file(adev->dev, dev_attr);
2137 	if (ret) {
2138 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2139 			name, ret);
2140 	}
2141 
2142 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2143 	if (!attr_entry)
2144 		return -ENOMEM;
2145 
2146 	attr_entry->attr = attr;
2147 	INIT_LIST_HEAD(&attr_entry->entry);
2148 
2149 	list_add_tail(&attr_entry->entry, attr_list);
2150 
2151 	return ret;
2152 }
2153 
2154 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2155 {
2156 	struct device_attribute *dev_attr = &attr->dev_attr;
2157 
2158 	device_remove_file(adev->dev, dev_attr);
2159 }
2160 
2161 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2162 					     struct list_head *attr_list);
2163 
2164 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2165 					    struct amdgpu_device_attr *attrs,
2166 					    uint32_t counts,
2167 					    uint32_t mask,
2168 					    struct list_head *attr_list)
2169 {
2170 	int ret = 0;
2171 	uint32_t i = 0;
2172 
2173 	for (i = 0; i < counts; i++) {
2174 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2175 		if (ret)
2176 			goto failed;
2177 	}
2178 
2179 	return 0;
2180 
2181 failed:
2182 	amdgpu_device_attr_remove_groups(adev, attr_list);
2183 
2184 	return ret;
2185 }
2186 
2187 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2188 					     struct list_head *attr_list)
2189 {
2190 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2191 
2192 	if (list_empty(attr_list))
2193 		return ;
2194 
2195 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2196 		amdgpu_device_attr_remove(adev, entry->attr);
2197 		list_del(&entry->entry);
2198 		kfree(entry);
2199 	}
2200 }
2201 
2202 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2203 				      struct device_attribute *attr,
2204 				      char *buf)
2205 {
2206 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2207 	int channel = to_sensor_dev_attr(attr)->index;
2208 	int r, temp = 0, size = sizeof(temp);
2209 
2210 	if (amdgpu_in_reset(adev))
2211 		return -EPERM;
2212 
2213 	if (channel >= PP_TEMP_MAX)
2214 		return -EINVAL;
2215 
2216 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2217 	if (r < 0) {
2218 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2219 		return r;
2220 	}
2221 
2222 	switch (channel) {
2223 	case PP_TEMP_JUNCTION:
2224 		/* get current junction temperature */
2225 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2226 					   (void *)&temp, &size);
2227 		break;
2228 	case PP_TEMP_EDGE:
2229 		/* get current edge temperature */
2230 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2231 					   (void *)&temp, &size);
2232 		break;
2233 	case PP_TEMP_MEM:
2234 		/* get current memory temperature */
2235 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2236 					   (void *)&temp, &size);
2237 		break;
2238 	default:
2239 		r = -EINVAL;
2240 		break;
2241 	}
2242 
2243 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2244 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2245 
2246 	if (r)
2247 		return r;
2248 
2249 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2250 }
2251 
2252 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2253 					     struct device_attribute *attr,
2254 					     char *buf)
2255 {
2256 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2257 	int hyst = to_sensor_dev_attr(attr)->index;
2258 	int temp;
2259 
2260 	if (hyst)
2261 		temp = adev->pm.dpm.thermal.min_temp;
2262 	else
2263 		temp = adev->pm.dpm.thermal.max_temp;
2264 
2265 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2266 }
2267 
2268 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2269 					     struct device_attribute *attr,
2270 					     char *buf)
2271 {
2272 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2273 	int hyst = to_sensor_dev_attr(attr)->index;
2274 	int temp;
2275 
2276 	if (hyst)
2277 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2278 	else
2279 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2280 
2281 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2282 }
2283 
2284 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2285 					     struct device_attribute *attr,
2286 					     char *buf)
2287 {
2288 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2289 	int hyst = to_sensor_dev_attr(attr)->index;
2290 	int temp;
2291 
2292 	if (hyst)
2293 		temp = adev->pm.dpm.thermal.min_mem_temp;
2294 	else
2295 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2296 
2297 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2298 }
2299 
2300 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2301 					     struct device_attribute *attr,
2302 					     char *buf)
2303 {
2304 	int channel = to_sensor_dev_attr(attr)->index;
2305 
2306 	if (channel >= PP_TEMP_MAX)
2307 		return -EINVAL;
2308 
2309 	return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label);
2310 }
2311 
2312 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2313 					     struct device_attribute *attr,
2314 					     char *buf)
2315 {
2316 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2317 	int channel = to_sensor_dev_attr(attr)->index;
2318 	int temp = 0;
2319 
2320 	if (channel >= PP_TEMP_MAX)
2321 		return -EINVAL;
2322 
2323 	switch (channel) {
2324 	case PP_TEMP_JUNCTION:
2325 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2326 		break;
2327 	case PP_TEMP_EDGE:
2328 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2329 		break;
2330 	case PP_TEMP_MEM:
2331 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2332 		break;
2333 	}
2334 
2335 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
2336 }
2337 
2338 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2339 					    struct device_attribute *attr,
2340 					    char *buf)
2341 {
2342 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2343 	u32 pwm_mode = 0;
2344 	int ret;
2345 
2346 	if (amdgpu_in_reset(adev))
2347 		return -EPERM;
2348 
2349 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2350 	if (ret < 0) {
2351 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2352 		return ret;
2353 	}
2354 
2355 	if (is_support_sw_smu(adev)) {
2356 		pwm_mode = smu_get_fan_control_mode(&adev->smu);
2357 	} else {
2358 		if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2359 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2360 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2361 			return -EINVAL;
2362 		}
2363 
2364 		pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2365 	}
2366 
2367 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2368 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2369 
2370 	return sprintf(buf, "%i\n", pwm_mode);
2371 }
2372 
2373 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2374 					    struct device_attribute *attr,
2375 					    const char *buf,
2376 					    size_t count)
2377 {
2378 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2379 	int err, ret;
2380 	int value;
2381 
2382 	if (amdgpu_in_reset(adev))
2383 		return -EPERM;
2384 
2385 	err = kstrtoint(buf, 10, &value);
2386 	if (err)
2387 		return err;
2388 
2389 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2390 	if (ret < 0) {
2391 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2392 		return ret;
2393 	}
2394 
2395 	if (is_support_sw_smu(adev)) {
2396 		smu_set_fan_control_mode(&adev->smu, value);
2397 	} else {
2398 		if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2399 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2400 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2401 			return -EINVAL;
2402 		}
2403 
2404 		amdgpu_dpm_set_fan_control_mode(adev, value);
2405 	}
2406 
2407 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2408 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2409 
2410 	return count;
2411 }
2412 
2413 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2414 					 struct device_attribute *attr,
2415 					 char *buf)
2416 {
2417 	return sprintf(buf, "%i\n", 0);
2418 }
2419 
2420 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2421 					 struct device_attribute *attr,
2422 					 char *buf)
2423 {
2424 	return sprintf(buf, "%i\n", 255);
2425 }
2426 
2427 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2428 				     struct device_attribute *attr,
2429 				     const char *buf, size_t count)
2430 {
2431 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2432 	int err;
2433 	u32 value;
2434 	u32 pwm_mode;
2435 
2436 	if (amdgpu_in_reset(adev))
2437 		return -EPERM;
2438 
2439 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2440 	if (err < 0) {
2441 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2442 		return err;
2443 	}
2444 
2445 	if (is_support_sw_smu(adev))
2446 		pwm_mode = smu_get_fan_control_mode(&adev->smu);
2447 	else
2448 		pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2449 
2450 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2451 		pr_info("manual fan speed control should be enabled first\n");
2452 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2453 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2454 		return -EINVAL;
2455 	}
2456 
2457 	err = kstrtou32(buf, 10, &value);
2458 	if (err) {
2459 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2460 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2461 		return err;
2462 	}
2463 
2464 	value = (value * 100) / 255;
2465 
2466 	if (is_support_sw_smu(adev))
2467 		err = smu_set_fan_speed_percent(&adev->smu, value);
2468 	else if (adev->powerplay.pp_funcs->set_fan_speed_percent)
2469 		err = amdgpu_dpm_set_fan_speed_percent(adev, value);
2470 	else
2471 		err = -EINVAL;
2472 
2473 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2474 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2475 
2476 	if (err)
2477 		return err;
2478 
2479 	return count;
2480 }
2481 
2482 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2483 				     struct device_attribute *attr,
2484 				     char *buf)
2485 {
2486 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2487 	int err;
2488 	u32 speed = 0;
2489 
2490 	if (amdgpu_in_reset(adev))
2491 		return -EPERM;
2492 
2493 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2494 	if (err < 0) {
2495 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2496 		return err;
2497 	}
2498 
2499 	if (is_support_sw_smu(adev))
2500 		err = smu_get_fan_speed_percent(&adev->smu, &speed);
2501 	else if (adev->powerplay.pp_funcs->get_fan_speed_percent)
2502 		err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
2503 	else
2504 		err = -EINVAL;
2505 
2506 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2507 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2508 
2509 	if (err)
2510 		return err;
2511 
2512 	speed = (speed * 255) / 100;
2513 
2514 	return sprintf(buf, "%i\n", speed);
2515 }
2516 
2517 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2518 					   struct device_attribute *attr,
2519 					   char *buf)
2520 {
2521 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2522 	int err;
2523 	u32 speed = 0;
2524 
2525 	if (amdgpu_in_reset(adev))
2526 		return -EPERM;
2527 
2528 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2529 	if (err < 0) {
2530 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2531 		return err;
2532 	}
2533 
2534 	if (is_support_sw_smu(adev))
2535 		err = smu_get_fan_speed_rpm(&adev->smu, &speed);
2536 	else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2537 		err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2538 	else
2539 		err = -EINVAL;
2540 
2541 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2542 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2543 
2544 	if (err)
2545 		return err;
2546 
2547 	return sprintf(buf, "%i\n", speed);
2548 }
2549 
2550 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2551 					 struct device_attribute *attr,
2552 					 char *buf)
2553 {
2554 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2555 	u32 min_rpm = 0;
2556 	u32 size = sizeof(min_rpm);
2557 	int r;
2558 
2559 	if (amdgpu_in_reset(adev))
2560 		return -EPERM;
2561 
2562 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2563 	if (r < 0) {
2564 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2565 		return r;
2566 	}
2567 
2568 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2569 				   (void *)&min_rpm, &size);
2570 
2571 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2572 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2573 
2574 	if (r)
2575 		return r;
2576 
2577 	return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
2578 }
2579 
2580 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2581 					 struct device_attribute *attr,
2582 					 char *buf)
2583 {
2584 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2585 	u32 max_rpm = 0;
2586 	u32 size = sizeof(max_rpm);
2587 	int r;
2588 
2589 	if (amdgpu_in_reset(adev))
2590 		return -EPERM;
2591 
2592 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2593 	if (r < 0) {
2594 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2595 		return r;
2596 	}
2597 
2598 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2599 				   (void *)&max_rpm, &size);
2600 
2601 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2602 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2603 
2604 	if (r)
2605 		return r;
2606 
2607 	return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
2608 }
2609 
2610 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2611 					   struct device_attribute *attr,
2612 					   char *buf)
2613 {
2614 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2615 	int err;
2616 	u32 rpm = 0;
2617 
2618 	if (amdgpu_in_reset(adev))
2619 		return -EPERM;
2620 
2621 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2622 	if (err < 0) {
2623 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2624 		return err;
2625 	}
2626 
2627 	if (is_support_sw_smu(adev))
2628 		err = smu_get_fan_speed_rpm(&adev->smu, &rpm);
2629 	else if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2630 		err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2631 	else
2632 		err = -EINVAL;
2633 
2634 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2635 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2636 
2637 	if (err)
2638 		return err;
2639 
2640 	return sprintf(buf, "%i\n", rpm);
2641 }
2642 
2643 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2644 				     struct device_attribute *attr,
2645 				     const char *buf, size_t count)
2646 {
2647 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2648 	int err;
2649 	u32 value;
2650 	u32 pwm_mode;
2651 
2652 	if (amdgpu_in_reset(adev))
2653 		return -EPERM;
2654 
2655 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2656 	if (err < 0) {
2657 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2658 		return err;
2659 	}
2660 
2661 	if (is_support_sw_smu(adev))
2662 		pwm_mode = smu_get_fan_control_mode(&adev->smu);
2663 	else
2664 		pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2665 
2666 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2667 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2668 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2669 		return -ENODATA;
2670 	}
2671 
2672 	err = kstrtou32(buf, 10, &value);
2673 	if (err) {
2674 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2675 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2676 		return err;
2677 	}
2678 
2679 	if (is_support_sw_smu(adev))
2680 		err = smu_set_fan_speed_rpm(&adev->smu, value);
2681 	else if (adev->powerplay.pp_funcs->set_fan_speed_rpm)
2682 		err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2683 	else
2684 		err = -EINVAL;
2685 
2686 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2687 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2688 
2689 	if (err)
2690 		return err;
2691 
2692 	return count;
2693 }
2694 
2695 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2696 					    struct device_attribute *attr,
2697 					    char *buf)
2698 {
2699 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2700 	u32 pwm_mode = 0;
2701 	int ret;
2702 
2703 	if (amdgpu_in_reset(adev))
2704 		return -EPERM;
2705 
2706 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2707 	if (ret < 0) {
2708 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2709 		return ret;
2710 	}
2711 
2712 	if (is_support_sw_smu(adev)) {
2713 		pwm_mode = smu_get_fan_control_mode(&adev->smu);
2714 	} else {
2715 		if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2716 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2717 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2718 			return -EINVAL;
2719 		}
2720 
2721 		pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2722 	}
2723 
2724 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2725 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2726 
2727 	return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2728 }
2729 
2730 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2731 					    struct device_attribute *attr,
2732 					    const char *buf,
2733 					    size_t count)
2734 {
2735 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2736 	int err;
2737 	int value;
2738 	u32 pwm_mode;
2739 
2740 	if (amdgpu_in_reset(adev))
2741 		return -EPERM;
2742 
2743 	err = kstrtoint(buf, 10, &value);
2744 	if (err)
2745 		return err;
2746 
2747 	if (value == 0)
2748 		pwm_mode = AMD_FAN_CTRL_AUTO;
2749 	else if (value == 1)
2750 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2751 	else
2752 		return -EINVAL;
2753 
2754 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2755 	if (err < 0) {
2756 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2757 		return err;
2758 	}
2759 
2760 	if (is_support_sw_smu(adev)) {
2761 		smu_set_fan_control_mode(&adev->smu, pwm_mode);
2762 	} else {
2763 		if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2764 			pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2765 			pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2766 			return -EINVAL;
2767 		}
2768 		amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2769 	}
2770 
2771 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2772 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2773 
2774 	return count;
2775 }
2776 
2777 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2778 					struct device_attribute *attr,
2779 					char *buf)
2780 {
2781 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2782 	u32 vddgfx;
2783 	int r, size = sizeof(vddgfx);
2784 
2785 	if (amdgpu_in_reset(adev))
2786 		return -EPERM;
2787 
2788 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2789 	if (r < 0) {
2790 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2791 		return r;
2792 	}
2793 
2794 	/* get the voltage */
2795 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2796 				   (void *)&vddgfx, &size);
2797 
2798 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2799 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2800 
2801 	if (r)
2802 		return r;
2803 
2804 	return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
2805 }
2806 
2807 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2808 					      struct device_attribute *attr,
2809 					      char *buf)
2810 {
2811 	return snprintf(buf, PAGE_SIZE, "vddgfx\n");
2812 }
2813 
2814 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2815 				       struct device_attribute *attr,
2816 				       char *buf)
2817 {
2818 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2819 	u32 vddnb;
2820 	int r, size = sizeof(vddnb);
2821 
2822 	if (amdgpu_in_reset(adev))
2823 		return -EPERM;
2824 
2825 	/* only APUs have vddnb */
2826 	if  (!(adev->flags & AMD_IS_APU))
2827 		return -EINVAL;
2828 
2829 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2830 	if (r < 0) {
2831 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2832 		return r;
2833 	}
2834 
2835 	/* get the voltage */
2836 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2837 				   (void *)&vddnb, &size);
2838 
2839 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2840 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2841 
2842 	if (r)
2843 		return r;
2844 
2845 	return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
2846 }
2847 
2848 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2849 					      struct device_attribute *attr,
2850 					      char *buf)
2851 {
2852 	return snprintf(buf, PAGE_SIZE, "vddnb\n");
2853 }
2854 
2855 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2856 					   struct device_attribute *attr,
2857 					   char *buf)
2858 {
2859 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2860 	u32 query = 0;
2861 	int r, size = sizeof(u32);
2862 	unsigned uw;
2863 
2864 	if (amdgpu_in_reset(adev))
2865 		return -EPERM;
2866 
2867 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2868 	if (r < 0) {
2869 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2870 		return r;
2871 	}
2872 
2873 	/* get the voltage */
2874 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2875 				   (void *)&query, &size);
2876 
2877 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2878 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2879 
2880 	if (r)
2881 		return r;
2882 
2883 	/* convert to microwatts */
2884 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2885 
2886 	return snprintf(buf, PAGE_SIZE, "%u\n", uw);
2887 }
2888 
2889 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2890 					 struct device_attribute *attr,
2891 					 char *buf)
2892 {
2893 	return sprintf(buf, "%i\n", 0);
2894 }
2895 
2896 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
2897 					 struct device_attribute *attr,
2898 					 char *buf)
2899 {
2900 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2901 	uint32_t limit = 0;
2902 	ssize_t size;
2903 	int r;
2904 
2905 	if (amdgpu_in_reset(adev))
2906 		return -EPERM;
2907 
2908 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2909 	if (r < 0) {
2910 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2911 		return r;
2912 	}
2913 
2914 	if (is_support_sw_smu(adev)) {
2915 		smu_get_power_limit(&adev->smu, &limit, true);
2916 		size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2917 	} else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2918 		adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
2919 		size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2920 	} else {
2921 		size = snprintf(buf, PAGE_SIZE, "\n");
2922 	}
2923 
2924 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2925 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2926 
2927 	return size;
2928 }
2929 
2930 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2931 					 struct device_attribute *attr,
2932 					 char *buf)
2933 {
2934 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2935 	uint32_t limit = 0;
2936 	ssize_t size;
2937 	int r;
2938 
2939 	if (amdgpu_in_reset(adev))
2940 		return -EPERM;
2941 
2942 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2943 	if (r < 0) {
2944 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2945 		return r;
2946 	}
2947 
2948 	if (is_support_sw_smu(adev)) {
2949 		smu_get_power_limit(&adev->smu, &limit, false);
2950 		size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2951 	} else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2952 		adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
2953 		size = snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2954 	} else {
2955 		size = snprintf(buf, PAGE_SIZE, "\n");
2956 	}
2957 
2958 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2959 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2960 
2961 	return size;
2962 }
2963 
2964 
2965 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2966 		struct device_attribute *attr,
2967 		const char *buf,
2968 		size_t count)
2969 {
2970 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2971 	int err;
2972 	u32 value;
2973 
2974 	if (amdgpu_in_reset(adev))
2975 		return -EPERM;
2976 
2977 	if (amdgpu_sriov_vf(adev))
2978 		return -EINVAL;
2979 
2980 	err = kstrtou32(buf, 10, &value);
2981 	if (err)
2982 		return err;
2983 
2984 	value = value / 1000000; /* convert to Watt */
2985 
2986 
2987 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2988 	if (err < 0) {
2989 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2990 		return err;
2991 	}
2992 
2993 	if (is_support_sw_smu(adev))
2994 		err = smu_set_power_limit(&adev->smu, value);
2995 	else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit)
2996 		err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
2997 	else
2998 		err = -EINVAL;
2999 
3000 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3001 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3002 
3003 	if (err)
3004 		return err;
3005 
3006 	return count;
3007 }
3008 
3009 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3010 				      struct device_attribute *attr,
3011 				      char *buf)
3012 {
3013 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3014 	uint32_t sclk;
3015 	int r, size = sizeof(sclk);
3016 
3017 	if (amdgpu_in_reset(adev))
3018 		return -EPERM;
3019 
3020 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3021 	if (r < 0) {
3022 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3023 		return r;
3024 	}
3025 
3026 	/* get the sclk */
3027 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3028 				   (void *)&sclk, &size);
3029 
3030 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3031 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3032 
3033 	if (r)
3034 		return r;
3035 
3036 	return snprintf(buf, PAGE_SIZE, "%u\n", sclk * 10 * 1000);
3037 }
3038 
3039 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3040 					    struct device_attribute *attr,
3041 					    char *buf)
3042 {
3043 	return snprintf(buf, PAGE_SIZE, "sclk\n");
3044 }
3045 
3046 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3047 				      struct device_attribute *attr,
3048 				      char *buf)
3049 {
3050 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3051 	uint32_t mclk;
3052 	int r, size = sizeof(mclk);
3053 
3054 	if (amdgpu_in_reset(adev))
3055 		return -EPERM;
3056 
3057 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3058 	if (r < 0) {
3059 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3060 		return r;
3061 	}
3062 
3063 	/* get the sclk */
3064 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3065 				   (void *)&mclk, &size);
3066 
3067 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3068 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3069 
3070 	if (r)
3071 		return r;
3072 
3073 	return snprintf(buf, PAGE_SIZE, "%u\n", mclk * 10 * 1000);
3074 }
3075 
3076 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3077 					    struct device_attribute *attr,
3078 					    char *buf)
3079 {
3080 	return snprintf(buf, PAGE_SIZE, "mclk\n");
3081 }
3082 
3083 /**
3084  * DOC: hwmon
3085  *
3086  * The amdgpu driver exposes the following sensor interfaces:
3087  *
3088  * - GPU temperature (via the on-die sensor)
3089  *
3090  * - GPU voltage
3091  *
3092  * - Northbridge voltage (APUs only)
3093  *
3094  * - GPU power
3095  *
3096  * - GPU fan
3097  *
3098  * - GPU gfx/compute engine clock
3099  *
3100  * - GPU memory clock (dGPU only)
3101  *
3102  * hwmon interfaces for GPU temperature:
3103  *
3104  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3105  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3106  *
3107  * - temp[1-3]_label: temperature channel label
3108  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3109  *
3110  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3111  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3112  *
3113  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3114  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3115  *
3116  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3117  *   - these are supported on SOC15 dGPUs only
3118  *
3119  * hwmon interfaces for GPU voltage:
3120  *
3121  * - in0_input: the voltage on the GPU in millivolts
3122  *
3123  * - in1_input: the voltage on the Northbridge in millivolts
3124  *
3125  * hwmon interfaces for GPU power:
3126  *
3127  * - power1_average: average power used by the GPU in microWatts
3128  *
3129  * - power1_cap_min: minimum cap supported in microWatts
3130  *
3131  * - power1_cap_max: maximum cap supported in microWatts
3132  *
3133  * - power1_cap: selected power cap in microWatts
3134  *
3135  * hwmon interfaces for GPU fan:
3136  *
3137  * - pwm1: pulse width modulation fan level (0-255)
3138  *
3139  * - 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)
3140  *
3141  * - pwm1_min: pulse width modulation fan control minimum level (0)
3142  *
3143  * - pwm1_max: pulse width modulation fan control maximum level (255)
3144  *
3145  * - fan1_min: an minimum value Unit: revolution/min (RPM)
3146  *
3147  * - fan1_max: an maxmum value Unit: revolution/max (RPM)
3148  *
3149  * - fan1_input: fan speed in RPM
3150  *
3151  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3152  *
3153  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3154  *
3155  * hwmon interfaces for GPU clocks:
3156  *
3157  * - freq1_input: the gfx/compute clock in hertz
3158  *
3159  * - freq2_input: the memory clock in hertz
3160  *
3161  * You can use hwmon tools like sensors to view this information on your system.
3162  *
3163  */
3164 
3165 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3166 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3167 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3168 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3169 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3170 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3171 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3172 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3173 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3174 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3175 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3176 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3177 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3178 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3179 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3180 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3181 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3182 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3183 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3184 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3185 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3186 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3187 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3188 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3189 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3190 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3191 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3192 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3193 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3194 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3195 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3196 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3197 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3198 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3199 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3200 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3201 
3202 static struct attribute *hwmon_attributes[] = {
3203 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3204 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3205 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3206 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3207 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3208 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3209 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3210 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3211 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3212 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3213 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3214 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3215 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3216 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3217 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3218 	&sensor_dev_attr_pwm1.dev_attr.attr,
3219 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3220 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3221 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3222 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3223 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3224 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3225 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3226 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3227 	&sensor_dev_attr_in0_input.dev_attr.attr,
3228 	&sensor_dev_attr_in0_label.dev_attr.attr,
3229 	&sensor_dev_attr_in1_input.dev_attr.attr,
3230 	&sensor_dev_attr_in1_label.dev_attr.attr,
3231 	&sensor_dev_attr_power1_average.dev_attr.attr,
3232 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3233 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3234 	&sensor_dev_attr_power1_cap.dev_attr.attr,
3235 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3236 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3237 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3238 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3239 	NULL
3240 };
3241 
3242 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3243 					struct attribute *attr, int index)
3244 {
3245 	struct device *dev = kobj_to_dev(kobj);
3246 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3247 	umode_t effective_mode = attr->mode;
3248 
3249 	/* under multi-vf mode, the hwmon attributes are all not supported */
3250 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3251 		return 0;
3252 
3253 	/* there is no fan under pp one vf mode */
3254 	if (amdgpu_sriov_is_pp_one_vf(adev) &&
3255 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3256 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3257 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3258 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3259 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3260 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3261 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3262 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3263 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3264 		return 0;
3265 
3266 	/* Skip fan attributes if fan is not present */
3267 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3268 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3269 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3270 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3271 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3272 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3273 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3274 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3275 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3276 		return 0;
3277 
3278 	/* Skip fan attributes on APU */
3279 	if ((adev->flags & AMD_IS_APU) &&
3280 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3281 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3282 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3283 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3284 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3285 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3286 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3287 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3288 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3289 		return 0;
3290 
3291 	/* Skip crit temp on APU */
3292 	if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) &&
3293 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3294 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3295 		return 0;
3296 
3297 	/* Skip limit attributes if DPM is not enabled */
3298 	if (!adev->pm.dpm_enabled &&
3299 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3300 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3301 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3302 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3303 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3304 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3305 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3306 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3307 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3308 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3309 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3310 		return 0;
3311 
3312 	if (!is_support_sw_smu(adev)) {
3313 		/* mask fan attributes if we have no bindings for this asic to expose */
3314 		if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
3315 		     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3316 		    (!adev->powerplay.pp_funcs->get_fan_control_mode &&
3317 		     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3318 			effective_mode &= ~S_IRUGO;
3319 
3320 		if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
3321 		     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3322 		    (!adev->powerplay.pp_funcs->set_fan_control_mode &&
3323 		     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3324 			effective_mode &= ~S_IWUSR;
3325 	}
3326 
3327 	if (((adev->flags & AMD_IS_APU) ||
3328 	     adev->family == AMDGPU_FAMILY_SI) &&	/* not implemented yet */
3329 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3330 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
3331 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
3332 		return 0;
3333 
3334 	if (((adev->family == AMDGPU_FAMILY_SI) ||
3335 	     ((adev->flags & AMD_IS_APU) &&
3336 	      (adev->asic_type < CHIP_RENOIR))) &&	/* not implemented yet */
3337 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3338 		return 0;
3339 
3340 	if (!is_support_sw_smu(adev)) {
3341 		/* hide max/min values if we can't both query and manage the fan */
3342 		if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
3343 		     !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
3344 		     (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3345 		     !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3346 		    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3347 		     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3348 			return 0;
3349 
3350 		if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3351 		     !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3352 		    (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3353 		     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3354 			return 0;
3355 	}
3356 
3357 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
3358 	     adev->family == AMDGPU_FAMILY_KV) &&	/* not implemented yet */
3359 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3360 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3361 		return 0;
3362 
3363 	/* only APUs have vddnb */
3364 	if (!(adev->flags & AMD_IS_APU) &&
3365 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3366 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3367 		return 0;
3368 
3369 	/* no mclk on APUs */
3370 	if ((adev->flags & AMD_IS_APU) &&
3371 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3372 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3373 		return 0;
3374 
3375 	/* only SOC15 dGPUs support hotspot and mem temperatures */
3376 	if (((adev->flags & AMD_IS_APU) ||
3377 	     adev->asic_type < CHIP_VEGA10) &&
3378 	    (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3379 	     attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3380 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3381 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3382 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3383 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3384 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3385 	     attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3386 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3387 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3388 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3389 		return 0;
3390 
3391 	return effective_mode;
3392 }
3393 
3394 static const struct attribute_group hwmon_attrgroup = {
3395 	.attrs = hwmon_attributes,
3396 	.is_visible = hwmon_attributes_visible,
3397 };
3398 
3399 static const struct attribute_group *hwmon_groups[] = {
3400 	&hwmon_attrgroup,
3401 	NULL
3402 };
3403 
3404 #endif /* __linux__ */
3405 
3406 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3407 {
3408 	return 0;
3409 #ifdef __linux__
3410 	int ret;
3411 	uint32_t mask = 0;
3412 
3413 	if (adev->pm.sysfs_initialized)
3414 		return 0;
3415 
3416 	if (adev->pm.dpm_enabled == 0)
3417 		return 0;
3418 
3419 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
3420 
3421 	adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3422 								   DRIVER_NAME, adev,
3423 								   hwmon_groups);
3424 	if (IS_ERR(adev->pm.int_hwmon_dev)) {
3425 		ret = PTR_ERR(adev->pm.int_hwmon_dev);
3426 		dev_err(adev->dev,
3427 			"Unable to register hwmon device: %d\n", ret);
3428 		return ret;
3429 	}
3430 
3431 	switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3432 	case SRIOV_VF_MODE_ONE_VF:
3433 		mask = ATTR_FLAG_ONEVF;
3434 		break;
3435 	case SRIOV_VF_MODE_MULTI_VF:
3436 		mask = 0;
3437 		break;
3438 	case SRIOV_VF_MODE_BARE_METAL:
3439 	default:
3440 		mask = ATTR_FLAG_MASK_ALL;
3441 		break;
3442 	}
3443 
3444 	ret = amdgpu_device_attr_create_groups(adev,
3445 					       amdgpu_device_attrs,
3446 					       ARRAY_SIZE(amdgpu_device_attrs),
3447 					       mask,
3448 					       &adev->pm.pm_attr_list);
3449 	if (ret)
3450 		return ret;
3451 
3452 	adev->pm.sysfs_initialized = true;
3453 
3454 	return 0;
3455 #endif
3456 }
3457 
3458 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3459 {
3460 #ifdef __linux__
3461 	if (adev->pm.dpm_enabled == 0)
3462 		return;
3463 
3464 	if (adev->pm.int_hwmon_dev)
3465 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
3466 
3467 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3468 #endif
3469 }
3470 
3471 /*
3472  * Debugfs info
3473  */
3474 #if defined(CONFIG_DEBUG_FS)
3475 
3476 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3477 {
3478 	uint32_t value;
3479 	uint64_t value64;
3480 	uint32_t query = 0;
3481 	int size;
3482 
3483 	/* GPU Clocks */
3484 	size = sizeof(value);
3485 	seq_printf(m, "GFX Clocks and Power:\n");
3486 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3487 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3488 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3489 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3490 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3491 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3492 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3493 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3494 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3495 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3496 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3497 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
3498 	size = sizeof(uint32_t);
3499 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3500 		seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3501 	size = sizeof(value);
3502 	seq_printf(m, "\n");
3503 
3504 	/* GPU Temp */
3505 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3506 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3507 
3508 	/* GPU Load */
3509 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3510 		seq_printf(m, "GPU Load: %u %%\n", value);
3511 	/* MEM Load */
3512 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3513 		seq_printf(m, "MEM Load: %u %%\n", value);
3514 
3515 	seq_printf(m, "\n");
3516 
3517 	/* SMC feature mask */
3518 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3519 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3520 
3521 	if (adev->asic_type > CHIP_VEGA20) {
3522 		/* VCN clocks */
3523 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3524 			if (!value) {
3525 				seq_printf(m, "VCN: Disabled\n");
3526 			} else {
3527 				seq_printf(m, "VCN: Enabled\n");
3528 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3529 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3530 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3531 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3532 			}
3533 		}
3534 		seq_printf(m, "\n");
3535 	} else {
3536 		/* UVD clocks */
3537 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3538 			if (!value) {
3539 				seq_printf(m, "UVD: Disabled\n");
3540 			} else {
3541 				seq_printf(m, "UVD: Enabled\n");
3542 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3543 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3544 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3545 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3546 			}
3547 		}
3548 		seq_printf(m, "\n");
3549 
3550 		/* VCE clocks */
3551 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3552 			if (!value) {
3553 				seq_printf(m, "VCE: Disabled\n");
3554 			} else {
3555 				seq_printf(m, "VCE: Enabled\n");
3556 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3557 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3558 			}
3559 		}
3560 	}
3561 
3562 	return 0;
3563 }
3564 
3565 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3566 {
3567 	int i;
3568 
3569 	for (i = 0; clocks[i].flag; i++)
3570 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
3571 			   (flags & clocks[i].flag) ? "On" : "Off");
3572 }
3573 
3574 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
3575 {
3576 	struct drm_info_node *node = (struct drm_info_node *) m->private;
3577 	struct drm_device *dev = node->minor->dev;
3578 	struct amdgpu_device *adev = drm_to_adev(dev);
3579 	u32 flags = 0;
3580 	int r;
3581 
3582 	if (amdgpu_in_reset(adev))
3583 		return -EPERM;
3584 
3585 	r = pm_runtime_get_sync(dev->dev);
3586 	if (r < 0) {
3587 		pm_runtime_put_autosuspend(dev->dev);
3588 		return r;
3589 	}
3590 
3591 	if (!adev->pm.dpm_enabled) {
3592 		seq_printf(m, "dpm not enabled\n");
3593 		pm_runtime_mark_last_busy(dev->dev);
3594 		pm_runtime_put_autosuspend(dev->dev);
3595 		return 0;
3596 	}
3597 
3598 	if (!is_support_sw_smu(adev) &&
3599 	    adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3600 		mutex_lock(&adev->pm.mutex);
3601 		if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3602 			adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3603 		else
3604 			seq_printf(m, "Debugfs support not implemented for this asic\n");
3605 		mutex_unlock(&adev->pm.mutex);
3606 		r = 0;
3607 	} else {
3608 		r = amdgpu_debugfs_pm_info_pp(m, adev);
3609 	}
3610 	if (r)
3611 		goto out;
3612 
3613 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
3614 
3615 	seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3616 	amdgpu_parse_cg_state(m, flags);
3617 	seq_printf(m, "\n");
3618 
3619 out:
3620 	pm_runtime_mark_last_busy(dev->dev);
3621 	pm_runtime_put_autosuspend(dev->dev);
3622 
3623 	return r;
3624 }
3625 
3626 static const struct drm_info_list amdgpu_pm_info_list[] = {
3627 	{"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
3628 };
3629 #endif
3630 
3631 int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3632 {
3633 #if defined(CONFIG_DEBUG_FS)
3634 	return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
3635 #else
3636 	return 0;
3637 #endif
3638 }
3639