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