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