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