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