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