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