xref: /linux/drivers/gpu/drm/amd/pm/amdgpu_pm.c (revision e0dd9b8e9e669c5f7de0bbf2e1d2706a7c7d63f7)
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 
112 	if (amdgpu_in_reset(adev))
113 		return -EBUSY;
114 
115 	if (adev->in_suspend && !runpm_check)
116 		return -EBUSY;
117 
118 	return 0;
119 }
120 
121 /**
122  * amdgpu_pm_get_access - Check if device can be accessed, resume if needed.
123  * @adev: Target device.
124  *
125  * Checks the state of the @adev for access. Use runtime pm API to resume if
126  * needed. Return 0 if the device is accessible or a negative error code
127  * otherwise.
128  */
129 static int amdgpu_pm_get_access(struct amdgpu_device *adev)
130 {
131 	int ret;
132 
133 	ret = amdgpu_pm_dev_state_check(adev, true);
134 	if (ret)
135 		return ret;
136 
137 	return pm_runtime_resume_and_get(adev->dev);
138 }
139 
140 /**
141  * amdgpu_pm_get_access_if_active - Check if device is active for access.
142  * @adev: Target device.
143  *
144  * Checks the state of the @adev for access. Use runtime pm API to determine
145  * if device is active. Allow access only if device is active.Return 0 if the
146  * device is accessible or a negative error code otherwise.
147  */
148 static int amdgpu_pm_get_access_if_active(struct amdgpu_device *adev)
149 {
150 	int ret;
151 
152 	/* Ignore runpm status. If device is in suspended state, deny access */
153 	ret = amdgpu_pm_dev_state_check(adev, false);
154 	if (ret)
155 		return ret;
156 
157 	/*
158 	 * Allow only if device is active. If runpm is disabled also, as in
159 	 * kernels without CONFIG_PM, allow access.
160 	 */
161 	ret = pm_runtime_get_if_active(adev->dev);
162 	if (!ret)
163 		return -EPERM;
164 
165 	return 0;
166 }
167 
168 /**
169  * amdgpu_pm_put_access - Put to auto suspend mode after a device access.
170  * @adev: Target device.
171  *
172  * Should be paired with amdgpu_pm_get_access* calls
173  */
174 static inline void amdgpu_pm_put_access(struct amdgpu_device *adev)
175 {
176 	pm_runtime_mark_last_busy(adev->dev);
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_hwmon_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_hwmon_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_hwmon_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_hwmon_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_hwmon_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_hwmon_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_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1910 		 (void *)&ss_power))
1911 		*states = ATTR_STATE_UNSUPPORTED;
1912 	else if (amdgpu_hwmon_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  * only two types of attributes are reported, baseboard temperature and
2085  * gpu board temperature. Both of them are reported as binary files.
2086  *
2087  * * .. code-block:: console
2088  *
2089  *      hexdump /sys/bus/pci/devices/.../board/baseboard_temp
2090  *
2091  *      hexdump /sys/bus/pci/devices/.../board/gpuboard_temp
2092  *
2093  */
2094 
2095 /**
2096  * DOC: baseboard_temp
2097  *
2098  * The amdgpu driver provides a sysfs API for retrieving current baseboard
2099  * temperature metrics data. The file baseboard_temp is used for this.
2100  * Reading the file will dump all the current baseboard temperature  metrics data.
2101  */
2102 static ssize_t amdgpu_get_baseboard_temp_metrics(struct device *dev,
2103 						 struct device_attribute *attr, char *buf)
2104 {
2105 	struct drm_device *ddev = dev_get_drvdata(dev);
2106 	struct amdgpu_device *adev = drm_to_adev(ddev);
2107 	ssize_t size;
2108 	int ret;
2109 
2110 	ret = amdgpu_pm_get_access_if_active(adev);
2111 	if (ret)
2112 		return ret;
2113 
2114 	size = amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_BASEBOARD, NULL);
2115 	if (size <= 0)
2116 		goto out;
2117 	if (size >= PAGE_SIZE) {
2118 		ret = -ENOSPC;
2119 		goto out;
2120 	}
2121 
2122 	amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_BASEBOARD, buf);
2123 
2124 out:
2125 	amdgpu_pm_put_access(adev);
2126 
2127 	if (ret)
2128 		return ret;
2129 
2130 	return size;
2131 }
2132 
2133 /**
2134  * DOC: gpuboard_temp
2135  *
2136  * The amdgpu driver provides a sysfs API for retrieving current gpuboard
2137  * temperature metrics data. The file gpuboard_temp is used for this.
2138  * Reading the file will dump all the current gpuboard temperature  metrics data.
2139  */
2140 static ssize_t amdgpu_get_gpuboard_temp_metrics(struct device *dev,
2141 						struct device_attribute *attr, char *buf)
2142 {
2143 	struct drm_device *ddev = dev_get_drvdata(dev);
2144 	struct amdgpu_device *adev = drm_to_adev(ddev);
2145 	ssize_t size;
2146 	int ret;
2147 
2148 	ret = amdgpu_pm_get_access_if_active(adev);
2149 	if (ret)
2150 		return ret;
2151 
2152 	size = amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_GPUBOARD, NULL);
2153 	if (size <= 0)
2154 		goto out;
2155 	if (size >= PAGE_SIZE) {
2156 		ret = -ENOSPC;
2157 		goto out;
2158 	}
2159 
2160 	amdgpu_dpm_get_temp_metrics(adev, SMU_TEMP_METRIC_GPUBOARD, buf);
2161 
2162 out:
2163 	amdgpu_pm_put_access(adev);
2164 
2165 	if (ret)
2166 		return ret;
2167 
2168 	return size;
2169 }
2170 
2171 static DEVICE_ATTR(baseboard_temp, 0444, amdgpu_get_baseboard_temp_metrics, NULL);
2172 static DEVICE_ATTR(gpuboard_temp, 0444, amdgpu_get_gpuboard_temp_metrics, NULL);
2173 
2174 static struct attribute *board_attrs[] = {
2175 	&dev_attr_baseboard_temp.attr,
2176 	&dev_attr_gpuboard_temp.attr,
2177 	NULL
2178 };
2179 
2180 static umode_t amdgpu_board_attr_visible(struct kobject *kobj, struct attribute *attr, int n)
2181 {
2182 	struct device *dev = kobj_to_dev(kobj);
2183 	struct drm_device *ddev = dev_get_drvdata(dev);
2184 	struct amdgpu_device *adev = drm_to_adev(ddev);
2185 
2186 	if (attr == &dev_attr_baseboard_temp.attr) {
2187 		if (!amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_BASEBOARD))
2188 			return 0;
2189 	}
2190 
2191 	if (attr == &dev_attr_gpuboard_temp.attr) {
2192 		if (!amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_GPUBOARD))
2193 			return 0;
2194 	}
2195 
2196 	return attr->mode;
2197 }
2198 
2199 const struct attribute_group amdgpu_board_attr_group = {
2200 	.name = "board",
2201 	.attrs = board_attrs,
2202 	.is_visible = amdgpu_board_attr_visible,
2203 };
2204 
2205 /* pm policy attributes */
2206 struct amdgpu_pm_policy_attr {
2207 	struct device_attribute dev_attr;
2208 	enum pp_pm_policy id;
2209 };
2210 
2211 /**
2212  * DOC: pm_policy
2213  *
2214  * Certain SOCs can support different power policies to optimize application
2215  * performance. However, this policy is provided only at SOC level and not at a
2216  * per-process level. This is useful especially when entire SOC is utilized for
2217  * dedicated workload.
2218  *
2219  * The amdgpu driver provides a sysfs API for selecting the policy. Presently,
2220  * only two types of policies are supported through this interface.
2221  *
2222  *  Pstate Policy Selection - This is to select different Pstate profiles which
2223  *  decides clock/throttling preferences.
2224  *
2225  *  XGMI PLPD Policy Selection - When multiple devices are connected over XGMI,
2226  *  this helps to select policy to be applied for per link power down.
2227  *
2228  * The list of available policies and policy levels vary between SOCs. They can
2229  * be viewed under pm_policy node directory. If SOC doesn't support any policy,
2230  * this node won't be available. The different policies supported will be
2231  * available as separate nodes under pm_policy.
2232  *
2233  *	cat /sys/bus/pci/devices/.../pm_policy/<policy_type>
2234  *
2235  * Reading the policy file shows the different levels supported. The level which
2236  * is applied presently is denoted by * (asterisk). E.g.,
2237  *
2238  * .. code-block:: console
2239  *
2240  *	cat /sys/bus/pci/devices/.../pm_policy/soc_pstate
2241  *	0 : soc_pstate_default
2242  *	1 : soc_pstate_0
2243  *	2 : soc_pstate_1*
2244  *	3 : soc_pstate_2
2245  *
2246  *	cat /sys/bus/pci/devices/.../pm_policy/xgmi_plpd
2247  *	0 : plpd_disallow
2248  *	1 : plpd_default
2249  *	2 : plpd_optimized*
2250  *
2251  * To apply a specific policy
2252  *
2253  * "echo  <level> > /sys/bus/pci/devices/.../pm_policy/<policy_type>"
2254  *
2255  * For the levels listed in the example above, to select "plpd_optimized" for
2256  * XGMI and "soc_pstate_2" for soc pstate policy -
2257  *
2258  * .. code-block:: console
2259  *
2260  *	echo "2" > /sys/bus/pci/devices/.../pm_policy/xgmi_plpd
2261  *	echo "3" > /sys/bus/pci/devices/.../pm_policy/soc_pstate
2262  *
2263  */
2264 static ssize_t amdgpu_get_pm_policy_attr(struct device *dev,
2265 					 struct device_attribute *attr,
2266 					 char *buf)
2267 {
2268 	struct drm_device *ddev = dev_get_drvdata(dev);
2269 	struct amdgpu_device *adev = drm_to_adev(ddev);
2270 	struct amdgpu_pm_policy_attr *policy_attr;
2271 
2272 	policy_attr =
2273 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
2274 
2275 	return amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, buf);
2276 }
2277 
2278 static ssize_t amdgpu_set_pm_policy_attr(struct device *dev,
2279 					 struct device_attribute *attr,
2280 					 const char *buf, size_t count)
2281 {
2282 	struct drm_device *ddev = dev_get_drvdata(dev);
2283 	struct amdgpu_device *adev = drm_to_adev(ddev);
2284 	struct amdgpu_pm_policy_attr *policy_attr;
2285 	int ret, num_params = 0;
2286 	char delimiter[] = " \n\t";
2287 	char tmp_buf[128];
2288 	char *tmp, *param;
2289 	long val;
2290 
2291 	count = min(count, sizeof(tmp_buf));
2292 	memcpy(tmp_buf, buf, count);
2293 	tmp_buf[count - 1] = '\0';
2294 	tmp = tmp_buf;
2295 
2296 	tmp = skip_spaces(tmp);
2297 	while ((param = strsep(&tmp, delimiter))) {
2298 		if (!strlen(param)) {
2299 			tmp = skip_spaces(tmp);
2300 			continue;
2301 		}
2302 		ret = kstrtol(param, 0, &val);
2303 		if (ret)
2304 			return -EINVAL;
2305 		num_params++;
2306 		if (num_params > 1)
2307 			return -EINVAL;
2308 	}
2309 
2310 	if (num_params != 1)
2311 		return -EINVAL;
2312 
2313 	policy_attr =
2314 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
2315 
2316 	ret = amdgpu_pm_get_access(adev);
2317 	if (ret < 0)
2318 		return ret;
2319 
2320 	ret = amdgpu_dpm_set_pm_policy(adev, policy_attr->id, val);
2321 
2322 	amdgpu_pm_put_access(adev);
2323 
2324 	if (ret)
2325 		return ret;
2326 
2327 	return count;
2328 }
2329 
2330 #define AMDGPU_PM_POLICY_ATTR(_name, _id)                                  \
2331 	static struct amdgpu_pm_policy_attr pm_policy_attr_##_name = {     \
2332 		.dev_attr = __ATTR(_name, 0644, amdgpu_get_pm_policy_attr, \
2333 				   amdgpu_set_pm_policy_attr),             \
2334 		.id = PP_PM_POLICY_##_id,                                  \
2335 	};
2336 
2337 #define AMDGPU_PM_POLICY_ATTR_VAR(_name) pm_policy_attr_##_name.dev_attr.attr
2338 
2339 AMDGPU_PM_POLICY_ATTR(soc_pstate, SOC_PSTATE)
2340 AMDGPU_PM_POLICY_ATTR(xgmi_plpd, XGMI_PLPD)
2341 
2342 static struct attribute *pm_policy_attrs[] = {
2343 	&AMDGPU_PM_POLICY_ATTR_VAR(soc_pstate),
2344 	&AMDGPU_PM_POLICY_ATTR_VAR(xgmi_plpd),
2345 	NULL
2346 };
2347 
2348 static umode_t amdgpu_pm_policy_attr_visible(struct kobject *kobj,
2349 					     struct attribute *attr, int n)
2350 {
2351 	struct device *dev = kobj_to_dev(kobj);
2352 	struct drm_device *ddev = dev_get_drvdata(dev);
2353 	struct amdgpu_device *adev = drm_to_adev(ddev);
2354 	struct amdgpu_pm_policy_attr *policy_attr;
2355 
2356 	policy_attr =
2357 		container_of(attr, struct amdgpu_pm_policy_attr, dev_attr.attr);
2358 
2359 	if (amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, NULL) ==
2360 	    -ENOENT)
2361 		return 0;
2362 
2363 	return attr->mode;
2364 }
2365 
2366 const struct attribute_group amdgpu_pm_policy_attr_group = {
2367 	.name = "pm_policy",
2368 	.attrs = pm_policy_attrs,
2369 	.is_visible = amdgpu_pm_policy_attr_visible,
2370 };
2371 
2372 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2373 	AMDGPU_DEVICE_ATTR_RW(power_dpm_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2374 	AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level,	ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2375 	AMDGPU_DEVICE_ATTR_RO(pp_num_states,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2376 	AMDGPU_DEVICE_ATTR_RO(pp_cur_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2377 	AMDGPU_DEVICE_ATTR_RW(pp_force_state,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2378 	AMDGPU_DEVICE_ATTR_RW(pp_table,					ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2379 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2380 			      .attr_update = pp_dpm_clk_default_attr_update),
2381 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2382 			      .attr_update = pp_dpm_clk_default_attr_update),
2383 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2384 			      .attr_update = pp_dpm_clk_default_attr_update),
2385 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2386 			      .attr_update = pp_dpm_clk_default_attr_update),
2387 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2388 			      .attr_update = pp_dpm_clk_default_attr_update),
2389 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2390 			      .attr_update = pp_dpm_clk_default_attr_update),
2391 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2392 			      .attr_update = pp_dpm_clk_default_attr_update),
2393 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2394 			      .attr_update = pp_dpm_clk_default_attr_update),
2395 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2396 			      .attr_update = pp_dpm_dcefclk_attr_update),
2397 	AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2398 			      .attr_update = pp_dpm_clk_default_attr_update),
2399 	AMDGPU_DEVICE_ATTR_RW(pp_sclk_od,				ATTR_FLAG_BASIC),
2400 	AMDGPU_DEVICE_ATTR_RW(pp_mclk_od,				ATTR_FLAG_BASIC),
2401 	AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode,			ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2402 	AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage,			ATTR_FLAG_BASIC,
2403 			      .attr_update = pp_od_clk_voltage_attr_update),
2404 	AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2405 	AMDGPU_DEVICE_ATTR_RO(mem_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2406 	AMDGPU_DEVICE_ATTR_RO(vcn_busy_percent,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2407 	AMDGPU_DEVICE_ATTR_RO(pcie_bw,					ATTR_FLAG_BASIC),
2408 	AMDGPU_DEVICE_ATTR_RW(pp_features,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2409 	AMDGPU_DEVICE_ATTR_RO(unique_id,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2410 	AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging,		ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2411 	AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2412 	AMDGPU_DEVICE_ATTR_RO(gpu_metrics,				ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2413 	AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power,			ATTR_FLAG_BASIC,
2414 			      .attr_update = ss_power_attr_update),
2415 	AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power,			ATTR_FLAG_BASIC,
2416 			      .attr_update = ss_power_attr_update),
2417 	AMDGPU_DEVICE_ATTR_RW(smartshift_bias,				ATTR_FLAG_BASIC,
2418 			      .attr_update = ss_bias_attr_update),
2419 	AMDGPU_DEVICE_ATTR_RO(pm_metrics,				ATTR_FLAG_BASIC,
2420 			      .attr_update = amdgpu_pm_metrics_attr_update),
2421 };
2422 
2423 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2424 			       uint32_t mask, enum amdgpu_device_attr_states *states)
2425 {
2426 	struct device_attribute *dev_attr = &attr->dev_attr;
2427 	enum amdgpu_device_attr_id attr_id = attr->attr_id;
2428 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2429 
2430 	if (!(attr->flags & mask)) {
2431 		*states = ATTR_STATE_UNSUPPORTED;
2432 		return 0;
2433 	}
2434 
2435 	if (DEVICE_ATTR_IS(mem_busy_percent)) {
2436 		if ((adev->flags & AMD_IS_APU &&
2437 		     gc_ver != IP_VERSION(9, 4, 3)) ||
2438 		    gc_ver == IP_VERSION(9, 0, 1))
2439 			*states = ATTR_STATE_UNSUPPORTED;
2440 	} else if (DEVICE_ATTR_IS(vcn_busy_percent)) {
2441 		if (!(gc_ver == IP_VERSION(9, 3, 0) ||
2442 		      gc_ver == IP_VERSION(10, 3, 1) ||
2443 		      gc_ver == IP_VERSION(10, 3, 3) ||
2444 		      gc_ver == IP_VERSION(10, 3, 6) ||
2445 		      gc_ver == IP_VERSION(10, 3, 7) ||
2446 		      gc_ver == IP_VERSION(11, 0, 0) ||
2447 		      gc_ver == IP_VERSION(11, 0, 1) ||
2448 		      gc_ver == IP_VERSION(11, 0, 2) ||
2449 		      gc_ver == IP_VERSION(11, 0, 3) ||
2450 		      gc_ver == IP_VERSION(11, 0, 4) ||
2451 		      gc_ver == IP_VERSION(11, 5, 0) ||
2452 		      gc_ver == IP_VERSION(11, 5, 1) ||
2453 		      gc_ver == IP_VERSION(11, 5, 2) ||
2454 		      gc_ver == IP_VERSION(11, 5, 3) ||
2455 		      gc_ver == IP_VERSION(12, 0, 0) ||
2456 		      gc_ver == IP_VERSION(12, 0, 1)))
2457 			*states = ATTR_STATE_UNSUPPORTED;
2458 	} else if (DEVICE_ATTR_IS(pcie_bw)) {
2459 		/* PCIe Perf counters won't work on APU nodes */
2460 		if (adev->flags & AMD_IS_APU ||
2461 		    !adev->asic_funcs->get_pcie_usage)
2462 			*states = ATTR_STATE_UNSUPPORTED;
2463 	} else if (DEVICE_ATTR_IS(unique_id)) {
2464 		switch (gc_ver) {
2465 		case IP_VERSION(9, 0, 1):
2466 		case IP_VERSION(9, 4, 0):
2467 		case IP_VERSION(9, 4, 1):
2468 		case IP_VERSION(9, 4, 2):
2469 		case IP_VERSION(9, 4, 3):
2470 		case IP_VERSION(9, 4, 4):
2471 		case IP_VERSION(9, 5, 0):
2472 		case IP_VERSION(10, 3, 0):
2473 		case IP_VERSION(11, 0, 0):
2474 		case IP_VERSION(11, 0, 1):
2475 		case IP_VERSION(11, 0, 2):
2476 		case IP_VERSION(11, 0, 3):
2477 		case IP_VERSION(12, 0, 0):
2478 		case IP_VERSION(12, 0, 1):
2479 			*states = ATTR_STATE_SUPPORTED;
2480 			break;
2481 		default:
2482 			*states = ATTR_STATE_UNSUPPORTED;
2483 		}
2484 	} else if (DEVICE_ATTR_IS(pp_features)) {
2485 		if ((adev->flags & AMD_IS_APU &&
2486 		     gc_ver != IP_VERSION(9, 4, 3)) ||
2487 		    gc_ver < IP_VERSION(9, 0, 0))
2488 			*states = ATTR_STATE_UNSUPPORTED;
2489 	} else if (DEVICE_ATTR_IS(gpu_metrics)) {
2490 		if (gc_ver < IP_VERSION(9, 1, 0))
2491 			*states = ATTR_STATE_UNSUPPORTED;
2492 	} else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
2493 		if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2494 			*states = ATTR_STATE_UNSUPPORTED;
2495 		else if ((gc_ver == IP_VERSION(10, 3, 0) ||
2496 			  gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev))
2497 			*states = ATTR_STATE_UNSUPPORTED;
2498 	} else if (DEVICE_ATTR_IS(pp_mclk_od)) {
2499 		if (amdgpu_dpm_get_mclk_od(adev) == -EOPNOTSUPP)
2500 			*states = ATTR_STATE_UNSUPPORTED;
2501 	} else if (DEVICE_ATTR_IS(pp_sclk_od)) {
2502 		if (amdgpu_dpm_get_sclk_od(adev) == -EOPNOTSUPP)
2503 			*states = ATTR_STATE_UNSUPPORTED;
2504 	} else if (DEVICE_ATTR_IS(apu_thermal_cap)) {
2505 		u32 limit;
2506 
2507 		if (amdgpu_dpm_get_apu_thermal_limit(adev, &limit) ==
2508 		    -EOPNOTSUPP)
2509 			*states = ATTR_STATE_UNSUPPORTED;
2510 	}
2511 
2512 	switch (gc_ver) {
2513 	case IP_VERSION(10, 3, 0):
2514 		if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
2515 		    amdgpu_sriov_vf(adev)) {
2516 			dev_attr->attr.mode &= ~0222;
2517 			dev_attr->store = NULL;
2518 		}
2519 		break;
2520 	default:
2521 		break;
2522 	}
2523 
2524 	return 0;
2525 }
2526 
2527 
2528 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2529 				     struct amdgpu_device_attr *attr,
2530 				     uint32_t mask, struct list_head *attr_list)
2531 {
2532 	int ret = 0;
2533 	enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2534 	struct amdgpu_device_attr_entry *attr_entry;
2535 	struct device_attribute *dev_attr;
2536 	const char *name;
2537 
2538 	int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2539 			   uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2540 
2541 	if (!attr)
2542 		return -EINVAL;
2543 
2544 	dev_attr = &attr->dev_attr;
2545 	name = dev_attr->attr.name;
2546 
2547 	attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2548 
2549 	ret = attr_update(adev, attr, mask, &attr_states);
2550 	if (ret) {
2551 		dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2552 			name, ret);
2553 		return ret;
2554 	}
2555 
2556 	if (attr_states == ATTR_STATE_UNSUPPORTED)
2557 		return 0;
2558 
2559 	ret = device_create_file(adev->dev, dev_attr);
2560 	if (ret) {
2561 		dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2562 			name, ret);
2563 	}
2564 
2565 	attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2566 	if (!attr_entry)
2567 		return -ENOMEM;
2568 
2569 	attr_entry->attr = attr;
2570 	INIT_LIST_HEAD(&attr_entry->entry);
2571 
2572 	list_add_tail(&attr_entry->entry, attr_list);
2573 
2574 	return ret;
2575 }
2576 
2577 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2578 {
2579 	struct device_attribute *dev_attr = &attr->dev_attr;
2580 
2581 	device_remove_file(adev->dev, dev_attr);
2582 }
2583 
2584 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2585 					     struct list_head *attr_list);
2586 
2587 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2588 					    struct amdgpu_device_attr *attrs,
2589 					    uint32_t counts,
2590 					    uint32_t mask,
2591 					    struct list_head *attr_list)
2592 {
2593 	int ret = 0;
2594 	uint32_t i = 0;
2595 
2596 	for (i = 0; i < counts; i++) {
2597 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2598 		if (ret)
2599 			goto failed;
2600 	}
2601 
2602 	return 0;
2603 
2604 failed:
2605 	amdgpu_device_attr_remove_groups(adev, attr_list);
2606 
2607 	return ret;
2608 }
2609 
2610 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2611 					     struct list_head *attr_list)
2612 {
2613 	struct amdgpu_device_attr_entry *entry, *entry_tmp;
2614 
2615 	if (list_empty(attr_list))
2616 		return ;
2617 
2618 	list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2619 		amdgpu_device_attr_remove(adev, entry->attr);
2620 		list_del(&entry->entry);
2621 		kfree(entry);
2622 	}
2623 }
2624 
2625 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2626 				      struct device_attribute *attr,
2627 				      char *buf)
2628 {
2629 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2630 	int channel = to_sensor_dev_attr(attr)->index;
2631 	int r, temp = 0;
2632 
2633 	if (channel >= PP_TEMP_MAX)
2634 		return -EINVAL;
2635 
2636 	switch (channel) {
2637 	case PP_TEMP_JUNCTION:
2638 		/* get current junction temperature */
2639 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2640 					   (void *)&temp);
2641 		break;
2642 	case PP_TEMP_EDGE:
2643 		/* get current edge temperature */
2644 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2645 					   (void *)&temp);
2646 		break;
2647 	case PP_TEMP_MEM:
2648 		/* get current memory temperature */
2649 		r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2650 					   (void *)&temp);
2651 		break;
2652 	default:
2653 		r = -EINVAL;
2654 		break;
2655 	}
2656 
2657 	if (r)
2658 		return r;
2659 
2660 	return sysfs_emit(buf, "%d\n", temp);
2661 }
2662 
2663 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2664 					     struct device_attribute *attr,
2665 					     char *buf)
2666 {
2667 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2668 	int hyst = to_sensor_dev_attr(attr)->index;
2669 	int temp;
2670 
2671 	if (hyst)
2672 		temp = adev->pm.dpm.thermal.min_temp;
2673 	else
2674 		temp = adev->pm.dpm.thermal.max_temp;
2675 
2676 	return sysfs_emit(buf, "%d\n", temp);
2677 }
2678 
2679 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2680 					     struct device_attribute *attr,
2681 					     char *buf)
2682 {
2683 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2684 	int hyst = to_sensor_dev_attr(attr)->index;
2685 	int temp;
2686 
2687 	if (hyst)
2688 		temp = adev->pm.dpm.thermal.min_hotspot_temp;
2689 	else
2690 		temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2691 
2692 	return sysfs_emit(buf, "%d\n", temp);
2693 }
2694 
2695 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2696 					     struct device_attribute *attr,
2697 					     char *buf)
2698 {
2699 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2700 	int hyst = to_sensor_dev_attr(attr)->index;
2701 	int temp;
2702 
2703 	if (hyst)
2704 		temp = adev->pm.dpm.thermal.min_mem_temp;
2705 	else
2706 		temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2707 
2708 	return sysfs_emit(buf, "%d\n", temp);
2709 }
2710 
2711 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2712 					     struct device_attribute *attr,
2713 					     char *buf)
2714 {
2715 	int channel = to_sensor_dev_attr(attr)->index;
2716 
2717 	if (channel >= PP_TEMP_MAX)
2718 		return -EINVAL;
2719 
2720 	return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2721 }
2722 
2723 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2724 					     struct device_attribute *attr,
2725 					     char *buf)
2726 {
2727 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2728 	int channel = to_sensor_dev_attr(attr)->index;
2729 	int temp = 0;
2730 
2731 	if (channel >= PP_TEMP_MAX)
2732 		return -EINVAL;
2733 
2734 	switch (channel) {
2735 	case PP_TEMP_JUNCTION:
2736 		temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2737 		break;
2738 	case PP_TEMP_EDGE:
2739 		temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2740 		break;
2741 	case PP_TEMP_MEM:
2742 		temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2743 		break;
2744 	}
2745 
2746 	return sysfs_emit(buf, "%d\n", temp);
2747 }
2748 
2749 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2750 					    struct device_attribute *attr,
2751 					    char *buf)
2752 {
2753 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2754 	u32 pwm_mode = 0;
2755 	int ret;
2756 
2757 	ret = amdgpu_pm_get_access_if_active(adev);
2758 	if (ret)
2759 		return ret;
2760 
2761 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2762 
2763 	amdgpu_pm_put_access(adev);
2764 
2765 	if (ret)
2766 		return -EINVAL;
2767 
2768 	return sysfs_emit(buf, "%u\n", pwm_mode);
2769 }
2770 
2771 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2772 					    struct device_attribute *attr,
2773 					    const char *buf,
2774 					    size_t count)
2775 {
2776 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2777 	int err, ret;
2778 	u32 pwm_mode;
2779 	int value;
2780 
2781 	err = kstrtoint(buf, 10, &value);
2782 	if (err)
2783 		return err;
2784 
2785 	if (value == 0)
2786 		pwm_mode = AMD_FAN_CTRL_NONE;
2787 	else if (value == 1)
2788 		pwm_mode = AMD_FAN_CTRL_MANUAL;
2789 	else if (value == 2)
2790 		pwm_mode = AMD_FAN_CTRL_AUTO;
2791 	else
2792 		return -EINVAL;
2793 
2794 	ret = amdgpu_pm_get_access(adev);
2795 	if (ret < 0)
2796 		return ret;
2797 
2798 	ret = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2799 
2800 	amdgpu_pm_put_access(adev);
2801 
2802 	if (ret)
2803 		return -EINVAL;
2804 
2805 	return count;
2806 }
2807 
2808 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2809 					 struct device_attribute *attr,
2810 					 char *buf)
2811 {
2812 	return sysfs_emit(buf, "%i\n", 0);
2813 }
2814 
2815 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2816 					 struct device_attribute *attr,
2817 					 char *buf)
2818 {
2819 	return sysfs_emit(buf, "%i\n", 255);
2820 }
2821 
2822 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2823 				     struct device_attribute *attr,
2824 				     const char *buf, size_t count)
2825 {
2826 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2827 	int err;
2828 	u32 value;
2829 	u32 pwm_mode;
2830 
2831 	err = kstrtou32(buf, 10, &value);
2832 	if (err)
2833 		return err;
2834 
2835 	err = amdgpu_pm_get_access(adev);
2836 	if (err < 0)
2837 		return err;
2838 
2839 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2840 	if (err)
2841 		goto out;
2842 
2843 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2844 		pr_info("manual fan speed control should be enabled first\n");
2845 		err = -EINVAL;
2846 		goto out;
2847 	}
2848 
2849 	err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
2850 
2851 out:
2852 	amdgpu_pm_put_access(adev);
2853 
2854 	if (err)
2855 		return err;
2856 
2857 	return count;
2858 }
2859 
2860 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2861 				     struct device_attribute *attr,
2862 				     char *buf)
2863 {
2864 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2865 	int err;
2866 	u32 speed = 0;
2867 
2868 	err = amdgpu_pm_get_access_if_active(adev);
2869 	if (err)
2870 		return err;
2871 
2872 	err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2873 
2874 	amdgpu_pm_put_access(adev);
2875 
2876 	if (err)
2877 		return err;
2878 
2879 	return sysfs_emit(buf, "%i\n", speed);
2880 }
2881 
2882 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2883 					   struct device_attribute *attr,
2884 					   char *buf)
2885 {
2886 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2887 	int err;
2888 	u32 speed = 0;
2889 
2890 	err = amdgpu_pm_get_access_if_active(adev);
2891 	if (err)
2892 		return err;
2893 
2894 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2895 
2896 	amdgpu_pm_put_access(adev);
2897 
2898 	if (err)
2899 		return err;
2900 
2901 	return sysfs_emit(buf, "%i\n", speed);
2902 }
2903 
2904 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2905 					 struct device_attribute *attr,
2906 					 char *buf)
2907 {
2908 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2909 	u32 min_rpm = 0;
2910 	int r;
2911 
2912 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2913 				   (void *)&min_rpm);
2914 
2915 	if (r)
2916 		return r;
2917 
2918 	return sysfs_emit(buf, "%d\n", min_rpm);
2919 }
2920 
2921 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2922 					 struct device_attribute *attr,
2923 					 char *buf)
2924 {
2925 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2926 	u32 max_rpm = 0;
2927 	int r;
2928 
2929 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2930 				   (void *)&max_rpm);
2931 
2932 	if (r)
2933 		return r;
2934 
2935 	return sysfs_emit(buf, "%d\n", max_rpm);
2936 }
2937 
2938 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2939 					   struct device_attribute *attr,
2940 					   char *buf)
2941 {
2942 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2943 	int err;
2944 	u32 rpm = 0;
2945 
2946 	err = amdgpu_pm_get_access_if_active(adev);
2947 	if (err)
2948 		return err;
2949 
2950 	err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2951 
2952 	amdgpu_pm_put_access(adev);
2953 
2954 	if (err)
2955 		return err;
2956 
2957 	return sysfs_emit(buf, "%i\n", rpm);
2958 }
2959 
2960 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2961 				     struct device_attribute *attr,
2962 				     const char *buf, size_t count)
2963 {
2964 	struct amdgpu_device *adev = dev_get_drvdata(dev);
2965 	int err;
2966 	u32 value;
2967 	u32 pwm_mode;
2968 
2969 	err = kstrtou32(buf, 10, &value);
2970 	if (err)
2971 		return err;
2972 
2973 	err = amdgpu_pm_get_access(adev);
2974 	if (err < 0)
2975 		return err;
2976 
2977 	err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2978 	if (err)
2979 		goto out;
2980 
2981 	if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2982 		err = -ENODATA;
2983 		goto out;
2984 	}
2985 
2986 	err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2987 
2988 out:
2989 	amdgpu_pm_put_access(adev);
2990 
2991 	if (err)
2992 		return err;
2993 
2994 	return count;
2995 }
2996 
2997 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2998 					    struct device_attribute *attr,
2999 					    char *buf)
3000 {
3001 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3002 	u32 pwm_mode = 0;
3003 	int ret;
3004 
3005 	ret = amdgpu_pm_get_access_if_active(adev);
3006 	if (ret)
3007 		return ret;
3008 
3009 	ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
3010 
3011 	amdgpu_pm_put_access(adev);
3012 
3013 	if (ret)
3014 		return -EINVAL;
3015 
3016 	return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
3017 }
3018 
3019 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
3020 					    struct device_attribute *attr,
3021 					    const char *buf,
3022 					    size_t count)
3023 {
3024 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3025 	int err;
3026 	int value;
3027 	u32 pwm_mode;
3028 
3029 	err = kstrtoint(buf, 10, &value);
3030 	if (err)
3031 		return err;
3032 
3033 	if (value == 0)
3034 		pwm_mode = AMD_FAN_CTRL_AUTO;
3035 	else if (value == 1)
3036 		pwm_mode = AMD_FAN_CTRL_MANUAL;
3037 	else
3038 		return -EINVAL;
3039 
3040 	err = amdgpu_pm_get_access(adev);
3041 	if (err < 0)
3042 		return err;
3043 
3044 	err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
3045 
3046 	amdgpu_pm_put_access(adev);
3047 
3048 	if (err)
3049 		return -EINVAL;
3050 
3051 	return count;
3052 }
3053 
3054 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
3055 					struct device_attribute *attr,
3056 					char *buf)
3057 {
3058 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3059 	u32 vddgfx;
3060 	int r;
3061 
3062 	/* get the voltage */
3063 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
3064 				   (void *)&vddgfx);
3065 	if (r)
3066 		return r;
3067 
3068 	return sysfs_emit(buf, "%d\n", vddgfx);
3069 }
3070 
3071 static ssize_t amdgpu_hwmon_show_vddboard(struct device *dev,
3072 					  struct device_attribute *attr,
3073 					  char *buf)
3074 {
3075 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3076 	u32 vddboard;
3077 	int r;
3078 
3079 	/* get the voltage */
3080 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDBOARD,
3081 					    (void *)&vddboard);
3082 	if (r)
3083 		return r;
3084 
3085 	return sysfs_emit(buf, "%d\n", vddboard);
3086 }
3087 
3088 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
3089 					      struct device_attribute *attr,
3090 					      char *buf)
3091 {
3092 	return sysfs_emit(buf, "vddgfx\n");
3093 }
3094 
3095 static ssize_t amdgpu_hwmon_show_vddboard_label(struct device *dev,
3096 						struct device_attribute *attr,
3097 						char *buf)
3098 {
3099 	return sysfs_emit(buf, "vddboard\n");
3100 }
3101 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
3102 				       struct device_attribute *attr,
3103 				       char *buf)
3104 {
3105 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3106 	u32 vddnb;
3107 	int r;
3108 
3109 	/* only APUs have vddnb */
3110 	if  (!(adev->flags & AMD_IS_APU))
3111 		return -EINVAL;
3112 
3113 	/* get the voltage */
3114 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
3115 				   (void *)&vddnb);
3116 	if (r)
3117 		return r;
3118 
3119 	return sysfs_emit(buf, "%d\n", vddnb);
3120 }
3121 
3122 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
3123 					      struct device_attribute *attr,
3124 					      char *buf)
3125 {
3126 	return sysfs_emit(buf, "vddnb\n");
3127 }
3128 
3129 static int amdgpu_hwmon_get_power(struct device *dev,
3130 				  enum amd_pp_sensors sensor)
3131 {
3132 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3133 	unsigned int uw;
3134 	u32 query = 0;
3135 	int r;
3136 
3137 	r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
3138 	if (r)
3139 		return r;
3140 
3141 	/* convert to microwatts */
3142 	uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
3143 
3144 	return uw;
3145 }
3146 
3147 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
3148 					   struct device_attribute *attr,
3149 					   char *buf)
3150 {
3151 	ssize_t val;
3152 
3153 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
3154 	if (val < 0)
3155 		return val;
3156 
3157 	return sysfs_emit(buf, "%zd\n", val);
3158 }
3159 
3160 static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
3161 					     struct device_attribute *attr,
3162 					     char *buf)
3163 {
3164 	ssize_t val;
3165 
3166 	val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
3167 	if (val < 0)
3168 		return val;
3169 
3170 	return sysfs_emit(buf, "%zd\n", val);
3171 }
3172 
3173 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
3174 					struct device_attribute *attr,
3175 					char *buf,
3176 					enum pp_power_limit_level pp_limit_level)
3177 {
3178 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3179 	enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
3180 	uint32_t limit;
3181 	ssize_t size;
3182 	int r;
3183 
3184 	r = amdgpu_pm_get_access_if_active(adev);
3185 	if (r)
3186 		return r;
3187 
3188 	r = amdgpu_dpm_get_power_limit(adev, &limit,
3189 				      pp_limit_level, power_type);
3190 
3191 	if (!r)
3192 		size = sysfs_emit(buf, "%u\n", limit * 1000000);
3193 	else
3194 		size = sysfs_emit(buf, "\n");
3195 
3196 	amdgpu_pm_put_access(adev);
3197 
3198 	return size;
3199 }
3200 
3201 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
3202 					 struct device_attribute *attr,
3203 					 char *buf)
3204 {
3205 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MIN);
3206 }
3207 
3208 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
3209 					 struct device_attribute *attr,
3210 					 char *buf)
3211 {
3212 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
3213 
3214 }
3215 
3216 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
3217 					 struct device_attribute *attr,
3218 					 char *buf)
3219 {
3220 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
3221 
3222 }
3223 
3224 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
3225 					 struct device_attribute *attr,
3226 					 char *buf)
3227 {
3228 	return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
3229 
3230 }
3231 
3232 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
3233 					 struct device_attribute *attr,
3234 					 char *buf)
3235 {
3236 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3237 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
3238 
3239 	if (gc_ver == IP_VERSION(10, 3, 1))
3240 		return sysfs_emit(buf, "%s\n",
3241 				  to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
3242 				  "fastPPT" : "slowPPT");
3243 	else
3244 		return sysfs_emit(buf, "PPT\n");
3245 }
3246 
3247 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3248 		struct device_attribute *attr,
3249 		const char *buf,
3250 		size_t count)
3251 {
3252 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3253 	int limit_type = to_sensor_dev_attr(attr)->index;
3254 	int err;
3255 	u32 value;
3256 
3257 	err = kstrtou32(buf, 10, &value);
3258 	if (err)
3259 		return err;
3260 
3261 	value = value / 1000000; /* convert to Watt */
3262 	value |= limit_type << 24;
3263 
3264 	err = amdgpu_pm_get_access(adev);
3265 	if (err < 0)
3266 		return err;
3267 
3268 	err = amdgpu_dpm_set_power_limit(adev, value);
3269 
3270 	amdgpu_pm_put_access(adev);
3271 
3272 	if (err)
3273 		return err;
3274 
3275 	return count;
3276 }
3277 
3278 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3279 				      struct device_attribute *attr,
3280 				      char *buf)
3281 {
3282 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3283 	uint32_t sclk;
3284 	int r;
3285 
3286 	/* get the sclk */
3287 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3288 				   (void *)&sclk);
3289 	if (r)
3290 		return r;
3291 
3292 	return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
3293 }
3294 
3295 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3296 					    struct device_attribute *attr,
3297 					    char *buf)
3298 {
3299 	return sysfs_emit(buf, "sclk\n");
3300 }
3301 
3302 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3303 				      struct device_attribute *attr,
3304 				      char *buf)
3305 {
3306 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3307 	uint32_t mclk;
3308 	int r;
3309 
3310 	/* get the sclk */
3311 	r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3312 				   (void *)&mclk);
3313 	if (r)
3314 		return r;
3315 
3316 	return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3317 }
3318 
3319 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3320 					    struct device_attribute *attr,
3321 					    char *buf)
3322 {
3323 	return sysfs_emit(buf, "mclk\n");
3324 }
3325 
3326 /**
3327  * DOC: hwmon
3328  *
3329  * The amdgpu driver exposes the following sensor interfaces:
3330  *
3331  * - GPU temperature (via the on-die sensor)
3332  *
3333  * - GPU voltage
3334  *
3335  * - Northbridge voltage (APUs only)
3336  *
3337  * - GPU power
3338  *
3339  * - GPU fan
3340  *
3341  * - GPU gfx/compute engine clock
3342  *
3343  * - GPU memory clock (dGPU only)
3344  *
3345  * hwmon interfaces for GPU temperature:
3346  *
3347  * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3348  *   - temp2_input and temp3_input are supported on SOC15 dGPUs only
3349  *
3350  * - temp[1-3]_label: temperature channel label
3351  *   - temp2_label and temp3_label are supported on SOC15 dGPUs only
3352  *
3353  * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3354  *   - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3355  *
3356  * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3357  *   - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3358  *
3359  * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3360  *   - these are supported on SOC15 dGPUs only
3361  *
3362  * hwmon interfaces for GPU voltage:
3363  *
3364  * - in0_input: the voltage on the GPU in millivolts
3365  *
3366  * - in1_input: the voltage on the Northbridge in millivolts
3367  *
3368  * hwmon interfaces for GPU power:
3369  *
3370  * - power1_average: average power used by the SoC in microWatts.  On APUs this includes the CPU.
3371  *
3372  * - power1_input: instantaneous power used by the SoC in microWatts.  On APUs this includes the CPU.
3373  *
3374  * - power1_cap_min: minimum cap supported in microWatts
3375  *
3376  * - power1_cap_max: maximum cap supported in microWatts
3377  *
3378  * - power1_cap: selected power cap in microWatts
3379  *
3380  * hwmon interfaces for GPU fan:
3381  *
3382  * - pwm1: pulse width modulation fan level (0-255)
3383  *
3384  * - 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)
3385  *
3386  * - pwm1_min: pulse width modulation fan control minimum level (0)
3387  *
3388  * - pwm1_max: pulse width modulation fan control maximum level (255)
3389  *
3390  * - fan1_min: a minimum value Unit: revolution/min (RPM)
3391  *
3392  * - fan1_max: a maximum value Unit: revolution/max (RPM)
3393  *
3394  * - fan1_input: fan speed in RPM
3395  *
3396  * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3397  *
3398  * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3399  *
3400  * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
3401  *       That will get the former one overridden.
3402  *
3403  * hwmon interfaces for GPU clocks:
3404  *
3405  * - freq1_input: the gfx/compute clock in hertz
3406  *
3407  * - freq2_input: the memory clock in hertz
3408  *
3409  * You can use hwmon tools like sensors to view this information on your system.
3410  *
3411  */
3412 
3413 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3414 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3415 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3416 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3417 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3418 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3419 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3420 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3421 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3422 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3423 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3424 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3425 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3426 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3427 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3428 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3429 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3430 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3431 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3432 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3433 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3434 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3435 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3436 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3437 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3438 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3439 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3440 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3441 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, amdgpu_hwmon_show_vddboard, NULL, 0);
3442 static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, amdgpu_hwmon_show_vddboard_label, NULL, 0);
3443 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3444 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
3445 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3446 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3447 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3448 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3449 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3450 static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3451 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3452 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3453 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
3454 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3455 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3456 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3457 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3458 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3459 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3460 
3461 static struct attribute *hwmon_attributes[] = {
3462 	&sensor_dev_attr_temp1_input.dev_attr.attr,
3463 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
3464 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3465 	&sensor_dev_attr_temp2_input.dev_attr.attr,
3466 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
3467 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3468 	&sensor_dev_attr_temp3_input.dev_attr.attr,
3469 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
3470 	&sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3471 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
3472 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
3473 	&sensor_dev_attr_temp3_emergency.dev_attr.attr,
3474 	&sensor_dev_attr_temp1_label.dev_attr.attr,
3475 	&sensor_dev_attr_temp2_label.dev_attr.attr,
3476 	&sensor_dev_attr_temp3_label.dev_attr.attr,
3477 	&sensor_dev_attr_pwm1.dev_attr.attr,
3478 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
3479 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
3480 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
3481 	&sensor_dev_attr_fan1_input.dev_attr.attr,
3482 	&sensor_dev_attr_fan1_min.dev_attr.attr,
3483 	&sensor_dev_attr_fan1_max.dev_attr.attr,
3484 	&sensor_dev_attr_fan1_target.dev_attr.attr,
3485 	&sensor_dev_attr_fan1_enable.dev_attr.attr,
3486 	&sensor_dev_attr_in0_input.dev_attr.attr,
3487 	&sensor_dev_attr_in0_label.dev_attr.attr,
3488 	&sensor_dev_attr_in1_input.dev_attr.attr,
3489 	&sensor_dev_attr_in1_label.dev_attr.attr,
3490 	&sensor_dev_attr_in2_input.dev_attr.attr,
3491 	&sensor_dev_attr_in2_label.dev_attr.attr,
3492 	&sensor_dev_attr_power1_average.dev_attr.attr,
3493 	&sensor_dev_attr_power1_input.dev_attr.attr,
3494 	&sensor_dev_attr_power1_cap_max.dev_attr.attr,
3495 	&sensor_dev_attr_power1_cap_min.dev_attr.attr,
3496 	&sensor_dev_attr_power1_cap.dev_attr.attr,
3497 	&sensor_dev_attr_power1_cap_default.dev_attr.attr,
3498 	&sensor_dev_attr_power1_label.dev_attr.attr,
3499 	&sensor_dev_attr_power2_average.dev_attr.attr,
3500 	&sensor_dev_attr_power2_cap_max.dev_attr.attr,
3501 	&sensor_dev_attr_power2_cap_min.dev_attr.attr,
3502 	&sensor_dev_attr_power2_cap.dev_attr.attr,
3503 	&sensor_dev_attr_power2_cap_default.dev_attr.attr,
3504 	&sensor_dev_attr_power2_label.dev_attr.attr,
3505 	&sensor_dev_attr_freq1_input.dev_attr.attr,
3506 	&sensor_dev_attr_freq1_label.dev_attr.attr,
3507 	&sensor_dev_attr_freq2_input.dev_attr.attr,
3508 	&sensor_dev_attr_freq2_label.dev_attr.attr,
3509 	NULL
3510 };
3511 
3512 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3513 					struct attribute *attr, int index)
3514 {
3515 	struct device *dev = kobj_to_dev(kobj);
3516 	struct amdgpu_device *adev = dev_get_drvdata(dev);
3517 	umode_t effective_mode = attr->mode;
3518 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
3519 	uint32_t tmp;
3520 
3521 	/* under pp one vf mode manage of hwmon attributes is not supported */
3522 	if (amdgpu_sriov_is_pp_one_vf(adev))
3523 		effective_mode &= ~S_IWUSR;
3524 
3525 	/* Skip fan attributes if fan is not present */
3526 	if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3527 	    attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3528 	    attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3529 	    attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3530 	    attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3531 	    attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3532 	    attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3533 	    attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3534 	    attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3535 		return 0;
3536 
3537 	/* Skip fan attributes on APU */
3538 	if ((adev->flags & AMD_IS_APU) &&
3539 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3540 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3541 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3542 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3543 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3544 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3545 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3546 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3547 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3548 		return 0;
3549 
3550 	/* Skip crit temp on APU */
3551 	if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
3552 	    (gc_ver == IP_VERSION(9, 4, 3) || gc_ver == IP_VERSION(9, 4, 4) ||
3553 	     gc_ver == IP_VERSION(9, 5, 0))) &&
3554 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3555 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3556 		return 0;
3557 
3558 	/* Skip limit attributes if DPM is not enabled */
3559 	if (!adev->pm.dpm_enabled &&
3560 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3561 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3562 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3563 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3564 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3565 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3566 	     attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3567 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3568 	     attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3569 	     attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3570 	     attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3571 		return 0;
3572 
3573 	/* mask fan attributes if we have no bindings for this asic to expose */
3574 	if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3575 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3576 	    ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3577 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3578 		effective_mode &= ~S_IRUGO;
3579 
3580 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3581 	      attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3582 	      ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3583 	      attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3584 		effective_mode &= ~S_IWUSR;
3585 
3586 	/* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
3587 	if (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3588 	    attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
3589 	    attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
3590 	    attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr) {
3591 		if (adev->family == AMDGPU_FAMILY_SI ||
3592 		    ((adev->flags & AMD_IS_APU) && gc_ver != IP_VERSION(10, 3, 1) &&
3593 		     (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4))) ||
3594 		    (amdgpu_sriov_vf(adev) && gc_ver == IP_VERSION(11, 0, 3)))
3595 			return 0;
3596 	}
3597 
3598 	if (attr == &sensor_dev_attr_power1_cap.dev_attr.attr &&
3599 	    amdgpu_virt_cap_is_rw(&adev->virt.virt_caps, AMDGPU_VIRT_CAP_POWER_LIMIT))
3600 		effective_mode |= S_IWUSR;
3601 
3602 	/* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
3603 	if (((adev->family == AMDGPU_FAMILY_SI) ||
3604 	     ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3605 	    (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3606 		return 0;
3607 
3608 	/* not all products support both average and instantaneous */
3609 	if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
3610 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
3611 		return 0;
3612 	if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
3613 	    amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
3614 		return 0;
3615 
3616 	/* hide max/min values if we can't both query and manage the fan */
3617 	if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3618 	      (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3619 	      (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3620 	      (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3621 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3622 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3623 		return 0;
3624 
3625 	if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3626 	     (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3627 	     (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3628 	     attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3629 		return 0;
3630 
3631 	if ((adev->family == AMDGPU_FAMILY_SI ||	/* not implemented yet */
3632 	     adev->family == AMDGPU_FAMILY_KV ||	/* not implemented yet */
3633 	     (gc_ver == IP_VERSION(9, 4, 3) ||
3634 	      gc_ver == IP_VERSION(9, 4, 4) ||
3635 	      gc_ver == IP_VERSION(9, 5, 0))) &&
3636 	    (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3637 	     attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3638 		return 0;
3639 
3640 	/* only APUs other than gc 9,4,3 have vddnb */
3641 	if ((!(adev->flags & AMD_IS_APU) ||
3642 	     (gc_ver == IP_VERSION(9, 4, 3) ||
3643 	      gc_ver == IP_VERSION(9, 4, 4) ||
3644 	      gc_ver == IP_VERSION(9, 5, 0))) &&
3645 	    (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3646 	     attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3647 		return 0;
3648 
3649 	/* only few boards support vddboard */
3650 	if ((attr == &sensor_dev_attr_in2_input.dev_attr.attr ||
3651 	     attr == &sensor_dev_attr_in2_label.dev_attr.attr) &&
3652 	     amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDBOARD,
3653 					     (void *)&tmp) == -EOPNOTSUPP)
3654 		return 0;
3655 
3656 	/* no mclk on APUs other than gc 9,4,3*/
3657 	if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
3658 	    (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3659 	     attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3660 		return 0;
3661 
3662 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3663 	    (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4)) &&
3664 	    (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3665 	     attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3666 	     attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3667 	     attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3668 	     attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
3669 	     attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
3670 		return 0;
3671 
3672 	/* hotspot temperature for gc 9,4,3*/
3673 	if (gc_ver == IP_VERSION(9, 4, 3) ||
3674 	    gc_ver == IP_VERSION(9, 4, 4) ||
3675 	    gc_ver == IP_VERSION(9, 5, 0)) {
3676 		if (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
3677 		    attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3678 		    attr == &sensor_dev_attr_temp1_label.dev_attr.attr)
3679 			return 0;
3680 
3681 		if (attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3682 		    attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)
3683 			return attr->mode;
3684 	}
3685 
3686 	/* only SOC15 dGPUs support hotspot and mem temperatures */
3687 	if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3688 	    (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3689 	     attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3690 	     attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3691 	     attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3692 	     attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
3693 		return 0;
3694 
3695 	/* only Vangogh has fast PPT limit and power labels */
3696 	if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3697 	    (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3698 	     attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3699 	     attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3700 	     attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
3701 	     attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3702 	     attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3703 		return 0;
3704 
3705 	return effective_mode;
3706 }
3707 
3708 static const struct attribute_group hwmon_attrgroup = {
3709 	.attrs = hwmon_attributes,
3710 	.is_visible = hwmon_attributes_visible,
3711 };
3712 
3713 static const struct attribute_group *hwmon_groups[] = {
3714 	&hwmon_attrgroup,
3715 	NULL
3716 };
3717 
3718 static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
3719 				       enum pp_clock_type od_type,
3720 				       char *buf)
3721 {
3722 	int size = 0;
3723 	int ret;
3724 
3725 	ret = amdgpu_pm_get_access_if_active(adev);
3726 	if (ret)
3727 		return ret;
3728 
3729 	size = amdgpu_dpm_print_clock_levels(adev, od_type, buf);
3730 	if (size == 0)
3731 		size = sysfs_emit(buf, "\n");
3732 
3733 	amdgpu_pm_put_access(adev);
3734 
3735 	return size;
3736 }
3737 
3738 static int parse_input_od_command_lines(const char *buf,
3739 					size_t count,
3740 					u32 *type,
3741 					long *params,
3742 					uint32_t *num_of_params)
3743 {
3744 	const char delimiter[3] = {' ', '\n', '\0'};
3745 	uint32_t parameter_size = 0;
3746 	char buf_cpy[128] = {0};
3747 	char *tmp_str, *sub_str;
3748 	int ret;
3749 
3750 	if (count > sizeof(buf_cpy) - 1)
3751 		return -EINVAL;
3752 
3753 	memcpy(buf_cpy, buf, count);
3754 	tmp_str = buf_cpy;
3755 
3756 	/* skip heading spaces */
3757 	while (isspace(*tmp_str))
3758 		tmp_str++;
3759 
3760 	switch (*tmp_str) {
3761 	case 'c':
3762 		*type = PP_OD_COMMIT_DPM_TABLE;
3763 		return 0;
3764 	case 'r':
3765 		params[parameter_size] = *type;
3766 		*num_of_params = 1;
3767 		*type = PP_OD_RESTORE_DEFAULT_TABLE;
3768 		return 0;
3769 	default:
3770 		break;
3771 	}
3772 
3773 	while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
3774 		if (strlen(sub_str) == 0)
3775 			continue;
3776 
3777 		ret = kstrtol(sub_str, 0, &params[parameter_size]);
3778 		if (ret)
3779 			return -EINVAL;
3780 		parameter_size++;
3781 
3782 		if (!tmp_str)
3783 			break;
3784 
3785 		while (isspace(*tmp_str))
3786 			tmp_str++;
3787 	}
3788 
3789 	*num_of_params = parameter_size;
3790 
3791 	return 0;
3792 }
3793 
3794 static int
3795 amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
3796 				     enum PP_OD_DPM_TABLE_COMMAND cmd_type,
3797 				     const char *in_buf,
3798 				     size_t count)
3799 {
3800 	uint32_t parameter_size = 0;
3801 	long parameter[64];
3802 	int ret;
3803 
3804 	ret = parse_input_od_command_lines(in_buf,
3805 					   count,
3806 					   &cmd_type,
3807 					   parameter,
3808 					   &parameter_size);
3809 	if (ret)
3810 		return ret;
3811 
3812 	ret = amdgpu_pm_get_access(adev);
3813 	if (ret < 0)
3814 		return ret;
3815 
3816 	ret = amdgpu_dpm_odn_edit_dpm_table(adev,
3817 					    cmd_type,
3818 					    parameter,
3819 					    parameter_size);
3820 	if (ret)
3821 		goto err_out;
3822 
3823 	if (cmd_type == PP_OD_COMMIT_DPM_TABLE) {
3824 		ret = amdgpu_dpm_dispatch_task(adev,
3825 					       AMD_PP_TASK_READJUST_POWER_STATE,
3826 					       NULL);
3827 		if (ret)
3828 			goto err_out;
3829 	}
3830 
3831 	amdgpu_pm_put_access(adev);
3832 
3833 	return count;
3834 
3835 err_out:
3836 	amdgpu_pm_put_access(adev);
3837 
3838 	return ret;
3839 }
3840 
3841 /**
3842  * DOC: fan_curve
3843  *
3844  * The amdgpu driver provides a sysfs API for checking and adjusting the fan
3845  * control curve line.
3846  *
3847  * Reading back the file shows you the current settings(temperature in Celsius
3848  * degree and fan speed in pwm) applied to every anchor point of the curve line
3849  * and their permitted ranges if changable.
3850  *
3851  * Writing a desired string(with the format like "anchor_point_index temperature
3852  * fan_speed_in_pwm") to the file, change the settings for the specific anchor
3853  * point accordingly.
3854  *
3855  * When you have finished the editing, write "c" (commit) to the file to commit
3856  * your changes.
3857  *
3858  * If you want to reset to the default value, write "r" (reset) to the file to
3859  * reset them
3860  *
3861  * There are two fan control modes supported: auto and manual. With auto mode,
3862  * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature).
3863  * While with manual mode, users can set their own fan curve line as what
3864  * described here. Normally the ASIC is booted up with auto mode. Any
3865  * settings via this interface will switch the fan control to manual mode
3866  * implicitly.
3867  */
3868 static ssize_t fan_curve_show(struct kobject *kobj,
3869 			      struct kobj_attribute *attr,
3870 			      char *buf)
3871 {
3872 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3873 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3874 
3875 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf);
3876 }
3877 
3878 static ssize_t fan_curve_store(struct kobject *kobj,
3879 			       struct kobj_attribute *attr,
3880 			       const char *buf,
3881 			       size_t count)
3882 {
3883 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3884 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3885 
3886 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3887 							     PP_OD_EDIT_FAN_CURVE,
3888 							     buf,
3889 							     count);
3890 }
3891 
3892 static umode_t fan_curve_visible(struct amdgpu_device *adev)
3893 {
3894 	umode_t umode = 0000;
3895 
3896 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE)
3897 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3898 
3899 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET)
3900 		umode |= S_IWUSR;
3901 
3902 	return umode;
3903 }
3904 
3905 /**
3906  * DOC: acoustic_limit_rpm_threshold
3907  *
3908  * The amdgpu driver provides a sysfs API for checking and adjusting the
3909  * acoustic limit in RPM for fan control.
3910  *
3911  * Reading back the file shows you the current setting and the permitted
3912  * ranges if changable.
3913  *
3914  * Writing an integer to the file, change the setting accordingly.
3915  *
3916  * When you have finished the editing, write "c" (commit) to the file to commit
3917  * your changes.
3918  *
3919  * If you want to reset to the default value, write "r" (reset) to the file to
3920  * reset them
3921  *
3922  * This setting works under auto fan control mode only. It adjusts the PMFW's
3923  * behavior about the maximum speed in RPM the fan can spin. Setting via this
3924  * interface will switch the fan control to auto mode implicitly.
3925  */
3926 static ssize_t acoustic_limit_threshold_show(struct kobject *kobj,
3927 					     struct kobj_attribute *attr,
3928 					     char *buf)
3929 {
3930 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3931 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3932 
3933 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf);
3934 }
3935 
3936 static ssize_t acoustic_limit_threshold_store(struct kobject *kobj,
3937 					      struct kobj_attribute *attr,
3938 					      const char *buf,
3939 					      size_t count)
3940 {
3941 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3942 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3943 
3944 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3945 							     PP_OD_EDIT_ACOUSTIC_LIMIT,
3946 							     buf,
3947 							     count);
3948 }
3949 
3950 static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev)
3951 {
3952 	umode_t umode = 0000;
3953 
3954 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE)
3955 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
3956 
3957 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET)
3958 		umode |= S_IWUSR;
3959 
3960 	return umode;
3961 }
3962 
3963 /**
3964  * DOC: acoustic_target_rpm_threshold
3965  *
3966  * The amdgpu driver provides a sysfs API for checking and adjusting the
3967  * acoustic target in RPM for fan control.
3968  *
3969  * Reading back the file shows you the current setting and the permitted
3970  * ranges if changable.
3971  *
3972  * Writing an integer to the file, change the setting accordingly.
3973  *
3974  * When you have finished the editing, write "c" (commit) to the file to commit
3975  * your changes.
3976  *
3977  * If you want to reset to the default value, write "r" (reset) to the file to
3978  * reset them
3979  *
3980  * This setting works under auto fan control mode only. It can co-exist with
3981  * other settings which can work also under auto mode. It adjusts the PMFW's
3982  * behavior about the maximum speed in RPM the fan can spin when ASIC
3983  * temperature is not greater than target temperature. Setting via this
3984  * interface will switch the fan control to auto mode implicitly.
3985  */
3986 static ssize_t acoustic_target_threshold_show(struct kobject *kobj,
3987 					      struct kobj_attribute *attr,
3988 					      char *buf)
3989 {
3990 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3991 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3992 
3993 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf);
3994 }
3995 
3996 static ssize_t acoustic_target_threshold_store(struct kobject *kobj,
3997 					       struct kobj_attribute *attr,
3998 					       const char *buf,
3999 					       size_t count)
4000 {
4001 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4002 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4003 
4004 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4005 							     PP_OD_EDIT_ACOUSTIC_TARGET,
4006 							     buf,
4007 							     count);
4008 }
4009 
4010 static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev)
4011 {
4012 	umode_t umode = 0000;
4013 
4014 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE)
4015 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4016 
4017 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET)
4018 		umode |= S_IWUSR;
4019 
4020 	return umode;
4021 }
4022 
4023 /**
4024  * DOC: fan_target_temperature
4025  *
4026  * The amdgpu driver provides a sysfs API for checking and adjusting the
4027  * target tempeature in Celsius degree for fan control.
4028  *
4029  * Reading back the file shows you the current setting and the permitted
4030  * ranges if changable.
4031  *
4032  * Writing an integer to the file, change the setting accordingly.
4033  *
4034  * When you have finished the editing, write "c" (commit) to the file to commit
4035  * your changes.
4036  *
4037  * If you want to reset to the default value, write "r" (reset) to the file to
4038  * reset them
4039  *
4040  * This setting works under auto fan control mode only. It can co-exist with
4041  * other settings which can work also under auto mode. Paring with the
4042  * acoustic_target_rpm_threshold setting, they define the maximum speed in
4043  * RPM the fan can spin when ASIC temperature is not greater than target
4044  * temperature. Setting via this interface will switch the fan control to
4045  * auto mode implicitly.
4046  */
4047 static ssize_t fan_target_temperature_show(struct kobject *kobj,
4048 					   struct kobj_attribute *attr,
4049 					   char *buf)
4050 {
4051 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4052 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4053 
4054 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf);
4055 }
4056 
4057 static ssize_t fan_target_temperature_store(struct kobject *kobj,
4058 					    struct kobj_attribute *attr,
4059 					    const char *buf,
4060 					    size_t count)
4061 {
4062 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4063 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4064 
4065 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4066 							     PP_OD_EDIT_FAN_TARGET_TEMPERATURE,
4067 							     buf,
4068 							     count);
4069 }
4070 
4071 static umode_t fan_target_temperature_visible(struct amdgpu_device *adev)
4072 {
4073 	umode_t umode = 0000;
4074 
4075 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE)
4076 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4077 
4078 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET)
4079 		umode |= S_IWUSR;
4080 
4081 	return umode;
4082 }
4083 
4084 /**
4085  * DOC: fan_minimum_pwm
4086  *
4087  * The amdgpu driver provides a sysfs API for checking and adjusting the
4088  * minimum fan speed in PWM.
4089  *
4090  * Reading back the file shows you the current setting and the permitted
4091  * ranges if changable.
4092  *
4093  * Writing an integer to the file, change the setting accordingly.
4094  *
4095  * When you have finished the editing, write "c" (commit) to the file to commit
4096  * your changes.
4097  *
4098  * If you want to reset to the default value, write "r" (reset) to the file to
4099  * reset them
4100  *
4101  * This setting works under auto fan control mode only. It can co-exist with
4102  * other settings which can work also under auto mode. It adjusts the PMFW's
4103  * behavior about the minimum fan speed in PWM the fan should spin. Setting
4104  * via this interface will switch the fan control to auto mode implicitly.
4105  */
4106 static ssize_t fan_minimum_pwm_show(struct kobject *kobj,
4107 				    struct kobj_attribute *attr,
4108 				    char *buf)
4109 {
4110 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4111 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4112 
4113 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf);
4114 }
4115 
4116 static ssize_t fan_minimum_pwm_store(struct kobject *kobj,
4117 				     struct kobj_attribute *attr,
4118 				     const char *buf,
4119 				     size_t count)
4120 {
4121 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4122 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4123 
4124 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4125 							     PP_OD_EDIT_FAN_MINIMUM_PWM,
4126 							     buf,
4127 							     count);
4128 }
4129 
4130 static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev)
4131 {
4132 	umode_t umode = 0000;
4133 
4134 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE)
4135 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4136 
4137 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET)
4138 		umode |= S_IWUSR;
4139 
4140 	return umode;
4141 }
4142 
4143 /**
4144  * DOC: fan_zero_rpm_enable
4145  *
4146  * The amdgpu driver provides a sysfs API for checking and adjusting the
4147  * zero RPM feature.
4148  *
4149  * Reading back the file shows you the current setting and the permitted
4150  * ranges if changable.
4151  *
4152  * Writing an integer to the file, change the setting accordingly.
4153  *
4154  * When you have finished the editing, write "c" (commit) to the file to commit
4155  * your changes.
4156  *
4157  * If you want to reset to the default value, write "r" (reset) to the file to
4158  * reset them.
4159  */
4160 static ssize_t fan_zero_rpm_enable_show(struct kobject *kobj,
4161 					   struct kobj_attribute *attr,
4162 					   char *buf)
4163 {
4164 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4165 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4166 
4167 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_ZERO_RPM_ENABLE, buf);
4168 }
4169 
4170 static ssize_t fan_zero_rpm_enable_store(struct kobject *kobj,
4171 					    struct kobj_attribute *attr,
4172 					    const char *buf,
4173 					    size_t count)
4174 {
4175 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4176 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4177 
4178 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4179 							     PP_OD_EDIT_FAN_ZERO_RPM_ENABLE,
4180 							     buf,
4181 							     count);
4182 }
4183 
4184 static umode_t fan_zero_rpm_enable_visible(struct amdgpu_device *adev)
4185 {
4186 	umode_t umode = 0000;
4187 
4188 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_RETRIEVE)
4189 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4190 
4191 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET)
4192 		umode |= S_IWUSR;
4193 
4194 	return umode;
4195 }
4196 
4197 /**
4198  * DOC: fan_zero_rpm_stop_temperature
4199  *
4200  * The amdgpu driver provides a sysfs API for checking and adjusting the
4201  * zero RPM stop temperature feature.
4202  *
4203  * Reading back the file shows you the current setting and the permitted
4204  * ranges if changable.
4205  *
4206  * Writing an integer to the file, change the setting accordingly.
4207  *
4208  * When you have finished the editing, write "c" (commit) to the file to commit
4209  * your changes.
4210  *
4211  * If you want to reset to the default value, write "r" (reset) to the file to
4212  * reset them.
4213  *
4214  * This setting works only if the Zero RPM setting is enabled. It adjusts the
4215  * temperature below which the fan can stop.
4216  */
4217 static ssize_t fan_zero_rpm_stop_temp_show(struct kobject *kobj,
4218 					   struct kobj_attribute *attr,
4219 					   char *buf)
4220 {
4221 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4222 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4223 
4224 	return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_ZERO_RPM_STOP_TEMP, buf);
4225 }
4226 
4227 static ssize_t fan_zero_rpm_stop_temp_store(struct kobject *kobj,
4228 					    struct kobj_attribute *attr,
4229 					    const char *buf,
4230 					    size_t count)
4231 {
4232 	struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4233 	struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4234 
4235 	return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4236 							     PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP,
4237 							     buf,
4238 							     count);
4239 }
4240 
4241 static umode_t fan_zero_rpm_stop_temp_visible(struct amdgpu_device *adev)
4242 {
4243 	umode_t umode = 0000;
4244 
4245 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_RETRIEVE)
4246 		umode |= S_IRUSR | S_IRGRP | S_IROTH;
4247 
4248 	if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_SET)
4249 		umode |= S_IWUSR;
4250 
4251 	return umode;
4252 }
4253 
4254 static struct od_feature_set amdgpu_od_set = {
4255 	.containers = {
4256 		[0] = {
4257 			.name = "fan_ctrl",
4258 			.sub_feature = {
4259 				[0] = {
4260 					.name = "fan_curve",
4261 					.ops = {
4262 						.is_visible = fan_curve_visible,
4263 						.show = fan_curve_show,
4264 						.store = fan_curve_store,
4265 					},
4266 				},
4267 				[1] = {
4268 					.name = "acoustic_limit_rpm_threshold",
4269 					.ops = {
4270 						.is_visible = acoustic_limit_threshold_visible,
4271 						.show = acoustic_limit_threshold_show,
4272 						.store = acoustic_limit_threshold_store,
4273 					},
4274 				},
4275 				[2] = {
4276 					.name = "acoustic_target_rpm_threshold",
4277 					.ops = {
4278 						.is_visible = acoustic_target_threshold_visible,
4279 						.show = acoustic_target_threshold_show,
4280 						.store = acoustic_target_threshold_store,
4281 					},
4282 				},
4283 				[3] = {
4284 					.name = "fan_target_temperature",
4285 					.ops = {
4286 						.is_visible = fan_target_temperature_visible,
4287 						.show = fan_target_temperature_show,
4288 						.store = fan_target_temperature_store,
4289 					},
4290 				},
4291 				[4] = {
4292 					.name = "fan_minimum_pwm",
4293 					.ops = {
4294 						.is_visible = fan_minimum_pwm_visible,
4295 						.show = fan_minimum_pwm_show,
4296 						.store = fan_minimum_pwm_store,
4297 					},
4298 				},
4299 				[5] = {
4300 					.name = "fan_zero_rpm_enable",
4301 					.ops = {
4302 						.is_visible = fan_zero_rpm_enable_visible,
4303 						.show = fan_zero_rpm_enable_show,
4304 						.store = fan_zero_rpm_enable_store,
4305 					},
4306 				},
4307 				[6] = {
4308 					.name = "fan_zero_rpm_stop_temperature",
4309 					.ops = {
4310 						.is_visible = fan_zero_rpm_stop_temp_visible,
4311 						.show = fan_zero_rpm_stop_temp_show,
4312 						.store = fan_zero_rpm_stop_temp_store,
4313 					},
4314 				},
4315 			},
4316 		},
4317 	},
4318 };
4319 
4320 static void od_kobj_release(struct kobject *kobj)
4321 {
4322 	struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj);
4323 
4324 	kfree(od_kobj);
4325 }
4326 
4327 static const struct kobj_type od_ktype = {
4328 	.release	= od_kobj_release,
4329 	.sysfs_ops	= &kobj_sysfs_ops,
4330 };
4331 
4332 static void amdgpu_od_set_fini(struct amdgpu_device *adev)
4333 {
4334 	struct od_kobj *container, *container_next;
4335 	struct od_attribute *attribute, *attribute_next;
4336 
4337 	if (list_empty(&adev->pm.od_kobj_list))
4338 		return;
4339 
4340 	list_for_each_entry_safe(container, container_next,
4341 				 &adev->pm.od_kobj_list, entry) {
4342 		list_del(&container->entry);
4343 
4344 		list_for_each_entry_safe(attribute, attribute_next,
4345 					 &container->attribute, entry) {
4346 			list_del(&attribute->entry);
4347 			sysfs_remove_file(&container->kobj,
4348 					  &attribute->attribute.attr);
4349 			kfree(attribute);
4350 		}
4351 
4352 		kobject_put(&container->kobj);
4353 	}
4354 }
4355 
4356 static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev,
4357 					   struct od_feature_ops *feature_ops)
4358 {
4359 	umode_t mode;
4360 
4361 	if (!feature_ops->is_visible)
4362 		return false;
4363 
4364 	/*
4365 	 * If the feature has no user read and write mode set,
4366 	 * we can assume the feature is actually not supported.(?)
4367 	 * And the revelant sysfs interface should not be exposed.
4368 	 */
4369 	mode = feature_ops->is_visible(adev);
4370 	if (mode & (S_IRUSR | S_IWUSR))
4371 		return true;
4372 
4373 	return false;
4374 }
4375 
4376 static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev,
4377 					struct od_feature_container *container)
4378 {
4379 	int i;
4380 
4381 	/*
4382 	 * If there is no valid entry within the container, the container
4383 	 * is recognized as a self contained container. And the valid entry
4384 	 * here means it has a valid naming and it is visible/supported by
4385 	 * the ASIC.
4386 	 */
4387 	for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) {
4388 		if (container->sub_feature[i].name &&
4389 		    amdgpu_is_od_feature_supported(adev,
4390 			&container->sub_feature[i].ops))
4391 			return false;
4392 	}
4393 
4394 	return true;
4395 }
4396 
4397 static int amdgpu_od_set_init(struct amdgpu_device *adev)
4398 {
4399 	struct od_kobj *top_set, *sub_set;
4400 	struct od_attribute *attribute;
4401 	struct od_feature_container *container;
4402 	struct od_feature_item *feature;
4403 	int i, j;
4404 	int ret;
4405 
4406 	/* Setup the top `gpu_od` directory which holds all other OD interfaces */
4407 	top_set = kzalloc(sizeof(*top_set), GFP_KERNEL);
4408 	if (!top_set)
4409 		return -ENOMEM;
4410 	list_add(&top_set->entry, &adev->pm.od_kobj_list);
4411 
4412 	ret = kobject_init_and_add(&top_set->kobj,
4413 				   &od_ktype,
4414 				   &adev->dev->kobj,
4415 				   "%s",
4416 				   "gpu_od");
4417 	if (ret)
4418 		goto err_out;
4419 	INIT_LIST_HEAD(&top_set->attribute);
4420 	top_set->priv = adev;
4421 
4422 	for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) {
4423 		container = &amdgpu_od_set.containers[i];
4424 
4425 		if (!container->name)
4426 			continue;
4427 
4428 		/*
4429 		 * If there is valid entries within the container, the container
4430 		 * will be presented as a sub directory and all its holding entries
4431 		 * will be presented as plain files under it.
4432 		 * While if there is no valid entry within the container, the container
4433 		 * itself will be presented as a plain file under top `gpu_od` directory.
4434 		 */
4435 		if (amdgpu_od_is_self_contained(adev, container)) {
4436 			if (!amdgpu_is_od_feature_supported(adev,
4437 			     &container->ops))
4438 				continue;
4439 
4440 			/*
4441 			 * The container is presented as a plain file under top `gpu_od`
4442 			 * directory.
4443 			 */
4444 			attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
4445 			if (!attribute) {
4446 				ret = -ENOMEM;
4447 				goto err_out;
4448 			}
4449 			list_add(&attribute->entry, &top_set->attribute);
4450 
4451 			attribute->attribute.attr.mode =
4452 					container->ops.is_visible(adev);
4453 			attribute->attribute.attr.name = container->name;
4454 			attribute->attribute.show =
4455 					container->ops.show;
4456 			attribute->attribute.store =
4457 					container->ops.store;
4458 			ret = sysfs_create_file(&top_set->kobj,
4459 						&attribute->attribute.attr);
4460 			if (ret)
4461 				goto err_out;
4462 		} else {
4463 			/* The container is presented as a sub directory. */
4464 			sub_set = kzalloc(sizeof(*sub_set), GFP_KERNEL);
4465 			if (!sub_set) {
4466 				ret = -ENOMEM;
4467 				goto err_out;
4468 			}
4469 			list_add(&sub_set->entry, &adev->pm.od_kobj_list);
4470 
4471 			ret = kobject_init_and_add(&sub_set->kobj,
4472 						   &od_ktype,
4473 						   &top_set->kobj,
4474 						   "%s",
4475 						   container->name);
4476 			if (ret)
4477 				goto err_out;
4478 			INIT_LIST_HEAD(&sub_set->attribute);
4479 			sub_set->priv = adev;
4480 
4481 			for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) {
4482 				feature = &container->sub_feature[j];
4483 				if (!feature->name)
4484 					continue;
4485 
4486 				if (!amdgpu_is_od_feature_supported(adev,
4487 				     &feature->ops))
4488 					continue;
4489 
4490 				/*
4491 				 * With the container presented as a sub directory, the entry within
4492 				 * it is presented as a plain file under the sub directory.
4493 				 */
4494 				attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
4495 				if (!attribute) {
4496 					ret = -ENOMEM;
4497 					goto err_out;
4498 				}
4499 				list_add(&attribute->entry, &sub_set->attribute);
4500 
4501 				attribute->attribute.attr.mode =
4502 						feature->ops.is_visible(adev);
4503 				attribute->attribute.attr.name = feature->name;
4504 				attribute->attribute.show =
4505 						feature->ops.show;
4506 				attribute->attribute.store =
4507 						feature->ops.store;
4508 				ret = sysfs_create_file(&sub_set->kobj,
4509 							&attribute->attribute.attr);
4510 				if (ret)
4511 					goto err_out;
4512 			}
4513 		}
4514 	}
4515 
4516 	/*
4517 	 * If gpu_od is the only member in the list, that means gpu_od is an
4518 	 * empty directory, so remove it.
4519 	 */
4520 	if (list_is_singular(&adev->pm.od_kobj_list))
4521 		goto err_out;
4522 
4523 	return 0;
4524 
4525 err_out:
4526 	amdgpu_od_set_fini(adev);
4527 
4528 	return ret;
4529 }
4530 
4531 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
4532 {
4533 	enum amdgpu_sriov_vf_mode mode;
4534 	uint32_t mask = 0;
4535 	int ret;
4536 
4537 	if (adev->pm.sysfs_initialized)
4538 		return 0;
4539 
4540 	INIT_LIST_HEAD(&adev->pm.pm_attr_list);
4541 
4542 	if (adev->pm.dpm_enabled == 0)
4543 		return 0;
4544 
4545 	mode = amdgpu_virt_get_sriov_vf_mode(adev);
4546 
4547 	/* under multi-vf mode, the hwmon attributes are all not supported */
4548 	if (mode != SRIOV_VF_MODE_MULTI_VF) {
4549 		adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
4550 									DRIVER_NAME, adev,
4551 									hwmon_groups);
4552 		if (IS_ERR(adev->pm.int_hwmon_dev)) {
4553 			ret = PTR_ERR(adev->pm.int_hwmon_dev);
4554 			dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret);
4555 			return ret;
4556 		}
4557 	}
4558 
4559 	switch (mode) {
4560 	case SRIOV_VF_MODE_ONE_VF:
4561 		mask = ATTR_FLAG_ONEVF;
4562 		break;
4563 	case SRIOV_VF_MODE_MULTI_VF:
4564 		mask = 0;
4565 		break;
4566 	case SRIOV_VF_MODE_BARE_METAL:
4567 	default:
4568 		mask = ATTR_FLAG_MASK_ALL;
4569 		break;
4570 	}
4571 
4572 	ret = amdgpu_device_attr_create_groups(adev,
4573 					       amdgpu_device_attrs,
4574 					       ARRAY_SIZE(amdgpu_device_attrs),
4575 					       mask,
4576 					       &adev->pm.pm_attr_list);
4577 	if (ret)
4578 		goto err_out0;
4579 
4580 	if (amdgpu_dpm_is_overdrive_supported(adev)) {
4581 		ret = amdgpu_od_set_init(adev);
4582 		if (ret)
4583 			goto err_out1;
4584 	} else if (adev->pm.pp_feature & PP_OVERDRIVE_MASK) {
4585 		dev_info(adev->dev, "overdrive feature is not supported\n");
4586 	}
4587 
4588 	if (amdgpu_dpm_get_pm_policy_info(adev, PP_PM_POLICY_NONE, NULL) !=
4589 	    -EOPNOTSUPP) {
4590 		ret = devm_device_add_group(adev->dev,
4591 					    &amdgpu_pm_policy_attr_group);
4592 		if (ret)
4593 			goto err_out0;
4594 	}
4595 
4596 	if (amdgpu_dpm_is_temp_metrics_supported(adev, SMU_TEMP_METRIC_GPUBOARD)) {
4597 		ret = devm_device_add_group(adev->dev,
4598 					    &amdgpu_board_attr_group);
4599 		if (ret)
4600 			goto err_out0;
4601 	}
4602 
4603 	adev->pm.sysfs_initialized = true;
4604 
4605 	return 0;
4606 
4607 err_out1:
4608 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4609 err_out0:
4610 	if (adev->pm.int_hwmon_dev)
4611 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
4612 
4613 	return ret;
4614 }
4615 
4616 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
4617 {
4618 	amdgpu_od_set_fini(adev);
4619 
4620 	if (adev->pm.int_hwmon_dev)
4621 		hwmon_device_unregister(adev->pm.int_hwmon_dev);
4622 
4623 	amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4624 }
4625 
4626 /*
4627  * Debugfs info
4628  */
4629 #if defined(CONFIG_DEBUG_FS)
4630 
4631 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
4632 					   struct amdgpu_device *adev)
4633 {
4634 	uint16_t *p_val;
4635 	uint32_t size;
4636 	int i;
4637 	uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
4638 
4639 	if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
4640 		p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
4641 				GFP_KERNEL);
4642 
4643 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
4644 					    (void *)p_val, &size)) {
4645 			for (i = 0; i < num_cpu_cores; i++)
4646 				seq_printf(m, "\t%u MHz (CPU%d)\n",
4647 					   *(p_val + i), i);
4648 		}
4649 
4650 		kfree(p_val);
4651 	}
4652 }
4653 
4654 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
4655 {
4656 	uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
4657 	uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
4658 	uint32_t value;
4659 	uint64_t value64 = 0;
4660 	uint32_t query = 0;
4661 	int size;
4662 
4663 	/* GPU Clocks */
4664 	size = sizeof(value);
4665 	seq_printf(m, "GFX Clocks and Power:\n");
4666 
4667 	amdgpu_debugfs_prints_cpu_info(m, adev);
4668 
4669 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
4670 		seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
4671 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
4672 		seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
4673 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
4674 		seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
4675 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
4676 		seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
4677 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
4678 		seq_printf(m, "\t%u mV (VDDGFX)\n", value);
4679 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
4680 		seq_printf(m, "\t%u mV (VDDNB)\n", value);
4681 	size = sizeof(uint32_t);
4682 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size)) {
4683 		if (adev->flags & AMD_IS_APU)
4684 			seq_printf(m, "\t%u.%02u W (average SoC including CPU)\n", query >> 8, query & 0xff);
4685 		else
4686 			seq_printf(m, "\t%u.%02u W (average SoC)\n", query >> 8, query & 0xff);
4687 	}
4688 	size = sizeof(uint32_t);
4689 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size)) {
4690 		if (adev->flags & AMD_IS_APU)
4691 			seq_printf(m, "\t%u.%02u W (current SoC including CPU)\n", query >> 8, query & 0xff);
4692 		else
4693 			seq_printf(m, "\t%u.%02u W (current SoC)\n", query >> 8, query & 0xff);
4694 	}
4695 	size = sizeof(value);
4696 	seq_printf(m, "\n");
4697 
4698 	/* GPU Temp */
4699 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
4700 		seq_printf(m, "GPU Temperature: %u C\n", value/1000);
4701 
4702 	/* GPU Load */
4703 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
4704 		seq_printf(m, "GPU Load: %u %%\n", value);
4705 	/* MEM Load */
4706 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
4707 		seq_printf(m, "MEM Load: %u %%\n", value);
4708 	/* VCN Load */
4709 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_LOAD, (void *)&value, &size))
4710 		seq_printf(m, "VCN Load: %u %%\n", value);
4711 
4712 	seq_printf(m, "\n");
4713 
4714 	/* SMC feature mask */
4715 	if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
4716 		seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
4717 
4718 	/* ASICs greater than CHIP_VEGA20 supports these sensors */
4719 	if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
4720 		/* VCN clocks */
4721 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
4722 			if (!value) {
4723 				seq_printf(m, "VCN: Powered down\n");
4724 			} else {
4725 				seq_printf(m, "VCN: Powered up\n");
4726 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4727 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4728 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4729 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4730 			}
4731 		}
4732 		seq_printf(m, "\n");
4733 	} else {
4734 		/* UVD clocks */
4735 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
4736 			if (!value) {
4737 				seq_printf(m, "UVD: Powered down\n");
4738 			} else {
4739 				seq_printf(m, "UVD: Powered up\n");
4740 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4741 					seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4742 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4743 					seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4744 			}
4745 		}
4746 		seq_printf(m, "\n");
4747 
4748 		/* VCE clocks */
4749 		if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
4750 			if (!value) {
4751 				seq_printf(m, "VCE: Powered down\n");
4752 			} else {
4753 				seq_printf(m, "VCE: Powered up\n");
4754 				if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
4755 					seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
4756 			}
4757 		}
4758 	}
4759 
4760 	return 0;
4761 }
4762 
4763 static const struct cg_flag_name clocks[] = {
4764 	{AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
4765 	{AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
4766 	{AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
4767 	{AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
4768 	{AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
4769 	{AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
4770 	{AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
4771 	{AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
4772 	{AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
4773 	{AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
4774 	{AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
4775 	{AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
4776 	{AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
4777 	{AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
4778 	{AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
4779 	{AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
4780 	{AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
4781 	{AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
4782 	{AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
4783 	{AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
4784 	{AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
4785 	{AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
4786 	{AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
4787 	{AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
4788 	{AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
4789 	{AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
4790 	{AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
4791 	{AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
4792 	{AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
4793 	{AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
4794 	{AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
4795 	{AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
4796 	{AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
4797 	{AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
4798 	{0, NULL},
4799 };
4800 
4801 static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
4802 {
4803 	int i;
4804 
4805 	for (i = 0; clocks[i].flag; i++)
4806 		seq_printf(m, "\t%s: %s\n", clocks[i].name,
4807 			   (flags & clocks[i].flag) ? "On" : "Off");
4808 }
4809 
4810 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
4811 {
4812 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
4813 	u64 flags = 0;
4814 	int r;
4815 
4816 	r = amdgpu_pm_get_access(adev);
4817 	if (r < 0)
4818 		return r;
4819 
4820 	if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
4821 		r = amdgpu_debugfs_pm_info_pp(m, adev);
4822 		if (r)
4823 			goto out;
4824 	}
4825 
4826 	amdgpu_device_ip_get_clockgating_state(adev, &flags);
4827 
4828 	seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
4829 	amdgpu_parse_cg_state(m, flags);
4830 	seq_printf(m, "\n");
4831 
4832 out:
4833 	amdgpu_pm_put_access(adev);
4834 
4835 	return r;
4836 }
4837 
4838 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
4839 
4840 /*
4841  * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
4842  *
4843  * Reads debug memory region allocated to PMFW
4844  */
4845 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
4846 					 size_t size, loff_t *pos)
4847 {
4848 	struct amdgpu_device *adev = file_inode(f)->i_private;
4849 	size_t smu_prv_buf_size;
4850 	void *smu_prv_buf;
4851 	int ret = 0;
4852 
4853 	ret = amdgpu_pm_dev_state_check(adev, true);
4854 	if (ret)
4855 		return ret;
4856 
4857 	ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
4858 	if (ret)
4859 		return ret;
4860 
4861 	if (!smu_prv_buf || !smu_prv_buf_size)
4862 		return -EINVAL;
4863 
4864 	return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
4865 				       smu_prv_buf_size);
4866 }
4867 
4868 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
4869 	.owner = THIS_MODULE,
4870 	.open = simple_open,
4871 	.read = amdgpu_pm_prv_buffer_read,
4872 	.llseek = default_llseek,
4873 };
4874 
4875 #endif
4876 
4877 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
4878 {
4879 #if defined(CONFIG_DEBUG_FS)
4880 	struct drm_minor *minor = adev_to_drm(adev)->primary;
4881 	struct dentry *root = minor->debugfs_root;
4882 
4883 	if (!adev->pm.dpm_enabled)
4884 		return;
4885 
4886 	debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
4887 			    &amdgpu_debugfs_pm_info_fops);
4888 
4889 	if (adev->pm.smu_prv_buffer_size > 0)
4890 		debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
4891 					 adev,
4892 					 &amdgpu_debugfs_pm_prv_buffer_fops,
4893 					 adev->pm.smu_prv_buffer_size);
4894 
4895 	amdgpu_dpm_stb_debug_fs_init(adev);
4896 #endif
4897 }
4898