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