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