2 * Copyright 2017 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
27 #include "amdgpu_drv.h"
28 #include "amdgpu_pm.h"
29 #include "amdgpu_dpm.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>
38 static const struct hwmon_temp_label {
39 enum PP_HWMON_TEMP channel;
42 {PP_TEMP_EDGE, "edge"},
43 {PP_TEMP_JUNCTION, "junction"},
47 const char * const amdgpu_pp_profile_name[] = {
61 * DOC: power_dpm_state
63 * The power_dpm_state file is a legacy interface and is only provided for
64 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
65 * certain power related parameters. The file power_dpm_state is used for this.
66 * It accepts the following arguments:
76 * On older GPUs, the vbios provided a special power state for battery
77 * operation. Selecting battery switched to this state. This is no
78 * longer provided on newer GPUs so the option does nothing in that case.
82 * On older GPUs, the vbios provided a special power state for balanced
83 * operation. Selecting balanced switched to this state. This is no
84 * longer provided on newer GPUs so the option does nothing in that case.
88 * On older GPUs, the vbios provided a special power state for performance
89 * operation. Selecting performance switched to this state. This is no
90 * longer provided on newer GPUs so the option does nothing in that case.
94 static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
95 struct device_attribute *attr,
98 struct drm_device *ddev = dev_get_drvdata(dev);
99 struct amdgpu_device *adev = drm_to_adev(ddev);
100 enum amd_pm_state_type pm;
103 if (amdgpu_in_reset(adev))
105 if (adev->in_suspend && !adev->in_runpm)
108 ret = pm_runtime_get_sync(ddev->dev);
110 pm_runtime_put_autosuspend(ddev->dev);
114 amdgpu_dpm_get_current_power_state(adev, &pm);
116 pm_runtime_mark_last_busy(ddev->dev);
117 pm_runtime_put_autosuspend(ddev->dev);
119 return sysfs_emit(buf, "%s\n",
120 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
121 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
124 static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
125 struct device_attribute *attr,
129 struct drm_device *ddev = dev_get_drvdata(dev);
130 struct amdgpu_device *adev = drm_to_adev(ddev);
131 enum amd_pm_state_type state;
134 if (amdgpu_in_reset(adev))
136 if (adev->in_suspend && !adev->in_runpm)
139 if (strncmp("battery", buf, strlen("battery")) == 0)
140 state = POWER_STATE_TYPE_BATTERY;
141 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
142 state = POWER_STATE_TYPE_BALANCED;
143 else if (strncmp("performance", buf, strlen("performance")) == 0)
144 state = POWER_STATE_TYPE_PERFORMANCE;
148 ret = pm_runtime_get_sync(ddev->dev);
150 pm_runtime_put_autosuspend(ddev->dev);
154 amdgpu_dpm_set_power_state(adev, state);
156 pm_runtime_mark_last_busy(ddev->dev);
157 pm_runtime_put_autosuspend(ddev->dev);
164 * DOC: power_dpm_force_performance_level
166 * The amdgpu driver provides a sysfs API for adjusting certain power
167 * related parameters. The file power_dpm_force_performance_level is
168 * used for this. It accepts the following arguments:
188 * When auto is selected, the driver will attempt to dynamically select
189 * the optimal power profile for current conditions in the driver.
193 * When low is selected, the clocks are forced to the lowest power state.
197 * When high is selected, the clocks are forced to the highest power state.
201 * When manual is selected, the user can manually adjust which power states
202 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
203 * and pp_dpm_pcie files and adjust the power state transition heuristics
204 * via the pp_power_profile_mode sysfs file.
211 * When the profiling modes are selected, clock and power gating are
212 * disabled and the clocks are set for different profiling cases. This
213 * mode is recommended for profiling specific work loads where you do
214 * not want clock or power gating for clock fluctuation to interfere
215 * with your results. profile_standard sets the clocks to a fixed clock
216 * level which varies from asic to asic. profile_min_sclk forces the sclk
217 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
218 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
222 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
223 struct device_attribute *attr,
226 struct drm_device *ddev = dev_get_drvdata(dev);
227 struct amdgpu_device *adev = drm_to_adev(ddev);
228 enum amd_dpm_forced_level level = 0xff;
231 if (amdgpu_in_reset(adev))
233 if (adev->in_suspend && !adev->in_runpm)
236 ret = pm_runtime_get_sync(ddev->dev);
238 pm_runtime_put_autosuspend(ddev->dev);
242 level = amdgpu_dpm_get_performance_level(adev);
244 pm_runtime_mark_last_busy(ddev->dev);
245 pm_runtime_put_autosuspend(ddev->dev);
247 return sysfs_emit(buf, "%s\n",
248 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
249 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
250 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
251 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
252 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
253 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
254 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
255 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
256 (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
260 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
261 struct device_attribute *attr,
265 struct drm_device *ddev = dev_get_drvdata(dev);
266 struct amdgpu_device *adev = drm_to_adev(ddev);
267 enum amd_dpm_forced_level level;
270 if (amdgpu_in_reset(adev))
272 if (adev->in_suspend && !adev->in_runpm)
275 if (strncmp("low", buf, strlen("low")) == 0) {
276 level = AMD_DPM_FORCED_LEVEL_LOW;
277 } else if (strncmp("high", buf, strlen("high")) == 0) {
278 level = AMD_DPM_FORCED_LEVEL_HIGH;
279 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
280 level = AMD_DPM_FORCED_LEVEL_AUTO;
281 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
282 level = AMD_DPM_FORCED_LEVEL_MANUAL;
283 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
284 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
285 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
286 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
287 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
288 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
289 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
290 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
291 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
292 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
293 } else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
294 level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
299 ret = pm_runtime_get_sync(ddev->dev);
301 pm_runtime_put_autosuspend(ddev->dev);
305 mutex_lock(&adev->pm.stable_pstate_ctx_lock);
306 if (amdgpu_dpm_force_performance_level(adev, level)) {
307 pm_runtime_mark_last_busy(ddev->dev);
308 pm_runtime_put_autosuspend(ddev->dev);
309 mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
312 /* override whatever a user ctx may have set */
313 adev->pm.stable_pstate_ctx = NULL;
314 mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
316 pm_runtime_mark_last_busy(ddev->dev);
317 pm_runtime_put_autosuspend(ddev->dev);
322 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
323 struct device_attribute *attr,
326 struct drm_device *ddev = dev_get_drvdata(dev);
327 struct amdgpu_device *adev = drm_to_adev(ddev);
328 struct pp_states_info data;
332 if (amdgpu_in_reset(adev))
334 if (adev->in_suspend && !adev->in_runpm)
337 ret = pm_runtime_get_sync(ddev->dev);
339 pm_runtime_put_autosuspend(ddev->dev);
343 if (amdgpu_dpm_get_pp_num_states(adev, &data))
344 memset(&data, 0, sizeof(data));
346 pm_runtime_mark_last_busy(ddev->dev);
347 pm_runtime_put_autosuspend(ddev->dev);
349 buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
350 for (i = 0; i < data.nums; i++)
351 buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
352 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
353 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
354 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
355 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
360 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
361 struct device_attribute *attr,
364 struct drm_device *ddev = dev_get_drvdata(dev);
365 struct amdgpu_device *adev = drm_to_adev(ddev);
366 struct pp_states_info data = {0};
367 enum amd_pm_state_type pm = 0;
370 if (amdgpu_in_reset(adev))
372 if (adev->in_suspend && !adev->in_runpm)
375 ret = pm_runtime_get_sync(ddev->dev);
377 pm_runtime_put_autosuspend(ddev->dev);
381 amdgpu_dpm_get_current_power_state(adev, &pm);
383 ret = amdgpu_dpm_get_pp_num_states(adev, &data);
385 pm_runtime_mark_last_busy(ddev->dev);
386 pm_runtime_put_autosuspend(ddev->dev);
391 for (i = 0; i < data.nums; i++) {
392 if (pm == data.states[i])
399 return sysfs_emit(buf, "%d\n", i);
402 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
403 struct device_attribute *attr,
406 struct drm_device *ddev = dev_get_drvdata(dev);
407 struct amdgpu_device *adev = drm_to_adev(ddev);
409 if (amdgpu_in_reset(adev))
411 if (adev->in_suspend && !adev->in_runpm)
414 if (adev->pm.pp_force_state_enabled)
415 return amdgpu_get_pp_cur_state(dev, attr, buf);
417 return sysfs_emit(buf, "\n");
420 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
421 struct device_attribute *attr,
425 struct drm_device *ddev = dev_get_drvdata(dev);
426 struct amdgpu_device *adev = drm_to_adev(ddev);
427 enum amd_pm_state_type state = 0;
428 struct pp_states_info data;
432 if (amdgpu_in_reset(adev))
434 if (adev->in_suspend && !adev->in_runpm)
437 adev->pm.pp_force_state_enabled = false;
439 if (strlen(buf) == 1)
442 ret = kstrtoul(buf, 0, &idx);
443 if (ret || idx >= ARRAY_SIZE(data.states))
446 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
448 ret = pm_runtime_get_sync(ddev->dev);
450 pm_runtime_put_autosuspend(ddev->dev);
454 ret = amdgpu_dpm_get_pp_num_states(adev, &data);
458 state = data.states[idx];
460 /* only set user selected power states */
461 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
462 state != POWER_STATE_TYPE_DEFAULT) {
463 ret = amdgpu_dpm_dispatch_task(adev,
464 AMD_PP_TASK_ENABLE_USER_STATE, &state);
468 adev->pm.pp_force_state_enabled = true;
471 pm_runtime_mark_last_busy(ddev->dev);
472 pm_runtime_put_autosuspend(ddev->dev);
477 pm_runtime_mark_last_busy(ddev->dev);
478 pm_runtime_put_autosuspend(ddev->dev);
485 * The amdgpu driver provides a sysfs API for uploading new powerplay
486 * tables. The file pp_table is used for this. Reading the file
487 * will dump the current power play table. Writing to the file
488 * will attempt to upload a new powerplay table and re-initialize
489 * powerplay using that new table.
493 static ssize_t amdgpu_get_pp_table(struct device *dev,
494 struct device_attribute *attr,
497 struct drm_device *ddev = dev_get_drvdata(dev);
498 struct amdgpu_device *adev = drm_to_adev(ddev);
502 if (amdgpu_in_reset(adev))
504 if (adev->in_suspend && !adev->in_runpm)
507 ret = pm_runtime_get_sync(ddev->dev);
509 pm_runtime_put_autosuspend(ddev->dev);
513 size = amdgpu_dpm_get_pp_table(adev, &table);
515 pm_runtime_mark_last_busy(ddev->dev);
516 pm_runtime_put_autosuspend(ddev->dev);
521 if (size >= PAGE_SIZE)
522 size = PAGE_SIZE - 1;
524 memcpy(buf, table, size);
529 static ssize_t amdgpu_set_pp_table(struct device *dev,
530 struct device_attribute *attr,
534 struct drm_device *ddev = dev_get_drvdata(dev);
535 struct amdgpu_device *adev = drm_to_adev(ddev);
538 if (amdgpu_in_reset(adev))
540 if (adev->in_suspend && !adev->in_runpm)
543 ret = pm_runtime_get_sync(ddev->dev);
545 pm_runtime_put_autosuspend(ddev->dev);
549 ret = amdgpu_dpm_set_pp_table(adev, buf, count);
551 pm_runtime_mark_last_busy(ddev->dev);
552 pm_runtime_put_autosuspend(ddev->dev);
561 * DOC: pp_od_clk_voltage
563 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
564 * in each power level within a power state. The pp_od_clk_voltage is used for
567 * Note that the actual memory controller clock rate are exposed, not
568 * the effective memory clock of the DRAMs. To translate it, use the
571 * Clock conversion (Mhz):
573 * HBM: effective_memory_clock = memory_controller_clock * 1
575 * G5: effective_memory_clock = memory_controller_clock * 1
577 * G6: effective_memory_clock = memory_controller_clock * 2
579 * DRAM data rate (MT/s):
581 * HBM: effective_memory_clock * 2 = data_rate
583 * G5: effective_memory_clock * 4 = data_rate
585 * G6: effective_memory_clock * 8 = data_rate
589 * data_rate * vram_bit_width / 8 = memory_bandwidth
595 * memory_controller_clock = 1750 Mhz
597 * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
599 * data rate = 1750 * 4 = 7000 MT/s
601 * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
605 * memory_controller_clock = 875 Mhz
607 * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
609 * data rate = 1750 * 8 = 14000 MT/s
611 * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
613 * < For Vega10 and previous ASICs >
615 * Reading the file will display:
617 * - a list of engine clock levels and voltages labeled OD_SCLK
619 * - a list of memory clock levels and voltages labeled OD_MCLK
621 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
623 * To manually adjust these settings, first select manual using
624 * power_dpm_force_performance_level. Enter a new value for each
625 * level by writing a string that contains "s/m level clock voltage" to
626 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
627 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
628 * 810 mV. When you have edited all of the states as needed, write
629 * "c" (commit) to the file to commit your changes. If you want to reset to the
630 * default power levels, write "r" (reset) to the file to reset them.
633 * < For Vega20 and newer ASICs >
635 * Reading the file will display:
637 * - minimum and maximum engine clock labeled OD_SCLK
639 * - minimum(not available for Vega20 and Navi1x) and maximum memory
640 * clock labeled OD_MCLK
642 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
643 * They can be used to calibrate the sclk voltage curve. This is
644 * available for Vega20 and NV1X.
646 * - voltage offset for the six anchor points of the v/f curve labeled
647 * OD_VDDC_CURVE. They can be used to calibrate the v/f curve. This
648 * is only availabe for some SMU13 ASICs.
650 * - voltage offset(in mV) applied on target voltage calculation.
651 * This is available for Sienna Cichlid, Navy Flounder and Dimgrey
652 * Cavefish. For these ASICs, the target voltage calculation can be
653 * illustrated by "voltage = voltage calculated from v/f curve +
654 * overdrive vddgfx offset"
656 * - a list of valid ranges for sclk, mclk, and voltage curve points
661 * Reading the file will display:
663 * - minimum and maximum engine clock labeled OD_SCLK
665 * - a list of valid ranges for sclk labeled OD_RANGE
669 * Reading the file will display:
671 * - minimum and maximum engine clock labeled OD_SCLK
672 * - minimum and maximum core clocks labeled OD_CCLK
674 * - a list of valid ranges for sclk and cclk labeled OD_RANGE
676 * To manually adjust these settings:
678 * - First select manual using power_dpm_force_performance_level
680 * - For clock frequency setting, enter a new value by writing a
681 * string that contains "s/m index clock" to the file. The index
682 * should be 0 if to set minimum clock. And 1 if to set maximum
683 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
684 * "m 1 800" will update maximum mclk to be 800Mhz. For core
685 * clocks on VanGogh, the string contains "p core index clock".
686 * E.g., "p 2 0 800" would set the minimum core clock on core
689 * For sclk voltage curve,
690 * - For NV1X, enter the new values by writing a string that
691 * contains "vc point clock voltage" to the file. The points
692 * are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will update
693 * point1 with clock set as 300Mhz and voltage as 600mV. "vc 2
694 * 1000 1000" will update point3 with clock set as 1000Mhz and
696 * - For SMU13 ASICs, enter the new values by writing a string that
697 * contains "vc anchor_point_index voltage_offset" to the file.
698 * There are total six anchor points defined on the v/f curve with
700 * - "vc 0 10" will update the voltage offset for point1 as 10mv.
701 * - "vc 5 -10" will update the voltage offset for point6 as -10mv.
703 * To update the voltage offset applied for gfxclk/voltage calculation,
704 * enter the new value by writing a string that contains "vo offset".
705 * This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish.
706 * And the offset can be a positive or negative value.
708 * - When you have edited all of the states as needed, write "c" (commit)
709 * to the file to commit your changes
711 * - If you want to reset to the default power levels, write "r" (reset)
712 * to the file to reset them
716 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
717 struct device_attribute *attr,
721 struct drm_device *ddev = dev_get_drvdata(dev);
722 struct amdgpu_device *adev = drm_to_adev(ddev);
724 uint32_t parameter_size = 0;
729 const char delimiter[3] = {' ', '\n', '\0'};
732 if (amdgpu_in_reset(adev))
734 if (adev->in_suspend && !adev->in_runpm)
741 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
742 else if (*buf == 'p')
743 type = PP_OD_EDIT_CCLK_VDDC_TABLE;
744 else if (*buf == 'm')
745 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
746 else if (*buf == 'r')
747 type = PP_OD_RESTORE_DEFAULT_TABLE;
748 else if (*buf == 'c')
749 type = PP_OD_COMMIT_DPM_TABLE;
750 else if (!strncmp(buf, "vc", 2))
751 type = PP_OD_EDIT_VDDC_CURVE;
752 else if (!strncmp(buf, "vo", 2))
753 type = PP_OD_EDIT_VDDGFX_OFFSET;
757 memcpy(buf_cpy, buf, count+1);
761 if ((type == PP_OD_EDIT_VDDC_CURVE) ||
762 (type == PP_OD_EDIT_VDDGFX_OFFSET))
764 while (isspace(*++tmp_str));
766 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
767 if (strlen(sub_str) == 0)
769 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
774 while (isspace(*tmp_str))
778 ret = pm_runtime_get_sync(ddev->dev);
780 pm_runtime_put_autosuspend(ddev->dev);
784 if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
790 if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
791 parameter, parameter_size))
794 if (type == PP_OD_COMMIT_DPM_TABLE) {
795 if (amdgpu_dpm_dispatch_task(adev,
796 AMD_PP_TASK_READJUST_POWER_STATE,
801 pm_runtime_mark_last_busy(ddev->dev);
802 pm_runtime_put_autosuspend(ddev->dev);
807 pm_runtime_mark_last_busy(ddev->dev);
808 pm_runtime_put_autosuspend(ddev->dev);
812 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
813 struct device_attribute *attr,
816 struct drm_device *ddev = dev_get_drvdata(dev);
817 struct amdgpu_device *adev = drm_to_adev(ddev);
820 enum pp_clock_type od_clocks[6] = {
830 if (amdgpu_in_reset(adev))
832 if (adev->in_suspend && !adev->in_runpm)
835 ret = pm_runtime_get_sync(ddev->dev);
837 pm_runtime_put_autosuspend(ddev->dev);
841 for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
842 ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
846 if (ret == -ENOENT) {
847 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
848 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
849 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
850 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
851 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
852 size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
856 size = sysfs_emit(buf, "\n");
858 pm_runtime_mark_last_busy(ddev->dev);
859 pm_runtime_put_autosuspend(ddev->dev);
867 * The amdgpu driver provides a sysfs API for adjusting what powerplay
868 * features to be enabled. The file pp_features is used for this. And
869 * this is only available for Vega10 and later dGPUs.
871 * Reading back the file will show you the followings:
872 * - Current ppfeature masks
873 * - List of the all supported powerplay features with their naming,
874 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
876 * To manually enable or disable a specific feature, just set or clear
877 * the corresponding bit from original ppfeature masks and input the
878 * new ppfeature masks.
880 static ssize_t amdgpu_set_pp_features(struct device *dev,
881 struct device_attribute *attr,
885 struct drm_device *ddev = dev_get_drvdata(dev);
886 struct amdgpu_device *adev = drm_to_adev(ddev);
887 uint64_t featuremask;
890 if (amdgpu_in_reset(adev))
892 if (adev->in_suspend && !adev->in_runpm)
895 ret = kstrtou64(buf, 0, &featuremask);
899 ret = pm_runtime_get_sync(ddev->dev);
901 pm_runtime_put_autosuspend(ddev->dev);
905 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
907 pm_runtime_mark_last_busy(ddev->dev);
908 pm_runtime_put_autosuspend(ddev->dev);
916 static ssize_t amdgpu_get_pp_features(struct device *dev,
917 struct device_attribute *attr,
920 struct drm_device *ddev = dev_get_drvdata(dev);
921 struct amdgpu_device *adev = drm_to_adev(ddev);
925 if (amdgpu_in_reset(adev))
927 if (adev->in_suspend && !adev->in_runpm)
930 ret = pm_runtime_get_sync(ddev->dev);
932 pm_runtime_put_autosuspend(ddev->dev);
936 size = amdgpu_dpm_get_ppfeature_status(adev, buf);
938 size = sysfs_emit(buf, "\n");
940 pm_runtime_mark_last_busy(ddev->dev);
941 pm_runtime_put_autosuspend(ddev->dev);
947 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
949 * The amdgpu driver provides a sysfs API for adjusting what power levels
950 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
951 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
954 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
955 * Vega10 and later ASICs.
956 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
958 * Reading back the files will show you the available power levels within
959 * the power state and the clock information for those levels.
961 * To manually adjust these states, first select manual using
962 * power_dpm_force_performance_level.
963 * Secondly, enter a new value for each level by inputing a string that
964 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
967 * .. code-block:: bash
969 * echo "4 5 6" > pp_dpm_sclk
971 * will enable sclk levels 4, 5, and 6.
973 * NOTE: change to the dcefclk max dpm level is not supported now
976 static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
977 enum pp_clock_type type,
980 struct drm_device *ddev = dev_get_drvdata(dev);
981 struct amdgpu_device *adev = drm_to_adev(ddev);
985 if (amdgpu_in_reset(adev))
987 if (adev->in_suspend && !adev->in_runpm)
990 ret = pm_runtime_get_sync(ddev->dev);
992 pm_runtime_put_autosuspend(ddev->dev);
996 ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
998 size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1001 size = sysfs_emit(buf, "\n");
1003 pm_runtime_mark_last_busy(ddev->dev);
1004 pm_runtime_put_autosuspend(ddev->dev);
1010 * Worst case: 32 bits individually specified, in octal at 12 characters
1011 * per line (+1 for \n).
1013 #define AMDGPU_MASK_BUF_MAX (32 * 13)
1015 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1018 unsigned long level;
1019 char *sub_str = NULL;
1021 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1022 const char delimiter[3] = {' ', '\n', '\0'};
1027 bytes = min(count, sizeof(buf_cpy) - 1);
1028 memcpy(buf_cpy, buf, bytes);
1029 buf_cpy[bytes] = '\0';
1031 while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1032 if (strlen(sub_str)) {
1033 ret = kstrtoul(sub_str, 0, &level);
1034 if (ret || level > 31)
1036 *mask |= 1 << level;
1044 static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
1045 enum pp_clock_type type,
1049 struct drm_device *ddev = dev_get_drvdata(dev);
1050 struct amdgpu_device *adev = drm_to_adev(ddev);
1054 if (amdgpu_in_reset(adev))
1056 if (adev->in_suspend && !adev->in_runpm)
1059 ret = amdgpu_read_mask(buf, count, &mask);
1063 ret = pm_runtime_get_sync(ddev->dev);
1065 pm_runtime_put_autosuspend(ddev->dev);
1069 ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1071 pm_runtime_mark_last_busy(ddev->dev);
1072 pm_runtime_put_autosuspend(ddev->dev);
1080 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
1081 struct device_attribute *attr,
1084 return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
1087 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1088 struct device_attribute *attr,
1092 return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
1095 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1096 struct device_attribute *attr,
1099 return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1102 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1103 struct device_attribute *attr,
1107 return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1110 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1111 struct device_attribute *attr,
1114 return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1117 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1118 struct device_attribute *attr,
1122 return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1125 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1126 struct device_attribute *attr,
1129 return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1132 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1133 struct device_attribute *attr,
1137 return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1140 static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
1141 struct device_attribute *attr,
1144 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
1147 static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
1148 struct device_attribute *attr,
1152 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
1155 static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
1156 struct device_attribute *attr,
1159 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
1162 static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
1163 struct device_attribute *attr,
1167 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
1170 static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
1171 struct device_attribute *attr,
1174 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
1177 static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
1178 struct device_attribute *attr,
1182 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
1185 static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
1186 struct device_attribute *attr,
1189 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
1192 static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
1193 struct device_attribute *attr,
1197 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
1200 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1201 struct device_attribute *attr,
1204 return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1207 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1208 struct device_attribute *attr,
1212 return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1215 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1216 struct device_attribute *attr,
1219 return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1222 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1223 struct device_attribute *attr,
1227 return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1230 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1231 struct device_attribute *attr,
1234 struct drm_device *ddev = dev_get_drvdata(dev);
1235 struct amdgpu_device *adev = drm_to_adev(ddev);
1239 if (amdgpu_in_reset(adev))
1241 if (adev->in_suspend && !adev->in_runpm)
1244 ret = pm_runtime_get_sync(ddev->dev);
1246 pm_runtime_put_autosuspend(ddev->dev);
1250 value = amdgpu_dpm_get_sclk_od(adev);
1252 pm_runtime_mark_last_busy(ddev->dev);
1253 pm_runtime_put_autosuspend(ddev->dev);
1255 return sysfs_emit(buf, "%d\n", value);
1258 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1259 struct device_attribute *attr,
1263 struct drm_device *ddev = dev_get_drvdata(dev);
1264 struct amdgpu_device *adev = drm_to_adev(ddev);
1268 if (amdgpu_in_reset(adev))
1270 if (adev->in_suspend && !adev->in_runpm)
1273 ret = kstrtol(buf, 0, &value);
1278 ret = pm_runtime_get_sync(ddev->dev);
1280 pm_runtime_put_autosuspend(ddev->dev);
1284 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1286 pm_runtime_mark_last_busy(ddev->dev);
1287 pm_runtime_put_autosuspend(ddev->dev);
1292 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1293 struct device_attribute *attr,
1296 struct drm_device *ddev = dev_get_drvdata(dev);
1297 struct amdgpu_device *adev = drm_to_adev(ddev);
1301 if (amdgpu_in_reset(adev))
1303 if (adev->in_suspend && !adev->in_runpm)
1306 ret = pm_runtime_get_sync(ddev->dev);
1308 pm_runtime_put_autosuspend(ddev->dev);
1312 value = amdgpu_dpm_get_mclk_od(adev);
1314 pm_runtime_mark_last_busy(ddev->dev);
1315 pm_runtime_put_autosuspend(ddev->dev);
1317 return sysfs_emit(buf, "%d\n", value);
1320 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1321 struct device_attribute *attr,
1325 struct drm_device *ddev = dev_get_drvdata(dev);
1326 struct amdgpu_device *adev = drm_to_adev(ddev);
1330 if (amdgpu_in_reset(adev))
1332 if (adev->in_suspend && !adev->in_runpm)
1335 ret = kstrtol(buf, 0, &value);
1340 ret = pm_runtime_get_sync(ddev->dev);
1342 pm_runtime_put_autosuspend(ddev->dev);
1346 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1348 pm_runtime_mark_last_busy(ddev->dev);
1349 pm_runtime_put_autosuspend(ddev->dev);
1355 * DOC: pp_power_profile_mode
1357 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1358 * related to switching between power levels in a power state. The file
1359 * pp_power_profile_mode is used for this.
1361 * Reading this file outputs a list of all of the predefined power profiles
1362 * and the relevant heuristics settings for that profile.
1364 * To select a profile or create a custom profile, first select manual using
1365 * power_dpm_force_performance_level. Writing the number of a predefined
1366 * profile to pp_power_profile_mode will enable those heuristics. To
1367 * create a custom set of heuristics, write a string of numbers to the file
1368 * starting with the number of the custom profile along with a setting
1369 * for each heuristic parameter. Due to differences across asic families
1370 * the heuristic parameters vary from family to family.
1374 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1375 struct device_attribute *attr,
1378 struct drm_device *ddev = dev_get_drvdata(dev);
1379 struct amdgpu_device *adev = drm_to_adev(ddev);
1383 if (amdgpu_in_reset(adev))
1385 if (adev->in_suspend && !adev->in_runpm)
1388 ret = pm_runtime_get_sync(ddev->dev);
1390 pm_runtime_put_autosuspend(ddev->dev);
1394 size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1396 size = sysfs_emit(buf, "\n");
1398 pm_runtime_mark_last_busy(ddev->dev);
1399 pm_runtime_put_autosuspend(ddev->dev);
1405 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1406 struct device_attribute *attr,
1411 struct drm_device *ddev = dev_get_drvdata(dev);
1412 struct amdgpu_device *adev = drm_to_adev(ddev);
1413 uint32_t parameter_size = 0;
1415 char *sub_str, buf_cpy[128];
1419 long int profile_mode = 0;
1420 const char delimiter[3] = {' ', '\n', '\0'};
1422 if (amdgpu_in_reset(adev))
1424 if (adev->in_suspend && !adev->in_runpm)
1429 ret = kstrtol(tmp, 0, &profile_mode);
1433 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1434 if (count < 2 || count > 127)
1436 while (isspace(*++buf))
1438 memcpy(buf_cpy, buf, count-i);
1440 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1441 if (strlen(sub_str) == 0)
1443 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
1447 while (isspace(*tmp_str))
1451 parameter[parameter_size] = profile_mode;
1453 ret = pm_runtime_get_sync(ddev->dev);
1455 pm_runtime_put_autosuspend(ddev->dev);
1459 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1461 pm_runtime_mark_last_busy(ddev->dev);
1462 pm_runtime_put_autosuspend(ddev->dev);
1470 static unsigned int amdgpu_hwmon_get_sensor_generic(struct amdgpu_device *adev,
1471 enum amd_pp_sensors sensor,
1474 int r, size = sizeof(uint32_t);
1476 if (amdgpu_in_reset(adev))
1478 if (adev->in_suspend && !adev->in_runpm)
1481 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1483 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1487 /* get the sensor value */
1488 r = amdgpu_dpm_read_sensor(adev, sensor, query, &size);
1490 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1491 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1497 * DOC: gpu_busy_percent
1499 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1500 * is as a percentage. The file gpu_busy_percent is used for this.
1501 * The SMU firmware computes a percentage of load based on the
1502 * aggregate activity level in the IP cores.
1504 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1505 struct device_attribute *attr,
1508 struct drm_device *ddev = dev_get_drvdata(dev);
1509 struct amdgpu_device *adev = drm_to_adev(ddev);
1513 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value);
1517 return sysfs_emit(buf, "%d\n", value);
1521 * DOC: mem_busy_percent
1523 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1524 * is as a percentage. The file mem_busy_percent is used for this.
1525 * The SMU firmware computes a percentage of load based on the
1526 * aggregate activity level in the IP cores.
1528 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1529 struct device_attribute *attr,
1532 struct drm_device *ddev = dev_get_drvdata(dev);
1533 struct amdgpu_device *adev = drm_to_adev(ddev);
1537 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value);
1541 return sysfs_emit(buf, "%d\n", value);
1547 * The amdgpu driver provides a sysfs API for estimating how much data
1548 * has been received and sent by the GPU in the last second through PCIe.
1549 * The file pcie_bw is used for this.
1550 * The Perf counters count the number of received and sent messages and return
1551 * those values, as well as the maximum payload size of a PCIe packet (mps).
1552 * Note that it is not possible to easily and quickly obtain the size of each
1553 * packet transmitted, so we output the max payload size (mps) to allow for
1554 * quick estimation of the PCIe bandwidth usage
1556 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1557 struct device_attribute *attr,
1560 struct drm_device *ddev = dev_get_drvdata(dev);
1561 struct amdgpu_device *adev = drm_to_adev(ddev);
1562 uint64_t count0 = 0, count1 = 0;
1565 if (amdgpu_in_reset(adev))
1567 if (adev->in_suspend && !adev->in_runpm)
1570 if (adev->flags & AMD_IS_APU)
1573 if (!adev->asic_funcs->get_pcie_usage)
1576 ret = pm_runtime_get_sync(ddev->dev);
1578 pm_runtime_put_autosuspend(ddev->dev);
1582 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1584 pm_runtime_mark_last_busy(ddev->dev);
1585 pm_runtime_put_autosuspend(ddev->dev);
1587 return sysfs_emit(buf, "%llu %llu %i\n",
1588 count0, count1, pcie_get_mps(adev->pdev));
1594 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1595 * The file unique_id is used for this.
1596 * This will provide a Unique ID that will persist from machine to machine
1598 * NOTE: This will only work for GFX9 and newer. This file will be absent
1599 * on unsupported ASICs (GFX8 and older)
1601 static ssize_t amdgpu_get_unique_id(struct device *dev,
1602 struct device_attribute *attr,
1605 struct drm_device *ddev = dev_get_drvdata(dev);
1606 struct amdgpu_device *adev = drm_to_adev(ddev);
1608 if (amdgpu_in_reset(adev))
1610 if (adev->in_suspend && !adev->in_runpm)
1613 if (adev->unique_id)
1614 return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1620 * DOC: thermal_throttling_logging
1622 * Thermal throttling pulls down the clock frequency and thus the performance.
1623 * It's an useful mechanism to protect the chip from overheating. Since it
1624 * impacts performance, the user controls whether it is enabled and if so,
1625 * the log frequency.
1627 * Reading back the file shows you the status(enabled or disabled) and
1628 * the interval(in seconds) between each thermal logging.
1630 * Writing an integer to the file, sets a new logging interval, in seconds.
1631 * The value should be between 1 and 3600. If the value is less than 1,
1632 * thermal logging is disabled. Values greater than 3600 are ignored.
1634 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1635 struct device_attribute *attr,
1638 struct drm_device *ddev = dev_get_drvdata(dev);
1639 struct amdgpu_device *adev = drm_to_adev(ddev);
1641 return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
1642 adev_to_drm(adev)->unique,
1643 atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1644 adev->throttling_logging_rs.interval / HZ + 1);
1647 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1648 struct device_attribute *attr,
1652 struct drm_device *ddev = dev_get_drvdata(dev);
1653 struct amdgpu_device *adev = drm_to_adev(ddev);
1654 long throttling_logging_interval;
1655 unsigned long flags;
1658 ret = kstrtol(buf, 0, &throttling_logging_interval);
1662 if (throttling_logging_interval > 3600)
1665 if (throttling_logging_interval > 0) {
1666 raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1668 * Reset the ratelimit timer internals.
1669 * This can effectively restart the timer.
1671 adev->throttling_logging_rs.interval =
1672 (throttling_logging_interval - 1) * HZ;
1673 adev->throttling_logging_rs.begin = 0;
1674 adev->throttling_logging_rs.printed = 0;
1675 adev->throttling_logging_rs.missed = 0;
1676 raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1678 atomic_set(&adev->throttling_logging_enabled, 1);
1680 atomic_set(&adev->throttling_logging_enabled, 0);
1687 * DOC: apu_thermal_cap
1689 * The amdgpu driver provides a sysfs API for retrieving/updating thermal
1690 * limit temperature in millidegrees Celsius
1692 * Reading back the file shows you core limit value
1694 * Writing an integer to the file, sets a new thermal limit. The value
1695 * should be between 0 and 100. If the value is less than 0 or greater
1696 * than 100, then the write request will be ignored.
1698 static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
1699 struct device_attribute *attr,
1704 struct drm_device *ddev = dev_get_drvdata(dev);
1705 struct amdgpu_device *adev = drm_to_adev(ddev);
1707 ret = pm_runtime_get_sync(ddev->dev);
1709 pm_runtime_put_autosuspend(ddev->dev);
1713 ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
1715 size = sysfs_emit(buf, "%u\n", limit);
1717 size = sysfs_emit(buf, "failed to get thermal limit\n");
1719 pm_runtime_mark_last_busy(ddev->dev);
1720 pm_runtime_put_autosuspend(ddev->dev);
1725 static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
1726 struct device_attribute *attr,
1732 struct drm_device *ddev = dev_get_drvdata(dev);
1733 struct amdgpu_device *adev = drm_to_adev(ddev);
1735 ret = kstrtou32(buf, 10, &value);
1740 dev_err(dev, "Invalid argument !\n");
1744 ret = pm_runtime_get_sync(ddev->dev);
1746 pm_runtime_put_autosuspend(ddev->dev);
1750 ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
1752 dev_err(dev, "failed to update thermal limit\n");
1756 pm_runtime_mark_last_busy(ddev->dev);
1757 pm_runtime_put_autosuspend(ddev->dev);
1765 * The amdgpu driver provides a sysfs API for retrieving current gpu
1766 * metrics data. The file gpu_metrics is used for this. Reading the
1767 * file will dump all the current gpu metrics data.
1769 * These data include temperature, frequency, engines utilization,
1770 * power consume, throttler status, fan speed and cpu core statistics(
1771 * available for APU only). That's it will give a snapshot of all sensors
1774 static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1775 struct device_attribute *attr,
1778 struct drm_device *ddev = dev_get_drvdata(dev);
1779 struct amdgpu_device *adev = drm_to_adev(ddev);
1784 if (amdgpu_in_reset(adev))
1786 if (adev->in_suspend && !adev->in_runpm)
1789 ret = pm_runtime_get_sync(ddev->dev);
1791 pm_runtime_put_autosuspend(ddev->dev);
1795 size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1799 if (size >= PAGE_SIZE)
1800 size = PAGE_SIZE - 1;
1802 memcpy(buf, gpu_metrics, size);
1805 pm_runtime_mark_last_busy(ddev->dev);
1806 pm_runtime_put_autosuspend(ddev->dev);
1811 static int amdgpu_show_powershift_percent(struct device *dev,
1812 char *buf, enum amd_pp_sensors sensor)
1814 struct drm_device *ddev = dev_get_drvdata(dev);
1815 struct amdgpu_device *adev = drm_to_adev(ddev);
1819 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1820 if (r == -EOPNOTSUPP) {
1821 /* sensor not available on dGPU, try to read from APU */
1823 mutex_lock(&mgpu_info.mutex);
1824 for (i = 0; i < mgpu_info.num_gpu; i++) {
1825 if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1826 adev = mgpu_info.gpu_ins[i].adev;
1830 mutex_unlock(&mgpu_info.mutex);
1832 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1838 return sysfs_emit(buf, "%u%%\n", ss_power);
1842 * DOC: smartshift_apu_power
1844 * The amdgpu driver provides a sysfs API for reporting APU power
1845 * shift in percentage if platform supports smartshift. Value 0 means that
1846 * there is no powershift and values between [1-100] means that the power
1847 * is shifted to APU, the percentage of boost is with respect to APU power
1848 * limit on the platform.
1851 static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1854 return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE);
1858 * DOC: smartshift_dgpu_power
1860 * The amdgpu driver provides a sysfs API for reporting dGPU power
1861 * shift in percentage if platform supports smartshift. Value 0 means that
1862 * there is no powershift and values between [1-100] means that the power is
1863 * shifted to dGPU, the percentage of boost is with respect to dGPU power
1864 * limit on the platform.
1867 static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1870 return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE);
1874 * DOC: smartshift_bias
1876 * The amdgpu driver provides a sysfs API for reporting the
1877 * smartshift(SS2.0) bias level. The value ranges from -100 to 100
1878 * and the default is 0. -100 sets maximum preference to APU
1879 * and 100 sets max perference to dGPU.
1882 static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
1883 struct device_attribute *attr,
1888 r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
1893 static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
1894 struct device_attribute *attr,
1895 const char *buf, size_t count)
1897 struct drm_device *ddev = dev_get_drvdata(dev);
1898 struct amdgpu_device *adev = drm_to_adev(ddev);
1902 if (amdgpu_in_reset(adev))
1904 if (adev->in_suspend && !adev->in_runpm)
1907 r = pm_runtime_get_sync(ddev->dev);
1909 pm_runtime_put_autosuspend(ddev->dev);
1913 r = kstrtoint(buf, 10, &bias);
1917 if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
1918 bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
1919 else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
1920 bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
1922 amdgpu_smartshift_bias = bias;
1925 /* TODO: update bias level with SMU message */
1928 pm_runtime_mark_last_busy(ddev->dev);
1929 pm_runtime_put_autosuspend(ddev->dev);
1933 static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1934 uint32_t mask, enum amdgpu_device_attr_states *states)
1936 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1937 *states = ATTR_STATE_UNSUPPORTED;
1942 static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1943 uint32_t mask, enum amdgpu_device_attr_states *states)
1947 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1948 *states = ATTR_STATE_UNSUPPORTED;
1949 else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1951 *states = ATTR_STATE_UNSUPPORTED;
1952 else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1954 *states = ATTR_STATE_UNSUPPORTED;
1959 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
1960 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1961 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1962 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1963 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1964 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1965 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1966 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1967 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1968 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1969 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1970 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1971 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1972 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1973 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1974 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1975 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1976 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC),
1977 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC),
1978 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1979 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC),
1980 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1981 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1982 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC),
1983 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1984 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1985 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1986 AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1987 AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
1988 AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC,
1989 .attr_update = ss_power_attr_update),
1990 AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC,
1991 .attr_update = ss_power_attr_update),
1992 AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC,
1993 .attr_update = ss_bias_attr_update),
1996 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1997 uint32_t mask, enum amdgpu_device_attr_states *states)
1999 struct device_attribute *dev_attr = &attr->dev_attr;
2000 uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
2001 uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
2002 const char *attr_name = dev_attr->attr.name;
2004 if (!(attr->flags & mask)) {
2005 *states = ATTR_STATE_UNSUPPORTED;
2009 #define DEVICE_ATTR_IS(_name) (!strcmp(attr_name, #_name))
2011 if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2012 if (gc_ver < IP_VERSION(9, 0, 0))
2013 *states = ATTR_STATE_UNSUPPORTED;
2014 } else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2015 if (gc_ver < IP_VERSION(9, 0, 0) ||
2016 !amdgpu_device_has_display_hardware(adev))
2017 *states = ATTR_STATE_UNSUPPORTED;
2018 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2019 if (mp1_ver < IP_VERSION(10, 0, 0))
2020 *states = ATTR_STATE_UNSUPPORTED;
2021 } else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2022 *states = ATTR_STATE_UNSUPPORTED;
2023 if (amdgpu_dpm_is_overdrive_supported(adev))
2024 *states = ATTR_STATE_SUPPORTED;
2025 } else if (DEVICE_ATTR_IS(mem_busy_percent)) {
2026 if (adev->flags & AMD_IS_APU || gc_ver == IP_VERSION(9, 0, 1))
2027 *states = ATTR_STATE_UNSUPPORTED;
2028 } else if (DEVICE_ATTR_IS(pcie_bw)) {
2029 /* PCIe Perf counters won't work on APU nodes */
2030 if (adev->flags & AMD_IS_APU)
2031 *states = ATTR_STATE_UNSUPPORTED;
2032 } else if (DEVICE_ATTR_IS(unique_id)) {
2034 case IP_VERSION(9, 0, 1):
2035 case IP_VERSION(9, 4, 0):
2036 case IP_VERSION(9, 4, 1):
2037 case IP_VERSION(9, 4, 2):
2038 case IP_VERSION(9, 4, 3):
2039 case IP_VERSION(10, 3, 0):
2040 case IP_VERSION(11, 0, 0):
2041 case IP_VERSION(11, 0, 1):
2042 case IP_VERSION(11, 0, 2):
2043 *states = ATTR_STATE_SUPPORTED;
2046 *states = ATTR_STATE_UNSUPPORTED;
2048 } else if (DEVICE_ATTR_IS(pp_features)) {
2049 if ((adev->flags & AMD_IS_APU &&
2050 gc_ver != IP_VERSION(9, 4, 3)) ||
2051 gc_ver < IP_VERSION(9, 0, 0))
2052 *states = ATTR_STATE_UNSUPPORTED;
2053 } else if (DEVICE_ATTR_IS(gpu_metrics)) {
2054 if (gc_ver < IP_VERSION(9, 1, 0))
2055 *states = ATTR_STATE_UNSUPPORTED;
2056 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
2057 if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2058 gc_ver == IP_VERSION(10, 3, 0) ||
2059 gc_ver == IP_VERSION(10, 1, 2) ||
2060 gc_ver == IP_VERSION(11, 0, 0) ||
2061 gc_ver == IP_VERSION(11, 0, 2) ||
2062 gc_ver == IP_VERSION(11, 0, 3) ||
2063 gc_ver == IP_VERSION(9, 4, 3)))
2064 *states = ATTR_STATE_UNSUPPORTED;
2065 } else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
2066 if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2067 gc_ver == IP_VERSION(10, 3, 0) ||
2068 gc_ver == IP_VERSION(11, 0, 2) ||
2069 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2070 *states = ATTR_STATE_UNSUPPORTED;
2071 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
2072 if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2073 gc_ver == IP_VERSION(10, 3, 0) ||
2074 gc_ver == IP_VERSION(10, 1, 2) ||
2075 gc_ver == IP_VERSION(11, 0, 0) ||
2076 gc_ver == IP_VERSION(11, 0, 2) ||
2077 gc_ver == IP_VERSION(11, 0, 3) ||
2078 gc_ver == IP_VERSION(9, 4, 3)))
2079 *states = ATTR_STATE_UNSUPPORTED;
2080 } else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
2081 if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2082 gc_ver == IP_VERSION(10, 3, 0) ||
2083 gc_ver == IP_VERSION(11, 0, 2) ||
2084 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2085 *states = ATTR_STATE_UNSUPPORTED;
2086 } else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
2087 if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2088 *states = ATTR_STATE_UNSUPPORTED;
2089 else if (gc_ver == IP_VERSION(10, 3, 0) && amdgpu_sriov_vf(adev))
2090 *states = ATTR_STATE_UNSUPPORTED;
2094 case IP_VERSION(9, 4, 1):
2095 case IP_VERSION(9, 4, 2):
2096 /* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2097 if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2098 DEVICE_ATTR_IS(pp_dpm_socclk) ||
2099 DEVICE_ATTR_IS(pp_dpm_fclk)) {
2100 dev_attr->attr.mode &= ~S_IWUGO;
2101 dev_attr->store = NULL;
2104 case IP_VERSION(10, 3, 0):
2105 if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
2106 amdgpu_sriov_vf(adev)) {
2107 dev_attr->attr.mode &= ~0222;
2108 dev_attr->store = NULL;
2115 if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2116 /* SMU MP1 does not support dcefclk level setting */
2117 if (gc_ver >= IP_VERSION(10, 0, 0)) {
2118 dev_attr->attr.mode &= ~S_IWUGO;
2119 dev_attr->store = NULL;
2123 /* setting should not be allowed from VF if not in one VF mode */
2124 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev)) {
2125 dev_attr->attr.mode &= ~S_IWUGO;
2126 dev_attr->store = NULL;
2129 #undef DEVICE_ATTR_IS
2135 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2136 struct amdgpu_device_attr *attr,
2137 uint32_t mask, struct list_head *attr_list)
2140 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2141 struct amdgpu_device_attr_entry *attr_entry;
2142 struct device_attribute *dev_attr;
2145 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2146 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2151 dev_attr = &attr->dev_attr;
2152 name = dev_attr->attr.name;
2154 attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2156 ret = attr_update(adev, attr, mask, &attr_states);
2158 dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2163 if (attr_states == ATTR_STATE_UNSUPPORTED)
2166 ret = device_create_file(adev->dev, dev_attr);
2168 dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2172 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2176 attr_entry->attr = attr;
2177 INIT_LIST_HEAD(&attr_entry->entry);
2179 list_add_tail(&attr_entry->entry, attr_list);
2184 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2186 struct device_attribute *dev_attr = &attr->dev_attr;
2188 device_remove_file(adev->dev, dev_attr);
2191 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2192 struct list_head *attr_list);
2194 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2195 struct amdgpu_device_attr *attrs,
2198 struct list_head *attr_list)
2203 for (i = 0; i < counts; i++) {
2204 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2212 amdgpu_device_attr_remove_groups(adev, attr_list);
2217 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2218 struct list_head *attr_list)
2220 struct amdgpu_device_attr_entry *entry, *entry_tmp;
2222 if (list_empty(attr_list))
2225 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2226 amdgpu_device_attr_remove(adev, entry->attr);
2227 list_del(&entry->entry);
2232 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2233 struct device_attribute *attr,
2236 struct amdgpu_device *adev = dev_get_drvdata(dev);
2237 int channel = to_sensor_dev_attr(attr)->index;
2240 if (channel >= PP_TEMP_MAX)
2244 case PP_TEMP_JUNCTION:
2245 /* get current junction temperature */
2246 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2250 /* get current edge temperature */
2251 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2255 /* get current memory temperature */
2256 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2267 return sysfs_emit(buf, "%d\n", temp);
2270 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2271 struct device_attribute *attr,
2274 struct amdgpu_device *adev = dev_get_drvdata(dev);
2275 int hyst = to_sensor_dev_attr(attr)->index;
2279 temp = adev->pm.dpm.thermal.min_temp;
2281 temp = adev->pm.dpm.thermal.max_temp;
2283 return sysfs_emit(buf, "%d\n", temp);
2286 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2287 struct device_attribute *attr,
2290 struct amdgpu_device *adev = dev_get_drvdata(dev);
2291 int hyst = to_sensor_dev_attr(attr)->index;
2295 temp = adev->pm.dpm.thermal.min_hotspot_temp;
2297 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2299 return sysfs_emit(buf, "%d\n", temp);
2302 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2303 struct device_attribute *attr,
2306 struct amdgpu_device *adev = dev_get_drvdata(dev);
2307 int hyst = to_sensor_dev_attr(attr)->index;
2311 temp = adev->pm.dpm.thermal.min_mem_temp;
2313 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2315 return sysfs_emit(buf, "%d\n", temp);
2318 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2319 struct device_attribute *attr,
2322 int channel = to_sensor_dev_attr(attr)->index;
2324 if (channel >= PP_TEMP_MAX)
2327 return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2330 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2331 struct device_attribute *attr,
2334 struct amdgpu_device *adev = dev_get_drvdata(dev);
2335 int channel = to_sensor_dev_attr(attr)->index;
2338 if (channel >= PP_TEMP_MAX)
2342 case PP_TEMP_JUNCTION:
2343 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2346 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2349 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2353 return sysfs_emit(buf, "%d\n", temp);
2356 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2357 struct device_attribute *attr,
2360 struct amdgpu_device *adev = dev_get_drvdata(dev);
2364 if (amdgpu_in_reset(adev))
2366 if (adev->in_suspend && !adev->in_runpm)
2369 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2371 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2375 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2377 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2378 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2383 return sysfs_emit(buf, "%u\n", pwm_mode);
2386 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2387 struct device_attribute *attr,
2391 struct amdgpu_device *adev = dev_get_drvdata(dev);
2395 if (amdgpu_in_reset(adev))
2397 if (adev->in_suspend && !adev->in_runpm)
2400 err = kstrtoint(buf, 10, &value);
2404 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2406 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2410 ret = amdgpu_dpm_set_fan_control_mode(adev, value);
2412 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2413 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2421 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2422 struct device_attribute *attr,
2425 return sysfs_emit(buf, "%i\n", 0);
2428 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2429 struct device_attribute *attr,
2432 return sysfs_emit(buf, "%i\n", 255);
2435 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2436 struct device_attribute *attr,
2437 const char *buf, size_t count)
2439 struct amdgpu_device *adev = dev_get_drvdata(dev);
2444 if (amdgpu_in_reset(adev))
2446 if (adev->in_suspend && !adev->in_runpm)
2449 err = kstrtou32(buf, 10, &value);
2453 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2455 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2459 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2463 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2464 pr_info("manual fan speed control should be enabled first\n");
2469 err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
2472 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2473 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2481 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2482 struct device_attribute *attr,
2485 struct amdgpu_device *adev = dev_get_drvdata(dev);
2489 if (amdgpu_in_reset(adev))
2491 if (adev->in_suspend && !adev->in_runpm)
2494 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2496 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2500 err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2502 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2503 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2508 return sysfs_emit(buf, "%i\n", speed);
2511 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2512 struct device_attribute *attr,
2515 struct amdgpu_device *adev = dev_get_drvdata(dev);
2519 if (amdgpu_in_reset(adev))
2521 if (adev->in_suspend && !adev->in_runpm)
2524 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2526 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2530 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2532 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2533 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2538 return sysfs_emit(buf, "%i\n", speed);
2541 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2542 struct device_attribute *attr,
2545 struct amdgpu_device *adev = dev_get_drvdata(dev);
2549 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2555 return sysfs_emit(buf, "%d\n", min_rpm);
2558 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2559 struct device_attribute *attr,
2562 struct amdgpu_device *adev = dev_get_drvdata(dev);
2566 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2572 return sysfs_emit(buf, "%d\n", max_rpm);
2575 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2576 struct device_attribute *attr,
2579 struct amdgpu_device *adev = dev_get_drvdata(dev);
2583 if (amdgpu_in_reset(adev))
2585 if (adev->in_suspend && !adev->in_runpm)
2588 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2590 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2594 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2596 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2597 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2602 return sysfs_emit(buf, "%i\n", rpm);
2605 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2606 struct device_attribute *attr,
2607 const char *buf, size_t count)
2609 struct amdgpu_device *adev = dev_get_drvdata(dev);
2614 if (amdgpu_in_reset(adev))
2616 if (adev->in_suspend && !adev->in_runpm)
2619 err = kstrtou32(buf, 10, &value);
2623 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2625 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2629 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2633 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2638 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2641 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2642 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2650 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2651 struct device_attribute *attr,
2654 struct amdgpu_device *adev = dev_get_drvdata(dev);
2658 if (amdgpu_in_reset(adev))
2660 if (adev->in_suspend && !adev->in_runpm)
2663 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2665 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2669 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2671 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2672 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2677 return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2680 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2681 struct device_attribute *attr,
2685 struct amdgpu_device *adev = dev_get_drvdata(dev);
2690 if (amdgpu_in_reset(adev))
2692 if (adev->in_suspend && !adev->in_runpm)
2695 err = kstrtoint(buf, 10, &value);
2700 pwm_mode = AMD_FAN_CTRL_AUTO;
2701 else if (value == 1)
2702 pwm_mode = AMD_FAN_CTRL_MANUAL;
2706 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2708 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2712 err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2714 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2715 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2723 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2724 struct device_attribute *attr,
2727 struct amdgpu_device *adev = dev_get_drvdata(dev);
2731 /* get the voltage */
2732 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
2737 return sysfs_emit(buf, "%d\n", vddgfx);
2740 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2741 struct device_attribute *attr,
2744 return sysfs_emit(buf, "vddgfx\n");
2747 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2748 struct device_attribute *attr,
2751 struct amdgpu_device *adev = dev_get_drvdata(dev);
2755 /* only APUs have vddnb */
2756 if (!(adev->flags & AMD_IS_APU))
2759 /* get the voltage */
2760 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
2765 return sysfs_emit(buf, "%d\n", vddnb);
2768 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2769 struct device_attribute *attr,
2772 return sysfs_emit(buf, "vddnb\n");
2775 static unsigned int amdgpu_hwmon_get_power(struct device *dev,
2776 enum amd_pp_sensors sensor)
2778 struct amdgpu_device *adev = dev_get_drvdata(dev);
2783 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
2787 /* convert to microwatts */
2788 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2793 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2794 struct device_attribute *attr,
2799 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
2803 return sysfs_emit(buf, "%u\n", val);
2806 static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
2807 struct device_attribute *attr,
2812 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
2816 return sysfs_emit(buf, "%u\n", val);
2819 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2820 struct device_attribute *attr,
2823 return sysfs_emit(buf, "%i\n", 0);
2827 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2828 struct device_attribute *attr,
2830 enum pp_power_limit_level pp_limit_level)
2832 struct amdgpu_device *adev = dev_get_drvdata(dev);
2833 enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2838 if (amdgpu_in_reset(adev))
2840 if (adev->in_suspend && !adev->in_runpm)
2843 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2845 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2849 r = amdgpu_dpm_get_power_limit(adev, &limit,
2850 pp_limit_level, power_type);
2853 size = sysfs_emit(buf, "%u\n", limit * 1000000);
2855 size = sysfs_emit(buf, "\n");
2857 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2858 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2864 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
2865 struct device_attribute *attr,
2868 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
2872 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2873 struct device_attribute *attr,
2876 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
2880 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
2881 struct device_attribute *attr,
2884 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
2888 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
2889 struct device_attribute *attr,
2892 struct amdgpu_device *adev = dev_get_drvdata(dev);
2893 uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
2895 if (gc_ver == IP_VERSION(10, 3, 1))
2896 return sysfs_emit(buf, "%s\n",
2897 to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
2898 "fastPPT" : "slowPPT");
2900 return sysfs_emit(buf, "PPT\n");
2903 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2904 struct device_attribute *attr,
2908 struct amdgpu_device *adev = dev_get_drvdata(dev);
2909 int limit_type = to_sensor_dev_attr(attr)->index;
2913 if (amdgpu_in_reset(adev))
2915 if (adev->in_suspend && !adev->in_runpm)
2918 if (amdgpu_sriov_vf(adev))
2921 err = kstrtou32(buf, 10, &value);
2925 value = value / 1000000; /* convert to Watt */
2926 value |= limit_type << 24;
2928 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2930 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2934 err = amdgpu_dpm_set_power_limit(adev, value);
2936 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2937 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2945 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2946 struct device_attribute *attr,
2949 struct amdgpu_device *adev = dev_get_drvdata(dev);
2954 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2959 return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
2962 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2963 struct device_attribute *attr,
2966 return sysfs_emit(buf, "sclk\n");
2969 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2970 struct device_attribute *attr,
2973 struct amdgpu_device *adev = dev_get_drvdata(dev);
2978 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2983 return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
2986 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
2987 struct device_attribute *attr,
2990 return sysfs_emit(buf, "mclk\n");
2996 * The amdgpu driver exposes the following sensor interfaces:
2998 * - GPU temperature (via the on-die sensor)
3002 * - Northbridge voltage (APUs only)
3008 * - GPU gfx/compute engine clock
3010 * - GPU memory clock (dGPU only)
3012 * hwmon interfaces for GPU temperature:
3014 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3015 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
3017 * - temp[1-3]_label: temperature channel label
3018 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
3020 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3021 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3023 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3024 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3026 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3027 * - these are supported on SOC15 dGPUs only
3029 * hwmon interfaces for GPU voltage:
3031 * - in0_input: the voltage on the GPU in millivolts
3033 * - in1_input: the voltage on the Northbridge in millivolts
3035 * hwmon interfaces for GPU power:
3037 * - power1_average: average power used by the SoC in microWatts. On APUs this includes the CPU.
3039 * - power1_input: instantaneous power used by the SoC in microWatts. On APUs this includes the CPU.
3041 * - power1_cap_min: minimum cap supported in microWatts
3043 * - power1_cap_max: maximum cap supported in microWatts
3045 * - power1_cap: selected power cap in microWatts
3047 * hwmon interfaces for GPU fan:
3049 * - pwm1: pulse width modulation fan level (0-255)
3051 * - 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)
3053 * - pwm1_min: pulse width modulation fan control minimum level (0)
3055 * - pwm1_max: pulse width modulation fan control maximum level (255)
3057 * - fan1_min: a minimum value Unit: revolution/min (RPM)
3059 * - fan1_max: a maximum value Unit: revolution/max (RPM)
3061 * - fan1_input: fan speed in RPM
3063 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3065 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3067 * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
3068 * That will get the former one overridden.
3070 * hwmon interfaces for GPU clocks:
3072 * - freq1_input: the gfx/compute clock in hertz
3074 * - freq2_input: the memory clock in hertz
3076 * You can use hwmon tools like sensors to view this information on your system.
3080 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3081 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3082 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3083 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3084 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3085 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3086 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3087 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3088 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3089 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3090 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3091 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3092 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3093 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3094 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3095 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3096 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3097 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3098 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3099 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3100 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3101 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3102 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3103 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3104 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3105 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3106 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3107 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3108 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3109 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
3110 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3111 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3112 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3113 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3114 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3115 static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3116 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3117 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3118 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
3119 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3120 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3121 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3122 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3123 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3124 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3126 static struct attribute *hwmon_attributes[] = {
3127 &sensor_dev_attr_temp1_input.dev_attr.attr,
3128 &sensor_dev_attr_temp1_crit.dev_attr.attr,
3129 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3130 &sensor_dev_attr_temp2_input.dev_attr.attr,
3131 &sensor_dev_attr_temp2_crit.dev_attr.attr,
3132 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3133 &sensor_dev_attr_temp3_input.dev_attr.attr,
3134 &sensor_dev_attr_temp3_crit.dev_attr.attr,
3135 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3136 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
3137 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
3138 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
3139 &sensor_dev_attr_temp1_label.dev_attr.attr,
3140 &sensor_dev_attr_temp2_label.dev_attr.attr,
3141 &sensor_dev_attr_temp3_label.dev_attr.attr,
3142 &sensor_dev_attr_pwm1.dev_attr.attr,
3143 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3144 &sensor_dev_attr_pwm1_min.dev_attr.attr,
3145 &sensor_dev_attr_pwm1_max.dev_attr.attr,
3146 &sensor_dev_attr_fan1_input.dev_attr.attr,
3147 &sensor_dev_attr_fan1_min.dev_attr.attr,
3148 &sensor_dev_attr_fan1_max.dev_attr.attr,
3149 &sensor_dev_attr_fan1_target.dev_attr.attr,
3150 &sensor_dev_attr_fan1_enable.dev_attr.attr,
3151 &sensor_dev_attr_in0_input.dev_attr.attr,
3152 &sensor_dev_attr_in0_label.dev_attr.attr,
3153 &sensor_dev_attr_in1_input.dev_attr.attr,
3154 &sensor_dev_attr_in1_label.dev_attr.attr,
3155 &sensor_dev_attr_power1_average.dev_attr.attr,
3156 &sensor_dev_attr_power1_input.dev_attr.attr,
3157 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
3158 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
3159 &sensor_dev_attr_power1_cap.dev_attr.attr,
3160 &sensor_dev_attr_power1_cap_default.dev_attr.attr,
3161 &sensor_dev_attr_power1_label.dev_attr.attr,
3162 &sensor_dev_attr_power2_average.dev_attr.attr,
3163 &sensor_dev_attr_power2_cap_max.dev_attr.attr,
3164 &sensor_dev_attr_power2_cap_min.dev_attr.attr,
3165 &sensor_dev_attr_power2_cap.dev_attr.attr,
3166 &sensor_dev_attr_power2_cap_default.dev_attr.attr,
3167 &sensor_dev_attr_power2_label.dev_attr.attr,
3168 &sensor_dev_attr_freq1_input.dev_attr.attr,
3169 &sensor_dev_attr_freq1_label.dev_attr.attr,
3170 &sensor_dev_attr_freq2_input.dev_attr.attr,
3171 &sensor_dev_attr_freq2_label.dev_attr.attr,
3175 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3176 struct attribute *attr, int index)
3178 struct device *dev = kobj_to_dev(kobj);
3179 struct amdgpu_device *adev = dev_get_drvdata(dev);
3180 umode_t effective_mode = attr->mode;
3181 uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3184 /* under multi-vf mode, the hwmon attributes are all not supported */
3185 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3188 /* under pp one vf mode manage of hwmon attributes is not supported */
3189 if (amdgpu_sriov_is_pp_one_vf(adev))
3190 effective_mode &= ~S_IWUSR;
3192 /* Skip fan attributes if fan is not present */
3193 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3194 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3195 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3196 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3197 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3198 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3199 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3200 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3201 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3204 /* Skip fan attributes on APU */
3205 if ((adev->flags & AMD_IS_APU) &&
3206 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3207 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3208 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3209 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3210 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3211 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3212 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3213 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3214 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3217 /* Skip crit temp on APU */
3218 if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
3219 (gc_ver == IP_VERSION(9, 4, 3))) &&
3220 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3221 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3224 /* Skip limit attributes if DPM is not enabled */
3225 if (!adev->pm.dpm_enabled &&
3226 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3227 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3228 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3229 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3230 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3231 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3232 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3233 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3234 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3235 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3236 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3239 /* mask fan attributes if we have no bindings for this asic to expose */
3240 if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3241 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3242 ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3243 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3244 effective_mode &= ~S_IRUGO;
3246 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3247 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3248 ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3249 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3250 effective_mode &= ~S_IWUSR;
3252 /* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
3253 if (((adev->family == AMDGPU_FAMILY_SI) ||
3254 ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)) &&
3255 (gc_ver != IP_VERSION(9, 4, 3)))) &&
3256 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3257 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
3258 attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
3259 attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3262 /* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
3263 if (((adev->family == AMDGPU_FAMILY_SI) ||
3264 ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3265 (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3268 /* not all products support both average and instantaneous */
3269 if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
3270 amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
3272 if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
3273 amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
3276 /* hide max/min values if we can't both query and manage the fan */
3277 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3278 (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3279 (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3280 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3281 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3282 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3285 if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3286 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3287 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3288 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3291 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
3292 adev->family == AMDGPU_FAMILY_KV || /* not implemented yet */
3293 (gc_ver == IP_VERSION(9, 4, 3))) &&
3294 (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3295 attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3298 /* only APUs other than gc 9,4,3 have vddnb */
3299 if ((!(adev->flags & AMD_IS_APU) || (gc_ver == IP_VERSION(9, 4, 3))) &&
3300 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3301 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3304 /* no mclk on APUs other than gc 9,4,3*/
3305 if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
3306 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3307 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3310 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3311 (gc_ver != IP_VERSION(9, 4, 3)) &&
3312 (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3313 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3314 attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3315 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3316 attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
3317 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
3320 /* hotspot temperature for gc 9,4,3*/
3321 if ((gc_ver == IP_VERSION(9, 4, 3)) &&
3322 (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
3323 attr == &sensor_dev_attr_temp1_label.dev_attr.attr))
3326 /* only SOC15 dGPUs support hotspot and mem temperatures */
3327 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0) ||
3328 (gc_ver == IP_VERSION(9, 4, 3))) &&
3329 (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3330 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3331 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3332 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3333 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
3336 /* only Vangogh has fast PPT limit and power labels */
3337 if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3338 (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3339 attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3340 attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3341 attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
3342 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3343 attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3346 return effective_mode;
3349 static const struct attribute_group hwmon_attrgroup = {
3350 .attrs = hwmon_attributes,
3351 .is_visible = hwmon_attributes_visible,
3354 static const struct attribute_group *hwmon_groups[] = {
3359 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3364 if (adev->pm.sysfs_initialized)
3367 INIT_LIST_HEAD(&adev->pm.pm_attr_list);
3369 if (adev->pm.dpm_enabled == 0)
3372 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3375 if (IS_ERR(adev->pm.int_hwmon_dev)) {
3376 ret = PTR_ERR(adev->pm.int_hwmon_dev);
3378 "Unable to register hwmon device: %d\n", ret);
3382 switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3383 case SRIOV_VF_MODE_ONE_VF:
3384 mask = ATTR_FLAG_ONEVF;
3386 case SRIOV_VF_MODE_MULTI_VF:
3389 case SRIOV_VF_MODE_BARE_METAL:
3391 mask = ATTR_FLAG_MASK_ALL;
3395 ret = amdgpu_device_attr_create_groups(adev,
3396 amdgpu_device_attrs,
3397 ARRAY_SIZE(amdgpu_device_attrs),
3399 &adev->pm.pm_attr_list);
3403 adev->pm.sysfs_initialized = true;
3408 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3410 if (adev->pm.int_hwmon_dev)
3411 hwmon_device_unregister(adev->pm.int_hwmon_dev);
3413 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3419 #if defined(CONFIG_DEBUG_FS)
3421 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3422 struct amdgpu_device *adev)
3427 uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
3429 if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
3430 p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
3433 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3434 (void *)p_val, &size)) {
3435 for (i = 0; i < num_cpu_cores; i++)
3436 seq_printf(m, "\t%u MHz (CPU%d)\n",
3444 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3446 uint32_t mp1_ver = adev->ip_versions[MP1_HWIP][0];
3447 uint32_t gc_ver = adev->ip_versions[GC_HWIP][0];
3449 uint64_t value64 = 0;
3454 size = sizeof(value);
3455 seq_printf(m, "GFX Clocks and Power:\n");
3457 amdgpu_debugfs_prints_cpu_info(m, adev);
3459 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3460 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3461 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3462 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3463 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3464 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3465 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3466 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3467 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3468 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3469 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3470 seq_printf(m, "\t%u mV (VDDNB)\n", value);
3471 size = sizeof(uint32_t);
3472 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size))
3473 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3474 size = sizeof(uint32_t);
3475 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size))
3476 seq_printf(m, "\t%u.%u W (current GPU)\n", query >> 8, query & 0xff);
3477 size = sizeof(value);
3478 seq_printf(m, "\n");
3481 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3482 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3485 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3486 seq_printf(m, "GPU Load: %u %%\n", value);
3488 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3489 seq_printf(m, "MEM Load: %u %%\n", value);
3491 seq_printf(m, "\n");
3493 /* SMC feature mask */
3494 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3495 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3497 /* ASICs greater than CHIP_VEGA20 supports these sensors */
3498 if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
3500 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3502 seq_printf(m, "VCN: Disabled\n");
3504 seq_printf(m, "VCN: Enabled\n");
3505 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3506 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3507 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3508 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3511 seq_printf(m, "\n");
3514 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3516 seq_printf(m, "UVD: Disabled\n");
3518 seq_printf(m, "UVD: Enabled\n");
3519 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3520 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3521 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3522 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3525 seq_printf(m, "\n");
3528 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3530 seq_printf(m, "VCE: Disabled\n");
3532 seq_printf(m, "VCE: Enabled\n");
3533 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3534 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3542 static const struct cg_flag_name clocks[] = {
3543 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
3544 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
3545 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
3546 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
3547 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
3548 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
3549 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
3550 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
3551 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
3552 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
3553 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
3554 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
3555 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
3556 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
3557 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
3558 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
3559 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
3560 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
3561 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
3562 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
3563 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
3564 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
3565 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
3566 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
3567 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
3568 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
3569 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
3570 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
3571 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
3572 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
3573 {AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
3574 {AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
3575 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
3576 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
3580 static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
3584 for (i = 0; clocks[i].flag; i++)
3585 seq_printf(m, "\t%s: %s\n", clocks[i].name,
3586 (flags & clocks[i].flag) ? "On" : "Off");
3589 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
3591 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
3592 struct drm_device *dev = adev_to_drm(adev);
3596 if (amdgpu_in_reset(adev))
3598 if (adev->in_suspend && !adev->in_runpm)
3601 r = pm_runtime_get_sync(dev->dev);
3603 pm_runtime_put_autosuspend(dev->dev);
3607 if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
3608 r = amdgpu_debugfs_pm_info_pp(m, adev);
3613 amdgpu_device_ip_get_clockgating_state(adev, &flags);
3615 seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
3616 amdgpu_parse_cg_state(m, flags);
3617 seq_printf(m, "\n");
3620 pm_runtime_mark_last_busy(dev->dev);
3621 pm_runtime_put_autosuspend(dev->dev);
3626 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
3629 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
3631 * Reads debug memory region allocated to PMFW
3633 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
3634 size_t size, loff_t *pos)
3636 struct amdgpu_device *adev = file_inode(f)->i_private;
3637 size_t smu_prv_buf_size;
3641 if (amdgpu_in_reset(adev))
3643 if (adev->in_suspend && !adev->in_runpm)
3646 ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
3650 if (!smu_prv_buf || !smu_prv_buf_size)
3653 return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
3657 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
3658 .owner = THIS_MODULE,
3659 .open = simple_open,
3660 .read = amdgpu_pm_prv_buffer_read,
3661 .llseek = default_llseek,
3666 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3668 #if defined(CONFIG_DEBUG_FS)
3669 struct drm_minor *minor = adev_to_drm(adev)->primary;
3670 struct dentry *root = minor->debugfs_root;
3672 if (!adev->pm.dpm_enabled)
3675 debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
3676 &amdgpu_debugfs_pm_info_fops);
3678 if (adev->pm.smu_prv_buffer_size > 0)
3679 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
3681 &amdgpu_debugfs_pm_prv_buffer_fops,
3682 adev->pm.smu_prv_buffer_size);
3684 amdgpu_dpm_stb_debug_fs_init(adev);