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