xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision ebc053cf0b4c8ed6bff9a0de6b25f819473ba83a)
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 <linux/string_choices.h>
37 #include <asm/processor.h>
38 
39 #define MAX_NUM_OF_FEATURES_PER_SUBSET		8
40 #define MAX_NUM_OF_SUBSETS			8
41 
42 #define DEVICE_ATTR_IS(_name)		(attr_id == device_attr_id__##_name)
43 
44 #define power_2_mwatt(power)	(((power) >> 8) * 1000 + ((power) & 0xff))
45 
46 struct od_attribute {
47 	struct kobj_attribute	attribute;
48 	struct list_head	entry;
49 };
50 
51 struct od_kobj {
52 	struct kobject		kobj;
53 	struct list_head	entry;
54 	struct list_head	attribute;
55 	void			*priv;
56 };
57 
58 struct od_feature_ops {
59 	umode_t (*is_visible)(struct amdgpu_device *adev);
60 	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
61 			char *buf);
62 	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
63 			 const char *buf, size_t count);
64 };
65 
66 struct od_feature_item {
67 	const char		*name;
68 	struct od_feature_ops	ops;
69 };
70 
71 struct od_feature_container {
72 	char				*name;
73 	struct od_feature_ops		ops;
74 	struct od_feature_item		sub_feature[MAX_NUM_OF_FEATURES_PER_SUBSET];
75 };
76 
77 struct od_feature_set {
78 	struct od_feature_container	containers[MAX_NUM_OF_SUBSETS];
79 };
80 
81 static const struct hwmon_temp_label {
82 	enum PP_HWMON_TEMP channel;
83 	const char *label;
84 } temp_label[] = {
85 	{PP_TEMP_EDGE, "edge"},
86 	{PP_TEMP_JUNCTION, "junction"},
87 	{PP_TEMP_MEM, "mem"},
88 };
89 
90 const char * const amdgpu_pp_profile_name[] = {
91 	"BOOTUP_DEFAULT",
92 	"3D_FULL_SCREEN",
93 	"POWER_SAVING",
94 	"VIDEO",
95 	"VR",
96 	"COMPUTE",
97 	"CUSTOM",
98 	"WINDOW_3D",
99 	"CAPPED",
100 	"UNCAPPED",
101 };
102 
103 static int amdgpu_pm_parse_long_params(char *str, long *params,
104 				       uint32_t max_params,
105 				       uint32_t *num_params)
106 {
107 	const char delimiter[] = { ' ', '\n', '\0' };
108 	uint32_t count = 0;
109 	char *sub_str;
110 	int ret;
111 
112 	if (!params || !num_params)
113 		return -EINVAL;
114 
115 	while ((sub_str = strsep(&str, delimiter)) != NULL) {
116 		if (strlen(sub_str) == 0)
117 			continue;
118 		if (count >= max_params)
119 			return -EINVAL;
120 		ret = kstrtol(sub_str, 0, &params[count]);
121 		if (ret)
122 			return -EINVAL;
123 		count++;
124 		if (!str)
125 			break;
126 		while (isspace(*str))
127 			str++;
128 	}
129 	*num_params = count;
130 
131 	return 0;
132 }
133 
134 /**
135  * amdgpu_pm_dev_state_check - Check if device can be accessed.
136  * @adev: Target device.
137  * @runpm: Check runpm status for suspend state checks.
138  *
139  * Checks the state of the @adev for access. Return 0 if the device is
140  * accessible or a negative error code otherwise.
141  */
142 static int amdgpu_pm_dev_state_check(struct amdgpu_device *adev, bool runpm)
143 {
144 	bool runpm_check = runpm ? adev->in_runpm : false;
145 	bool full_init = (adev->init_lvl->level == AMDGPU_INIT_LEVEL_DEFAULT);
146 
147 	if (amdgpu_in_reset(adev) || !full_init)
148 		return -EBUSY;
149 
150 	if (adev->in_suspend && !runpm_check)
151 		return -EBUSY;
152 
153 	return 0;
154 }
155 
156 /**
157  * amdgpu_pm_get_access - Check if device can be accessed, resume if needed.
158  * @adev: Target device.
159  *
160  * Checks the state of the @adev for access. Use runtime pm API to resume if
161  * needed. Return 0 if the device is accessible or a negative error code
162  * otherwise.
163  */
164 static int amdgpu_pm_get_access(struct amdgpu_device *adev)
165 {
166 	int ret;
167 
168 	ret = amdgpu_pm_dev_state_check(adev, true);
169 	if (ret)
170 		return ret;
171 
172 	return pm_runtime_resume_and_get(adev->dev);
173 }
174 
175 /**
176  * amdgpu_pm_get_access_if_active - Check if device is active for access.
177  * @adev: Target device.
178  *
179  * Checks the state of the @adev for access. Use runtime pm API to determine
180  * if device is active. Allow access only if device is active.Return 0 if the
181  * device is accessible or a negative error code otherwise.
182  */
183 static int amdgpu_pm_get_access_if_active(struct amdgpu_device *adev)
184 {
185 	int ret;
186 
187 	/* Ignore runpm status. If device is in suspended state, deny access */
188 	ret = amdgpu_pm_dev_state_check(adev, false);
189 	if (ret)
190 		return ret;
191 
192 	/*
193 	 * Allow only if device is active. If runpm is disabled also, as in
194 	 * kernels without CONFIG_PM, allow access.
195 	 */
196 	ret = pm_runtime_get_if_active(adev->dev);
197 	if (!ret)
198 		return -EPERM;
199 
200 	return 0;
201 }
202 
203 /**
204  * amdgpu_pm_put_access - Put to auto suspend mode after a device access.
205  * @adev: Target device.
206  *
207  * Should be paired with amdgpu_pm_get_access* calls
208  */
209 static inline void amdgpu_pm_put_access(struct amdgpu_device *adev)
210 {
211 	pm_runtime_put_autosuspend(adev->dev);
212 }
213 
214 /**
215  * DOC: power_dpm_state
216  *
217  * The power_dpm_state file is a legacy interface and is only provided for
218  * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
219  * certain power related parameters.  The file power_dpm_state is used for this.
220  * It accepts the following arguments:
221  *
222  * - battery
223  *
224  * - balanced
225  *
226  * - performance
227  *
228  * battery
229  *
230  * On older GPUs, the vbios provided a special power state for battery
231  * operation.  Selecting battery switched to this state.  This is no
232  * longer provided on newer GPUs so the option does nothing in that case.
233  *
234  * balanced
235  *
236  * On older GPUs, the vbios provided a special power state for balanced
237  * operation.  Selecting balanced switched to this state.  This is no
238  * longer provided on newer GPUs so the option does nothing in that case.
239  *
240  * performance
241  *
242  * On older GPUs, the vbios provided a special power state for performance
243  * operation.  Selecting performance switched to this state.  This is no
244  * longer provided on newer GPUs so the option does nothing in that case.
245  *
246  */
247 
248 static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
249 					  struct device_attribute *attr,
250 					  char *buf)
251 {
252 	struct drm_device *ddev = dev_get_drvdata(dev);
253 	struct amdgpu_device *adev = drm_to_adev(ddev);
254 	enum amd_pm_state_type pm;
255 	int ret;
256 
257 	ret = amdgpu_pm_get_access_if_active(adev);
258 	if (ret)
259 		return ret;
260 
261 	amdgpu_dpm_get_current_power_state(adev, &pm);
262 
263 	amdgpu_pm_put_access(adev);
264 
265 	return sysfs_emit(buf, "%s\n",
266 			  (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
267 			  (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
268 }
269 
270 static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
271 					  struct device_attribute *attr,
272 					  const char *buf,
273 					  size_t count)
274 {
275 	struct drm_device *ddev = dev_get_drvdata(dev);
276 	struct amdgpu_device *adev = drm_to_adev(ddev);
277 	enum amd_pm_state_type  state;
278 	int ret;
279 
280 	/* Reject empty/whitespace strings - fuzzing found this is not validated */
281 	if (count == 0 || sysfs_streq(buf, ""))
282 		return -EINVAL;
283 
284 	if (sysfs_streq(buf, "battery"))
285 		state = POWER_STATE_TYPE_BATTERY;
286 	else if (sysfs_streq(buf, "balanced"))
287 		state = POWER_STATE_TYPE_BALANCED;
288 	else if (sysfs_streq(buf, "performance"))
289 		state = POWER_STATE_TYPE_PERFORMANCE;
290 	else
291 		return -EINVAL;
292 
293 	ret = amdgpu_pm_get_access(adev);
294 	if (ret < 0)
295 		return ret;
296 
297 	amdgpu_dpm_set_power_state(adev, state);
298 
299 	amdgpu_pm_put_access(adev);
300 
301 	return count;
302 }
303 
304 
305 /**
306  * DOC: power_dpm_force_performance_level
307  *
308  * The amdgpu driver provides a sysfs API for adjusting certain power
309  * related parameters.  The file power_dpm_force_performance_level is
310  * used for this.  It accepts the following arguments:
311  *
312  * - auto
313  *
314  * - low
315  *
316  * - high
317  *
318  * - manual
319  *
320  * - profile_standard
321  *
322  * - profile_min_sclk
323  *
324  * - profile_min_mclk
325  *
326  * - profile_peak
327  *
328  * auto
329  *
330  * When auto is selected, the driver will attempt to dynamically select
331  * the optimal power profile for current conditions in the driver.
332  *
333  * low
334  *
335  * When low is selected, the clocks are forced to the lowest power state.
336  *
337  * high
338  *
339  * When high is selected, the clocks are forced to the highest power state.
340  *
341  * manual
342  *
343  * When manual is selected, the user can manually adjust which power states
344  * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
345  * and pp_dpm_pcie files and adjust the power state transition heuristics
346  * via the pp_power_profile_mode sysfs file.
347  *
348  * profile_standard
349  * profile_min_sclk
350  * profile_min_mclk
351  * profile_peak
352  *
353  * When the profiling modes are selected, clock and power gating are
354  * disabled and the clocks are set for different profiling cases. This
355  * mode is recommended for profiling specific work loads where you do
356  * not want clock or power gating for clock fluctuation to interfere
357  * with your results. profile_standard sets the clocks to a fixed clock
358  * level which varies from asic to asic.  profile_min_sclk forces the sclk
359  * to the lowest level.  profile_min_mclk forces the mclk to the lowest level.
360  * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
361  *
362  */
363 
364 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
365 							    struct device_attribute *attr,
366 							    char *buf)
367 {
368 	struct drm_device *ddev = dev_get_drvdata(dev);
369 	struct amdgpu_device *adev = drm_to_adev(ddev);
370 	enum amd_dpm_forced_level level = 0xff;
371 	int ret;
372 
373 	ret = amdgpu_pm_get_access_if_active(adev);
374 	if (ret)
375 		return ret;
376 
377 	level = amdgpu_dpm_get_performance_level(adev);
378 
379 	amdgpu_pm_put_access(adev);
380 
381 	return sysfs_emit(buf, "%s\n",
382 			  (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
383 			  (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
384 			  (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
385 			  (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
386 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
387 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
388 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
389 			  (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
390 			  (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
391 			  "unknown");
392 }
393 
394 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
395 							    struct device_attribute *attr,
396 							    const char *buf,
397 							    size_t count)
398 {
399 	struct drm_device *ddev = dev_get_drvdata(dev);
400 	struct amdgpu_device *adev = drm_to_adev(ddev);
401 	enum amd_dpm_forced_level level;
402 	int ret = 0;
403 
404 	/* Reject empty/whitespace strings - fuzzing found this is not validated */
405 	if (count == 0 || sysfs_streq(buf, ""))
406 		return -EINVAL;
407 
408 	if (sysfs_streq(buf, "low"))
409 		level = AMD_DPM_FORCED_LEVEL_LOW;
410 	else if (sysfs_streq(buf, "high"))
411 		level = AMD_DPM_FORCED_LEVEL_HIGH;
412 	else if (sysfs_streq(buf, "auto"))
413 		level = AMD_DPM_FORCED_LEVEL_AUTO;
414 	else if (sysfs_streq(buf, "manual"))
415 		level = AMD_DPM_FORCED_LEVEL_MANUAL;
416 	else if (sysfs_streq(buf, "profile_exit"))
417 		level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
418 	else if (sysfs_streq(buf, "profile_standard"))
419 		level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
420 	else if (sysfs_streq(buf, "profile_min_sclk"))
421 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
422 	else if (sysfs_streq(buf, "profile_min_mclk"))
423 		level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
424 	else if (sysfs_streq(buf, "profile_peak"))
425 		level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
426 	else if (sysfs_streq(buf, "perf_determinism"))
427 		level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
428 	else
429 		return -EINVAL;
430 
431 	ret = amdgpu_pm_get_access(adev);
432 	if (ret < 0)
433 		return ret;
434 
435 	mutex_lock(&adev->pm.stable_pstate_ctx_lock);
436 	if (amdgpu_dpm_force_performance_level(adev, level)) {
437 		amdgpu_pm_put_access(adev);
438 		mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
439 		return -EINVAL;
440 	}
441 	/* override whatever a user ctx may have set */
442 	adev->pm.stable_pstate_ctx = NULL;
443 	mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
444 
445 	amdgpu_pm_put_access(adev);
446 
447 	return count;
448 }
449 
450 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
451 		struct device_attribute *attr,
452 		char *buf)
453 {
454 	struct drm_device *ddev = dev_get_drvdata(dev);
455 	struct amdgpu_device *adev = drm_to_adev(ddev);
456 	struct pp_states_info data;
457 	uint32_t i;
458 	int buf_len, ret;
459 
460 	ret = amdgpu_pm_get_access_if_active(adev);
461 	if (ret)
462 		return ret;
463 
464 	if (amdgpu_dpm_get_pp_num_states(adev, &data))
465 		memset(&data, 0, sizeof(data));
466 
467 	amdgpu_pm_put_access(adev);
468 
469 	buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
470 	for (i = 0; i < data.nums; i++)
471 		buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
472 				(data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
473 				(data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
474 				(data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
475 				(data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
476 
477 	return buf_len;
478 }
479 
480 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
481 		struct device_attribute *attr,
482 		char *buf)
483 {
484 	struct drm_device *ddev = dev_get_drvdata(dev);
485 	struct amdgpu_device *adev = drm_to_adev(ddev);
486 	struct pp_states_info data = {0};
487 	enum amd_pm_state_type pm = 0;
488 	int i = 0, ret = 0;
489 
490 	ret = amdgpu_pm_get_access_if_active(adev);
491 	if (ret)
492 		return ret;
493 
494 	amdgpu_dpm_get_current_power_state(adev, &pm);
495 
496 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
497 
498 	amdgpu_pm_put_access(adev);
499 
500 	if (ret)
501 		return ret;
502 
503 	for (i = 0; i < data.nums; i++) {
504 		if (pm == data.states[i])
505 			break;
506 	}
507 
508 	if (i == data.nums)
509 		i = -EINVAL;
510 
511 	return sysfs_emit(buf, "%d\n", i);
512 }
513 
514 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
515 		struct device_attribute *attr,
516 		char *buf)
517 {
518 	struct drm_device *ddev = dev_get_drvdata(dev);
519 	struct amdgpu_device *adev = drm_to_adev(ddev);
520 
521 	if (adev->pm.pp_force_state_enabled)
522 		return amdgpu_get_pp_cur_state(dev, attr, buf);
523 	else
524 		return sysfs_emit(buf, "\n");
525 }
526 
527 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
528 		struct device_attribute *attr,
529 		const char *buf,
530 		size_t count)
531 {
532 	struct drm_device *ddev = dev_get_drvdata(dev);
533 	struct amdgpu_device *adev = drm_to_adev(ddev);
534 	enum amd_pm_state_type state = 0;
535 	struct pp_states_info data;
536 	unsigned long idx;
537 	int ret;
538 
539 	adev->pm.pp_force_state_enabled = false;
540 
541 	if (strlen(buf) == 1)
542 		return count;
543 
544 	ret = kstrtoul(buf, 0, &idx);
545 	if (ret || idx >= ARRAY_SIZE(data.states))
546 		return -EINVAL;
547 
548 	idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
549 
550 	ret = amdgpu_pm_get_access(adev);
551 	if (ret < 0)
552 		return ret;
553 
554 	ret = amdgpu_dpm_get_pp_num_states(adev, &data);
555 	if (ret)
556 		goto err_out;
557 
558 	state = data.states[idx];
559 
560 	/* only set user selected power states */
561 	if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
562 	    state != POWER_STATE_TYPE_DEFAULT) {
563 		ret = amdgpu_dpm_dispatch_task(adev,
564 				AMD_PP_TASK_ENABLE_USER_STATE, &state);
565 		if (ret)
566 			goto err_out;
567 
568 		adev->pm.pp_force_state_enabled = true;
569 	}
570 
571 	amdgpu_pm_put_access(adev);
572 
573 	return count;
574 
575 err_out:
576 	amdgpu_pm_put_access(adev);
577 
578 	return ret;
579 }
580 
581 /**
582  * DOC: pp_table
583  *
584  * The amdgpu driver provides a sysfs API for uploading new powerplay
585  * tables.  The file pp_table is used for this.  Reading the file
586  * will dump the current power play table.  Writing to the file
587  * will attempt to upload a new powerplay table and re-initialize
588  * powerplay using that new table.
589  *
590  */
591 
592 static ssize_t amdgpu_get_pp_table(struct device *dev,
593 		struct device_attribute *attr,
594 		char *buf)
595 {
596 	struct drm_device *ddev = dev_get_drvdata(dev);
597 	struct amdgpu_device *adev = drm_to_adev(ddev);
598 	char *table = NULL;
599 	int size, ret;
600 
601 	ret = amdgpu_pm_get_access_if_active(adev);
602 	if (ret)
603 		return ret;
604 
605 	size = amdgpu_dpm_get_pp_table(adev, &table);
606 
607 	amdgpu_pm_put_access(adev);
608 
609 	if (size <= 0)
610 		return size;
611 
612 	if (size >= PAGE_SIZE)
613 		size = PAGE_SIZE - 1;
614 
615 	memcpy(buf, table, size);
616 
617 	return size;
618 }
619 
620 static ssize_t amdgpu_set_pp_table(struct device *dev,
621 		struct device_attribute *attr,
622 		const char *buf,
623 		size_t count)
624 {
625 	struct drm_device *ddev = dev_get_drvdata(dev);
626 	struct amdgpu_device *adev = drm_to_adev(ddev);
627 	int ret = 0;
628 
629 	ret = amdgpu_pm_get_access(adev);
630 	if (ret < 0)
631 		return ret;
632 
633 	ret = amdgpu_dpm_set_pp_table(adev, buf, count);
634 
635 	amdgpu_pm_put_access(adev);
636 
637 	if (ret)
638 		return ret;
639 
640 	return count;
641 }
642 
643 /**
644  * DOC: pp_od_clk_voltage
645  *
646  * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
647  * in each power level within a power state.  The pp_od_clk_voltage is used for
648  * this.
649  *
650  * Note that the actual memory controller clock rate are exposed, not
651  * the effective memory clock of the DRAMs. To translate it, use the
652  * following formula:
653  *
654  * Clock conversion (Mhz):
655  *
656  * HBM: effective_memory_clock = memory_controller_clock * 1
657  *
658  * G5: effective_memory_clock = memory_controller_clock * 1
659  *
660  * G6: effective_memory_clock = memory_controller_clock * 2
661  *
662  * DRAM data rate (MT/s):
663  *
664  * HBM: effective_memory_clock * 2 = data_rate
665  *
666  * G5: effective_memory_clock * 4 = data_rate
667  *
668  * G6: effective_memory_clock * 8 = data_rate
669  *
670  * Bandwidth (MB/s):
671  *
672  * data_rate * vram_bit_width / 8 = memory_bandwidth
673  *
674  * Some examples:
675  *
676  * G5 on RX460:
677  *
678  * memory_controller_clock = 1750 Mhz
679  *
680  * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
681  *
682  * data rate = 1750 * 4 = 7000 MT/s
683  *
684  * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
685  *
686  * G6 on RX5700:
687  *
688  * memory_controller_clock = 875 Mhz
689  *
690  * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
691  *
692  * data rate = 1750 * 8 = 14000 MT/s
693  *
694  * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
695  *
696  * < For Vega10 and previous ASICs >
697  *
698  * Reading the file will display:
699  *
700  * - a list of engine clock levels and voltages labeled OD_SCLK
701  *
702  * - a list of memory clock levels and voltages labeled OD_MCLK
703  *
704  * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
705  *
706  * To manually adjust these settings, first select manual using
707  * power_dpm_force_performance_level. Enter a new value for each
708  * level by writing a string that contains "s/m level clock voltage" to
709  * the file.  E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
710  * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
711  * 810 mV.  When you have edited all of the states as needed, write
712  * "c" (commit) to the file to commit your changes.  If you want to reset to the
713  * default power levels, write "r" (reset) to the file to reset them.
714  *
715  *
716  * < For Vega20 and newer ASICs >
717  *
718  * Reading the file will display:
719  *
720  * - minimum and maximum engine clock labeled OD_SCLK
721  *
722  * - minimum(not available for Vega20 and Navi1x) and maximum memory
723  *   clock labeled OD_MCLK
724  *
725  * - minimum and maximum fabric clock labeled OD_FCLK (SMU13)
726  *
727  * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
728  *   They can be used to calibrate the sclk voltage curve. This is
729  *   available for Vega20 and NV1X.
730  *
731  * - voltage offset(in mV) applied on target voltage calculation.
732  *   This is available for Sienna Cichlid, Navy Flounder, Dimgrey
733  *   Cavefish and some later SMU13 ASICs. For these ASICs, the target
734  *   voltage calculation can be illustrated by "voltage = voltage
735  *   calculated from v/f curve + overdrive vddgfx offset"
736  *
737  * - a list of valid ranges for sclk, mclk, voltage curve points
738  *   or voltage offset labeled OD_RANGE
739  *
740  * < For APUs >
741  *
742  * Reading the file will display:
743  *
744  * - minimum and maximum engine clock labeled OD_SCLK
745  *
746  * - a list of valid ranges for sclk labeled OD_RANGE
747  *
748  * < For VanGogh >
749  *
750  * Reading the file will display:
751  *
752  * - minimum and maximum engine clock labeled OD_SCLK
753  * - minimum and maximum core clocks labeled OD_CCLK
754  *
755  * - a list of valid ranges for sclk and cclk labeled OD_RANGE
756  *
757  * To manually adjust these settings:
758  *
759  * - First select manual using power_dpm_force_performance_level
760  *
761  * - For clock frequency setting, enter a new value by writing a
762  *   string that contains "s/m/f index clock" to the file. The index
763  *   should be 0 if to set minimum clock. And 1 if to set maximum
764  *   clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
765  *   "m 1 800" will update maximum mclk to be 800Mhz. "f 1 1600" will
766  *   update maximum fabric clock to be 1600Mhz. For core
767  *   clocks on VanGogh, the string contains "p core index clock".
768  *   E.g., "p 2 0 800" would set the minimum core clock on core
769  *   2 to 800Mhz.
770  *
771  *   For sclk voltage curve supported by Vega20 and NV1X, enter the new
772  *   values by writing a string that contains "vc point clock voltage"
773  *   to the file. The points are indexed by 0, 1 and 2. E.g., "vc 0 300
774  *   600" will update point1 with clock set as 300Mhz and voltage as 600mV.
775  *   "vc 2 1000 1000" will update point3 with clock set as 1000Mhz and
776  *   voltage 1000mV.
777  *
778  *   For voltage offset supported by Sienna Cichlid, Navy Flounder, Dimgrey
779  *   Cavefish and some later SMU13 ASICs, enter the new value by writing a
780  *   string that contains "vo offset". E.g., "vo -10" will update the extra
781  *   voltage offset applied to the whole v/f curve line as -10mv.
782  *
783  * - When you have edited all of the states as needed, write "c" (commit)
784  *   to the file to commit your changes
785  *
786  * - If you want to reset to the default power levels, write "r" (reset)
787  *   to the file to reset them
788  *
789  */
790 
791 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
792 		struct device_attribute *attr,
793 		const char *buf,
794 		size_t count)
795 {
796 	struct drm_device *ddev = dev_get_drvdata(dev);
797 	struct amdgpu_device *adev = drm_to_adev(ddev);
798 	int ret;
799 	uint32_t parameter_size = 0;
800 	long parameter[64];
801 	char buf_cpy[128];
802 	char *tmp_str;
803 	uint32_t type;
804 
805 	if (count > 127 || count == 0)
806 		return -EINVAL;
807 
808 	if (*buf == 's')
809 		type = PP_OD_EDIT_SCLK_VDDC_TABLE;
810 	else if (*buf == 'p')
811 		type = PP_OD_EDIT_CCLK_VDDC_TABLE;
812 	else if (*buf == 'm')
813 		type = PP_OD_EDIT_MCLK_VDDC_TABLE;
814 	else if (*buf == 'f')
815 		type = PP_OD_EDIT_FCLK_TABLE;
816 	else if (*buf == 'r')
817 		type = PP_OD_RESTORE_DEFAULT_TABLE;
818 	else if (*buf == 'c')
819 		type = PP_OD_COMMIT_DPM_TABLE;
820 	else if (!strncmp(buf, "vc", 2))
821 		type = PP_OD_EDIT_VDDC_CURVE;
822 	else if (!strncmp(buf, "vo", 2))
823 		type = PP_OD_EDIT_VDDGFX_OFFSET;
824 	else
825 		return -EINVAL;
826 
827 	memcpy(buf_cpy, buf, count);
828 	buf_cpy[count] = 0;
829 
830 	tmp_str = buf_cpy;
831 
832 	if ((type == PP_OD_EDIT_VDDC_CURVE) ||
833 	     (type == PP_OD_EDIT_VDDGFX_OFFSET))
834 		tmp_str++;
835 	while (isspace(*++tmp_str));
836 
837 	ret = amdgpu_pm_parse_long_params(
838 		tmp_str, parameter, ARRAY_SIZE(parameter), &parameter_size);
839 	if (ret)
840 		return ret;
841 
842 	ret = amdgpu_pm_get_access(adev);
843 	if (ret < 0)
844 		return ret;
845 
846 	if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
847 					      type,
848 					      parameter,
849 					      parameter_size))
850 		goto err_out;
851 
852 	if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
853 					  parameter, parameter_size))
854 		goto err_out;
855 
856 	if (type == PP_OD_COMMIT_DPM_TABLE) {
857 		if (amdgpu_dpm_dispatch_task(adev,
858 					     AMD_PP_TASK_READJUST_POWER_STATE,
859 					     NULL))
860 			goto err_out;
861 	}
862 
863 	amdgpu_pm_put_access(adev);
864 
865 	return count;
866 
867 err_out:
868 	amdgpu_pm_put_access(adev);
869 
870 	return -EINVAL;
871 }
872 
873 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
874 		struct device_attribute *attr,
875 		char *buf)
876 {
877 	struct drm_device *ddev = dev_get_drvdata(dev);
878 	struct amdgpu_device *adev = drm_to_adev(ddev);
879 	int size = 0;
880 	int ret;
881 	enum pp_clock_type od_clocks[] = {
882 		OD_SCLK,
883 		OD_MCLK,
884 		OD_FCLK,
885 		OD_VDDC_CURVE,
886 		OD_RANGE,
887 		OD_VDDGFX_OFFSET,
888 		OD_CCLK,
889 	};
890 	uint clk_index;
891 
892 	ret = amdgpu_pm_get_access_if_active(adev);
893 	if (ret)
894 		return ret;
895 
896 	for (clk_index = 0 ; clk_index < ARRAY_SIZE(od_clocks) ; clk_index++) {
897 		amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
898 		if (unlikely(size >= (PAGE_SIZE - 1)))
899 			break;
900 	}
901 
902 	if (size == 0)
903 		size = sysfs_emit(buf, "\n");
904 
905 	amdgpu_pm_put_access(adev);
906 
907 	return size;
908 }
909 
910 /**
911  * DOC: pp_features
912  *
913  * The amdgpu driver provides a sysfs API for adjusting what powerplay
914  * features to be enabled. The file pp_features is used for this. And
915  * this is only available for Vega10 and later dGPUs.
916  *
917  * Reading back the file will show you the followings:
918  * - Current ppfeature masks
919  * - List of the all supported powerplay features with their naming,
920  *   bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
921  *
922  * To manually enable or disable a specific feature, just set or clear
923  * the corresponding bit from original ppfeature masks and input the
924  * new ppfeature masks.
925  */
926 static ssize_t amdgpu_set_pp_features(struct device *dev,
927 				      struct device_attribute *attr,
928 				      const char *buf,
929 				      size_t count)
930 {
931 	struct drm_device *ddev = dev_get_drvdata(dev);
932 	struct amdgpu_device *adev = drm_to_adev(ddev);
933 	uint64_t featuremask;
934 	int ret;
935 
936 	/* Reject empty/whitespace strings - fuzzing found kstrtou64 accepts "" as 0 */
937 	if (count == 0 || sysfs_streq(buf, ""))
938 		return -EINVAL;
939 
940 	ret = kstrtou64(buf, 0, &featuremask);
941 	if (ret)
942 		return -EINVAL;
943 
944 	ret = amdgpu_pm_get_access(adev);
945 	if (ret < 0)
946 		return ret;
947 
948 	ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
949 
950 	amdgpu_pm_put_access(adev);
951 
952 	if (ret)
953 		return -EINVAL;
954 
955 	return count;
956 }
957 
958 static ssize_t amdgpu_get_pp_features(struct device *dev,
959 				      struct device_attribute *attr,
960 				      char *buf)
961 {
962 	struct drm_device *ddev = dev_get_drvdata(dev);
963 	struct amdgpu_device *adev = drm_to_adev(ddev);
964 	ssize_t size;
965 	int ret;
966 
967 	ret = amdgpu_pm_get_access_if_active(adev);
968 	if (ret)
969 		return ret;
970 
971 	size = amdgpu_dpm_get_ppfeature_status(adev, buf);
972 	if (size <= 0)
973 		size = sysfs_emit(buf, "\n");
974 
975 	amdgpu_pm_put_access(adev);
976 
977 	return size;
978 }
979 
980 /**
981  * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
982  *
983  * The amdgpu driver provides a sysfs API for adjusting what power levels
984  * are enabled for a given power state.  The files pp_dpm_sclk, pp_dpm_mclk,
985  * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
986  * this.
987  *
988  * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
989  * Vega10 and later ASICs.
990  * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
991  *
992  * Reading back the files will show you the available power levels within
993  * the power state and the clock information for those levels. If deep sleep is
994  * applied to a clock, the level will be denoted by a special level 'S:'
995  * E.g., ::
996  *
997  *  S: 19Mhz *
998  *  0: 615Mhz
999  *  1: 800Mhz
1000  *  2: 888Mhz
1001  *  3: 1000Mhz
1002  *
1003  *
1004  * To manually adjust these states, first select manual using
1005  * power_dpm_force_performance_level.
1006  * Secondly, enter a new value for each level by inputing a string that
1007  * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1008  * E.g.,
1009  *
1010  * .. code-block:: bash
1011  *
1012  *	echo "4 5 6" > pp_dpm_sclk
1013  *
1014  * will enable sclk levels 4, 5, and 6.
1015  *
1016  * NOTE: change to the dcefclk max dpm level is not supported now
1017  */
1018 
1019 static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
1020 		enum pp_clock_type type,
1021 		char *buf)
1022 {
1023 	struct drm_device *ddev = dev_get_drvdata(dev);
1024 	struct amdgpu_device *adev = drm_to_adev(ddev);
1025 	int size = 0;
1026 	int ret = 0;
1027 
1028 	ret = amdgpu_pm_get_access_if_active(adev);
1029 	if (ret)
1030 		return ret;
1031 
1032 	ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
1033 	if (ret) {
1034 		size = ret;
1035 		goto out_pm_put;
1036 	}
1037 
1038 	if (size == 0)
1039 		size = sysfs_emit(buf, "\n");
1040 
1041 out_pm_put:
1042 	amdgpu_pm_put_access(adev);
1043 
1044 	return size;
1045 }
1046 
1047 /*
1048  * Worst case: 32 bits individually specified, in octal at 12 characters
1049  * per line (+1 for \n).
1050  */
1051 #define AMDGPU_MASK_BUF_MAX	(32 * 13)
1052 
1053 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1054 {
1055 	int ret;
1056 	unsigned long level;
1057 	char *sub_str = NULL;
1058 	char *tmp;
1059 	char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1060 	const char delimiter[3] = {' ', '\n', '\0'};
1061 	size_t bytes;
1062 
1063 	*mask = 0;
1064 
1065 	/* Reject empty/whitespace strings - fuzzing found this is not validated */
1066 	if (count == 0 || sysfs_streq(buf, ""))
1067 		return -EINVAL;
1068 
1069 	bytes = min(count, sizeof(buf_cpy) - 1);
1070 	memcpy(buf_cpy, buf, bytes);
1071 	buf_cpy[bytes] = '\0';
1072 	tmp = buf_cpy;
1073 	while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1074 		if (strlen(sub_str)) {
1075 			ret = kstrtoul(sub_str, 0, &level);
1076 			if (ret || level > 31)
1077 				return -EINVAL;
1078 			*mask |= 1 << level;
1079 		} else
1080 			break;
1081 	}
1082 
1083 	return 0;
1084 }
1085 
1086 static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
1087 		enum pp_clock_type type,
1088 		const char *buf,
1089 		size_t count)
1090 {
1091 	struct drm_device *ddev = dev_get_drvdata(dev);
1092 	struct amdgpu_device *adev = drm_to_adev(ddev);
1093 	int ret;
1094 	uint32_t mask = 0;
1095 
1096 	ret = amdgpu_read_mask(buf, count, &mask);
1097 	if (ret)
1098 		return ret;
1099 
1100 	ret = amdgpu_pm_get_access(adev);
1101 	if (ret < 0)
1102 		return ret;
1103 
1104 	ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1105 
1106 	amdgpu_pm_put_access(adev);
1107 
1108 	if (ret)
1109 		return -EINVAL;
1110 
1111 	return count;
1112 }
1113 
1114 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
1115 		struct device_attribute *attr,
1116 		char *buf)
1117 {
1118 	return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
1119 }
1120 
1121 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1122 		struct device_attribute *attr,
1123 		const char *buf,
1124 		size_t count)
1125 {
1126 	return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
1127 }
1128 
1129 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1130 		struct device_attribute *attr,
1131 		char *buf)
1132 {
1133 	return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1134 }
1135 
1136 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1137 		struct device_attribute *attr,
1138 		const char *buf,
1139 		size_t count)
1140 {
1141 	return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1142 }
1143 
1144 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1145 		struct device_attribute *attr,
1146 		char *buf)
1147 {
1148 	return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1149 }
1150 
1151 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1152 		struct device_attribute *attr,
1153 		const char *buf,
1154 		size_t count)
1155 {
1156 	return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1157 }
1158 
1159 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1160 		struct device_attribute *attr,
1161 		char *buf)
1162 {
1163 	return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1164 }
1165 
1166 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1167 		struct device_attribute *attr,
1168 		const char *buf,
1169 		size_t count)
1170 {
1171 	return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1172 }
1173 
1174 static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
1175 		struct device_attribute *attr,
1176 		char *buf)
1177 {
1178 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
1179 }
1180 
1181 static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
1182 		struct device_attribute *attr,
1183 		const char *buf,
1184 		size_t count)
1185 {
1186 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
1187 }
1188 
1189 static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
1190 		struct device_attribute *attr,
1191 		char *buf)
1192 {
1193 	return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
1194 }
1195 
1196 static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
1197 		struct device_attribute *attr,
1198 		const char *buf,
1199 		size_t count)
1200 {
1201 	return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
1202 }
1203 
1204 static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
1205 		struct device_attribute *attr,
1206 		char *buf)
1207 {
1208 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
1209 }
1210 
1211 static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
1212 		struct device_attribute *attr,
1213 		const char *buf,
1214 		size_t count)
1215 {
1216 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
1217 }
1218 
1219 static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
1220 		struct device_attribute *attr,
1221 		char *buf)
1222 {
1223 	return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
1224 }
1225 
1226 static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
1227 		struct device_attribute *attr,
1228 		const char *buf,
1229 		size_t count)
1230 {
1231 	return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
1232 }
1233 
1234 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1235 		struct device_attribute *attr,
1236 		char *buf)
1237 {
1238 	return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1239 }
1240 
1241 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1242 		struct device_attribute *attr,
1243 		const char *buf,
1244 		size_t count)
1245 {
1246 	return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1247 }
1248 
1249 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1250 		struct device_attribute *attr,
1251 		char *buf)
1252 {
1253 	return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1254 }
1255 
1256 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1257 		struct device_attribute *attr,
1258 		const char *buf,
1259 		size_t count)
1260 {
1261 	return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1262 }
1263 
1264 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1265 		struct device_attribute *attr,
1266 		char *buf)
1267 {
1268 	struct drm_device *ddev = dev_get_drvdata(dev);
1269 	struct amdgpu_device *adev = drm_to_adev(ddev);
1270 	uint32_t value = 0;
1271 	int ret;
1272 
1273 	ret = amdgpu_pm_get_access_if_active(adev);
1274 	if (ret)
1275 		return ret;
1276 
1277 	value = amdgpu_dpm_get_sclk_od(adev);
1278 
1279 	amdgpu_pm_put_access(adev);
1280 
1281 	return sysfs_emit(buf, "%d\n", value);
1282 }
1283 
1284 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1285 		struct device_attribute *attr,
1286 		const char *buf,
1287 		size_t count)
1288 {
1289 	struct drm_device *ddev = dev_get_drvdata(dev);
1290 	struct amdgpu_device *adev = drm_to_adev(ddev);
1291 	int ret;
1292 	long int value;
1293 
1294 	ret = kstrtol(buf, 0, &value);
1295 
1296 	if (ret)
1297 		return -EINVAL;
1298 
1299 	ret = amdgpu_pm_get_access(adev);
1300 	if (ret < 0)
1301 		return ret;
1302 
1303 	amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1304 
1305 	amdgpu_pm_put_access(adev);
1306 
1307 	return count;
1308 }
1309 
1310 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1311 		struct device_attribute *attr,
1312 		char *buf)
1313 {
1314 	struct drm_device *ddev = dev_get_drvdata(dev);
1315 	struct amdgpu_device *adev = drm_to_adev(ddev);
1316 	uint32_t value = 0;
1317 	int ret;
1318 
1319 	ret = amdgpu_pm_get_access_if_active(adev);
1320 	if (ret)
1321 		return ret;
1322 
1323 	value = amdgpu_dpm_get_mclk_od(adev);
1324 
1325 	amdgpu_pm_put_access(adev);
1326 
1327 	return sysfs_emit(buf, "%d\n", value);
1328 }
1329 
1330 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1331 		struct device_attribute *attr,
1332 		const char *buf,
1333 		size_t count)
1334 {
1335 	struct drm_device *ddev = dev_get_drvdata(dev);
1336 	struct amdgpu_device *adev = drm_to_adev(ddev);
1337 	int ret;
1338 	long int value;
1339 
1340 	ret = kstrtol(buf, 0, &value);
1341 
1342 	if (ret)
1343 		return -EINVAL;
1344 
1345 	ret = amdgpu_pm_get_access(adev);
1346 	if (ret < 0)
1347 		return ret;
1348 
1349 	amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1350 
1351 	amdgpu_pm_put_access(adev);
1352 
1353 	return count;
1354 }
1355 
1356 /**
1357  * DOC: pp_power_profile_mode
1358  *
1359  * The amdgpu driver provides a sysfs API for adjusting the heuristics
1360  * related to switching between power levels in a power state.  The file
1361  * pp_power_profile_mode is used for this.
1362  *
1363  * Reading this file outputs a list of all of the predefined power profiles
1364  * and the relevant heuristics settings for that profile.
1365  *
1366  * To select a profile or create a custom profile, first select manual using
1367  * power_dpm_force_performance_level.  Writing the number of a predefined
1368  * profile to pp_power_profile_mode will enable those heuristics.  To
1369  * create a custom set of heuristics, write a string of numbers to the file
1370  * starting with the number of the custom profile along with a setting
1371  * for each heuristic parameter.  Due to differences across asic families
1372  * the heuristic parameters vary from family to family. Additionally,
1373  * you can apply the custom heuristics to different clock domains.  Each
1374  * clock domain is considered a distinct operation so if you modify the
1375  * gfxclk heuristics and then the memclk heuristics, the all of the
1376  * custom heuristics will be retained until you switch to another profile.
1377  *
1378  */
1379 
1380 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1381 		struct device_attribute *attr,
1382 		char *buf)
1383 {
1384 	struct drm_device *ddev = dev_get_drvdata(dev);
1385 	struct amdgpu_device *adev = drm_to_adev(ddev);
1386 	ssize_t size;
1387 	int ret;
1388 
1389 	ret = amdgpu_pm_get_access_if_active(adev);
1390 	if (ret)
1391 		return ret;
1392 
1393 	size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1394 	if (size <= 0)
1395 		size = sysfs_emit(buf, "\n");
1396 
1397 	amdgpu_pm_put_access(adev);
1398 
1399 	return size;
1400 }
1401 
1402 
1403 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1404 		struct device_attribute *attr,
1405 		const char *buf,
1406 		size_t count)
1407 {
1408 	int ret;
1409 	struct drm_device *ddev = dev_get_drvdata(dev);
1410 	struct amdgpu_device *adev = drm_to_adev(ddev);
1411 	uint32_t parameter_size = 0;
1412 	long parameter[64];
1413 	char buf_cpy[128];
1414 	char tmp[2];
1415 	long int profile_mode = 0;
1416 
1417 	/* Reject empty/whitespace strings - fuzzing found this is not validated */
1418 	if (count == 0 || sysfs_streq(buf, ""))
1419 		return -EINVAL;
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 > sizeof(buf_cpy))
1429 			return -EINVAL;
1430 		while (isspace(*buf))
1431 			buf++;
1432 		strscpy(buf_cpy, buf, sizeof(buf_cpy));
1433 		ret = amdgpu_pm_parse_long_params(buf_cpy, parameter,
1434 						  ARRAY_SIZE(parameter) - 1,
1435 						  &parameter_size);
1436 		if (ret)
1437 			return ret;
1438 	}
1439 	parameter[parameter_size] = profile_mode;
1440 
1441 	ret = amdgpu_pm_get_access(adev);
1442 	if (ret < 0)
1443 		return ret;
1444 
1445 	ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1446 
1447 	amdgpu_pm_put_access(adev);
1448 
1449 	if (!ret)
1450 		return count;
1451 
1452 	return -EINVAL;
1453 }
1454 
1455 static int amdgpu_pm_get_sensor_generic(struct amdgpu_device *adev,
1456 					enum amd_pp_sensors sensor,
1457 					void *query)
1458 {
1459 	int r, size = sizeof(uint32_t);
1460 
1461 	r = amdgpu_pm_get_access_if_active(adev);
1462 	if (r)
1463 		return r;
1464 
1465 	/* get the sensor value */
1466 	r = amdgpu_dpm_read_sensor(adev, sensor, query, &size);
1467 
1468 	amdgpu_pm_put_access(adev);
1469 
1470 	return r;
1471 }
1472 
1473 /**
1474  * DOC: gpu_busy_percent
1475  *
1476  * The amdgpu driver provides a sysfs API for reading how busy the GPU
1477  * is as a percentage.  The file gpu_busy_percent is used for this.
1478  * The SMU firmware computes a percentage of load based on the
1479  * aggregate activity level in the IP cores.
1480  */
1481 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1482 					   struct device_attribute *attr,
1483 					   char *buf)
1484 {
1485 	struct drm_device *ddev = dev_get_drvdata(dev);
1486 	struct amdgpu_device *adev = drm_to_adev(ddev);
1487 	unsigned int value;
1488 	int r;
1489 
1490 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value);
1491 	if (r)
1492 		return r;
1493 
1494 	return sysfs_emit(buf, "%d\n", value);
1495 }
1496 
1497 /**
1498  * DOC: mem_busy_percent
1499  *
1500  * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1501  * is as a percentage.  The file mem_busy_percent is used for this.
1502  * The SMU firmware computes a percentage of load based on the
1503  * aggregate activity level in the IP cores.
1504  */
1505 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1506 					   struct device_attribute *attr,
1507 					   char *buf)
1508 {
1509 	struct drm_device *ddev = dev_get_drvdata(dev);
1510 	struct amdgpu_device *adev = drm_to_adev(ddev);
1511 	unsigned int value;
1512 	int r;
1513 
1514 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value);
1515 	if (r)
1516 		return r;
1517 
1518 	return sysfs_emit(buf, "%d\n", value);
1519 }
1520 
1521 /**
1522  * DOC: vcn_busy_percent
1523  *
1524  * The amdgpu driver provides a sysfs API for reading how busy the VCN
1525  * is as a percentage.  The file vcn_busy_percent is used for this.
1526  * The SMU firmware computes a percentage of load based on the
1527  * aggregate activity level in the IP cores.
1528  */
1529 static ssize_t amdgpu_get_vcn_busy_percent(struct device *dev,
1530 						  struct device_attribute *attr,
1531 						  char *buf)
1532 {
1533 	struct drm_device *ddev = dev_get_drvdata(dev);
1534 	struct amdgpu_device *adev = drm_to_adev(ddev);
1535 	unsigned int value;
1536 	int r;
1537 
1538 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VCN_LOAD, &value);
1539 	if (r)
1540 		return r;
1541 
1542 	return sysfs_emit(buf, "%d\n", value);
1543 }
1544 
1545 /**
1546  * DOC: pcie_bw
1547  *
1548  * The amdgpu driver provides a sysfs API for estimating how much data
1549  * has been received and sent by the GPU in the last second through PCIe.
1550  * The file pcie_bw is used for this.
1551  * The Perf counters count the number of received and sent messages and return
1552  * those values, as well as the maximum payload size of a PCIe packet (mps).
1553  * Note that it is not possible to easily and quickly obtain the size of each
1554  * packet transmitted, so we output the max payload size (mps) to allow for
1555  * quick estimation of the PCIe bandwidth usage
1556  */
1557 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1558 		struct device_attribute *attr,
1559 		char *buf)
1560 {
1561 	struct drm_device *ddev = dev_get_drvdata(dev);
1562 	struct amdgpu_device *adev = drm_to_adev(ddev);
1563 	uint64_t count0 = 0, count1 = 0;
1564 	int ret;
1565 
1566 	if (adev->flags & AMD_IS_APU)
1567 		return -ENODATA;
1568 
1569 	if (!adev->asic_funcs->get_pcie_usage)
1570 		return -ENODATA;
1571 
1572 	ret = amdgpu_pm_get_access_if_active(adev);
1573 	if (ret)
1574 		return ret;
1575 
1576 	amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1577 
1578 	amdgpu_pm_put_access(adev);
1579 
1580 	return sysfs_emit(buf, "%llu %llu %i\n",
1581 			  count0, count1, pcie_get_mps(adev->pdev));
1582 }
1583 
1584 /**
1585  * DOC: unique_id
1586  *
1587  * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1588  * The file unique_id is used for this.
1589  * This will provide a Unique ID that will persist from machine to machine
1590  *
1591  * NOTE: This will only work for GFX9 and newer. This file will be absent
1592  * on unsupported ASICs (GFX8 and older)
1593  */
1594 static ssize_t amdgpu_get_unique_id(struct device *dev,
1595 		struct device_attribute *attr,
1596 		char *buf)
1597 {
1598 	struct drm_device *ddev = dev_get_drvdata(dev);
1599 	struct amdgpu_device *adev = drm_to_adev(ddev);
1600 
1601 	if (adev->unique_id)
1602 		return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1603 
1604 	return 0;
1605 }
1606 
1607 /**
1608  * DOC: thermal_throttling_logging
1609  *
1610  * Thermal throttling pulls down the clock frequency and thus the performance.
1611  * It's an useful mechanism to protect the chip from overheating. Since it
1612  * impacts performance, the user controls whether it is enabled and if so,
1613  * the log frequency.
1614  *
1615  * Reading back the file shows you the status(enabled or disabled) and
1616  * the interval(in seconds) between each thermal logging.
1617  *
1618  * Writing an integer to the file, sets a new logging interval, in seconds.
1619  * The value should be between 1 and 3600. If the value is less than 1,
1620  * thermal logging is disabled. Values greater than 3600 are ignored.
1621  */
1622 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1623 						     struct device_attribute *attr,
1624 						     char *buf)
1625 {
1626 	struct drm_device *ddev = dev_get_drvdata(dev);
1627 	struct amdgpu_device *adev = drm_to_adev(ddev);
1628 
1629 	return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
1630 			  adev_to_drm(adev)->unique,
1631 			  str_enabled_disabled(atomic_read(&adev->throttling_logging_enabled)),
1632 			  adev->throttling_logging_rs.interval / HZ + 1);
1633 }
1634 
1635 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1636 						     struct device_attribute *attr,
1637 						     const char *buf,
1638 						     size_t count)
1639 {
1640 	struct drm_device *ddev = dev_get_drvdata(dev);
1641 	struct amdgpu_device *adev = drm_to_adev(ddev);
1642 	long throttling_logging_interval;
1643 	int ret = 0;
1644 
1645 	ret = kstrtol(buf, 0, &throttling_logging_interval);
1646 	if (ret)
1647 		return ret;
1648 
1649 	/* Reject negative values - only 0 (disable) or 1-3600 (seconds) are valid */
1650 	if (throttling_logging_interval < 0)
1651 		return -EINVAL;
1652 
1653 	if (throttling_logging_interval > 3600)
1654 		return -EINVAL;
1655 
1656 	if (throttling_logging_interval > 0) {
1657 		/*
1658 		 * Reset the ratelimit timer internals.
1659 		 * This can effectively restart the timer.
1660 		 */
1661 		ratelimit_state_reset_interval(&adev->throttling_logging_rs,
1662 					       (throttling_logging_interval - 1) * HZ);
1663 		atomic_set(&adev->throttling_logging_enabled, 1);
1664 	} else {
1665 		atomic_set(&adev->throttling_logging_enabled, 0);
1666 	}
1667 
1668 	return count;
1669 }
1670 
1671 /**
1672  * DOC: apu_thermal_cap
1673  *
1674  * The amdgpu driver provides a sysfs API for retrieving/updating thermal
1675  * limit temperature in millidegrees Celsius
1676  *
1677  * Reading back the file shows you core limit value
1678  *
1679  * Writing an integer to the file, sets a new thermal limit. The value
1680  * should be between 0 and 100. If the value is less than 0 or greater
1681  * than 100, then the write request will be ignored.
1682  */
1683 static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
1684 					 struct device_attribute *attr,
1685 					 char *buf)
1686 {
1687 	int ret, size;
1688 	u32 limit;
1689 	struct drm_device *ddev = dev_get_drvdata(dev);
1690 	struct amdgpu_device *adev = drm_to_adev(ddev);
1691 
1692 	ret = amdgpu_pm_get_access_if_active(adev);
1693 	if (ret)
1694 		return ret;
1695 
1696 	ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
1697 	if (!ret)
1698 		size = sysfs_emit(buf, "%u\n", limit);
1699 	else
1700 		size = sysfs_emit(buf, "failed to get thermal limit\n");
1701 
1702 	amdgpu_pm_put_access(adev);
1703 
1704 	return size;
1705 }
1706 
1707 static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
1708 					 struct device_attribute *attr,
1709 					 const char *buf,
1710 					 size_t count)
1711 {
1712 	int ret;
1713 	u32 value;
1714 	struct drm_device *ddev = dev_get_drvdata(dev);
1715 	struct amdgpu_device *adev = drm_to_adev(ddev);
1716 
1717 	ret = kstrtou32(buf, 10, &value);
1718 	if (ret)
1719 		return ret;
1720 
1721 	if (value > 100) {
1722 		dev_err(dev, "Invalid argument !\n");
1723 		return -EINVAL;
1724 	}
1725 
1726 	ret = amdgpu_pm_get_access(adev);
1727 	if (ret < 0)
1728 		return ret;
1729 
1730 	ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
1731 	if (ret) {
1732 		amdgpu_pm_put_access(adev);
1733 		dev_err(dev, "failed to update thermal limit\n");
1734 		return ret;
1735 	}
1736 
1737 	amdgpu_pm_put_access(adev);
1738 
1739 	return count;
1740 }
1741 
1742 static int amdgpu_pm_metrics_attr_update(struct amdgpu_device *adev,
1743 					 struct amdgpu_device_attr *attr,
1744 					 uint32_t mask,
1745 					 enum amdgpu_device_attr_states *states)
1746 {
1747 	if (amdgpu_dpm_get_pm_metrics(adev, NULL, 0) == -EOPNOTSUPP)
1748 		*states = ATTR_STATE_UNSUPPORTED;
1749 
1750 	return 0;
1751 }
1752 
1753 static ssize_t amdgpu_get_pm_metrics(struct device *dev,
1754 				     struct device_attribute *attr, char *buf)
1755 {
1756 	struct drm_device *ddev = dev_get_drvdata(dev);
1757 	struct amdgpu_device *adev = drm_to_adev(ddev);
1758 	ssize_t size = 0;
1759 	int ret;
1760 
1761 	ret = amdgpu_pm_get_access_if_active(adev);
1762 	if (ret)
1763 		return ret;
1764 
1765 	size = amdgpu_dpm_get_pm_metrics(adev, buf, PAGE_SIZE);
1766 
1767 	amdgpu_pm_put_access(adev);
1768 
1769 	return size;
1770 }
1771 
1772 /**
1773  * DOC: gpu_metrics
1774  *
1775  * The amdgpu driver provides a sysfs API for retrieving current gpu
1776  * metrics data. The file gpu_metrics is used for this. Reading the
1777  * file will dump all the current gpu metrics data.
1778  *
1779  * These data include temperature, frequency, engines utilization,
1780  * power consume, throttler status, fan speed and cpu core statistics(
1781  * available for APU only). That's it will give a snapshot of all sensors
1782  * at the same time.
1783  */
1784 static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1785 				      struct device_attribute *attr,
1786 				      char *buf)
1787 {
1788 	struct drm_device *ddev = dev_get_drvdata(dev);
1789 	struct amdgpu_device *adev = drm_to_adev(ddev);
1790 	void *gpu_metrics;
1791 	ssize_t size = 0;
1792 	int ret;
1793 
1794 	ret = amdgpu_pm_get_access_if_active(adev);
1795 	if (ret)
1796 		return ret;
1797 
1798 	size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1799 	if (size <= 0)
1800 		goto out;
1801 
1802 	if (size >= PAGE_SIZE)
1803 		size = PAGE_SIZE - 1;
1804 
1805 	memcpy(buf, gpu_metrics, size);
1806 
1807 out:
1808 	amdgpu_pm_put_access(adev);
1809 
1810 	return size;
1811 }
1812 
1813 static int amdgpu_show_powershift_percent(struct device *dev,
1814 					char *buf, enum amd_pp_sensors sensor)
1815 {
1816 	struct drm_device *ddev = dev_get_drvdata(dev);
1817 	struct amdgpu_device *adev = drm_to_adev(ddev);
1818 	uint32_t ss_power;
1819 	int r = 0, i;
1820 
1821 	r = amdgpu_pm_get_sensor_generic(adev, sensor, (void *)&ss_power);
1822 	if (r == -EOPNOTSUPP) {
1823 		/* sensor not available on dGPU, try to read from APU */
1824 		adev = NULL;
1825 		mutex_lock(&mgpu_info.mutex);
1826 		for (i = 0; i < mgpu_info.num_gpu; i++) {
1827 			if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1828 				adev = mgpu_info.gpu_ins[i].adev;
1829 				break;
1830 			}
1831 		}
1832 		mutex_unlock(&mgpu_info.mutex);
1833 		if (adev)
1834 			r = amdgpu_pm_get_sensor_generic(adev, sensor, (void *)&ss_power);
1835 	}
1836 
1837 	if (r)
1838 		return r;
1839 
1840 	return sysfs_emit(buf, "%u%%\n", ss_power);
1841 }
1842 
1843 /**
1844  * DOC: smartshift_apu_power
1845  *
1846  * The amdgpu driver provides a sysfs API for reporting APU power
1847  * shift in percentage if platform supports smartshift. Value 0 means that
1848  * there is no powershift and values between [1-100] means that the power
1849  * is shifted to APU, the percentage of boost is with respect to APU power
1850  * limit on the platform.
1851  */
1852 
1853 static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1854 					       char *buf)
1855 {
1856 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE);
1857 }
1858 
1859 /**
1860  * DOC: smartshift_dgpu_power
1861  *
1862  * The amdgpu driver provides a sysfs API for reporting dGPU power
1863  * shift in percentage if platform supports smartshift. Value 0 means that
1864  * there is no powershift and values between [1-100] means that the power is
1865  * shifted to dGPU, the percentage of boost is with respect to dGPU power
1866  * limit on the platform.
1867  */
1868 
1869 static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1870 						char *buf)
1871 {
1872 	return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE);
1873 }
1874 
1875 /**
1876  * DOC: smartshift_bias
1877  *
1878  * The amdgpu driver provides a sysfs API for reporting the
1879  * smartshift(SS2.0) bias level. The value ranges from -100 to 100
1880  * and the default is 0. -100 sets maximum preference to APU
1881  * and 100 sets max perference to dGPU.
1882  */
1883 
1884 static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
1885 					  struct device_attribute *attr,
1886 					  char *buf)
1887 {
1888 	int r = 0;
1889 
1890 	r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
1891 
1892 	return r;
1893 }
1894 
1895 static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
1896 					  struct device_attribute *attr,
1897 					  const char *buf, size_t count)
1898 {
1899 	struct drm_device *ddev = dev_get_drvdata(dev);
1900 	struct amdgpu_device *adev = drm_to_adev(ddev);
1901 	int r;
1902 	int bias = 0;
1903 
1904 	r = kstrtoint(buf, 10, &bias);
1905 	if (r)
1906 		return r;
1907 
1908 	r = amdgpu_pm_get_access(adev);
1909 	if (r < 0)
1910 		return r;
1911 
1912 	if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
1913 		bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
1914 	else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
1915 		bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
1916 
1917 	amdgpu_smartshift_bias = bias;
1918 
1919 	/* TODO: update bias level with SMU message */
1920 
1921 	amdgpu_pm_put_access(adev);
1922 
1923 	return count;
1924 }
1925 
1926 static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1927 				uint32_t mask, enum amdgpu_device_attr_states *states)
1928 {
1929 	if (!amdgpu_device_supports_smart_shift(adev))
1930 		*states = ATTR_STATE_UNSUPPORTED;
1931 
1932 	return 0;
1933 }
1934 
1935 static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1936 			       uint32_t mask, enum amdgpu_device_attr_states *states)
1937 {
1938 	uint32_t ss_power;
1939 
1940 	if (!amdgpu_device_supports_smart_shift(adev))
1941 		*states = ATTR_STATE_UNSUPPORTED;
1942 	else if (amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1943 					      (void *)&ss_power))
1944 		*states = ATTR_STATE_UNSUPPORTED;
1945 	else if (amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1946 					      (void *)&ss_power))
1947 		*states = ATTR_STATE_UNSUPPORTED;
1948 
1949 	return 0;
1950 }
1951 
1952 static int pp_od_clk_voltage_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1953 					 uint32_t mask, enum amdgpu_device_attr_states *states)
1954 {
1955 	*states = ATTR_STATE_SUPPORTED;
1956 
1957 	if (!amdgpu_dpm_is_overdrive_supported(adev)) {
1958 		*states = ATTR_STATE_UNSUPPORTED;
1959 		return 0;
1960 	}
1961 
1962 	/* Enable pp_od_clk_voltage node for gc 9.4.3, 9.4.4, 9.5.0, 12.1.0 SRIOV/BM support */
1963 	if (amdgpu_is_multi_aid(adev)) {
1964 		if (amdgpu_sriov_multi_vf_mode(adev))
1965 			*states = ATTR_STATE_UNSUPPORTED;
1966 		return 0;
1967 	}
1968 
1969 	if (!(attr->flags & mask))
1970 		*states = ATTR_STATE_UNSUPPORTED;
1971 
1972 	return 0;
1973 }
1974 
1975 static int pp_dpm_dcefclk_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1976 				      uint32_t mask, enum amdgpu_device_attr_states *states)
1977 {
1978 	struct device_attribute *dev_attr = &attr->dev_attr;
1979 	uint32_t gc_ver;
1980 
1981 	*states = ATTR_STATE_SUPPORTED;
1982 
1983 	if (!(attr->flags & mask)) {
1984 		*states = ATTR_STATE_UNSUPPORTED;
1985 		return 0;
1986 	}
1987 
1988 	gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
1989 	/* dcefclk node is not available on gfx 11.0.3 sriov */
1990 	if ((gc_ver == IP_VERSION(11, 0, 3) && amdgpu_sriov_is_pp_one_vf(adev)) ||
1991 	    gc_ver < IP_VERSION(9, 0, 0) ||
1992 	    !amdgpu_device_has_display_hardware(adev))
1993 		*states = ATTR_STATE_UNSUPPORTED;
1994 
1995 	/* SMU MP1 does not support dcefclk level setting,
1996 	 * setting should not be allowed from VF if not in one VF mode.
1997 	 */
1998 	if (gc_ver >= IP_VERSION(10, 0, 0) ||
1999 	    (amdgpu_sriov_multi_vf_mode(adev))) {
2000 		dev_attr->attr.mode &= ~S_IWUGO;
2001 		dev_attr->store = NULL;
2002 	}
2003 
2004 	return 0;
2005 }
2006 
2007 static int pp_dpm_clk_default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2008 					  uint32_t mask, enum amdgpu_device_attr_states *states)
2009 {
2010 	struct device_attribute *dev_attr = &attr->dev_attr;
2011 	enum amdgpu_device_attr_id attr_id = attr->attr_id;
2012 	uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
2013 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2014 
2015 	*states = ATTR_STATE_SUPPORTED;
2016 
2017 	if (!(attr->flags & mask)) {
2018 		*states = ATTR_STATE_UNSUPPORTED;
2019 		return 0;
2020 	}
2021 
2022 	if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2023 		if (gc_ver < IP_VERSION(9, 0, 0))
2024 			*states = ATTR_STATE_UNSUPPORTED;
2025 	} else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2026 		if (mp1_ver < IP_VERSION(10, 0, 0))
2027 			*states = ATTR_STATE_UNSUPPORTED;
2028 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
2029 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2030 		      gc_ver == IP_VERSION(10, 3, 3) ||
2031 		      gc_ver == IP_VERSION(10, 3, 6) ||
2032 		      gc_ver == IP_VERSION(10, 3, 7) ||
2033 		      gc_ver == IP_VERSION(10, 3, 0) ||
2034 		      gc_ver == IP_VERSION(10, 1, 2) ||
2035 		      gc_ver == IP_VERSION(11, 0, 0) ||
2036 		      gc_ver == IP_VERSION(11, 0, 1) ||
2037 		      gc_ver == IP_VERSION(11, 0, 4) ||
2038 		      gc_ver == IP_VERSION(11, 5, 0) ||
2039 		      gc_ver == IP_VERSION(11, 0, 2) ||
2040 		      gc_ver == IP_VERSION(11, 0, 3) ||
2041 		      amdgpu_is_multi_aid(adev)))
2042 			*states = ATTR_STATE_UNSUPPORTED;
2043 	} else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
2044 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2045 		       gc_ver == IP_VERSION(10, 3, 0) ||
2046 		       gc_ver == IP_VERSION(11, 0, 2) ||
2047 		       gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2048 			*states = ATTR_STATE_UNSUPPORTED;
2049 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
2050 		if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2051 		      gc_ver == IP_VERSION(10, 3, 3) ||
2052 		      gc_ver == IP_VERSION(10, 3, 6) ||
2053 		      gc_ver == IP_VERSION(10, 3, 7) ||
2054 		      gc_ver == IP_VERSION(10, 3, 0) ||
2055 		      gc_ver == IP_VERSION(10, 1, 2) ||
2056 		      gc_ver == IP_VERSION(11, 0, 0) ||
2057 		      gc_ver == IP_VERSION(11, 0, 1) ||
2058 		      gc_ver == IP_VERSION(11, 0, 4) ||
2059 		      gc_ver == IP_VERSION(11, 5, 0) ||
2060 		      gc_ver == IP_VERSION(11, 0, 2) ||
2061 		      gc_ver == IP_VERSION(11, 0, 3) ||
2062 		      amdgpu_is_multi_aid(adev)))
2063 			*states = ATTR_STATE_UNSUPPORTED;
2064 	} else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
2065 		if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2066 		       gc_ver == IP_VERSION(10, 3, 0) ||
2067 		       gc_ver == IP_VERSION(11, 0, 2) ||
2068 		       gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2069 			*states = ATTR_STATE_UNSUPPORTED;
2070 	} else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
2071 		if (amdgpu_is_multi_aid(adev))
2072 			*states = ATTR_STATE_UNSUPPORTED;
2073 	}
2074 
2075 	switch (gc_ver) {
2076 	case IP_VERSION(9, 4, 1):
2077 		/* Arcturus does not support standalone mclk/socclk/fclk level setting */
2078 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2079 		    DEVICE_ATTR_IS(pp_dpm_socclk) ||
2080 		    DEVICE_ATTR_IS(pp_dpm_fclk)) {
2081 			dev_attr->attr.mode &= ~S_IWUGO;
2082 			dev_attr->store = NULL;
2083 		}
2084 		break;
2085 	case IP_VERSION(9, 4, 2):
2086 		if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2087 		    DEVICE_ATTR_IS(pp_dpm_socclk)) {
2088 			/* Aldebaran mclk/socclk DPM only supports voltage control,
2089 			 * not allow to set dpm level directly */
2090 			dev_attr->attr.mode &= ~S_IWUGO;
2091 			dev_attr->store = NULL;
2092 		} else if (DEVICE_ATTR_IS(pp_dpm_fclk) ||
2093 			   DEVICE_ATTR_IS(pp_dpm_pcie)) {
2094 			/* Aldebaran does not support fclk/pcie dpm */
2095 			*states = ATTR_STATE_UNSUPPORTED;
2096 		}
2097 		break;
2098 	default:
2099 		break;
2100 	}
2101 
2102 	/* setting should not be allowed from VF if not in one VF mode */
2103 	if (amdgpu_sriov_vf(adev) && amdgpu_sriov_is_pp_one_vf(adev)) {
2104 		dev_attr->attr.mode &= ~S_IWUGO;
2105 		dev_attr->store = NULL;
2106 	}
2107 
2108 	return 0;
2109 }
2110 
2111 /**
2112  * DOC: board
2113  *
2114  * Certain SOCs can support various board attributes reporting. This is useful
2115  * for user application to monitor various board reated attributes.
2116  *
2117  * The amdgpu driver provides a sysfs API for reporting board attributes. Presently,
2118  * nine types of attributes are reported. Baseboard temperature and
2119  * gpu board temperature are reported as binary files. Npm status, current node power limit,
2120  * max node power limit, node power, global ppt residency, baseboard_power, baseboard_power_limit
2121  * is reported as ASCII text file.
2122  *
2123  * * .. code-block:: console
2124  *
2125  *      hexdump /sys/bus/pci/devices/.../board/baseboard_temp
2126  *
2127  *      hexdump /sys/bus/pci/devices/.../board/gpuboard_temp
2128  *
2129  *      hexdump /sys/bus/pci/devices/.../board/npm_status
2130  *
2131  *      hexdump /sys/bus/pci/devices/.../board/cur_node_power_limit
2132  *
2133  *      hexdump /sys/bus/pci/devices/.../board/max_node_power_limit
2134  *
2135  *      hexdump /sys/bus/pci/devices/.../board/node_power
2136  *
2137  *      hexdump /sys/bus/pci/devices/.../board/global_ppt_resid
2138  *
2139  *      hexdump /sys/bus/pci/devices/.../board/baseboard_power
2140  *
2141  *      hexdump /sys/bus/pci/devices/.../board/baseboard_power_limit
2142  */
2143 
2144 /**
2145  * DOC: baseboard_temp
2146  *
2147  * The amdgpu driver provides a sysfs API for retrieving current baseboard
2148  * temperature metrics data. The file baseboard_temp is used for this.
2149  * Reading the file will dump all the current baseboard temperature  metrics data.
2150  */
2151 static ssize_t amdgpu_get_baseboard_temp_metrics(struct device *dev,
2152 						 struct device_attribute *attr, char *buf)
2153 {
2154 	struct drm_device *ddev = dev_get_drvdata(dev);
2155 	struct amdgpu_device *adev = drm_to_adev(ddev);
2156 	ssize_t size;
2157 	int ret;
2158 
2159 	ret = amdgpu_pm_get_access_if_active(adev);
2160 	if (ret)
2161 		return ret;
2162 
2163 	size = amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_BASEBOARD, NULL);
2164 	if (size <= 0)
2165 		goto out;
2166 	if (size >= PAGE_SIZE) {
2167 		ret = -ENOSPC;
2168 		goto out;
2169 	}
2170 
2171 	amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_BASEBOARD, buf);
2172 
2173 out:
2174 	amdgpu_pm_put_access(adev);
2175 
2176 	if (ret)
2177 		return ret;
2178 
2179 	return size;
2180 }
2181 
2182 /**
2183  * DOC: gpuboard_temp
2184  *
2185  * The amdgpu driver provides a sysfs API for retrieving current gpuboard
2186  * temperature metrics data. The file gpuboard_temp is used for this.
2187  * Reading the file will dump all the current gpuboard temperature  metrics data.
2188  */
2189 static ssize_t amdgpu_get_gpuboard_temp_metrics(struct device *dev,
2190 						struct device_attribute *attr, char *buf)
2191 {
2192 	struct drm_device *ddev = dev_get_drvdata(dev);
2193 	struct amdgpu_device *adev = drm_to_adev(ddev);
2194 	ssize_t size;
2195 	int ret;
2196 
2197 	ret = amdgpu_pm_get_access_if_active(adev);
2198 	if (ret)
2199 		return ret;
2200 
2201 	size = amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_GPUBOARD, NULL);
2202 	if (size <= 0)
2203 		goto out;
2204 	if (size >= PAGE_SIZE) {
2205 		ret = -ENOSPC;
2206 		goto out;
2207 	}
2208 
2209 	amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_GPUBOARD, buf);
2210 
2211 out:
2212 	amdgpu_pm_put_access(adev);
2213 
2214 	if (ret)
2215 		return ret;
2216 
2217 	return size;
2218 }
2219 
2220 /**
2221  * DOC: cur_node_power_limit
2222  *
2223  * The amdgpu driver provides a sysfs API for retrieving current node power limit.
2224  * The file cur_node_power_limit is used for this.
2225  */
2226 static ssize_t amdgpu_show_cur_node_power_limit(struct device *dev,
2227 						struct device_attribute *attr, char *buf)
2228 {
2229 	struct drm_device *ddev = dev_get_drvdata(dev);
2230 	struct amdgpu_device *adev = drm_to_adev(ddev);
2231 	u32 nplimit;
2232 	int r;
2233 
2234 	/* get the current node power limit */
2235 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_NODEPOWERLIMIT,
2236 					 (void *)&nplimit);
2237 	if (r)
2238 		return r;
2239 
2240 	return sysfs_emit(buf, "%u\n", nplimit);
2241 }
2242 
2243 /**
2244  * DOC: node_power
2245  *
2246  * The amdgpu driver provides a sysfs API for retrieving current node power.
2247  * The file node_power is used for this.
2248  */
2249 static ssize_t amdgpu_show_node_power(struct device *dev,
2250 				      struct device_attribute *attr, char *buf)
2251 {
2252 	struct drm_device *ddev = dev_get_drvdata(dev);
2253 	struct amdgpu_device *adev = drm_to_adev(ddev);
2254 	u32 npower;
2255 	int r;
2256 
2257 	/* get the node power */
2258 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_NODEPOWER,
2259 					 (void *)&npower);
2260 	if (r)
2261 		return r;
2262 
2263 	return sysfs_emit(buf, "%u\n", npower);
2264 }
2265 
2266 /**
2267  * DOC: npm_status
2268  *
2269  * The amdgpu driver provides a sysfs API for retrieving current node power management status.
2270  * The file npm_status is used for this. It shows the status as enabled or disabled based on
2271  * current node power value. If node power is zero, status is disabled else enabled.
2272  */
2273 static ssize_t amdgpu_show_npm_status(struct device *dev,
2274 				      struct device_attribute *attr, char *buf)
2275 {
2276 	struct drm_device *ddev = dev_get_drvdata(dev);
2277 	struct amdgpu_device *adev = drm_to_adev(ddev);
2278 	u32 npower;
2279 	int r;
2280 
2281 	/* get the node power */
2282 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_NODEPOWER,
2283 					 (void *)&npower);
2284 	if (r)
2285 		return r;
2286 
2287 	return sysfs_emit(buf, "%s\n", str_enabled_disabled(npower));
2288 }
2289 
2290 /**
2291  * DOC: global_ppt_resid
2292  *
2293  * The amdgpu driver provides a sysfs API for retrieving global ppt residency.
2294  * The file global_ppt_resid is used for this.
2295  */
2296 static ssize_t amdgpu_show_global_ppt_resid(struct device *dev,
2297 					    struct device_attribute *attr, char *buf)
2298 {
2299 	struct drm_device *ddev = dev_get_drvdata(dev);
2300 	struct amdgpu_device *adev = drm_to_adev(ddev);
2301 	u32 gpptresid;
2302 	int r;
2303 
2304 	/* get the global ppt residency */
2305 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPPTRESIDENCY,
2306 					 (void *)&gpptresid);
2307 	if (r)
2308 		return r;
2309 
2310 	return sysfs_emit(buf, "%u\n", gpptresid);
2311 }
2312 
2313 /**
2314  * DOC: max_node_power_limit
2315  *
2316  * The amdgpu driver provides a sysfs API for retrieving maximum node power limit.
2317  * The file max_node_power_limit is used for this.
2318  */
2319 static ssize_t amdgpu_show_max_node_power_limit(struct device *dev,
2320 						struct device_attribute *attr, char *buf)
2321 {
2322 	struct drm_device *ddev = dev_get_drvdata(dev);
2323 	struct amdgpu_device *adev = drm_to_adev(ddev);
2324 	u32 max_nplimit;
2325 	int r;
2326 
2327 	/* get the max node power limit */
2328 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAXNODEPOWERLIMIT,
2329 					 (void *)&max_nplimit);
2330 	if (r)
2331 		return r;
2332 
2333 	return sysfs_emit(buf, "%u\n", max_nplimit);
2334 }
2335 
2336 /**
2337  * DOC: baseboard_power
2338  *
2339  * The amdgpu driver provides a sysfs API for retrieving current ubb power in watts.
2340  * The file baseboard_power is used for this.
2341  */
2342 static ssize_t amdgpu_show_baseboard_power(struct device *dev,
2343 					   struct device_attribute *attr, char *buf)
2344 {
2345 	struct drm_device *ddev = dev_get_drvdata(dev);
2346 	struct amdgpu_device *adev = drm_to_adev(ddev);
2347 	u32 ubbpower;
2348 	int r;
2349 
2350 	/* get the ubb power */
2351 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_UBB_POWER,
2352 					 (void *)&ubbpower);
2353 	if (r)
2354 		return r;
2355 
2356 	return sysfs_emit(buf, "%u\n", ubbpower);
2357 }
2358 
2359 /**
2360  * DOC: baseboard_power_limit
2361  *
2362  * The amdgpu driver provides a sysfs API for retrieving threshold ubb power in watts.
2363  * The file baseboard_power_limit is used for this.
2364  */
2365 static ssize_t amdgpu_show_baseboard_power_limit(struct device *dev,
2366 						 struct device_attribute *attr, char *buf)
2367 {
2368 	struct drm_device *ddev = dev_get_drvdata(dev);
2369 	struct amdgpu_device *adev = drm_to_adev(ddev);
2370 	u32 ubbpowerlimit;
2371 	int r;
2372 
2373 	/* get the ubb power limit */
2374 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_UBB_POWER_LIMIT,
2375 					 (void *)&ubbpowerlimit);
2376 	if (r)
2377 		return r;
2378 
2379 	return sysfs_emit(buf, "%u\n", ubbpowerlimit);
2380 }
2381 
2382 static DEVICE_ATTR(baseboard_temp, 0444, amdgpu_get_baseboard_temp_metrics, NULL);
2383 static DEVICE_ATTR(gpuboard_temp, 0444, amdgpu_get_gpuboard_temp_metrics, NULL);
2384 static DEVICE_ATTR(cur_node_power_limit, 0444, amdgpu_show_cur_node_power_limit, NULL);
2385 static DEVICE_ATTR(node_power, 0444, amdgpu_show_node_power, NULL);
2386 static DEVICE_ATTR(global_ppt_resid, 0444, amdgpu_show_global_ppt_resid, NULL);
2387 static DEVICE_ATTR(max_node_power_limit, 0444, amdgpu_show_max_node_power_limit, NULL);
2388 static DEVICE_ATTR(npm_status, 0444, amdgpu_show_npm_status, NULL);
2389 static DEVICE_ATTR(baseboard_power, 0444, amdgpu_show_baseboard_power, NULL);
2390 static DEVICE_ATTR(baseboard_power_limit, 0444, amdgpu_show_baseboard_power_limit, NULL);
2391 
2392 static struct attribute *board_attrs[] = {
2393 	&dev_attr_baseboard_temp.attr,
2394 	&dev_attr_gpuboard_temp.attr,
2395 	NULL
2396 };
2397 
2398 static umode_t amdgpu_board_attr_visible(struct kobject *kobj, struct attribute *attr, int n)
2399 {
2400 	struct device *dev = kobj_to_dev(kobj);
2401 	struct drm_device *ddev = dev_get_drvdata(dev);
2402 	struct amdgpu_device *adev = drm_to_adev(ddev);
2403 
2404 	if (attr == &dev_attr_baseboard_temp.attr) {
2405 		if (!amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_BASEBOARD))
2406 			return 0;
2407 	}
2408 
2409 	if (attr == &dev_attr_gpuboard_temp.attr) {
2410 		if (!amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_GPUBOARD))
2411 			return 0;
2412 	}
2413 
2414 	return attr->mode;
2415 }
2416 
2417 const struct attribute_group amdgpu_board_attr_group = {
2418 	.name = "board",
2419 	.attrs = board_attrs,
2420 	.is_visible = amdgpu_board_attr_visible,
2421 };
2422 
2423 /* pm policy attributes */
2424 struct amdgpu_pm_policy_attr {
2425 	struct device_attribute dev_attr;
2426 	enum pp_pm_policy id;
2427 };
2428 
2429 /**
2430  * DOC: pm_policy
2431  *
2432  * Certain SOCs can support different power policies to optimize application
2433  * performance. However, this policy is provided only at SOC level and not at a
2434  * per-process level. This is useful especially when entire SOC is utilized for
2435  * dedicated workload.
2436  *
2437  * The amdgpu driver provides a sysfs API for selecting the policy. Presently,
2438  * only two types of policies are supported through this interface.
2439  *
2440  *  Pstate Policy Selection - This is to select different Pstate profiles which
2441  *  decides clock/throttling preferences.
2442  *
2443  *  XGMI PLPD Policy Selection - When multiple devices are connected over XGMI,
2444  *  this helps to select policy to be applied for per link power down.
2445  *
2446  * The list of available policies and policy levels vary between SOCs. They can
2447  * be viewed under pm_policy node directory. If SOC doesn't support any policy,
2448  * this node won't be available. The different policies supported will be
2449  * available as separate nodes under pm_policy.
2450  *
2451  *	cat /sys/bus/pci/devices/.../pm_policy/<policy_type>
2452  *
2453  * Reading the policy file shows the different levels supported. The level which
2454  * is applied presently is denoted by * (asterisk). E.g.,
2455  *
2456  * .. code-block:: console
2457  *
2458  *	cat /sys/bus/pci/devices/.../pm_policy/soc_pstate
2459  *	0 : soc_pstate_default
2460  *	1 : soc_pstate_0
2461  *	2 : soc_pstate_1*
2462  *	3 : soc_pstate_2
2463  *
2464  *	cat /sys/bus/pci/devices/.../pm_policy/xgmi_plpd
2465  *	0 : plpd_disallow
2466  *	1 : plpd_default
2467  *	2 : plpd_optimized*
2468  *
2469  * To apply a specific policy
2470  *
2471  * "echo  <level> > /sys/bus/pci/devices/.../pm_policy/<policy_type>"
2472  *
2473  * For the levels listed in the example above, to select "plpd_optimized" for
2474  * XGMI and "soc_pstate_2" for soc pstate policy -
2475  *
2476  * .. code-block:: console
2477  *
2478  *	echo "2" > /sys/bus/pci/devices/.../pm_policy/xgmi_plpd
2479  *	echo "3" > /sys/bus/pci/devices/.../pm_policy/soc_pstate
2480  *
2481  */
2482 static ssize_t amdgpu_get_pm_policy_attr(struct device *dev,
2483 					 struct device_attribute *attr,
2484 					 char *buf)
2485 {
2486 	struct drm_device *ddev = dev_get_drvdata(dev);
2487 	struct amdgpu_device *adev = drm_to_adev(ddev);
2488 	struct amdgpu_pm_policy_attr *policy_attr;
2489 
2490 	policy_attr =
2491 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
2492 
2493 	return amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, buf);
2494 }
2495 
2496 static ssize_t amdgpu_set_pm_policy_attr(struct device *dev,
2497 					 struct device_attribute *attr,
2498 					 const char *buf, size_t count)
2499 {
2500 	struct drm_device *ddev = dev_get_drvdata(dev);
2501 	struct amdgpu_device *adev = drm_to_adev(ddev);
2502 	struct amdgpu_pm_policy_attr *policy_attr;
2503 	int ret, num_params = 0;
2504 	char delimiter[] = " \n\t";
2505 	char tmp_buf[128];
2506 	char *tmp, *param;
2507 	long val;
2508 
2509 	count = min(count, sizeof(tmp_buf));
2510 	memcpy(tmp_buf, buf, count);
2511 	tmp_buf[count - 1] = '\0';
2512 	tmp = tmp_buf;
2513 
2514 	tmp = skip_spaces(tmp);
2515 	while ((param = strsep(&tmp, delimiter))) {
2516 		if (!strlen(param)) {
2517 			tmp = skip_spaces(tmp);
2518 			continue;
2519 		}
2520 		ret = kstrtol(param, 0, &val);
2521 		if (ret)
2522 			return -EINVAL;
2523 		num_params++;
2524 		if (num_params > 1)
2525 			return -EINVAL;
2526 	}
2527 
2528 	if (num_params != 1)
2529 		return -EINVAL;
2530 
2531 	policy_attr =
2532 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
2533 
2534 	ret = amdgpu_pm_get_access(adev);
2535 	if (ret < 0)
2536 		return ret;
2537 
2538 	ret = amdgpu_dpm_set_pm_policy(adev, policy_attr->id, val);
2539 
2540 	amdgpu_pm_put_access(adev);
2541 
2542 	if (ret)
2543 		return ret;
2544 
2545 	return count;
2546 }
2547 
2548 #define AMDGPU_PM_POLICY_ATTR(_name, _id)                                  \
2549 	static struct amdgpu_pm_policy_attr pm_policy_attr_##_name = {     \
2550 		.dev_attr = __ATTR(_name, 0644, amdgpu_get_pm_policy_attr, \
2551 				   amdgpu_set_pm_policy_attr),             \
2552 		.id = PP_PM_POLICY_##_id,                                  \
2553 	}
2554 
2555 #define AMDGPU_PM_POLICY_ATTR_VAR(_name) pm_policy_attr_##_name.dev_attr.attr
2556 
2557 AMDGPU_PM_POLICY_ATTR(soc_pstate, SOC_PSTATE);
2558 AMDGPU_PM_POLICY_ATTR(xgmi_plpd, XGMI_PLPD);
2559 
2560 static struct attribute *pm_policy_attrs[] = {
2561 	&AMDGPU_PM_POLICY_ATTR_VAR(soc_pstate),
2562 	&AMDGPU_PM_POLICY_ATTR_VAR(xgmi_plpd),
2563 	NULL
2564 };
2565 
2566 static umode_t amdgpu_pm_policy_attr_visible(struct kobject *kobj,
2567 					     struct attribute *attr, int n)
2568 {
2569 	struct device *dev = kobj_to_dev(kobj);
2570 	struct drm_device *ddev = dev_get_drvdata(dev);
2571 	struct amdgpu_device *adev = drm_to_adev(ddev);
2572 	struct amdgpu_pm_policy_attr *policy_attr;
2573 
2574 	policy_attr =
2575 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr.attr);
2576 
2577 	if (amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, NULL) ==
2578 	    -ENOENT)
2579 		return 0;
2580 
2581 	return attr->mode;
2582 }
2583 
2584 const struct attribute_group amdgpu_pm_policy_attr_group = {
2585 	.name = "pm_policy",
2586 	.attrs = pm_policy_attrs,
2587 	.is_visible = amdgpu_pm_policy_attr_visible,
2588 };
2589 
2590 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2591 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2592 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2593 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2594 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2595 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2596 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC),
2597 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2598 			      .attr_update = pp_dpm_clk_default_attr_update),
2599 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2600 			      .attr_update = pp_dpm_clk_default_attr_update),
2601 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2602 			      .attr_update = pp_dpm_clk_default_attr_update),
2603 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2604 			      .attr_update = pp_dpm_clk_default_attr_update),
2605 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2606 			      .attr_update = pp_dpm_clk_default_attr_update),
2607 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2608 			      .attr_update = pp_dpm_clk_default_attr_update),
2609 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2610 			      .attr_update = pp_dpm_clk_default_attr_update),
2611 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2612 			      .attr_update = pp_dpm_clk_default_attr_update),
2613 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2614 			      .attr_update = pp_dpm_dcefclk_attr_update),
2615 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2616 			      .attr_update = pp_dpm_clk_default_attr_update),
2617 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2618 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2619 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2620 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC,
2621 			      .attr_update = pp_od_clk_voltage_attr_update),
2622 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2623 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2624 	AMDGPU_DEVICE_ATTR_RO(vcn_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2625 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2626 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2627 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2628 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2629 	AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2630 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2631 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
2632 			      .attr_update = ss_power_attr_update),
2633 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
2634 			      .attr_update = ss_power_attr_update),
2635 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
2636 			      .attr_update = ss_bias_attr_update),
2637 	AMDGPU_DEVICE_ATTR_RO(pm_metrics,				ATTR_FLAG_BASIC,
2638 			      .attr_update = amdgpu_pm_metrics_attr_update),
2639 };
2640 
2641 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2642 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2643 {
2644 	struct device_attribute *dev_attr = &attr->dev_attr;
2645 	enum amdgpu_device_attr_id attr_id = attr->attr_id;
2646 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2647 
2648 	if (!(attr->flags & mask)) {
2649 		*states = ATTR_STATE_UNSUPPORTED;
2650 		return 0;
2651 	}
2652 
2653 	if (DEVICE_ATTR_IS(mem_busy_percent)) {
2654 		if ((adev->flags & AMD_IS_APU &&
2655 		     gc_ver != IP_VERSION(9, 4, 3)) ||
2656 		    gc_ver == IP_VERSION(9, 0, 1))
2657 			*states = ATTR_STATE_UNSUPPORTED;
2658 	} else if (DEVICE_ATTR_IS(vcn_busy_percent)) {
2659 		if (!(gc_ver == IP_VERSION(9, 3, 0) ||
2660 		      gc_ver == IP_VERSION(10, 3, 1) ||
2661 		      gc_ver == IP_VERSION(10, 3, 3) ||
2662 		      gc_ver == IP_VERSION(10, 3, 6) ||
2663 		      gc_ver == IP_VERSION(10, 3, 7) ||
2664 		      gc_ver == IP_VERSION(11, 0, 0) ||
2665 		      gc_ver == IP_VERSION(11, 0, 1) ||
2666 		      gc_ver == IP_VERSION(11, 0, 2) ||
2667 		      gc_ver == IP_VERSION(11, 0, 3) ||
2668 		      gc_ver == IP_VERSION(11, 0, 4) ||
2669 		      gc_ver == IP_VERSION(11, 5, 0) ||
2670 		      gc_ver == IP_VERSION(11, 5, 1) ||
2671 		      gc_ver == IP_VERSION(11, 5, 2) ||
2672 		      gc_ver == IP_VERSION(11, 5, 3) ||
2673 		      gc_ver == IP_VERSION(12, 0, 0) ||
2674 		      gc_ver == IP_VERSION(12, 0, 1)))
2675 			*states = ATTR_STATE_UNSUPPORTED;
2676 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2677 		/* PCIe Perf counters won't work on APU nodes */
2678 		if (adev->flags & AMD_IS_APU ||
2679 		    !adev->asic_funcs->get_pcie_usage)
2680 			*states = ATTR_STATE_UNSUPPORTED;
2681 	} else if (DEVICE_ATTR_IS(unique_id)) {
2682 		switch (gc_ver) {
2683 		case IP_VERSION(9, 0, 1):
2684 		case IP_VERSION(9, 4, 0):
2685 		case IP_VERSION(9, 4, 1):
2686 		case IP_VERSION(9, 4, 2):
2687 		case IP_VERSION(9, 4, 3):
2688 		case IP_VERSION(9, 4, 4):
2689 		case IP_VERSION(9, 5, 0):
2690 		case IP_VERSION(10, 3, 0):
2691 		case IP_VERSION(11, 0, 0):
2692 		case IP_VERSION(11, 0, 1):
2693 		case IP_VERSION(11, 0, 2):
2694 		case IP_VERSION(11, 0, 3):
2695 		case IP_VERSION(12, 0, 0):
2696 		case IP_VERSION(12, 0, 1):
2697 		case IP_VERSION(12, 1, 0):
2698 			*states = ATTR_STATE_SUPPORTED;
2699 			break;
2700 		default:
2701 			*states = ATTR_STATE_UNSUPPORTED;
2702 		}
2703 	} else if (DEVICE_ATTR_IS(pp_features)) {
2704 		if ((adev->flags & AMD_IS_APU &&
2705 		     gc_ver != IP_VERSION(9, 4, 3)) ||
2706 		    gc_ver < IP_VERSION(9, 0, 0))
2707 			*states = ATTR_STATE_UNSUPPORTED;
2708 
2709 		if (adev->scpm_enabled) {
2710 			dev_attr->attr.mode &= ~S_IWUGO;
2711 			dev_attr->store = NULL;
2712 		}
2713 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
2714 		if (gc_ver < IP_VERSION(9, 1, 0))
2715 			*states = ATTR_STATE_UNSUPPORTED;
2716 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
2717 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2718 			*states = ATTR_STATE_UNSUPPORTED;
2719 		else if ((gc_ver == IP_VERSION(10, 3, 0) ||
2720 			  gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev))
2721 			*states = ATTR_STATE_UNSUPPORTED;
2722 	} else if (DEVICE_ATTR_IS(pp_mclk_od)) {
2723 		if (amdgpu_dpm_get_mclk_od(adev) == -EOPNOTSUPP)
2724 			*states = ATTR_STATE_UNSUPPORTED;
2725 	} else if (DEVICE_ATTR_IS(pp_sclk_od)) {
2726 		if (amdgpu_dpm_get_sclk_od(adev) == -EOPNOTSUPP)
2727 			*states = ATTR_STATE_UNSUPPORTED;
2728 	} else if (DEVICE_ATTR_IS(apu_thermal_cap)) {
2729 		u32 limit;
2730 
2731 		if (amdgpu_dpm_get_apu_thermal_limit(adev, &limit) ==
2732 		    -EOPNOTSUPP)
2733 			*states = ATTR_STATE_UNSUPPORTED;
2734 	} else if (DEVICE_ATTR_IS(pp_table)) {
2735 		int ret;
2736 		char *tmp = NULL;
2737 
2738 		ret = amdgpu_dpm_get_pp_table(adev, &tmp);
2739 		if (ret == -EOPNOTSUPP || !tmp)
2740 			*states = ATTR_STATE_UNSUPPORTED;
2741 		else
2742 			*states = ATTR_STATE_SUPPORTED;
2743 	}
2744 
2745 	switch (gc_ver) {
2746 	case IP_VERSION(10, 3, 0):
2747 		if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
2748 		    amdgpu_sriov_vf(adev)) {
2749 			dev_attr->attr.mode &= ~0222;
2750 			dev_attr->store = NULL;
2751 		}
2752 		break;
2753 	default:
2754 		break;
2755 	}
2756 
2757 	return 0;
2758 }
2759 
2760 
2761 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2762 				     struct amdgpu_device_attr *attr,
2763 				     uint32_t mask, struct list_head *attr_list)
2764 {
2765 	int ret = 0;
2766 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2767 	struct amdgpu_device_attr_entry *attr_entry;
2768 	struct device_attribute *dev_attr;
2769 	const char *name;
2770 
2771 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2772 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2773 
2774 	if (!attr)
2775 		return -EINVAL;
2776 
2777 	dev_attr = &attr->dev_attr;
2778 	name = dev_attr->attr.name;
2779 
2780 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2781 
2782 	ret = attr_update(adev, attr, mask, &attr_states);
2783 	if (ret) {
2784 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2785 			name, ret);
2786 		return ret;
2787 	}
2788 
2789 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2790 		return 0;
2791 
2792 	ret = device_create_file(adev->dev, dev_attr);
2793 	if (ret) {
2794 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2795 			name, ret);
2796 	}
2797 
2798 	attr_entry = kmalloc_obj(*attr_entry);
2799 	if (!attr_entry)
2800 		return -ENOMEM;
2801 
2802 	attr_entry->attr = attr;
2803 	INIT_LIST_HEAD(&attr_entry->entry);
2804 
2805 	list_add_tail(&attr_entry->entry, attr_list);
2806 
2807 	return ret;
2808 }
2809 
2810 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2811 {
2812 	struct device_attribute *dev_attr = &attr->dev_attr;
2813 
2814 	device_remove_file(adev->dev, dev_attr);
2815 }
2816 
2817 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2818 					     struct list_head *attr_list);
2819 
2820 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2821 					    struct amdgpu_device_attr *attrs,
2822 					    uint32_t counts,
2823 					    uint32_t mask,
2824 					    struct list_head *attr_list)
2825 {
2826 	int ret = 0;
2827 	uint32_t i = 0;
2828 
2829 	for (i = 0; i < counts; i++) {
2830 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2831 		if (ret)
2832 			goto failed;
2833 	}
2834 
2835 	return 0;
2836 
2837 failed:
2838 	amdgpu_device_attr_remove_groups(adev, attr_list);
2839 
2840 	return ret;
2841 }
2842 
2843 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2844 					     struct list_head *attr_list)
2845 {
2846 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2847 
2848 	if (list_empty(attr_list))
2849 		return ;
2850 
2851 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2852 		amdgpu_device_attr_remove(adev, entry->attr);
2853 		list_del(&entry->entry);
2854 		kfree(entry);
2855 	}
2856 }
2857 
2858 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2859 				      struct device_attribute *attr,
2860 				      char *buf)
2861 {
2862 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2863 	int channel = to_sensor_dev_attr(attr)->index;
2864 	int r, temp = 0;
2865 
2866 	if (channel >= PP_TEMP_MAX)
2867 		return -EINVAL;
2868 
2869 	switch (channel) {
2870 	case PP_TEMP_JUNCTION:
2871 		/* get current junction temperature */
2872 		r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2873 						 (void *)&temp);
2874 		break;
2875 	case PP_TEMP_EDGE:
2876 		/* get current edge temperature */
2877 		r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2878 						 (void *)&temp);
2879 		break;
2880 	case PP_TEMP_MEM:
2881 		/* get current memory temperature */
2882 		r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2883 						 (void *)&temp);
2884 		break;
2885 	default:
2886 		r = -EINVAL;
2887 		break;
2888 	}
2889 
2890 	if (r)
2891 		return r;
2892 
2893 	return sysfs_emit(buf, "%d\n", temp);
2894 }
2895 
2896 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2897 					     struct device_attribute *attr,
2898 					     char *buf)
2899 {
2900 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2901 	int hyst = to_sensor_dev_attr(attr)->index;
2902 	int temp;
2903 
2904 	if (hyst)
2905 		temp = adev->pm.dpm.thermal.min_temp;
2906 	else
2907 		temp = adev->pm.dpm.thermal.max_temp;
2908 
2909 	return sysfs_emit(buf, "%d\n", temp);
2910 }
2911 
2912 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2913 					     struct device_attribute *attr,
2914 					     char *buf)
2915 {
2916 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2917 	int hyst = to_sensor_dev_attr(attr)->index;
2918 	int temp;
2919 
2920 	if (hyst)
2921 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2922 	else
2923 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2924 
2925 	return sysfs_emit(buf, "%d\n", temp);
2926 }
2927 
2928 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2929 					     struct device_attribute *attr,
2930 					     char *buf)
2931 {
2932 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2933 	int hyst = to_sensor_dev_attr(attr)->index;
2934 	int temp;
2935 
2936 	if (hyst)
2937 		temp = adev->pm.dpm.thermal.min_mem_temp;
2938 	else
2939 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2940 
2941 	return sysfs_emit(buf, "%d\n", temp);
2942 }
2943 
2944 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2945 					     struct device_attribute *attr,
2946 					     char *buf)
2947 {
2948 	int channel = to_sensor_dev_attr(attr)->index;
2949 
2950 	if (channel >= PP_TEMP_MAX)
2951 		return -EINVAL;
2952 
2953 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2954 }
2955 
2956 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2957 					     struct device_attribute *attr,
2958 					     char *buf)
2959 {
2960 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2961 	int channel = to_sensor_dev_attr(attr)->index;
2962 	int temp = 0;
2963 
2964 	if (channel >= PP_TEMP_MAX)
2965 		return -EINVAL;
2966 
2967 	switch (channel) {
2968 	case PP_TEMP_JUNCTION:
2969 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2970 		break;
2971 	case PP_TEMP_EDGE:
2972 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2973 		break;
2974 	case PP_TEMP_MEM:
2975 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2976 		break;
2977 	}
2978 
2979 	return sysfs_emit(buf, "%d\n", temp);
2980 }
2981 
2982 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2983 					    struct device_attribute *attr,
2984 					    char *buf)
2985 {
2986 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2987 	u32 pwm_mode = 0;
2988 	int ret;
2989 
2990 	ret = amdgpu_pm_get_access_if_active(adev);
2991 	if (ret)
2992 		return ret;
2993 
2994 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2995 
2996 	amdgpu_pm_put_access(adev);
2997 
2998 	if (ret)
2999 		return -EINVAL;
3000 
3001 	return sysfs_emit(buf, "%u\n", pwm_mode);
3002 }
3003 
3004 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
3005 					    struct device_attribute *attr,
3006 					    const char *buf,
3007 					    size_t count)
3008 {
3009 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3010 	int err, ret;
3011 	u32 pwm_mode;
3012 	int value;
3013 
3014 	err = kstrtoint(buf, 10, &value);
3015 	if (err)
3016 		return err;
3017 
3018 	if (value == 0)
3019 		pwm_mode = AMD_FAN_CTRL_NONE;
3020 	else if (value == 1)
3021 		pwm_mode = AMD_FAN_CTRL_MANUAL;
3022 	else if (value == 2)
3023 		pwm_mode = AMD_FAN_CTRL_AUTO;
3024 	else
3025 		return -EINVAL;
3026 
3027 	ret = amdgpu_pm_get_access(adev);
3028 	if (ret < 0)
3029 		return ret;
3030 
3031 	ret = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
3032 
3033 	amdgpu_pm_put_access(adev);
3034 
3035 	if (ret)
3036 		return -EINVAL;
3037 
3038 	return count;
3039 }
3040 
3041 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
3042 					 struct device_attribute *attr,
3043 					 char *buf)
3044 {
3045 	return sysfs_emit(buf, "%i\n", 0);
3046 }
3047 
3048 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
3049 					 struct device_attribute *attr,
3050 					 char *buf)
3051 {
3052 	return sysfs_emit(buf, "%i\n", 255);
3053 }
3054 
3055 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
3056 				     struct device_attribute *attr,
3057 				     const char *buf, size_t count)
3058 {
3059 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3060 	int err;
3061 	u32 value;
3062 	u32 pwm_mode;
3063 
3064 	err = kstrtou32(buf, 10, &value);
3065 	if (err)
3066 		return err;
3067 
3068 	err = amdgpu_pm_get_access(adev);
3069 	if (err < 0)
3070 		return err;
3071 
3072 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
3073 	if (err)
3074 		goto out;
3075 
3076 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
3077 		pr_info("manual fan speed control should be enabled first\n");
3078 		err = -EINVAL;
3079 		goto out;
3080 	}
3081 
3082 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
3083 
3084 out:
3085 	amdgpu_pm_put_access(adev);
3086 
3087 	if (err)
3088 		return err;
3089 
3090 	return count;
3091 }
3092 
3093 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
3094 				     struct device_attribute *attr,
3095 				     char *buf)
3096 {
3097 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3098 	int err;
3099 	u32 speed = 0;
3100 
3101 	err = amdgpu_pm_get_access_if_active(adev);
3102 	if (err)
3103 		return err;
3104 
3105 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
3106 
3107 	amdgpu_pm_put_access(adev);
3108 
3109 	if (err)
3110 		return err;
3111 
3112 	return sysfs_emit(buf, "%i\n", speed);
3113 }
3114 
3115 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
3116 					   struct device_attribute *attr,
3117 					   char *buf)
3118 {
3119 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3120 	int err;
3121 	u32 speed = 0;
3122 
3123 	err = amdgpu_pm_get_access_if_active(adev);
3124 	if (err)
3125 		return err;
3126 
3127 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
3128 
3129 	amdgpu_pm_put_access(adev);
3130 
3131 	if (err)
3132 		return err;
3133 
3134 	return sysfs_emit(buf, "%i\n", speed);
3135 }
3136 
3137 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
3138 					 struct device_attribute *attr,
3139 					 char *buf)
3140 {
3141 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3142 	u32 min_rpm = 0;
3143 	int r;
3144 
3145 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
3146 					 (void *)&min_rpm);
3147 
3148 	if (r)
3149 		return r;
3150 
3151 	return sysfs_emit(buf, "%d\n", min_rpm);
3152 }
3153 
3154 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
3155 					 struct device_attribute *attr,
3156 					 char *buf)
3157 {
3158 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3159 	u32 max_rpm = 0;
3160 	int r;
3161 
3162 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
3163 					 (void *)&max_rpm);
3164 
3165 	if (r)
3166 		return r;
3167 
3168 	return sysfs_emit(buf, "%d\n", max_rpm);
3169 }
3170 
3171 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
3172 					   struct device_attribute *attr,
3173 					   char *buf)
3174 {
3175 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3176 	int err;
3177 	u32 rpm = 0;
3178 
3179 	err = amdgpu_pm_get_access_if_active(adev);
3180 	if (err)
3181 		return err;
3182 
3183 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
3184 
3185 	amdgpu_pm_put_access(adev);
3186 
3187 	if (err)
3188 		return err;
3189 
3190 	return sysfs_emit(buf, "%i\n", rpm);
3191 }
3192 
3193 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
3194 				     struct device_attribute *attr,
3195 				     const char *buf, size_t count)
3196 {
3197 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3198 	int err;
3199 	u32 value;
3200 	u32 pwm_mode;
3201 
3202 	err = kstrtou32(buf, 10, &value);
3203 	if (err)
3204 		return err;
3205 
3206 	err = amdgpu_pm_get_access(adev);
3207 	if (err < 0)
3208 		return err;
3209 
3210 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
3211 	if (err)
3212 		goto out;
3213 
3214 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
3215 		err = -ENODATA;
3216 		goto out;
3217 	}
3218 
3219 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
3220 
3221 out:
3222 	amdgpu_pm_put_access(adev);
3223 
3224 	if (err)
3225 		return err;
3226 
3227 	return count;
3228 }
3229 
3230 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
3231 					    struct device_attribute *attr,
3232 					    char *buf)
3233 {
3234 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3235 	u32 pwm_mode = 0;
3236 	int ret;
3237 
3238 	ret = amdgpu_pm_get_access_if_active(adev);
3239 	if (ret)
3240 		return ret;
3241 
3242 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
3243 
3244 	amdgpu_pm_put_access(adev);
3245 
3246 	if (ret)
3247 		return -EINVAL;
3248 
3249 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
3250 }
3251 
3252 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
3253 					    struct device_attribute *attr,
3254 					    const char *buf,
3255 					    size_t count)
3256 {
3257 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3258 	int err;
3259 	int value;
3260 	u32 pwm_mode;
3261 
3262 	err = kstrtoint(buf, 10, &value);
3263 	if (err)
3264 		return err;
3265 
3266 	if (value == 0)
3267 		pwm_mode = AMD_FAN_CTRL_AUTO;
3268 	else if (value == 1)
3269 		pwm_mode = AMD_FAN_CTRL_MANUAL;
3270 	else
3271 		return -EINVAL;
3272 
3273 	err = amdgpu_pm_get_access(adev);
3274 	if (err < 0)
3275 		return err;
3276 
3277 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
3278 
3279 	amdgpu_pm_put_access(adev);
3280 
3281 	if (err)
3282 		return -EINVAL;
3283 
3284 	return count;
3285 }
3286 
3287 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
3288 					struct device_attribute *attr,
3289 					char *buf)
3290 {
3291 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3292 	u32 vddgfx;
3293 	int r;
3294 
3295 	/* get the voltage */
3296 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
3297 					 (void *)&vddgfx);
3298 	if (r)
3299 		return r;
3300 
3301 	return sysfs_emit(buf, "%d\n", vddgfx);
3302 }
3303 
3304 static ssize_t amdgpu_hwmon_show_vddboard(struct device *dev,
3305 					  struct device_attribute *attr,
3306 					  char *buf)
3307 {
3308 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3309 	u32 vddboard;
3310 	int r;
3311 
3312 	/* get the voltage */
3313 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDBOARD,
3314 					 (void *)&vddboard);
3315 	if (r)
3316 		return r;
3317 
3318 	return sysfs_emit(buf, "%d\n", vddboard);
3319 }
3320 
3321 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
3322 					      struct device_attribute *attr,
3323 					      char *buf)
3324 {
3325 	return sysfs_emit(buf, "vddgfx\n");
3326 }
3327 
3328 static ssize_t amdgpu_hwmon_show_vddboard_label(struct device *dev,
3329 						struct device_attribute *attr,
3330 						char *buf)
3331 {
3332 	return sysfs_emit(buf, "vddboard\n");
3333 }
3334 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
3335 				       struct device_attribute *attr,
3336 				       char *buf)
3337 {
3338 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3339 	u32 vddnb;
3340 	int r;
3341 
3342 	/* only APUs have vddnb */
3343 	if  (!(adev->flags & AMD_IS_APU))
3344 		return -EINVAL;
3345 
3346 	/* get the voltage */
3347 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
3348 					 (void *)&vddnb);
3349 	if (r)
3350 		return r;
3351 
3352 	return sysfs_emit(buf, "%d\n", vddnb);
3353 }
3354 
3355 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
3356 					      struct device_attribute *attr,
3357 					      char *buf)
3358 {
3359 	return sysfs_emit(buf, "vddnb\n");
3360 }
3361 
3362 static int amdgpu_hwmon_get_power(struct device *dev,
3363 				  enum amd_pp_sensors sensor)
3364 {
3365 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3366 	u32 query = 0;
3367 	int r;
3368 
3369 	r = amdgpu_pm_get_sensor_generic(adev, sensor, (void *)&query);
3370 	if (r)
3371 		return r;
3372 
3373 	/* convert to microwatts */
3374 	return power_2_mwatt(query) * 1000;
3375 }
3376 
3377 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
3378 					   struct device_attribute *attr,
3379 					   char *buf)
3380 {
3381 	ssize_t val;
3382 
3383 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
3384 	if (val < 0)
3385 		return val;
3386 
3387 	return sysfs_emit(buf, "%zd\n", val);
3388 }
3389 
3390 static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
3391 					     struct device_attribute *attr,
3392 					     char *buf)
3393 {
3394 	ssize_t val;
3395 
3396 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
3397 	if (val < 0)
3398 		return val;
3399 
3400 	return sysfs_emit(buf, "%zd\n", val);
3401 }
3402 
3403 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
3404 					struct device_attribute *attr,
3405 					char *buf,
3406 					enum pp_power_limit_level pp_limit_level)
3407 {
3408 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3409 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
3410 	uint32_t limit;
3411 	ssize_t size;
3412 	int r;
3413 
3414 	r = amdgpu_pm_get_access_if_active(adev);
3415 	if (r)
3416 		return r;
3417 
3418 	r = amdgpu_dpm_get_power_limit(adev, &limit,
3419 				      pp_limit_level, power_type);
3420 
3421 	if (!r)
3422 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
3423 	else
3424 		size = sysfs_emit(buf, "\n");
3425 
3426 	amdgpu_pm_put_access(adev);
3427 
3428 	return size;
3429 }
3430 
3431 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
3432 					 struct device_attribute *attr,
3433 					 char *buf)
3434 {
3435 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MIN);
3436 }
3437 
3438 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
3439 					 struct device_attribute *attr,
3440 					 char *buf)
3441 {
3442 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
3443 
3444 }
3445 
3446 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
3447 					 struct device_attribute *attr,
3448 					 char *buf)
3449 {
3450 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
3451 
3452 }
3453 
3454 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
3455 					 struct device_attribute *attr,
3456 					 char *buf)
3457 {
3458 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
3459 
3460 }
3461 
3462 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
3463 					 struct device_attribute *attr,
3464 					 char *buf)
3465 {
3466 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3467 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
3468 
3469 	if (gc_ver == IP_VERSION(10, 3, 1))
3470 		return sysfs_emit(buf, "%s\n",
3471 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
3472 				  "fastPPT" : "slowPPT");
3473 	else
3474 		return sysfs_emit(buf, "%s\n",
3475 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
3476 				  "PPT1" : "PPT");
3477 }
3478 
3479 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3480 		struct device_attribute *attr,
3481 		const char *buf,
3482 		size_t count)
3483 {
3484 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3485 	int limit_type = to_sensor_dev_attr(attr)->index;
3486 	int err;
3487 	u32 value;
3488 
3489 	err = kstrtou32(buf, 10, &value);
3490 	if (err)
3491 		return err;
3492 
3493 	value = value / 1000000; /* convert to Watt */
3494 
3495 	err = amdgpu_pm_get_access(adev);
3496 	if (err < 0)
3497 		return err;
3498 
3499 	err = amdgpu_dpm_set_power_limit(adev, limit_type, value);
3500 
3501 	amdgpu_pm_put_access(adev);
3502 
3503 	if (err)
3504 		return err;
3505 
3506 	return count;
3507 }
3508 
3509 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3510 				      struct device_attribute *attr,
3511 				      char *buf)
3512 {
3513 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3514 	uint32_t sclk;
3515 	int r;
3516 
3517 	/* get the sclk */
3518 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3519 					 (void *)&sclk);
3520 	if (r)
3521 		return r;
3522 
3523 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
3524 }
3525 
3526 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3527 					    struct device_attribute *attr,
3528 					    char *buf)
3529 {
3530 	return sysfs_emit(buf, "sclk\n");
3531 }
3532 
3533 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3534 				      struct device_attribute *attr,
3535 				      char *buf)
3536 {
3537 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3538 	uint32_t mclk;
3539 	int r;
3540 
3541 	/* get the sclk */
3542 	r = amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3543 					 (void *)&mclk);
3544 	if (r)
3545 		return r;
3546 
3547 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3548 }
3549 
3550 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3551 					    struct device_attribute *attr,
3552 					    char *buf)
3553 {
3554 	return sysfs_emit(buf, "mclk\n");
3555 }
3556 
3557 /**
3558  * DOC: hwmon
3559  *
3560  * The amdgpu driver exposes the following sensor interfaces:
3561  *
3562  * - GPU temperature (via the on-die sensor)
3563  *
3564  * - GPU voltage
3565  *
3566  * - Northbridge voltage (APUs only)
3567  *
3568  * - GPU power
3569  *
3570  * - GPU fan
3571  *
3572  * - GPU gfx/compute engine clock
3573  *
3574  * - GPU memory clock (dGPU only)
3575  *
3576  * hwmon interfaces for GPU temperature:
3577  *
3578  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3579  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3580  *
3581  * - temp[1-3]_label: temperature channel label
3582  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3583  *
3584  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3585  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3586  *
3587  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3588  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3589  *
3590  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3591  *   - these are supported on SOC15 dGPUs only
3592  *
3593  * hwmon interfaces for GPU voltage:
3594  *
3595  * - in0_input: the voltage on the GPU in millivolts
3596  *
3597  * - in1_input: the voltage on the Northbridge in millivolts
3598  *
3599  * hwmon interfaces for GPU power:
3600  *
3601  * - power1_average: average power used by the SoC in microWatts.  On APUs this includes the CPU.
3602  *
3603  * - power1_input: instantaneous power used by the SoC in microWatts.  On APUs this includes the CPU.
3604  *
3605  * - power1_cap_min: minimum cap supported in microWatts
3606  *
3607  * - power1_cap_max: maximum cap supported in microWatts
3608  *
3609  * - power1_cap: selected power cap in microWatts
3610  *
3611  * hwmon interfaces for GPU fan:
3612  *
3613  * - pwm1: pulse width modulation fan level (0-255)
3614  *
3615  * - 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)
3616  *
3617  * - pwm1_min: pulse width modulation fan control minimum level (0)
3618  *
3619  * - pwm1_max: pulse width modulation fan control maximum level (255)
3620  *
3621  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3622  *
3623  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3624  *
3625  * - fan1_input: fan speed in RPM
3626  *
3627  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3628  *
3629  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3630  *
3631  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
3632  *       That will get the former one overridden.
3633  *
3634  * hwmon interfaces for GPU clocks:
3635  *
3636  * - freq1_input: the gfx/compute clock in hertz
3637  *
3638  * - freq2_input: the memory clock in hertz
3639  *
3640  * You can use hwmon tools like sensors to view this information on your system.
3641  *
3642  */
3643 
3644 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3645 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3646 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3647 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3648 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3649 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3650 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3651 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3652 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3653 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3654 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3655 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3656 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3657 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3658 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3659 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3660 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3661 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3662 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3663 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3664 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3665 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3666 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3667 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3668 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3669 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3670 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3671 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3672 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, amdgpu_hwmon_show_vddboard, NULL, 0);
3673 static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, amdgpu_hwmon_show_vddboard_label, NULL, 0);
3674 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3675 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
3676 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3677 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3678 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3679 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3680 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3681 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3682 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3683 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
3684 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3685 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3686 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3687 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3688 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3689 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3690 
3691 static struct attribute *hwmon_attributes[] = {
3692 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3693 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3694 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3695 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3696 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3697 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3698 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3699 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3700 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3701 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3702 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3703 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3704 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3705 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3706 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3707 	&sensor_dev_attr_pwm1.dev_attr.attr,
3708 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3709 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3710 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3711 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3712 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3713 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3714 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3715 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3716 	&sensor_dev_attr_in0_input.dev_attr.attr,
3717 	&sensor_dev_attr_in0_label.dev_attr.attr,
3718 	&sensor_dev_attr_in1_input.dev_attr.attr,
3719 	&sensor_dev_attr_in1_label.dev_attr.attr,
3720 	&sensor_dev_attr_in2_input.dev_attr.attr,
3721 	&sensor_dev_attr_in2_label.dev_attr.attr,
3722 	&sensor_dev_attr_power1_average.dev_attr.attr,
3723 	&sensor_dev_attr_power1_input.dev_attr.attr,
3724 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3725 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3726 	&sensor_dev_attr_power1_cap.dev_attr.attr,
3727 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3728 	&sensor_dev_attr_power1_label.dev_attr.attr,
3729 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3730 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3731 	&sensor_dev_attr_power2_cap.dev_attr.attr,
3732 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3733 	&sensor_dev_attr_power2_label.dev_attr.attr,
3734 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3735 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3736 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3737 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3738 	NULL
3739 };
3740 
3741 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3742 					struct attribute *attr, int index)
3743 {
3744 	struct device *dev = kobj_to_dev(kobj);
3745 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3746 	umode_t effective_mode = attr->mode;
3747 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
3748 	uint32_t tmp;
3749 
3750 	/* under pp one vf mode manage of hwmon attributes is not supported */
3751 	if (amdgpu_sriov_is_pp_one_vf(adev))
3752 		effective_mode &= ~S_IWUSR;
3753 
3754 	/* Skip fan attributes if fan is not present */
3755 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3756 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3757 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3758 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3759 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3760 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3761 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3762 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3763 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3764 		return 0;
3765 
3766 	/* Skip fan attributes on APU */
3767 	if ((adev->flags & AMD_IS_APU) &&
3768 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3769 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3770 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3771 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3772 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3773 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3774 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3775 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3776 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3777 		return 0;
3778 
3779 	/* Skip crit temp on APU */
3780 	if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
3781 	     amdgpu_is_multi_aid(adev)) &&
3782 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3783 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3784 		return 0;
3785 
3786 	/* Skip limit attributes if DPM is not enabled */
3787 	if (!adev->pm.dpm_enabled &&
3788 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3789 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3790 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3791 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3792 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3793 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3794 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3795 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3796 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3797 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3798 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3799 		return 0;
3800 
3801 	/* mask fan attributes if we have no bindings for this asic to expose */
3802 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3803 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3804 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3805 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3806 		effective_mode &= ~S_IRUGO;
3807 
3808 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3809 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3810 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3811 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3812 		effective_mode &= ~S_IWUSR;
3813 
3814 	/* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
3815 	if (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3816 	    attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
3817 	    attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
3818 	    attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr) {
3819 		if (adev->family == AMDGPU_FAMILY_SI ||
3820 		    ((adev->flags & AMD_IS_APU) && gc_ver != IP_VERSION(10, 3, 1) &&
3821 		     (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4))) ||
3822 		    (amdgpu_sriov_vf(adev) && gc_ver == IP_VERSION(11, 0, 3)))
3823 			return 0;
3824 	}
3825 
3826 	if (attr == &sensor_dev_attr_power1_cap.dev_attr.attr &&
3827 	    amdgpu_virt_cap_is_rw(&adev->virt.virt_caps, AMDGPU_VIRT_CAP_POWER_LIMIT))
3828 		effective_mode |= S_IWUSR;
3829 
3830 	/* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
3831 	if (((adev->family == AMDGPU_FAMILY_SI) ||
3832 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3833 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3834 		return 0;
3835 
3836 	/* not all products support both average and instantaneous */
3837 	if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
3838 	    amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER,
3839 					 (void *)&tmp) == -EOPNOTSUPP)
3840 		return 0;
3841 	if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
3842 	    amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER,
3843 					 (void *)&tmp) == -EOPNOTSUPP)
3844 		return 0;
3845 
3846 	/* hide max/min values if we can't both query and manage the fan */
3847 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3848 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3849 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3850 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3851 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3852 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3853 		return 0;
3854 
3855 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3856 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3857 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3858 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3859 		return 0;
3860 
3861 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
3862 	     adev->family == AMDGPU_FAMILY_KV ||	/* not implemented yet */
3863 	     amdgpu_is_multi_aid(adev)) &&
3864 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3865 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3866 		return 0;
3867 
3868 	/* only APUs other than gc 9,4,3 have vddnb */
3869 	if ((!(adev->flags & AMD_IS_APU) ||
3870 	     amdgpu_is_multi_aid(adev)) &&
3871 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3872 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3873 		return 0;
3874 
3875 	/* only few boards support vddboard */
3876 	if ((attr == &sensor_dev_attr_in2_input.dev_attr.attr ||
3877 	     attr == &sensor_dev_attr_in2_label.dev_attr.attr) &&
3878 	     amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDBOARD,
3879 					  (void *)&tmp) == -EOPNOTSUPP)
3880 		return 0;
3881 
3882 	/* no mclk on APUs other than gc 9,4,3*/
3883 	if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
3884 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3885 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3886 		return 0;
3887 
3888 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3889 	    (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4)) &&
3890 	    (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3891 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3892 	     attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3893 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3894 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
3895 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
3896 		return 0;
3897 
3898 	/* hotspot temperature for gc 9,4,3*/
3899 	if (amdgpu_is_multi_aid(adev)) {
3900 		if (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
3901 		    attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3902 		    attr == &sensor_dev_attr_temp1_label.dev_attr.attr)
3903 			return 0;
3904 
3905 		if (attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3906 		    attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)
3907 			return attr->mode;
3908 	}
3909 
3910 	/* only SOC15 dGPUs support hotspot and mem temperatures */
3911 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3912 	    (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3913 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3914 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3915 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3916 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
3917 		return 0;
3918 
3919 	/* only a few GPUs have fast PPT limit and power labels */
3920 	if ((attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3921 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3922 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
3923 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3924 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr) &&
3925 	     (amdgpu_dpm_get_power_limit(adev, &tmp,
3926 					 PP_PWR_LIMIT_MAX,
3927 					 PP_PWR_TYPE_FAST) == -EOPNOTSUPP))
3928 		return 0;
3929 
3930 	return effective_mode;
3931 }
3932 
3933 static const struct attribute_group hwmon_attrgroup = {
3934 	.attrs = hwmon_attributes,
3935 	.is_visible = hwmon_attributes_visible,
3936 };
3937 
3938 static const struct attribute_group *hwmon_groups[] = {
3939 	&hwmon_attrgroup,
3940 	NULL
3941 };
3942 
3943 static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
3944 				       enum pp_clock_type od_type,
3945 				       char *buf)
3946 {
3947 	int size = 0;
3948 	int ret;
3949 
3950 	ret = amdgpu_pm_get_access_if_active(adev);
3951 	if (ret)
3952 		return ret;
3953 
3954 	ret = amdgpu_dpm_emit_clock_levels(adev, od_type, buf, &size);
3955 	if (ret) {
3956 		size = ret;
3957 		goto out_pm_put;
3958 	}
3959 	if (size == 0)
3960 		size = sysfs_emit(buf, "\n");
3961 
3962 out_pm_put:
3963 	amdgpu_pm_put_access(adev);
3964 
3965 	return size;
3966 }
3967 
3968 static int parse_input_od_command_lines(const char *buf, size_t count,
3969 					u32 *type, long *params,
3970 					uint32_t max_params,
3971 					uint32_t *num_of_params)
3972 {
3973 	uint32_t parameter_size = 0;
3974 	char buf_cpy[128] = {0};
3975 	char *tmp_str;
3976 
3977 	if (count > sizeof(buf_cpy) - 1)
3978 		return -EINVAL;
3979 
3980 	memcpy(buf_cpy, buf, count);
3981 	tmp_str = buf_cpy;
3982 
3983 	/* skip heading spaces */
3984 	while (isspace(*tmp_str))
3985 		tmp_str++;
3986 
3987 	switch (*tmp_str) {
3988 	case 'c':
3989 		*type = PP_OD_COMMIT_DPM_TABLE;
3990 		return 0;
3991 	case 'r':
3992 		params[parameter_size] = *type;
3993 		*num_of_params = 1;
3994 		*type = PP_OD_RESTORE_DEFAULT_TABLE;
3995 		return 0;
3996 	default:
3997 		break;
3998 	}
3999 
4000 	return amdgpu_pm_parse_long_params(tmp_str, params, max_params,
4001 					   num_of_params);
4002 }
4003 
4004 static int
4005 amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
4006 				     enum PP_OD_DPM_TABLE_COMMAND cmd_type,
4007 				     const char *in_buf,
4008 				     size_t count)
4009 {
4010 	uint32_t parameter_size = 0;
4011 	long parameter[64];
4012 	int ret;
4013 
4014 	ret = parse_input_od_command_lines(in_buf, count, &cmd_type, parameter,
4015 					   ARRAY_SIZE(parameter),
4016 					   &parameter_size);
4017 	if (ret)
4018 		return ret;
4019 
4020 	ret = amdgpu_pm_get_access(adev);
4021 	if (ret < 0)
4022 		return ret;
4023 
4024 	ret = amdgpu_dpm_odn_edit_dpm_table(adev,
4025 					    cmd_type,
4026 					    parameter,
4027 					    parameter_size);
4028 	if (ret)
4029 		goto err_out;
4030 
4031 	if (cmd_type == PP_OD_COMMIT_DPM_TABLE) {
4032 		ret = amdgpu_dpm_dispatch_task(adev,
4033 					       AMD_PP_TASK_READJUST_POWER_STATE,
4034 					       NULL);
4035 		if (ret)
4036 			goto err_out;
4037 	}
4038 
4039 	amdgpu_pm_put_access(adev);
4040 
4041 	return count;
4042 
4043 err_out:
4044 	amdgpu_pm_put_access(adev);
4045 
4046 	return ret;
4047 }
4048 
4049 /**
4050  * DOC: fan_curve
4051  *
4052  * The amdgpu driver provides a sysfs API for checking and adjusting the fan
4053  * control curve line.
4054  *
4055  * Reading back the file shows you the current settings(temperature in Celsius
4056  * degree and fan speed in pwm) applied to every anchor point of the curve line
4057  * and their permitted ranges if changable.
4058  *
4059  * Writing a desired string(with the format like "anchor_point_index temperature
4060  * fan_speed_in_pwm") to the file, change the settings for the specific anchor
4061  * point accordingly.
4062  *
4063  * When you have finished the editing, write "c" (commit) to the file to commit
4064  * your changes.
4065  *
4066  * If you want to reset to the default value, write "r" (reset) to the file to
4067  * reset them
4068  *
4069  * There are two fan control modes supported: auto and manual. With auto mode,
4070  * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature).
4071  * While with manual mode, users can set their own fan curve line as what
4072  * described here. Normally the ASIC is booted up with auto mode. Any
4073  * settings via this interface will switch the fan control to manual mode
4074  * implicitly.
4075  */
4076 static ssize_t fan_curve_show(struct kobject *kobj,
4077 			      struct kobj_attribute *attr,
4078 			      char *buf)
4079 {
4080 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4081 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4082 
4083 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf);
4084 }
4085 
4086 static ssize_t fan_curve_store(struct kobject *kobj,
4087 			       struct kobj_attribute *attr,
4088 			       const char *buf,
4089 			       size_t count)
4090 {
4091 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4092 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4093 
4094 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4095 							     PP_OD_EDIT_FAN_CURVE,
4096 							     buf,
4097 							     count);
4098 }
4099 
4100 static umode_t fan_curve_visible(struct amdgpu_device *adev)
4101 {
4102 	umode_t umode = 0000;
4103 
4104 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE)
4105 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4106 
4107 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET)
4108 		umode |= S_IWUSR;
4109 
4110 	return umode;
4111 }
4112 
4113 /**
4114  * DOC: acoustic_limit_rpm_threshold
4115  *
4116  * The amdgpu driver provides a sysfs API for checking and adjusting the
4117  * acoustic limit in RPM for fan control.
4118  *
4119  * Reading back the file shows you the current setting and the permitted
4120  * ranges if changable.
4121  *
4122  * Writing an integer to the file, change the setting accordingly.
4123  *
4124  * When you have finished the editing, write "c" (commit) to the file to commit
4125  * your changes.
4126  *
4127  * If you want to reset to the default value, write "r" (reset) to the file to
4128  * reset them
4129  *
4130  * This setting works under auto fan control mode only. It adjusts the PMFW's
4131  * behavior about the maximum speed in RPM the fan can spin. Setting via this
4132  * interface will switch the fan control to auto mode implicitly.
4133  */
4134 static ssize_t acoustic_limit_threshold_show(struct kobject *kobj,
4135 					     struct kobj_attribute *attr,
4136 					     char *buf)
4137 {
4138 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4139 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4140 
4141 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf);
4142 }
4143 
4144 static ssize_t acoustic_limit_threshold_store(struct kobject *kobj,
4145 					      struct kobj_attribute *attr,
4146 					      const char *buf,
4147 					      size_t count)
4148 {
4149 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4150 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4151 
4152 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4153 							     PP_OD_EDIT_ACOUSTIC_LIMIT,
4154 							     buf,
4155 							     count);
4156 }
4157 
4158 static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev)
4159 {
4160 	umode_t umode = 0000;
4161 
4162 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE)
4163 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4164 
4165 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET)
4166 		umode |= S_IWUSR;
4167 
4168 	return umode;
4169 }
4170 
4171 /**
4172  * DOC: acoustic_target_rpm_threshold
4173  *
4174  * The amdgpu driver provides a sysfs API for checking and adjusting the
4175  * acoustic target in RPM for fan control.
4176  *
4177  * Reading back the file shows you the current setting and the permitted
4178  * ranges if changable.
4179  *
4180  * Writing an integer to the file, change the setting accordingly.
4181  *
4182  * When you have finished the editing, write "c" (commit) to the file to commit
4183  * your changes.
4184  *
4185  * If you want to reset to the default value, write "r" (reset) to the file to
4186  * reset them
4187  *
4188  * This setting works under auto fan control mode only. It can co-exist with
4189  * other settings which can work also under auto mode. It adjusts the PMFW's
4190  * behavior about the maximum speed in RPM the fan can spin when ASIC
4191  * temperature is not greater than target temperature. Setting via this
4192  * interface will switch the fan control to auto mode implicitly.
4193  */
4194 static ssize_t acoustic_target_threshold_show(struct kobject *kobj,
4195 					      struct kobj_attribute *attr,
4196 					      char *buf)
4197 {
4198 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4199 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4200 
4201 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf);
4202 }
4203 
4204 static ssize_t acoustic_target_threshold_store(struct kobject *kobj,
4205 					       struct kobj_attribute *attr,
4206 					       const char *buf,
4207 					       size_t count)
4208 {
4209 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4210 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4211 
4212 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4213 							     PP_OD_EDIT_ACOUSTIC_TARGET,
4214 							     buf,
4215 							     count);
4216 }
4217 
4218 static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev)
4219 {
4220 	umode_t umode = 0000;
4221 
4222 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE)
4223 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4224 
4225 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET)
4226 		umode |= S_IWUSR;
4227 
4228 	return umode;
4229 }
4230 
4231 /**
4232  * DOC: fan_target_temperature
4233  *
4234  * The amdgpu driver provides a sysfs API for checking and adjusting the
4235  * target tempeature in Celsius degree for fan control.
4236  *
4237  * Reading back the file shows you the current setting and the permitted
4238  * ranges if changable.
4239  *
4240  * Writing an integer to the file, change the setting accordingly.
4241  *
4242  * When you have finished the editing, write "c" (commit) to the file to commit
4243  * your changes.
4244  *
4245  * If you want to reset to the default value, write "r" (reset) to the file to
4246  * reset them
4247  *
4248  * This setting works under auto fan control mode only. It can co-exist with
4249  * other settings which can work also under auto mode. Paring with the
4250  * acoustic_target_rpm_threshold setting, they define the maximum speed in
4251  * RPM the fan can spin when ASIC temperature is not greater than target
4252  * temperature. Setting via this interface will switch the fan control to
4253  * auto mode implicitly.
4254  */
4255 static ssize_t fan_target_temperature_show(struct kobject *kobj,
4256 					   struct kobj_attribute *attr,
4257 					   char *buf)
4258 {
4259 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4260 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4261 
4262 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf);
4263 }
4264 
4265 static ssize_t fan_target_temperature_store(struct kobject *kobj,
4266 					    struct kobj_attribute *attr,
4267 					    const char *buf,
4268 					    size_t count)
4269 {
4270 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4271 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4272 
4273 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4274 							     PP_OD_EDIT_FAN_TARGET_TEMPERATURE,
4275 							     buf,
4276 							     count);
4277 }
4278 
4279 static umode_t fan_target_temperature_visible(struct amdgpu_device *adev)
4280 {
4281 	umode_t umode = 0000;
4282 
4283 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE)
4284 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4285 
4286 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET)
4287 		umode |= S_IWUSR;
4288 
4289 	return umode;
4290 }
4291 
4292 /**
4293  * DOC: fan_minimum_pwm
4294  *
4295  * The amdgpu driver provides a sysfs API for checking and adjusting the
4296  * minimum fan speed in PWM.
4297  *
4298  * Reading back the file shows you the current setting and the permitted
4299  * ranges if changable.
4300  *
4301  * Writing an integer to the file, change the setting accordingly.
4302  *
4303  * When you have finished the editing, write "c" (commit) to the file to commit
4304  * your changes.
4305  *
4306  * If you want to reset to the default value, write "r" (reset) to the file to
4307  * reset them
4308  *
4309  * This setting works under auto fan control mode only. It can co-exist with
4310  * other settings which can work also under auto mode. It adjusts the PMFW's
4311  * behavior about the minimum fan speed in PWM the fan should spin. Setting
4312  * via this interface will switch the fan control to auto mode implicitly.
4313  */
4314 static ssize_t fan_minimum_pwm_show(struct kobject *kobj,
4315 				    struct kobj_attribute *attr,
4316 				    char *buf)
4317 {
4318 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4319 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4320 
4321 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf);
4322 }
4323 
4324 static ssize_t fan_minimum_pwm_store(struct kobject *kobj,
4325 				     struct kobj_attribute *attr,
4326 				     const char *buf,
4327 				     size_t count)
4328 {
4329 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4330 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4331 
4332 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4333 							     PP_OD_EDIT_FAN_MINIMUM_PWM,
4334 							     buf,
4335 							     count);
4336 }
4337 
4338 static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev)
4339 {
4340 	umode_t umode = 0000;
4341 
4342 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE)
4343 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4344 
4345 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET)
4346 		umode |= S_IWUSR;
4347 
4348 	return umode;
4349 }
4350 
4351 /**
4352  * DOC: fan_zero_rpm_enable
4353  *
4354  * The amdgpu driver provides a sysfs API for checking and adjusting the
4355  * zero RPM feature.
4356  *
4357  * Reading back the file shows you the current setting and the permitted
4358  * ranges if changable.
4359  *
4360  * Writing an integer to the file, change the setting accordingly.
4361  *
4362  * When you have finished the editing, write "c" (commit) to the file to commit
4363  * your changes.
4364  *
4365  * If you want to reset to the default value, write "r" (reset) to the file to
4366  * reset them.
4367  */
4368 static ssize_t fan_zero_rpm_enable_show(struct kobject *kobj,
4369 					   struct kobj_attribute *attr,
4370 					   char *buf)
4371 {
4372 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4373 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4374 
4375 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_ZERO_RPM_ENABLE, buf);
4376 }
4377 
4378 static ssize_t fan_zero_rpm_enable_store(struct kobject *kobj,
4379 					    struct kobj_attribute *attr,
4380 					    const char *buf,
4381 					    size_t count)
4382 {
4383 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4384 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4385 
4386 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4387 							     PP_OD_EDIT_FAN_ZERO_RPM_ENABLE,
4388 							     buf,
4389 							     count);
4390 }
4391 
4392 static umode_t fan_zero_rpm_enable_visible(struct amdgpu_device *adev)
4393 {
4394 	umode_t umode = 0000;
4395 
4396 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_RETRIEVE)
4397 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4398 
4399 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET)
4400 		umode |= S_IWUSR;
4401 
4402 	return umode;
4403 }
4404 
4405 /**
4406  * DOC: fan_zero_rpm_stop_temperature
4407  *
4408  * The amdgpu driver provides a sysfs API for checking and adjusting the
4409  * zero RPM stop temperature feature.
4410  *
4411  * Reading back the file shows you the current setting and the permitted
4412  * ranges if changable.
4413  *
4414  * Writing an integer to the file, change the setting accordingly.
4415  *
4416  * When you have finished the editing, write "c" (commit) to the file to commit
4417  * your changes.
4418  *
4419  * If you want to reset to the default value, write "r" (reset) to the file to
4420  * reset them.
4421  *
4422  * This setting works only if the Zero RPM setting is enabled. It adjusts the
4423  * temperature below which the fan can stop.
4424  */
4425 static ssize_t fan_zero_rpm_stop_temp_show(struct kobject *kobj,
4426 					   struct kobj_attribute *attr,
4427 					   char *buf)
4428 {
4429 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4430 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4431 
4432 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_ZERO_RPM_STOP_TEMP, buf);
4433 }
4434 
4435 static ssize_t fan_zero_rpm_stop_temp_store(struct kobject *kobj,
4436 					    struct kobj_attribute *attr,
4437 					    const char *buf,
4438 					    size_t count)
4439 {
4440 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4441 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4442 
4443 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4444 							     PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP,
4445 							     buf,
4446 							     count);
4447 }
4448 
4449 static umode_t fan_zero_rpm_stop_temp_visible(struct amdgpu_device *adev)
4450 {
4451 	umode_t umode = 0000;
4452 
4453 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_RETRIEVE)
4454 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4455 
4456 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_SET)
4457 		umode |= S_IWUSR;
4458 
4459 	return umode;
4460 }
4461 
4462 static struct od_feature_set amdgpu_od_set = {
4463 	.containers = {
4464 		[0] = {
4465 			.name = "fan_ctrl",
4466 			.sub_feature = {
4467 				[0] = {
4468 					.name = "fan_curve",
4469 					.ops = {
4470 						.is_visible = fan_curve_visible,
4471 						.show = fan_curve_show,
4472 						.store = fan_curve_store,
4473 					},
4474 				},
4475 				[1] = {
4476 					.name = "acoustic_limit_rpm_threshold",
4477 					.ops = {
4478 						.is_visible = acoustic_limit_threshold_visible,
4479 						.show = acoustic_limit_threshold_show,
4480 						.store = acoustic_limit_threshold_store,
4481 					},
4482 				},
4483 				[2] = {
4484 					.name = "acoustic_target_rpm_threshold",
4485 					.ops = {
4486 						.is_visible = acoustic_target_threshold_visible,
4487 						.show = acoustic_target_threshold_show,
4488 						.store = acoustic_target_threshold_store,
4489 					},
4490 				},
4491 				[3] = {
4492 					.name = "fan_target_temperature",
4493 					.ops = {
4494 						.is_visible = fan_target_temperature_visible,
4495 						.show = fan_target_temperature_show,
4496 						.store = fan_target_temperature_store,
4497 					},
4498 				},
4499 				[4] = {
4500 					.name = "fan_minimum_pwm",
4501 					.ops = {
4502 						.is_visible = fan_minimum_pwm_visible,
4503 						.show = fan_minimum_pwm_show,
4504 						.store = fan_minimum_pwm_store,
4505 					},
4506 				},
4507 				[5] = {
4508 					.name = "fan_zero_rpm_enable",
4509 					.ops = {
4510 						.is_visible = fan_zero_rpm_enable_visible,
4511 						.show = fan_zero_rpm_enable_show,
4512 						.store = fan_zero_rpm_enable_store,
4513 					},
4514 				},
4515 				[6] = {
4516 					.name = "fan_zero_rpm_stop_temperature",
4517 					.ops = {
4518 						.is_visible = fan_zero_rpm_stop_temp_visible,
4519 						.show = fan_zero_rpm_stop_temp_show,
4520 						.store = fan_zero_rpm_stop_temp_store,
4521 					},
4522 				},
4523 			},
4524 		},
4525 	},
4526 };
4527 
4528 static void od_kobj_release(struct kobject *kobj)
4529 {
4530 	struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj);
4531 
4532 	kfree(od_kobj);
4533 }
4534 
4535 static const struct kobj_type od_ktype = {
4536 	.release	= od_kobj_release,
4537 	.sysfs_ops	= &kobj_sysfs_ops,
4538 };
4539 
4540 static void amdgpu_od_set_fini(struct amdgpu_device *adev)
4541 {
4542 	struct od_kobj *container, *container_next;
4543 	struct od_attribute *attribute, *attribute_next;
4544 
4545 	if (list_empty(&adev->pm.od_kobj_list))
4546 		return;
4547 
4548 	list_for_each_entry_safe(container, container_next,
4549 				 &adev->pm.od_kobj_list, entry) {
4550 		list_del(&container->entry);
4551 
4552 		list_for_each_entry_safe(attribute, attribute_next,
4553 					 &container->attribute, entry) {
4554 			list_del(&attribute->entry);
4555 			sysfs_remove_file(&container->kobj,
4556 					  &attribute->attribute.attr);
4557 			kfree(attribute);
4558 		}
4559 
4560 		kobject_put(&container->kobj);
4561 	}
4562 }
4563 
4564 static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev,
4565 					   struct od_feature_ops *feature_ops)
4566 {
4567 	umode_t mode;
4568 
4569 	if (!feature_ops->is_visible)
4570 		return false;
4571 
4572 	/*
4573 	 * If the feature has no user read and write mode set,
4574 	 * we can assume the feature is actually not supported.(?)
4575 	 * And the revelant sysfs interface should not be exposed.
4576 	 */
4577 	mode = feature_ops->is_visible(adev);
4578 	if (mode & (S_IRUSR | S_IWUSR))
4579 		return true;
4580 
4581 	return false;
4582 }
4583 
4584 static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev,
4585 					struct od_feature_container *container)
4586 {
4587 	int i;
4588 
4589 	/*
4590 	 * If there is no valid entry within the container, the container
4591 	 * is recognized as a self contained container. And the valid entry
4592 	 * here means it has a valid naming and it is visible/supported by
4593 	 * the ASIC.
4594 	 */
4595 	for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) {
4596 		if (container->sub_feature[i].name &&
4597 		    amdgpu_is_od_feature_supported(adev,
4598 			&container->sub_feature[i].ops))
4599 			return false;
4600 	}
4601 
4602 	return true;
4603 }
4604 
4605 static int amdgpu_od_set_init(struct amdgpu_device *adev)
4606 {
4607 	struct od_kobj *top_set, *sub_set;
4608 	struct od_attribute *attribute;
4609 	struct od_feature_container *container;
4610 	struct od_feature_item *feature;
4611 	int i, j;
4612 	int ret;
4613 
4614 	/* Setup the top `gpu_od` directory which holds all other OD interfaces */
4615 	top_set = kzalloc_obj(*top_set);
4616 	if (!top_set)
4617 		return -ENOMEM;
4618 	list_add(&top_set->entry, &adev->pm.od_kobj_list);
4619 
4620 	ret = kobject_init_and_add(&top_set->kobj,
4621 				   &od_ktype,
4622 				   &adev->dev->kobj,
4623 				   "%s",
4624 				   "gpu_od");
4625 	if (ret)
4626 		goto err_out;
4627 	INIT_LIST_HEAD(&top_set->attribute);
4628 	top_set->priv = adev;
4629 
4630 	for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) {
4631 		container = &amdgpu_od_set.containers[i];
4632 
4633 		if (!container->name)
4634 			continue;
4635 
4636 		/*
4637 		 * If there is valid entries within the container, the container
4638 		 * will be presented as a sub directory and all its holding entries
4639 		 * will be presented as plain files under it.
4640 		 * While if there is no valid entry within the container, the container
4641 		 * itself will be presented as a plain file under top `gpu_od` directory.
4642 		 */
4643 		if (amdgpu_od_is_self_contained(adev, container)) {
4644 			if (!amdgpu_is_od_feature_supported(adev,
4645 			     &container->ops))
4646 				continue;
4647 
4648 			/*
4649 			 * The container is presented as a plain file under top `gpu_od`
4650 			 * directory.
4651 			 */
4652 			attribute = kzalloc_obj(*attribute);
4653 			if (!attribute) {
4654 				ret = -ENOMEM;
4655 				goto err_out;
4656 			}
4657 			list_add(&attribute->entry, &top_set->attribute);
4658 
4659 			attribute->attribute.attr.mode =
4660 					container->ops.is_visible(adev);
4661 			attribute->attribute.attr.name = container->name;
4662 			attribute->attribute.show =
4663 					container->ops.show;
4664 			attribute->attribute.store =
4665 					container->ops.store;
4666 			ret = sysfs_create_file(&top_set->kobj,
4667 						&attribute->attribute.attr);
4668 			if (ret)
4669 				goto err_out;
4670 		} else {
4671 			/* The container is presented as a sub directory. */
4672 			sub_set = kzalloc_obj(*sub_set);
4673 			if (!sub_set) {
4674 				ret = -ENOMEM;
4675 				goto err_out;
4676 			}
4677 			list_add(&sub_set->entry, &adev->pm.od_kobj_list);
4678 
4679 			ret = kobject_init_and_add(&sub_set->kobj,
4680 						   &od_ktype,
4681 						   &top_set->kobj,
4682 						   "%s",
4683 						   container->name);
4684 			if (ret)
4685 				goto err_out;
4686 			INIT_LIST_HEAD(&sub_set->attribute);
4687 			sub_set->priv = adev;
4688 
4689 			for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) {
4690 				feature = &container->sub_feature[j];
4691 				if (!feature->name)
4692 					continue;
4693 
4694 				if (!amdgpu_is_od_feature_supported(adev,
4695 				     &feature->ops))
4696 					continue;
4697 
4698 				/*
4699 				 * With the container presented as a sub directory, the entry within
4700 				 * it is presented as a plain file under the sub directory.
4701 				 */
4702 				attribute = kzalloc_obj(*attribute);
4703 				if (!attribute) {
4704 					ret = -ENOMEM;
4705 					goto err_out;
4706 				}
4707 				list_add(&attribute->entry, &sub_set->attribute);
4708 
4709 				attribute->attribute.attr.mode =
4710 						feature->ops.is_visible(adev);
4711 				attribute->attribute.attr.name = feature->name;
4712 				attribute->attribute.show =
4713 						feature->ops.show;
4714 				attribute->attribute.store =
4715 						feature->ops.store;
4716 				ret = sysfs_create_file(&sub_set->kobj,
4717 							&attribute->attribute.attr);
4718 				if (ret)
4719 					goto err_out;
4720 			}
4721 		}
4722 	}
4723 
4724 	/*
4725 	 * If gpu_od is the only member in the list, that means gpu_od is an
4726 	 * empty directory, so remove it.
4727 	 */
4728 	if (list_is_singular(&adev->pm.od_kobj_list))
4729 		goto err_out;
4730 
4731 	return 0;
4732 
4733 err_out:
4734 	amdgpu_od_set_fini(adev);
4735 
4736 	return ret;
4737 }
4738 
4739 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
4740 {
4741 	enum amdgpu_sriov_vf_mode mode;
4742 	uint32_t mask = 0;
4743 	uint32_t tmp;
4744 	int ret;
4745 
4746 	if (adev->pm.sysfs_initialized)
4747 		return 0;
4748 
4749 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
4750 
4751 	if (adev->pm.dpm_enabled == 0)
4752 		return 0;
4753 
4754 	mode = amdgpu_virt_get_sriov_vf_mode(adev);
4755 
4756 	/* under multi-vf mode, the hwmon attributes are all not supported */
4757 	if (mode != SRIOV_VF_MODE_MULTI_VF) {
4758 		adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
4759 									DRIVER_NAME, adev,
4760 									hwmon_groups);
4761 		if (IS_ERR(adev->pm.int_hwmon_dev)) {
4762 			ret = PTR_ERR(adev->pm.int_hwmon_dev);
4763 			dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret);
4764 			return ret;
4765 		}
4766 	}
4767 
4768 	switch (mode) {
4769 	case SRIOV_VF_MODE_ONE_VF:
4770 		mask = ATTR_FLAG_ONEVF;
4771 		break;
4772 	case SRIOV_VF_MODE_MULTI_VF:
4773 		mask = 0;
4774 		break;
4775 	case SRIOV_VF_MODE_BARE_METAL:
4776 	default:
4777 		mask = ATTR_FLAG_MASK_ALL;
4778 		break;
4779 	}
4780 
4781 	ret = amdgpu_device_attr_create_groups(adev,
4782 					       amdgpu_device_attrs,
4783 					       ARRAY_SIZE(amdgpu_device_attrs),
4784 					       mask,
4785 					       &adev->pm.pm_attr_list);
4786 	if (ret)
4787 		goto err_out0;
4788 
4789 	if (amdgpu_dpm_is_overdrive_supported(adev)) {
4790 		ret = amdgpu_od_set_init(adev);
4791 		if (ret)
4792 			goto err_out1;
4793 	} else if (adev->pm.pp_feature & PP_OVERDRIVE_MASK) {
4794 		dev_info(adev->dev, "overdrive feature is not supported\n");
4795 	}
4796 
4797 	if (amdgpu_dpm_get_pm_policy_info(adev, PP_PM_POLICY_NONE, NULL) !=
4798 	    -EOPNOTSUPP) {
4799 		ret = devm_device_add_group(adev->dev,
4800 					    &amdgpu_pm_policy_attr_group);
4801 		if (ret)
4802 			goto err_out1;
4803 	}
4804 
4805 	if (amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_GPUBOARD)) {
4806 		ret = devm_device_add_group(adev->dev,
4807 					    &amdgpu_board_attr_group);
4808 		if (ret)
4809 			goto err_out1;
4810 		if (amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAXNODEPOWERLIMIT,
4811 						 (void *)&tmp) != -EOPNOTSUPP) {
4812 			sysfs_add_file_to_group(&adev->dev->kobj,
4813 						&dev_attr_cur_node_power_limit.attr,
4814 						amdgpu_board_attr_group.name);
4815 			sysfs_add_file_to_group(&adev->dev->kobj, &dev_attr_node_power.attr,
4816 						amdgpu_board_attr_group.name);
4817 			sysfs_add_file_to_group(&adev->dev->kobj, &dev_attr_global_ppt_resid.attr,
4818 						amdgpu_board_attr_group.name);
4819 			sysfs_add_file_to_group(&adev->dev->kobj,
4820 						&dev_attr_max_node_power_limit.attr,
4821 						amdgpu_board_attr_group.name);
4822 			sysfs_add_file_to_group(&adev->dev->kobj, &dev_attr_npm_status.attr,
4823 						amdgpu_board_attr_group.name);
4824 		}
4825 		if (amdgpu_pm_get_sensor_generic(adev, AMDGPU_PP_SENSOR_UBB_POWER_LIMIT,
4826 						 (void *)&tmp) != -EOPNOTSUPP) {
4827 			sysfs_add_file_to_group(&adev->dev->kobj,
4828 						&dev_attr_baseboard_power_limit.attr,
4829 						amdgpu_board_attr_group.name);
4830 			sysfs_add_file_to_group(&adev->dev->kobj, &dev_attr_baseboard_power.attr,
4831 						amdgpu_board_attr_group.name);
4832 		}
4833 	}
4834 
4835 	adev->pm.sysfs_initialized = true;
4836 
4837 	return 0;
4838 
4839 err_out1:
4840 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4841 err_out0:
4842 	if (adev->pm.int_hwmon_dev)
4843 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
4844 
4845 	return ret;
4846 }
4847 
4848 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
4849 {
4850 	amdgpu_od_set_fini(adev);
4851 
4852 	if (adev->pm.int_hwmon_dev)
4853 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
4854 
4855 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4856 }
4857 
4858 /*
4859  * Debugfs info
4860  */
4861 #if defined(CONFIG_DEBUG_FS)
4862 
4863 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
4864 					   struct amdgpu_device *adev)
4865 {
4866 	uint16_t *p_val;
4867 	uint32_t size;
4868 	int i;
4869 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
4870 
4871 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
4872 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
4873 				GFP_KERNEL);
4874 
4875 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
4876 					    (void *)p_val, &size)) {
4877 			for (i = 0; i < num_cpu_cores; i++)
4878 				seq_printf(m, "\t%u MHz (CPU%d)\n",
4879 					   *(p_val + i), i);
4880 		}
4881 
4882 		kfree(p_val);
4883 	}
4884 }
4885 
4886 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
4887 {
4888 	uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
4889 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
4890 	uint32_t value, mwatt, centiwatt;
4891 	uint64_t value64 = 0;
4892 	uint32_t query = 0;
4893 	int size;
4894 
4895 	/* GPU Clocks */
4896 	size = sizeof(value);
4897 	seq_printf(m, "GFX Clocks and Power:\n");
4898 
4899 	amdgpu_debugfs_prints_cpu_info(m, adev);
4900 
4901 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
4902 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
4903 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
4904 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
4905 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
4906 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
4907 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
4908 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
4909 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
4910 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
4911 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
4912 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
4913 	size = sizeof(uint32_t);
4914 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size)) {
4915 		mwatt = power_2_mwatt(query);
4916 		centiwatt = DIV_ROUND_CLOSEST(mwatt, 10);
4917 		if (adev->flags & AMD_IS_APU)
4918 			seq_printf(m, "\t%u.%02u W (average SoC including CPU)\n", centiwatt / 100, centiwatt % 100);
4919 		else
4920 			seq_printf(m, "\t%u.%02u W (average SoC)\n", centiwatt / 100, centiwatt % 100);
4921 	}
4922 	size = sizeof(uint32_t);
4923 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size)) {
4924 		mwatt = power_2_mwatt(query);
4925 		centiwatt = DIV_ROUND_CLOSEST(mwatt, 10);
4926 		if (adev->flags & AMD_IS_APU)
4927 			seq_printf(m, "\t%u.%02u W (current SoC including CPU)\n", centiwatt / 100, centiwatt % 100);
4928 		else
4929 			seq_printf(m, "\t%u.%02u W (current SoC)\n", centiwatt / 100, centiwatt % 100);
4930 	}
4931 	size = sizeof(value);
4932 	seq_printf(m, "\n");
4933 
4934 	/* GPU Temp */
4935 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
4936 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
4937 
4938 	/* GPU Load */
4939 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
4940 		seq_printf(m, "GPU Load: %u %%\n", value);
4941 	/* MEM Load */
4942 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
4943 		seq_printf(m, "MEM Load: %u %%\n", value);
4944 	/* VCN Load */
4945 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_LOAD, (void *)&value, &size))
4946 		seq_printf(m, "VCN Load: %u %%\n", value);
4947 
4948 	seq_printf(m, "\n");
4949 
4950 	/* SMC feature mask */
4951 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
4952 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
4953 
4954 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
4955 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
4956 		/* VCN clocks */
4957 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
4958 			if (!value) {
4959 				seq_printf(m, "VCN: Powered down\n");
4960 			} else {
4961 				seq_printf(m, "VCN: Powered up\n");
4962 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4963 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4964 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4965 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4966 			}
4967 		}
4968 		seq_printf(m, "\n");
4969 	} else {
4970 		/* UVD clocks */
4971 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
4972 			if (!value) {
4973 				seq_printf(m, "UVD: Powered down\n");
4974 			} else {
4975 				seq_printf(m, "UVD: Powered up\n");
4976 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4977 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4978 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4979 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4980 			}
4981 		}
4982 		seq_printf(m, "\n");
4983 
4984 		/* VCE clocks */
4985 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
4986 			if (!value) {
4987 				seq_printf(m, "VCE: Powered down\n");
4988 			} else {
4989 				seq_printf(m, "VCE: Powered up\n");
4990 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
4991 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
4992 			}
4993 		}
4994 	}
4995 
4996 	return 0;
4997 }
4998 
4999 static const struct cg_flag_name clocks[] = {
5000 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
5001 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
5002 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
5003 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
5004 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
5005 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
5006 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
5007 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
5008 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
5009 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
5010 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
5011 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
5012 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
5013 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
5014 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
5015 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
5016 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
5017 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
5018 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
5019 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
5020 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
5021 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
5022 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
5023 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
5024 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
5025 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
5026 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
5027 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
5028 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
5029 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
5030 	{AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
5031 	{AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
5032 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
5033 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
5034 	{0, NULL},
5035 };
5036 
5037 static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
5038 {
5039 	int i;
5040 
5041 	for (i = 0; clocks[i].flag; i++)
5042 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
5043 			   (flags & clocks[i].flag) ? "On" : "Off");
5044 }
5045 
5046 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
5047 {
5048 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
5049 	u64 flags = 0;
5050 	int r;
5051 
5052 	r = amdgpu_pm_get_access(adev);
5053 	if (r < 0)
5054 		return r;
5055 
5056 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
5057 		r = amdgpu_debugfs_pm_info_pp(m, adev);
5058 		if (r)
5059 			goto out;
5060 	}
5061 
5062 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
5063 
5064 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
5065 	amdgpu_parse_cg_state(m, flags);
5066 	seq_printf(m, "\n");
5067 
5068 out:
5069 	amdgpu_pm_put_access(adev);
5070 
5071 	return r;
5072 }
5073 
5074 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
5075 
5076 /*
5077  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
5078  *
5079  * Reads debug memory region allocated to PMFW
5080  */
5081 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
5082 					 size_t size, loff_t *pos)
5083 {
5084 	struct amdgpu_device *adev = file_inode(f)->i_private;
5085 	size_t smu_prv_buf_size;
5086 	void *smu_prv_buf;
5087 	int ret = 0;
5088 
5089 	ret = amdgpu_pm_dev_state_check(adev, true);
5090 	if (ret)
5091 		return ret;
5092 
5093 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
5094 	if (ret)
5095 		return ret;
5096 
5097 	if (!smu_prv_buf || !smu_prv_buf_size)
5098 		return -EINVAL;
5099 
5100 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
5101 				       smu_prv_buf_size);
5102 }
5103 
5104 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
5105 	.owner = THIS_MODULE,
5106 	.open = simple_open,
5107 	.read = amdgpu_pm_prv_buffer_read,
5108 	.llseek = default_llseek,
5109 };
5110 
5111 #endif
5112 
5113 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
5114 {
5115 #if defined(CONFIG_DEBUG_FS)
5116 	struct drm_minor *minor = adev_to_drm(adev)->primary;
5117 	struct dentry *root = minor->debugfs_root;
5118 
5119 	if (!adev->pm.dpm_enabled)
5120 		return;
5121 
5122 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
5123 			    &amdgpu_debugfs_pm_info_fops);
5124 
5125 	if (adev->pm.smu_prv_buffer_size > 0)
5126 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
5127 					 adev,
5128 					 &amdgpu_debugfs_pm_prv_buffer_fops,
5129 					 adev->pm.smu_prv_buffer_size);
5130 
5131 	amdgpu_dpm_stb_debug_fs_init(adev);
5132 #endif
5133 }
5134