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