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