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