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