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