xref: /openbmc/linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision e6b58555558a1ea653e415fc45308964087f9053)
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 		case IP_VERSION(11, 0, 0):
1998 			*states = ATTR_STATE_SUPPORTED;
1999 			break;
2000 		default:
2001 			*states = ATTR_STATE_UNSUPPORTED;
2002 		}
2003 	} else if (DEVICE_ATTR_IS(pp_features)) {
2004 		if (adev->flags & AMD_IS_APU || gc_ver < IP_VERSION(9, 0, 0))
2005 			*states = ATTR_STATE_UNSUPPORTED;
2006 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
2007 		if (gc_ver < IP_VERSION(9, 1, 0))
2008 			*states = ATTR_STATE_UNSUPPORTED;
2009 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
2010 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2011 		      gc_ver == IP_VERSION(10, 3, 0) ||
2012 		      gc_ver == IP_VERSION(10, 1, 2) ||
2013 		      gc_ver == IP_VERSION(11, 0, 0)))
2014 			*states = ATTR_STATE_UNSUPPORTED;
2015 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
2016 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2017 		      gc_ver == IP_VERSION(10, 3, 0) ||
2018 		      gc_ver == IP_VERSION(10, 1, 2) ||
2019 		      gc_ver == IP_VERSION(11, 0, 0)))
2020 			*states = ATTR_STATE_UNSUPPORTED;
2021 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
2022 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2023 			*states = ATTR_STATE_UNSUPPORTED;
2024 	}
2025 
2026 	switch (gc_ver) {
2027 	case IP_VERSION(9, 4, 1):
2028 	case IP_VERSION(9, 4, 2):
2029 		/* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2030 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2031 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2032 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2033 			dev_attr->attr.mode &= ~S_IWUGO;
2034 			dev_attr->store = NULL;
2035 		}
2036 		break;
2037 	default:
2038 		break;
2039 	}
2040 
2041 	if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2042 		/* SMU MP1 does not support dcefclk level setting */
2043 		if (gc_ver >= IP_VERSION(10, 0, 0)) {
2044 			dev_attr->attr.mode &= ~S_IWUGO;
2045 			dev_attr->store = NULL;
2046 		}
2047 	}
2048 
2049 	/* setting should not be allowed from VF if not in one VF mode */
2050 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
2051 		dev_attr->attr.mode &= ~S_IWUGO;
2052 		dev_attr->store = NULL;
2053 	}
2054 
2055 #undef DEVICE_ATTR_IS
2056 
2057 	return 0;
2058 }
2059 
2060 
2061 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2062 				     struct amdgpu_device_attr *attr,
2063 				     uint32_t mask, struct list_head *attr_list)
2064 {
2065 	int ret = 0;
2066 	struct device_attribute *dev_attr = &attr->dev_attr;
2067 	const char *name = dev_attr->attr.name;
2068 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2069 	struct amdgpu_device_attr_entry *attr_entry;
2070 
2071 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2072 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2073 
2074 	BUG_ON(!attr);
2075 
2076 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2077 
2078 	ret = attr_update(adev, attr, mask, &attr_states);
2079 	if (ret) {
2080 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2081 			name, ret);
2082 		return ret;
2083 	}
2084 
2085 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2086 		return 0;
2087 
2088 	ret = device_create_file(adev->dev, dev_attr);
2089 	if (ret) {
2090 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2091 			name, ret);
2092 	}
2093 
2094 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2095 	if (!attr_entry)
2096 		return -ENOMEM;
2097 
2098 	attr_entry->attr = attr;
2099 	INIT_LIST_HEAD(&attr_entry->entry);
2100 
2101 	list_add_tail(&attr_entry->entry, attr_list);
2102 
2103 	return ret;
2104 }
2105 
2106 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2107 {
2108 	struct device_attribute *dev_attr = &attr->dev_attr;
2109 
2110 	device_remove_file(adev->dev, dev_attr);
2111 }
2112 
2113 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2114 					     struct list_head *attr_list);
2115 
2116 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2117 					    struct amdgpu_device_attr *attrs,
2118 					    uint32_t counts,
2119 					    uint32_t mask,
2120 					    struct list_head *attr_list)
2121 {
2122 	int ret = 0;
2123 	uint32_t i = 0;
2124 
2125 	for (i = 0; i < counts; i++) {
2126 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2127 		if (ret)
2128 			goto failed;
2129 	}
2130 
2131 	return 0;
2132 
2133 failed:
2134 	amdgpu_device_attr_remove_groups(adev, attr_list);
2135 
2136 	return ret;
2137 }
2138 
2139 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2140 					     struct list_head *attr_list)
2141 {
2142 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2143 
2144 	if (list_empty(attr_list))
2145 		return ;
2146 
2147 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2148 		amdgpu_device_attr_remove(adev, entry->attr);
2149 		list_del(&entry->entry);
2150 		kfree(entry);
2151 	}
2152 }
2153 
2154 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2155 				      struct device_attribute *attr,
2156 				      char *buf)
2157 {
2158 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2159 	int channel = to_sensor_dev_attr(attr)->index;
2160 	int r, temp = 0, size = sizeof(temp);
2161 
2162 	if (amdgpu_in_reset(adev))
2163 		return -EPERM;
2164 	if (adev->in_suspend && !adev->in_runpm)
2165 		return -EPERM;
2166 
2167 	if (channel >= PP_TEMP_MAX)
2168 		return -EINVAL;
2169 
2170 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2171 	if (r < 0) {
2172 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2173 		return r;
2174 	}
2175 
2176 	switch (channel) {
2177 	case PP_TEMP_JUNCTION:
2178 		/* get current junction temperature */
2179 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2180 					   (void *)&temp, &size);
2181 		break;
2182 	case PP_TEMP_EDGE:
2183 		/* get current edge temperature */
2184 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2185 					   (void *)&temp, &size);
2186 		break;
2187 	case PP_TEMP_MEM:
2188 		/* get current memory temperature */
2189 		r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2190 					   (void *)&temp, &size);
2191 		break;
2192 	default:
2193 		r = -EINVAL;
2194 		break;
2195 	}
2196 
2197 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2198 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2199 
2200 	if (r)
2201 		return r;
2202 
2203 	return sysfs_emit(buf, "%d\n", temp);
2204 }
2205 
2206 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2207 					     struct device_attribute *attr,
2208 					     char *buf)
2209 {
2210 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2211 	int hyst = to_sensor_dev_attr(attr)->index;
2212 	int temp;
2213 
2214 	if (hyst)
2215 		temp = adev->pm.dpm.thermal.min_temp;
2216 	else
2217 		temp = adev->pm.dpm.thermal.max_temp;
2218 
2219 	return sysfs_emit(buf, "%d\n", temp);
2220 }
2221 
2222 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2223 					     struct device_attribute *attr,
2224 					     char *buf)
2225 {
2226 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2227 	int hyst = to_sensor_dev_attr(attr)->index;
2228 	int temp;
2229 
2230 	if (hyst)
2231 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2232 	else
2233 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2234 
2235 	return sysfs_emit(buf, "%d\n", temp);
2236 }
2237 
2238 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2239 					     struct device_attribute *attr,
2240 					     char *buf)
2241 {
2242 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2243 	int hyst = to_sensor_dev_attr(attr)->index;
2244 	int temp;
2245 
2246 	if (hyst)
2247 		temp = adev->pm.dpm.thermal.min_mem_temp;
2248 	else
2249 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2250 
2251 	return sysfs_emit(buf, "%d\n", temp);
2252 }
2253 
2254 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2255 					     struct device_attribute *attr,
2256 					     char *buf)
2257 {
2258 	int channel = to_sensor_dev_attr(attr)->index;
2259 
2260 	if (channel >= PP_TEMP_MAX)
2261 		return -EINVAL;
2262 
2263 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2264 }
2265 
2266 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2267 					     struct device_attribute *attr,
2268 					     char *buf)
2269 {
2270 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2271 	int channel = to_sensor_dev_attr(attr)->index;
2272 	int temp = 0;
2273 
2274 	if (channel >= PP_TEMP_MAX)
2275 		return -EINVAL;
2276 
2277 	switch (channel) {
2278 	case PP_TEMP_JUNCTION:
2279 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2280 		break;
2281 	case PP_TEMP_EDGE:
2282 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2283 		break;
2284 	case PP_TEMP_MEM:
2285 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2286 		break;
2287 	}
2288 
2289 	return sysfs_emit(buf, "%d\n", temp);
2290 }
2291 
2292 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2293 					    struct device_attribute *attr,
2294 					    char *buf)
2295 {
2296 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2297 	u32 pwm_mode = 0;
2298 	int ret;
2299 
2300 	if (amdgpu_in_reset(adev))
2301 		return -EPERM;
2302 	if (adev->in_suspend && !adev->in_runpm)
2303 		return -EPERM;
2304 
2305 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2306 	if (ret < 0) {
2307 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2308 		return ret;
2309 	}
2310 
2311 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2312 
2313 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2314 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2315 
2316 	if (ret)
2317 		return -EINVAL;
2318 
2319 	return sysfs_emit(buf, "%u\n", pwm_mode);
2320 }
2321 
2322 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2323 					    struct device_attribute *attr,
2324 					    const char *buf,
2325 					    size_t count)
2326 {
2327 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2328 	int err, ret;
2329 	int value;
2330 
2331 	if (amdgpu_in_reset(adev))
2332 		return -EPERM;
2333 	if (adev->in_suspend && !adev->in_runpm)
2334 		return -EPERM;
2335 
2336 	err = kstrtoint(buf, 10, &value);
2337 	if (err)
2338 		return err;
2339 
2340 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2341 	if (ret < 0) {
2342 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2343 		return ret;
2344 	}
2345 
2346 	ret = amdgpu_dpm_set_fan_control_mode(adev, value);
2347 
2348 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2349 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2350 
2351 	if (ret)
2352 		return -EINVAL;
2353 
2354 	return count;
2355 }
2356 
2357 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2358 					 struct device_attribute *attr,
2359 					 char *buf)
2360 {
2361 	return sysfs_emit(buf, "%i\n", 0);
2362 }
2363 
2364 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2365 					 struct device_attribute *attr,
2366 					 char *buf)
2367 {
2368 	return sysfs_emit(buf, "%i\n", 255);
2369 }
2370 
2371 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2372 				     struct device_attribute *attr,
2373 				     const char *buf, size_t count)
2374 {
2375 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2376 	int err;
2377 	u32 value;
2378 	u32 pwm_mode;
2379 
2380 	if (amdgpu_in_reset(adev))
2381 		return -EPERM;
2382 	if (adev->in_suspend && !adev->in_runpm)
2383 		return -EPERM;
2384 
2385 	err = kstrtou32(buf, 10, &value);
2386 	if (err)
2387 		return err;
2388 
2389 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2390 	if (err < 0) {
2391 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2392 		return err;
2393 	}
2394 
2395 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2396 	if (err)
2397 		goto out;
2398 
2399 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2400 		pr_info("manual fan speed control should be enabled first\n");
2401 		err = -EINVAL;
2402 		goto out;
2403 	}
2404 
2405 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
2406 
2407 out:
2408 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2409 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2410 
2411 	if (err)
2412 		return err;
2413 
2414 	return count;
2415 }
2416 
2417 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2418 				     struct device_attribute *attr,
2419 				     char *buf)
2420 {
2421 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2422 	int err;
2423 	u32 speed = 0;
2424 
2425 	if (amdgpu_in_reset(adev))
2426 		return -EPERM;
2427 	if (adev->in_suspend && !adev->in_runpm)
2428 		return -EPERM;
2429 
2430 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2431 	if (err < 0) {
2432 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2433 		return err;
2434 	}
2435 
2436 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2437 
2438 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2439 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2440 
2441 	if (err)
2442 		return err;
2443 
2444 	return sysfs_emit(buf, "%i\n", speed);
2445 }
2446 
2447 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2448 					   struct device_attribute *attr,
2449 					   char *buf)
2450 {
2451 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2452 	int err;
2453 	u32 speed = 0;
2454 
2455 	if (amdgpu_in_reset(adev))
2456 		return -EPERM;
2457 	if (adev->in_suspend && !adev->in_runpm)
2458 		return -EPERM;
2459 
2460 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2461 	if (err < 0) {
2462 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2463 		return err;
2464 	}
2465 
2466 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2467 
2468 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2469 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2470 
2471 	if (err)
2472 		return err;
2473 
2474 	return sysfs_emit(buf, "%i\n", speed);
2475 }
2476 
2477 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2478 					 struct device_attribute *attr,
2479 					 char *buf)
2480 {
2481 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2482 	u32 min_rpm = 0;
2483 	u32 size = sizeof(min_rpm);
2484 	int r;
2485 
2486 	if (amdgpu_in_reset(adev))
2487 		return -EPERM;
2488 	if (adev->in_suspend && !adev->in_runpm)
2489 		return -EPERM;
2490 
2491 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2492 	if (r < 0) {
2493 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2494 		return r;
2495 	}
2496 
2497 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2498 				   (void *)&min_rpm, &size);
2499 
2500 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2501 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2502 
2503 	if (r)
2504 		return r;
2505 
2506 	return sysfs_emit(buf, "%d\n", min_rpm);
2507 }
2508 
2509 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2510 					 struct device_attribute *attr,
2511 					 char *buf)
2512 {
2513 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2514 	u32 max_rpm = 0;
2515 	u32 size = sizeof(max_rpm);
2516 	int r;
2517 
2518 	if (amdgpu_in_reset(adev))
2519 		return -EPERM;
2520 	if (adev->in_suspend && !adev->in_runpm)
2521 		return -EPERM;
2522 
2523 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2524 	if (r < 0) {
2525 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2526 		return r;
2527 	}
2528 
2529 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2530 				   (void *)&max_rpm, &size);
2531 
2532 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2533 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2534 
2535 	if (r)
2536 		return r;
2537 
2538 	return sysfs_emit(buf, "%d\n", max_rpm);
2539 }
2540 
2541 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2542 					   struct device_attribute *attr,
2543 					   char *buf)
2544 {
2545 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2546 	int err;
2547 	u32 rpm = 0;
2548 
2549 	if (amdgpu_in_reset(adev))
2550 		return -EPERM;
2551 	if (adev->in_suspend && !adev->in_runpm)
2552 		return -EPERM;
2553 
2554 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2555 	if (err < 0) {
2556 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2557 		return err;
2558 	}
2559 
2560 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2561 
2562 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2563 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2564 
2565 	if (err)
2566 		return err;
2567 
2568 	return sysfs_emit(buf, "%i\n", rpm);
2569 }
2570 
2571 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2572 				     struct device_attribute *attr,
2573 				     const char *buf, size_t count)
2574 {
2575 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2576 	int err;
2577 	u32 value;
2578 	u32 pwm_mode;
2579 
2580 	if (amdgpu_in_reset(adev))
2581 		return -EPERM;
2582 	if (adev->in_suspend && !adev->in_runpm)
2583 		return -EPERM;
2584 
2585 	err = kstrtou32(buf, 10, &value);
2586 	if (err)
2587 		return err;
2588 
2589 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2590 	if (err < 0) {
2591 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2592 		return err;
2593 	}
2594 
2595 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2596 	if (err)
2597 		goto out;
2598 
2599 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2600 		err = -ENODATA;
2601 		goto out;
2602 	}
2603 
2604 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2605 
2606 out:
2607 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2608 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2609 
2610 	if (err)
2611 		return err;
2612 
2613 	return count;
2614 }
2615 
2616 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2617 					    struct device_attribute *attr,
2618 					    char *buf)
2619 {
2620 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2621 	u32 pwm_mode = 0;
2622 	int ret;
2623 
2624 	if (amdgpu_in_reset(adev))
2625 		return -EPERM;
2626 	if (adev->in_suspend && !adev->in_runpm)
2627 		return -EPERM;
2628 
2629 	ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2630 	if (ret < 0) {
2631 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2632 		return ret;
2633 	}
2634 
2635 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2636 
2637 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2638 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2639 
2640 	if (ret)
2641 		return -EINVAL;
2642 
2643 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2644 }
2645 
2646 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2647 					    struct device_attribute *attr,
2648 					    const char *buf,
2649 					    size_t count)
2650 {
2651 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2652 	int err;
2653 	int value;
2654 	u32 pwm_mode;
2655 
2656 	if (amdgpu_in_reset(adev))
2657 		return -EPERM;
2658 	if (adev->in_suspend && !adev->in_runpm)
2659 		return -EPERM;
2660 
2661 	err = kstrtoint(buf, 10, &value);
2662 	if (err)
2663 		return err;
2664 
2665 	if (value == 0)
2666 		pwm_mode = AMD_FAN_CTRL_AUTO;
2667 	else if (value == 1)
2668 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2669 	else
2670 		return -EINVAL;
2671 
2672 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2673 	if (err < 0) {
2674 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2675 		return err;
2676 	}
2677 
2678 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2679 
2680 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2681 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2682 
2683 	if (err)
2684 		return -EINVAL;
2685 
2686 	return count;
2687 }
2688 
2689 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2690 					struct device_attribute *attr,
2691 					char *buf)
2692 {
2693 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2694 	u32 vddgfx;
2695 	int r, size = sizeof(vddgfx);
2696 
2697 	if (amdgpu_in_reset(adev))
2698 		return -EPERM;
2699 	if (adev->in_suspend && !adev->in_runpm)
2700 		return -EPERM;
2701 
2702 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2703 	if (r < 0) {
2704 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2705 		return r;
2706 	}
2707 
2708 	/* get the voltage */
2709 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2710 				   (void *)&vddgfx, &size);
2711 
2712 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2713 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2714 
2715 	if (r)
2716 		return r;
2717 
2718 	return sysfs_emit(buf, "%d\n", vddgfx);
2719 }
2720 
2721 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2722 					      struct device_attribute *attr,
2723 					      char *buf)
2724 {
2725 	return sysfs_emit(buf, "vddgfx\n");
2726 }
2727 
2728 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2729 				       struct device_attribute *attr,
2730 				       char *buf)
2731 {
2732 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2733 	u32 vddnb;
2734 	int r, size = sizeof(vddnb);
2735 
2736 	if (amdgpu_in_reset(adev))
2737 		return -EPERM;
2738 	if (adev->in_suspend && !adev->in_runpm)
2739 		return -EPERM;
2740 
2741 	/* only APUs have vddnb */
2742 	if  (!(adev->flags & AMD_IS_APU))
2743 		return -EINVAL;
2744 
2745 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2746 	if (r < 0) {
2747 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2748 		return r;
2749 	}
2750 
2751 	/* get the voltage */
2752 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2753 				   (void *)&vddnb, &size);
2754 
2755 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2756 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2757 
2758 	if (r)
2759 		return r;
2760 
2761 	return sysfs_emit(buf, "%d\n", vddnb);
2762 }
2763 
2764 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2765 					      struct device_attribute *attr,
2766 					      char *buf)
2767 {
2768 	return sysfs_emit(buf, "vddnb\n");
2769 }
2770 
2771 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2772 					   struct device_attribute *attr,
2773 					   char *buf)
2774 {
2775 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2776 	u32 query = 0;
2777 	int r, size = sizeof(u32);
2778 	unsigned uw;
2779 
2780 	if (amdgpu_in_reset(adev))
2781 		return -EPERM;
2782 	if (adev->in_suspend && !adev->in_runpm)
2783 		return -EPERM;
2784 
2785 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2786 	if (r < 0) {
2787 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2788 		return r;
2789 	}
2790 
2791 	/* get the voltage */
2792 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2793 				   (void *)&query, &size);
2794 
2795 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2796 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2797 
2798 	if (r)
2799 		return r;
2800 
2801 	/* convert to microwatts */
2802 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2803 
2804 	return sysfs_emit(buf, "%u\n", uw);
2805 }
2806 
2807 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2808 					 struct device_attribute *attr,
2809 					 char *buf)
2810 {
2811 	return sysfs_emit(buf, "%i\n", 0);
2812 }
2813 
2814 
2815 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2816 					struct device_attribute *attr,
2817 					char *buf,
2818 					enum pp_power_limit_level pp_limit_level)
2819 {
2820 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2821 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2822 	uint32_t limit;
2823 	ssize_t size;
2824 	int r;
2825 
2826 	if (amdgpu_in_reset(adev))
2827 		return -EPERM;
2828 	if (adev->in_suspend && !adev->in_runpm)
2829 		return -EPERM;
2830 
2831 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2832 	if (r < 0) {
2833 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2834 		return r;
2835 	}
2836 
2837 	r = amdgpu_dpm_get_power_limit(adev, &limit,
2838 				      pp_limit_level, power_type);
2839 
2840 	if (!r)
2841 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
2842 	else
2843 		size = sysfs_emit(buf, "\n");
2844 
2845 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2846 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2847 
2848 	return size;
2849 }
2850 
2851 
2852 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
2853 					 struct device_attribute *attr,
2854 					 char *buf)
2855 {
2856 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
2857 
2858 }
2859 
2860 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2861 					 struct device_attribute *attr,
2862 					 char *buf)
2863 {
2864 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
2865 
2866 }
2867 
2868 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
2869 					 struct device_attribute *attr,
2870 					 char *buf)
2871 {
2872 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
2873 
2874 }
2875 
2876 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
2877 					 struct device_attribute *attr,
2878 					 char *buf)
2879 {
2880 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2881 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
2882 
2883 	if (gc_ver == IP_VERSION(10, 3, 1))
2884 		return sysfs_emit(buf, "%s\n",
2885 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
2886 				  "fastPPT" : "slowPPT");
2887 	else
2888 		return sysfs_emit(buf, "PPT\n");
2889 }
2890 
2891 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2892 		struct device_attribute *attr,
2893 		const char *buf,
2894 		size_t count)
2895 {
2896 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2897 	int limit_type = to_sensor_dev_attr(attr)->index;
2898 	int err;
2899 	u32 value;
2900 
2901 	if (amdgpu_in_reset(adev))
2902 		return -EPERM;
2903 	if (adev->in_suspend && !adev->in_runpm)
2904 		return -EPERM;
2905 
2906 	if (amdgpu_sriov_vf(adev))
2907 		return -EINVAL;
2908 
2909 	err = kstrtou32(buf, 10, &value);
2910 	if (err)
2911 		return err;
2912 
2913 	value = value / 1000000; /* convert to Watt */
2914 	value |= limit_type << 24;
2915 
2916 	err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2917 	if (err < 0) {
2918 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2919 		return err;
2920 	}
2921 
2922 	err = amdgpu_dpm_set_power_limit(adev, value);
2923 
2924 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2925 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2926 
2927 	if (err)
2928 		return err;
2929 
2930 	return count;
2931 }
2932 
2933 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2934 				      struct device_attribute *attr,
2935 				      char *buf)
2936 {
2937 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2938 	uint32_t sclk;
2939 	int r, size = sizeof(sclk);
2940 
2941 	if (amdgpu_in_reset(adev))
2942 		return -EPERM;
2943 	if (adev->in_suspend && !adev->in_runpm)
2944 		return -EPERM;
2945 
2946 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2947 	if (r < 0) {
2948 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2949 		return r;
2950 	}
2951 
2952 	/* get the sclk */
2953 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2954 				   (void *)&sclk, &size);
2955 
2956 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2957 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2958 
2959 	if (r)
2960 		return r;
2961 
2962 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
2963 }
2964 
2965 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2966 					    struct device_attribute *attr,
2967 					    char *buf)
2968 {
2969 	return sysfs_emit(buf, "sclk\n");
2970 }
2971 
2972 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2973 				      struct device_attribute *attr,
2974 				      char *buf)
2975 {
2976 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2977 	uint32_t mclk;
2978 	int r, size = sizeof(mclk);
2979 
2980 	if (amdgpu_in_reset(adev))
2981 		return -EPERM;
2982 	if (adev->in_suspend && !adev->in_runpm)
2983 		return -EPERM;
2984 
2985 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2986 	if (r < 0) {
2987 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2988 		return r;
2989 	}
2990 
2991 	/* get the sclk */
2992 	r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2993 				   (void *)&mclk, &size);
2994 
2995 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2996 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2997 
2998 	if (r)
2999 		return r;
3000 
3001 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3002 }
3003 
3004 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3005 					    struct device_attribute *attr,
3006 					    char *buf)
3007 {
3008 	return sysfs_emit(buf, "mclk\n");
3009 }
3010 
3011 /**
3012  * DOC: hwmon
3013  *
3014  * The amdgpu driver exposes the following sensor interfaces:
3015  *
3016  * - GPU temperature (via the on-die sensor)
3017  *
3018  * - GPU voltage
3019  *
3020  * - Northbridge voltage (APUs only)
3021  *
3022  * - GPU power
3023  *
3024  * - GPU fan
3025  *
3026  * - GPU gfx/compute engine clock
3027  *
3028  * - GPU memory clock (dGPU only)
3029  *
3030  * hwmon interfaces for GPU temperature:
3031  *
3032  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3033  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3034  *
3035  * - temp[1-3]_label: temperature channel label
3036  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3037  *
3038  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3039  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3040  *
3041  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3042  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3043  *
3044  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3045  *   - these are supported on SOC15 dGPUs only
3046  *
3047  * hwmon interfaces for GPU voltage:
3048  *
3049  * - in0_input: the voltage on the GPU in millivolts
3050  *
3051  * - in1_input: the voltage on the Northbridge in millivolts
3052  *
3053  * hwmon interfaces for GPU power:
3054  *
3055  * - power1_average: average power used by the GPU in microWatts
3056  *
3057  * - power1_cap_min: minimum cap supported in microWatts
3058  *
3059  * - power1_cap_max: maximum cap supported in microWatts
3060  *
3061  * - power1_cap: selected power cap in microWatts
3062  *
3063  * hwmon interfaces for GPU fan:
3064  *
3065  * - pwm1: pulse width modulation fan level (0-255)
3066  *
3067  * - 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)
3068  *
3069  * - pwm1_min: pulse width modulation fan control minimum level (0)
3070  *
3071  * - pwm1_max: pulse width modulation fan control maximum level (255)
3072  *
3073  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3074  *
3075  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3076  *
3077  * - fan1_input: fan speed in RPM
3078  *
3079  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3080  *
3081  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3082  *
3083  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
3084  *       That will get the former one overridden.
3085  *
3086  * hwmon interfaces for GPU clocks:
3087  *
3088  * - freq1_input: the gfx/compute clock in hertz
3089  *
3090  * - freq2_input: the memory clock in hertz
3091  *
3092  * You can use hwmon tools like sensors to view this information on your system.
3093  *
3094  */
3095 
3096 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3097 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3098 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3099 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3100 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3101 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3102 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3103 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3104 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3105 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3106 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3107 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3108 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3109 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3110 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3111 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3112 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3113 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3114 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3115 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3116 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3117 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3118 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3119 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3120 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3121 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3122 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3123 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3124 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3125 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3126 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3127 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3128 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3129 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3130 static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3131 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3132 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3133 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
3134 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3135 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3136 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3137 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3138 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3139 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3140 
3141 static struct attribute *hwmon_attributes[] = {
3142 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3143 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3144 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3145 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3146 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3147 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3148 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3149 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3150 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3151 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3152 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3153 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3154 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3155 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3156 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3157 	&sensor_dev_attr_pwm1.dev_attr.attr,
3158 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3159 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3160 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3161 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3162 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3163 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3164 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3165 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3166 	&sensor_dev_attr_in0_input.dev_attr.attr,
3167 	&sensor_dev_attr_in0_label.dev_attr.attr,
3168 	&sensor_dev_attr_in1_input.dev_attr.attr,
3169 	&sensor_dev_attr_in1_label.dev_attr.attr,
3170 	&sensor_dev_attr_power1_average.dev_attr.attr,
3171 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3172 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3173 	&sensor_dev_attr_power1_cap.dev_attr.attr,
3174 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3175 	&sensor_dev_attr_power1_label.dev_attr.attr,
3176 	&sensor_dev_attr_power2_average.dev_attr.attr,
3177 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3178 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3179 	&sensor_dev_attr_power2_cap.dev_attr.attr,
3180 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3181 	&sensor_dev_attr_power2_label.dev_attr.attr,
3182 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3183 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3184 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3185 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3186 	NULL
3187 };
3188 
3189 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3190 					struct attribute *attr, int index)
3191 {
3192 	struct device *dev = kobj_to_dev(kobj);
3193 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3194 	umode_t effective_mode = attr->mode;
3195 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3196 
3197 	/* under multi-vf mode, the hwmon attributes are all not supported */
3198 	if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3199 		return 0;
3200 
3201 	/* under pp one vf mode manage of hwmon attributes is not supported */
3202 	if (amdgpu_sriov_is_pp_one_vf(adev))
3203 		effective_mode &= ~S_IWUSR;
3204 
3205 	/* Skip fan attributes if fan is not present */
3206 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3207 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3208 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3209 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3210 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3211 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3212 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3213 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3214 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3215 		return 0;
3216 
3217 	/* Skip fan attributes on APU */
3218 	if ((adev->flags & AMD_IS_APU) &&
3219 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3220 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3221 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3222 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3223 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3224 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3225 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3226 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3227 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3228 		return 0;
3229 
3230 	/* Skip crit temp on APU */
3231 	if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) &&
3232 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3233 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3234 		return 0;
3235 
3236 	/* Skip limit attributes if DPM is not enabled */
3237 	if (!adev->pm.dpm_enabled &&
3238 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3239 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3240 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3241 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3242 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3243 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3244 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3245 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3246 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3247 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3248 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3249 		return 0;
3250 
3251 	/* mask fan attributes if we have no bindings for this asic to expose */
3252 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3253 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3254 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3255 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3256 		effective_mode &= ~S_IRUGO;
3257 
3258 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3259 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3260 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3261 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3262 		effective_mode &= ~S_IWUSR;
3263 
3264 	/* not implemented yet for GC 10.3.1 APUs */
3265 	if (((adev->family == AMDGPU_FAMILY_SI) ||
3266 	     ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)))) &&
3267 	    (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3268 	     attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
3269 	     attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
3270 	     attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3271 		return 0;
3272 
3273 	/* not implemented yet for APUs having <= GC 9.3.0 */
3274 	if (((adev->family == AMDGPU_FAMILY_SI) ||
3275 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3276 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3277 		return 0;
3278 
3279 	/* hide max/min values if we can't both query and manage the fan */
3280 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3281 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3282 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3283 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3284 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3285 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3286 		return 0;
3287 
3288 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3289 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3290 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3291 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3292 		return 0;
3293 
3294 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
3295 	     adev->family == AMDGPU_FAMILY_KV) &&	/* not implemented yet */
3296 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3297 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3298 		return 0;
3299 
3300 	/* only APUs have vddnb */
3301 	if (!(adev->flags & AMD_IS_APU) &&
3302 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3303 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3304 		return 0;
3305 
3306 	/* no mclk on APUs */
3307 	if ((adev->flags & AMD_IS_APU) &&
3308 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3309 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3310 		return 0;
3311 
3312 	/* only SOC15 dGPUs support hotspot and mem temperatures */
3313 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3314 	    (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3315 	     attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3316 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3317 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3318 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3319 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3320 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3321 	     attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3322 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3323 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3324 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3325 		return 0;
3326 
3327 	/* only Vangogh has fast PPT limit and power labels */
3328 	if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3329 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3330 	     attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3331 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3332 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
3333 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3334 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3335 		return 0;
3336 
3337 	return effective_mode;
3338 }
3339 
3340 static const struct attribute_group hwmon_attrgroup = {
3341 	.attrs = hwmon_attributes,
3342 	.is_visible = hwmon_attributes_visible,
3343 };
3344 
3345 static const struct attribute_group *hwmon_groups[] = {
3346 	&hwmon_attrgroup,
3347 	NULL
3348 };
3349 
3350 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3351 {
3352 	int ret;
3353 	uint32_t mask = 0;
3354 
3355 	if (adev->pm.sysfs_initialized)
3356 		return 0;
3357 
3358 	if (adev->pm.dpm_enabled == 0)
3359 		return 0;
3360 
3361 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
3362 
3363 	adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3364 								   DRIVER_NAME, adev,
3365 								   hwmon_groups);
3366 	if (IS_ERR(adev->pm.int_hwmon_dev)) {
3367 		ret = PTR_ERR(adev->pm.int_hwmon_dev);
3368 		dev_err(adev->dev,
3369 			"Unable to register hwmon device: %d\n", ret);
3370 		return ret;
3371 	}
3372 
3373 	switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3374 	case SRIOV_VF_MODE_ONE_VF:
3375 		mask = ATTR_FLAG_ONEVF;
3376 		break;
3377 	case SRIOV_VF_MODE_MULTI_VF:
3378 		mask = 0;
3379 		break;
3380 	case SRIOV_VF_MODE_BARE_METAL:
3381 	default:
3382 		mask = ATTR_FLAG_MASK_ALL;
3383 		break;
3384 	}
3385 
3386 	ret = amdgpu_device_attr_create_groups(adev,
3387 					       amdgpu_device_attrs,
3388 					       ARRAY_SIZE(amdgpu_device_attrs),
3389 					       mask,
3390 					       &adev->pm.pm_attr_list);
3391 	if (ret)
3392 		return ret;
3393 
3394 	adev->pm.sysfs_initialized = true;
3395 
3396 	return 0;
3397 }
3398 
3399 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3400 {
3401 	if (adev->pm.dpm_enabled == 0)
3402 		return;
3403 
3404 	if (adev->pm.int_hwmon_dev)
3405 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
3406 
3407 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3408 }
3409 
3410 /*
3411  * Debugfs info
3412  */
3413 #if defined(CONFIG_DEBUG_FS)
3414 
3415 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3416 					   struct amdgpu_device *adev) {
3417 	uint16_t *p_val;
3418 	uint32_t size;
3419 	int i;
3420 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
3421 
3422 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
3423 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
3424 				GFP_KERNEL);
3425 
3426 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3427 					    (void *)p_val, &size)) {
3428 			for (i = 0; i < num_cpu_cores; i++)
3429 				seq_printf(m, "\t%u MHz (CPU%d)\n",
3430 					   *(p_val + i), i);
3431 		}
3432 
3433 		kfree(p_val);
3434 	}
3435 }
3436 
3437 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3438 {
3439 	uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
3440 	uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3441 	uint32_t value;
3442 	uint64_t value64 = 0;
3443 	uint32_t query = 0;
3444 	int size;
3445 
3446 	/* GPU Clocks */
3447 	size = sizeof(value);
3448 	seq_printf(m, "GFX Clocks and Power:\n");
3449 
3450 	amdgpu_debugfs_prints_cpu_info(m, adev);
3451 
3452 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3453 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3454 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3455 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3456 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3457 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3458 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3459 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3460 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3461 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3462 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3463 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
3464 	size = sizeof(uint32_t);
3465 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3466 		seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3467 	size = sizeof(value);
3468 	seq_printf(m, "\n");
3469 
3470 	/* GPU Temp */
3471 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3472 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3473 
3474 	/* GPU Load */
3475 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3476 		seq_printf(m, "GPU Load: %u %%\n", value);
3477 	/* MEM Load */
3478 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3479 		seq_printf(m, "MEM Load: %u %%\n", value);
3480 
3481 	seq_printf(m, "\n");
3482 
3483 	/* SMC feature mask */
3484 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3485 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3486 
3487 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
3488 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
3489 		/* VCN clocks */
3490 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3491 			if (!value) {
3492 				seq_printf(m, "VCN: Disabled\n");
3493 			} else {
3494 				seq_printf(m, "VCN: Enabled\n");
3495 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3496 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3497 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3498 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3499 			}
3500 		}
3501 		seq_printf(m, "\n");
3502 	} else {
3503 		/* UVD clocks */
3504 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3505 			if (!value) {
3506 				seq_printf(m, "UVD: Disabled\n");
3507 			} else {
3508 				seq_printf(m, "UVD: Enabled\n");
3509 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3510 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3511 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3512 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3513 			}
3514 		}
3515 		seq_printf(m, "\n");
3516 
3517 		/* VCE clocks */
3518 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3519 			if (!value) {
3520 				seq_printf(m, "VCE: Disabled\n");
3521 			} else {
3522 				seq_printf(m, "VCE: Enabled\n");
3523 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3524 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3525 			}
3526 		}
3527 	}
3528 
3529 	return 0;
3530 }
3531 
3532 static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
3533 {
3534 	int i;
3535 
3536 	for (i = 0; clocks[i].flag; i++)
3537 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
3538 			   (flags & clocks[i].flag) ? "On" : "Off");
3539 }
3540 
3541 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
3542 {
3543 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
3544 	struct drm_device *dev = adev_to_drm(adev);
3545 	u64 flags = 0;
3546 	int r;
3547 
3548 	if (amdgpu_in_reset(adev))
3549 		return -EPERM;
3550 	if (adev->in_suspend && !adev->in_runpm)
3551 		return -EPERM;
3552 
3553 	r = pm_runtime_get_sync(dev->dev);
3554 	if (r < 0) {
3555 		pm_runtime_put_autosuspend(dev->dev);
3556 		return r;
3557 	}
3558 
3559 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
3560 		r = amdgpu_debugfs_pm_info_pp(m, adev);
3561 		if (r)
3562 			goto out;
3563 	}
3564 
3565 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
3566 
3567 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
3568 	amdgpu_parse_cg_state(m, flags);
3569 	seq_printf(m, "\n");
3570 
3571 out:
3572 	pm_runtime_mark_last_busy(dev->dev);
3573 	pm_runtime_put_autosuspend(dev->dev);
3574 
3575 	return r;
3576 }
3577 
3578 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
3579 
3580 /*
3581  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
3582  *
3583  * Reads debug memory region allocated to PMFW
3584  */
3585 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
3586 					 size_t size, loff_t *pos)
3587 {
3588 	struct amdgpu_device *adev = file_inode(f)->i_private;
3589 	size_t smu_prv_buf_size;
3590 	void *smu_prv_buf;
3591 	int ret = 0;
3592 
3593 	if (amdgpu_in_reset(adev))
3594 		return -EPERM;
3595 	if (adev->in_suspend && !adev->in_runpm)
3596 		return -EPERM;
3597 
3598 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
3599 	if (ret)
3600 		return ret;
3601 
3602 	if (!smu_prv_buf || !smu_prv_buf_size)
3603 		return -EINVAL;
3604 
3605 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
3606 				       smu_prv_buf_size);
3607 }
3608 
3609 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
3610 	.owner = THIS_MODULE,
3611 	.open = simple_open,
3612 	.read = amdgpu_pm_prv_buffer_read,
3613 	.llseek = default_llseek,
3614 };
3615 
3616 #endif
3617 
3618 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3619 {
3620 #if defined(CONFIG_DEBUG_FS)
3621 	struct drm_minor *minor = adev_to_drm(adev)->primary;
3622 	struct dentry *root = minor->debugfs_root;
3623 
3624 	if (!adev->pm.dpm_enabled)
3625 		return;
3626 
3627 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
3628 			    &amdgpu_debugfs_pm_info_fops);
3629 
3630 	if (adev->pm.smu_prv_buffer_size > 0)
3631 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
3632 					 adev,
3633 					 &amdgpu_debugfs_pm_prv_buffer_fops,
3634 					 adev->pm.smu_prv_buffer_size);
3635 
3636 	amdgpu_dpm_stb_debug_fs_init(adev);
3637 #endif
3638 }
3639