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