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 #define MAX_NUM_OF_FEATURES_PER_SUBSET 8
39 #define MAX_NUM_OF_SUBSETS 8
41 #define DEVICE_ATTR_IS(_name) (attr_id == device_attr_id__##_name)
44 struct kobj_attribute attribute;
45 struct list_head entry;
50 struct list_head entry;
51 struct list_head attribute;
55 struct od_feature_ops {
56 umode_t (*is_visible)(struct amdgpu_device *adev);
57 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
59 ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
60 const char *buf, size_t count);
63 struct od_feature_item {
65 struct od_feature_ops ops;
68 struct od_feature_container {
70 struct od_feature_ops ops;
71 struct od_feature_item sub_feature[MAX_NUM_OF_FEATURES_PER_SUBSET];
74 struct od_feature_set {
75 struct od_feature_container containers[MAX_NUM_OF_SUBSETS];
78 static const struct hwmon_temp_label {
79 enum PP_HWMON_TEMP channel;
82 {PP_TEMP_EDGE, "edge"},
83 {PP_TEMP_JUNCTION, "junction"},
87 const char * const amdgpu_pp_profile_name[] = {
101 * DOC: power_dpm_state
103 * The power_dpm_state file is a legacy interface and is only provided for
104 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
105 * certain power related parameters. The file power_dpm_state is used for this.
106 * It accepts the following arguments:
116 * On older GPUs, the vbios provided a special power state for battery
117 * operation. Selecting battery switched to this state. This is no
118 * longer provided on newer GPUs so the option does nothing in that case.
122 * On older GPUs, the vbios provided a special power state for balanced
123 * operation. Selecting balanced switched to this state. This is no
124 * longer provided on newer GPUs so the option does nothing in that case.
128 * On older GPUs, the vbios provided a special power state for performance
129 * operation. Selecting performance switched to this state. This is no
130 * longer provided on newer GPUs so the option does nothing in that case.
134 static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
135 struct device_attribute *attr,
138 struct drm_device *ddev = dev_get_drvdata(dev);
139 struct amdgpu_device *adev = drm_to_adev(ddev);
140 enum amd_pm_state_type pm;
143 if (amdgpu_in_reset(adev))
145 if (adev->in_suspend && !adev->in_runpm)
148 ret = pm_runtime_get_sync(ddev->dev);
150 pm_runtime_put_autosuspend(ddev->dev);
154 amdgpu_dpm_get_current_power_state(adev, &pm);
156 pm_runtime_mark_last_busy(ddev->dev);
157 pm_runtime_put_autosuspend(ddev->dev);
159 return sysfs_emit(buf, "%s\n",
160 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
161 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
164 static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
165 struct device_attribute *attr,
169 struct drm_device *ddev = dev_get_drvdata(dev);
170 struct amdgpu_device *adev = drm_to_adev(ddev);
171 enum amd_pm_state_type state;
174 if (amdgpu_in_reset(adev))
176 if (adev->in_suspend && !adev->in_runpm)
179 if (strncmp("battery", buf, strlen("battery")) == 0)
180 state = POWER_STATE_TYPE_BATTERY;
181 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
182 state = POWER_STATE_TYPE_BALANCED;
183 else if (strncmp("performance", buf, strlen("performance")) == 0)
184 state = POWER_STATE_TYPE_PERFORMANCE;
188 ret = pm_runtime_get_sync(ddev->dev);
190 pm_runtime_put_autosuspend(ddev->dev);
194 amdgpu_dpm_set_power_state(adev, state);
196 pm_runtime_mark_last_busy(ddev->dev);
197 pm_runtime_put_autosuspend(ddev->dev);
204 * DOC: power_dpm_force_performance_level
206 * The amdgpu driver provides a sysfs API for adjusting certain power
207 * related parameters. The file power_dpm_force_performance_level is
208 * used for this. It accepts the following arguments:
228 * When auto is selected, the driver will attempt to dynamically select
229 * the optimal power profile for current conditions in the driver.
233 * When low is selected, the clocks are forced to the lowest power state.
237 * When high is selected, the clocks are forced to the highest power state.
241 * When manual is selected, the user can manually adjust which power states
242 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
243 * and pp_dpm_pcie files and adjust the power state transition heuristics
244 * via the pp_power_profile_mode sysfs file.
251 * When the profiling modes are selected, clock and power gating are
252 * disabled and the clocks are set for different profiling cases. This
253 * mode is recommended for profiling specific work loads where you do
254 * not want clock or power gating for clock fluctuation to interfere
255 * with your results. profile_standard sets the clocks to a fixed clock
256 * level which varies from asic to asic. profile_min_sclk forces the sclk
257 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
258 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
262 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
263 struct device_attribute *attr,
266 struct drm_device *ddev = dev_get_drvdata(dev);
267 struct amdgpu_device *adev = drm_to_adev(ddev);
268 enum amd_dpm_forced_level level = 0xff;
271 if (amdgpu_in_reset(adev))
273 if (adev->in_suspend && !adev->in_runpm)
276 ret = pm_runtime_get_sync(ddev->dev);
278 pm_runtime_put_autosuspend(ddev->dev);
282 level = amdgpu_dpm_get_performance_level(adev);
284 pm_runtime_mark_last_busy(ddev->dev);
285 pm_runtime_put_autosuspend(ddev->dev);
287 return sysfs_emit(buf, "%s\n",
288 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
289 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
290 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
291 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
292 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
293 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
294 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
295 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
296 (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
300 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
301 struct device_attribute *attr,
305 struct drm_device *ddev = dev_get_drvdata(dev);
306 struct amdgpu_device *adev = drm_to_adev(ddev);
307 enum amd_dpm_forced_level level;
310 if (amdgpu_in_reset(adev))
312 if (adev->in_suspend && !adev->in_runpm)
315 if (strncmp("low", buf, strlen("low")) == 0) {
316 level = AMD_DPM_FORCED_LEVEL_LOW;
317 } else if (strncmp("high", buf, strlen("high")) == 0) {
318 level = AMD_DPM_FORCED_LEVEL_HIGH;
319 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
320 level = AMD_DPM_FORCED_LEVEL_AUTO;
321 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
322 level = AMD_DPM_FORCED_LEVEL_MANUAL;
323 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
324 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
325 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
326 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
327 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
328 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
329 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
330 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
331 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
332 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
333 } else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
334 level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
339 ret = pm_runtime_get_sync(ddev->dev);
341 pm_runtime_put_autosuspend(ddev->dev);
345 mutex_lock(&adev->pm.stable_pstate_ctx_lock);
346 if (amdgpu_dpm_force_performance_level(adev, level)) {
347 pm_runtime_mark_last_busy(ddev->dev);
348 pm_runtime_put_autosuspend(ddev->dev);
349 mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
352 /* override whatever a user ctx may have set */
353 adev->pm.stable_pstate_ctx = NULL;
354 mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
356 pm_runtime_mark_last_busy(ddev->dev);
357 pm_runtime_put_autosuspend(ddev->dev);
362 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
363 struct device_attribute *attr,
366 struct drm_device *ddev = dev_get_drvdata(dev);
367 struct amdgpu_device *adev = drm_to_adev(ddev);
368 struct pp_states_info data;
372 if (amdgpu_in_reset(adev))
374 if (adev->in_suspend && !adev->in_runpm)
377 ret = pm_runtime_get_sync(ddev->dev);
379 pm_runtime_put_autosuspend(ddev->dev);
383 if (amdgpu_dpm_get_pp_num_states(adev, &data))
384 memset(&data, 0, sizeof(data));
386 pm_runtime_mark_last_busy(ddev->dev);
387 pm_runtime_put_autosuspend(ddev->dev);
389 buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
390 for (i = 0; i < data.nums; i++)
391 buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
392 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
393 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
394 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
395 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
400 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
401 struct device_attribute *attr,
404 struct drm_device *ddev = dev_get_drvdata(dev);
405 struct amdgpu_device *adev = drm_to_adev(ddev);
406 struct pp_states_info data = {0};
407 enum amd_pm_state_type pm = 0;
410 if (amdgpu_in_reset(adev))
412 if (adev->in_suspend && !adev->in_runpm)
415 ret = pm_runtime_get_sync(ddev->dev);
417 pm_runtime_put_autosuspend(ddev->dev);
421 amdgpu_dpm_get_current_power_state(adev, &pm);
423 ret = amdgpu_dpm_get_pp_num_states(adev, &data);
425 pm_runtime_mark_last_busy(ddev->dev);
426 pm_runtime_put_autosuspend(ddev->dev);
431 for (i = 0; i < data.nums; i++) {
432 if (pm == data.states[i])
439 return sysfs_emit(buf, "%d\n", i);
442 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
443 struct device_attribute *attr,
446 struct drm_device *ddev = dev_get_drvdata(dev);
447 struct amdgpu_device *adev = drm_to_adev(ddev);
449 if (amdgpu_in_reset(adev))
451 if (adev->in_suspend && !adev->in_runpm)
454 if (adev->pm.pp_force_state_enabled)
455 return amdgpu_get_pp_cur_state(dev, attr, buf);
457 return sysfs_emit(buf, "\n");
460 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
461 struct device_attribute *attr,
465 struct drm_device *ddev = dev_get_drvdata(dev);
466 struct amdgpu_device *adev = drm_to_adev(ddev);
467 enum amd_pm_state_type state = 0;
468 struct pp_states_info data;
472 if (amdgpu_in_reset(adev))
474 if (adev->in_suspend && !adev->in_runpm)
477 adev->pm.pp_force_state_enabled = false;
479 if (strlen(buf) == 1)
482 ret = kstrtoul(buf, 0, &idx);
483 if (ret || idx >= ARRAY_SIZE(data.states))
486 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
488 ret = pm_runtime_get_sync(ddev->dev);
490 pm_runtime_put_autosuspend(ddev->dev);
494 ret = amdgpu_dpm_get_pp_num_states(adev, &data);
498 state = data.states[idx];
500 /* only set user selected power states */
501 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
502 state != POWER_STATE_TYPE_DEFAULT) {
503 ret = amdgpu_dpm_dispatch_task(adev,
504 AMD_PP_TASK_ENABLE_USER_STATE, &state);
508 adev->pm.pp_force_state_enabled = true;
511 pm_runtime_mark_last_busy(ddev->dev);
512 pm_runtime_put_autosuspend(ddev->dev);
517 pm_runtime_mark_last_busy(ddev->dev);
518 pm_runtime_put_autosuspend(ddev->dev);
525 * The amdgpu driver provides a sysfs API for uploading new powerplay
526 * tables. The file pp_table is used for this. Reading the file
527 * will dump the current power play table. Writing to the file
528 * will attempt to upload a new powerplay table and re-initialize
529 * powerplay using that new table.
533 static ssize_t amdgpu_get_pp_table(struct device *dev,
534 struct device_attribute *attr,
537 struct drm_device *ddev = dev_get_drvdata(dev);
538 struct amdgpu_device *adev = drm_to_adev(ddev);
542 if (amdgpu_in_reset(adev))
544 if (adev->in_suspend && !adev->in_runpm)
547 ret = pm_runtime_get_sync(ddev->dev);
549 pm_runtime_put_autosuspend(ddev->dev);
553 size = amdgpu_dpm_get_pp_table(adev, &table);
555 pm_runtime_mark_last_busy(ddev->dev);
556 pm_runtime_put_autosuspend(ddev->dev);
561 if (size >= PAGE_SIZE)
562 size = PAGE_SIZE - 1;
564 memcpy(buf, table, size);
569 static ssize_t amdgpu_set_pp_table(struct device *dev,
570 struct device_attribute *attr,
574 struct drm_device *ddev = dev_get_drvdata(dev);
575 struct amdgpu_device *adev = drm_to_adev(ddev);
578 if (amdgpu_in_reset(adev))
580 if (adev->in_suspend && !adev->in_runpm)
583 ret = pm_runtime_get_sync(ddev->dev);
585 pm_runtime_put_autosuspend(ddev->dev);
589 ret = amdgpu_dpm_set_pp_table(adev, buf, count);
591 pm_runtime_mark_last_busy(ddev->dev);
592 pm_runtime_put_autosuspend(ddev->dev);
601 * DOC: pp_od_clk_voltage
603 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
604 * in each power level within a power state. The pp_od_clk_voltage is used for
607 * Note that the actual memory controller clock rate are exposed, not
608 * the effective memory clock of the DRAMs. To translate it, use the
611 * Clock conversion (Mhz):
613 * HBM: effective_memory_clock = memory_controller_clock * 1
615 * G5: effective_memory_clock = memory_controller_clock * 1
617 * G6: effective_memory_clock = memory_controller_clock * 2
619 * DRAM data rate (MT/s):
621 * HBM: effective_memory_clock * 2 = data_rate
623 * G5: effective_memory_clock * 4 = data_rate
625 * G6: effective_memory_clock * 8 = data_rate
629 * data_rate * vram_bit_width / 8 = memory_bandwidth
635 * memory_controller_clock = 1750 Mhz
637 * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
639 * data rate = 1750 * 4 = 7000 MT/s
641 * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
645 * memory_controller_clock = 875 Mhz
647 * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
649 * data rate = 1750 * 8 = 14000 MT/s
651 * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
653 * < For Vega10 and previous ASICs >
655 * Reading the file will display:
657 * - a list of engine clock levels and voltages labeled OD_SCLK
659 * - a list of memory clock levels and voltages labeled OD_MCLK
661 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
663 * To manually adjust these settings, first select manual using
664 * power_dpm_force_performance_level. Enter a new value for each
665 * level by writing a string that contains "s/m level clock voltage" to
666 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
667 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
668 * 810 mV. When you have edited all of the states as needed, write
669 * "c" (commit) to the file to commit your changes. If you want to reset to the
670 * default power levels, write "r" (reset) to the file to reset them.
673 * < For Vega20 and newer ASICs >
675 * Reading the file will display:
677 * - minimum and maximum engine clock labeled OD_SCLK
679 * - minimum(not available for Vega20 and Navi1x) and maximum memory
680 * clock labeled OD_MCLK
682 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
683 * They can be used to calibrate the sclk voltage curve. This is
684 * available for Vega20 and NV1X.
686 * - voltage offset(in mV) applied on target voltage calculation.
687 * This is available for Sienna Cichlid, Navy Flounder, Dimgrey
688 * Cavefish and some later SMU13 ASICs. For these ASICs, the target
689 * voltage calculation can be illustrated by "voltage = voltage
690 * calculated from v/f curve + overdrive vddgfx offset"
692 * - a list of valid ranges for sclk, mclk, voltage curve points
693 * or voltage offset labeled OD_RANGE
697 * Reading the file will display:
699 * - minimum and maximum engine clock labeled OD_SCLK
701 * - a list of valid ranges for sclk labeled OD_RANGE
705 * Reading the file will display:
707 * - minimum and maximum engine clock labeled OD_SCLK
708 * - minimum and maximum core clocks labeled OD_CCLK
710 * - a list of valid ranges for sclk and cclk labeled OD_RANGE
712 * To manually adjust these settings:
714 * - First select manual using power_dpm_force_performance_level
716 * - For clock frequency setting, enter a new value by writing a
717 * string that contains "s/m index clock" to the file. The index
718 * should be 0 if to set minimum clock. And 1 if to set maximum
719 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
720 * "m 1 800" will update maximum mclk to be 800Mhz. For core
721 * clocks on VanGogh, the string contains "p core index clock".
722 * E.g., "p 2 0 800" would set the minimum core clock on core
725 * For sclk voltage curve supported by Vega20 and NV1X, enter the new
726 * values by writing a string that contains "vc point clock voltage"
727 * to the file. The points are indexed by 0, 1 and 2. E.g., "vc 0 300
728 * 600" will update point1 with clock set as 300Mhz and voltage as 600mV.
729 * "vc 2 1000 1000" will update point3 with clock set as 1000Mhz and
732 * For voltage offset supported by Sienna Cichlid, Navy Flounder, Dimgrey
733 * Cavefish and some later SMU13 ASICs, enter the new value by writing a
734 * string that contains "vo offset". E.g., "vo -10" will update the extra
735 * voltage offset applied to the whole v/f curve line as -10mv.
737 * - When you have edited all of the states as needed, write "c" (commit)
738 * to the file to commit your changes
740 * - If you want to reset to the default power levels, write "r" (reset)
741 * to the file to reset them
745 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
746 struct device_attribute *attr,
750 struct drm_device *ddev = dev_get_drvdata(dev);
751 struct amdgpu_device *adev = drm_to_adev(ddev);
753 uint32_t parameter_size = 0;
758 const char delimiter[3] = {' ', '\n', '\0'};
761 if (amdgpu_in_reset(adev))
763 if (adev->in_suspend && !adev->in_runpm)
766 if (count > 127 || count == 0)
770 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
771 else if (*buf == 'p')
772 type = PP_OD_EDIT_CCLK_VDDC_TABLE;
773 else if (*buf == 'm')
774 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
775 else if (*buf == 'r')
776 type = PP_OD_RESTORE_DEFAULT_TABLE;
777 else if (*buf == 'c')
778 type = PP_OD_COMMIT_DPM_TABLE;
779 else if (!strncmp(buf, "vc", 2))
780 type = PP_OD_EDIT_VDDC_CURVE;
781 else if (!strncmp(buf, "vo", 2))
782 type = PP_OD_EDIT_VDDGFX_OFFSET;
786 memcpy(buf_cpy, buf, count);
791 if ((type == PP_OD_EDIT_VDDC_CURVE) ||
792 (type == PP_OD_EDIT_VDDGFX_OFFSET))
794 while (isspace(*++tmp_str));
796 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
797 if (strlen(sub_str) == 0)
799 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
807 while (isspace(*tmp_str))
811 ret = pm_runtime_get_sync(ddev->dev);
813 pm_runtime_put_autosuspend(ddev->dev);
817 if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
823 if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
824 parameter, parameter_size))
827 if (type == PP_OD_COMMIT_DPM_TABLE) {
828 if (amdgpu_dpm_dispatch_task(adev,
829 AMD_PP_TASK_READJUST_POWER_STATE,
834 pm_runtime_mark_last_busy(ddev->dev);
835 pm_runtime_put_autosuspend(ddev->dev);
840 pm_runtime_mark_last_busy(ddev->dev);
841 pm_runtime_put_autosuspend(ddev->dev);
845 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
846 struct device_attribute *attr,
849 struct drm_device *ddev = dev_get_drvdata(dev);
850 struct amdgpu_device *adev = drm_to_adev(ddev);
853 enum pp_clock_type od_clocks[6] = {
863 if (amdgpu_in_reset(adev))
865 if (adev->in_suspend && !adev->in_runpm)
868 ret = pm_runtime_get_sync(ddev->dev);
870 pm_runtime_put_autosuspend(ddev->dev);
874 for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
875 ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
879 if (ret == -ENOENT) {
880 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
881 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
882 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
883 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
884 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
885 size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
889 size = sysfs_emit(buf, "\n");
891 pm_runtime_mark_last_busy(ddev->dev);
892 pm_runtime_put_autosuspend(ddev->dev);
900 * The amdgpu driver provides a sysfs API for adjusting what powerplay
901 * features to be enabled. The file pp_features is used for this. And
902 * this is only available for Vega10 and later dGPUs.
904 * Reading back the file will show you the followings:
905 * - Current ppfeature masks
906 * - List of the all supported powerplay features with their naming,
907 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
909 * To manually enable or disable a specific feature, just set or clear
910 * the corresponding bit from original ppfeature masks and input the
911 * new ppfeature masks.
913 static ssize_t amdgpu_set_pp_features(struct device *dev,
914 struct device_attribute *attr,
918 struct drm_device *ddev = dev_get_drvdata(dev);
919 struct amdgpu_device *adev = drm_to_adev(ddev);
920 uint64_t featuremask;
923 if (amdgpu_in_reset(adev))
925 if (adev->in_suspend && !adev->in_runpm)
928 ret = kstrtou64(buf, 0, &featuremask);
932 ret = pm_runtime_get_sync(ddev->dev);
934 pm_runtime_put_autosuspend(ddev->dev);
938 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
940 pm_runtime_mark_last_busy(ddev->dev);
941 pm_runtime_put_autosuspend(ddev->dev);
949 static ssize_t amdgpu_get_pp_features(struct device *dev,
950 struct device_attribute *attr,
953 struct drm_device *ddev = dev_get_drvdata(dev);
954 struct amdgpu_device *adev = drm_to_adev(ddev);
958 if (amdgpu_in_reset(adev))
960 if (adev->in_suspend && !adev->in_runpm)
963 ret = pm_runtime_get_sync(ddev->dev);
965 pm_runtime_put_autosuspend(ddev->dev);
969 size = amdgpu_dpm_get_ppfeature_status(adev, buf);
971 size = sysfs_emit(buf, "\n");
973 pm_runtime_mark_last_busy(ddev->dev);
974 pm_runtime_put_autosuspend(ddev->dev);
980 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
982 * The amdgpu driver provides a sysfs API for adjusting what power levels
983 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
984 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
987 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
988 * Vega10 and later ASICs.
989 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
991 * Reading back the files will show you the available power levels within
992 * the power state and the clock information for those levels. If deep sleep is
993 * applied to a clock, the level will be denoted by a special level 'S:'
1003 * To manually adjust these states, first select manual using
1004 * power_dpm_force_performance_level.
1005 * Secondly, enter a new value for each level by inputing a string that
1006 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1009 * .. code-block:: bash
1011 * echo "4 5 6" > pp_dpm_sclk
1013 * will enable sclk levels 4, 5, and 6.
1015 * NOTE: change to the dcefclk max dpm level is not supported now
1018 static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
1019 enum pp_clock_type type,
1022 struct drm_device *ddev = dev_get_drvdata(dev);
1023 struct amdgpu_device *adev = drm_to_adev(ddev);
1027 if (amdgpu_in_reset(adev))
1029 if (adev->in_suspend && !adev->in_runpm)
1032 ret = pm_runtime_get_sync(ddev->dev);
1034 pm_runtime_put_autosuspend(ddev->dev);
1038 ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
1040 size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1043 size = sysfs_emit(buf, "\n");
1045 pm_runtime_mark_last_busy(ddev->dev);
1046 pm_runtime_put_autosuspend(ddev->dev);
1052 * Worst case: 32 bits individually specified, in octal at 12 characters
1053 * per line (+1 for \n).
1055 #define AMDGPU_MASK_BUF_MAX (32 * 13)
1057 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1060 unsigned long level;
1061 char *sub_str = NULL;
1063 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1064 const char delimiter[3] = {' ', '\n', '\0'};
1069 bytes = min(count, sizeof(buf_cpy) - 1);
1070 memcpy(buf_cpy, buf, bytes);
1071 buf_cpy[bytes] = '\0';
1073 while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1074 if (strlen(sub_str)) {
1075 ret = kstrtoul(sub_str, 0, &level);
1076 if (ret || level > 31)
1078 *mask |= 1 << level;
1086 static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
1087 enum pp_clock_type type,
1091 struct drm_device *ddev = dev_get_drvdata(dev);
1092 struct amdgpu_device *adev = drm_to_adev(ddev);
1096 if (amdgpu_in_reset(adev))
1098 if (adev->in_suspend && !adev->in_runpm)
1101 ret = amdgpu_read_mask(buf, count, &mask);
1105 ret = pm_runtime_get_sync(ddev->dev);
1107 pm_runtime_put_autosuspend(ddev->dev);
1111 ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1113 pm_runtime_mark_last_busy(ddev->dev);
1114 pm_runtime_put_autosuspend(ddev->dev);
1122 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
1123 struct device_attribute *attr,
1126 return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
1129 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1130 struct device_attribute *attr,
1134 return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
1137 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1138 struct device_attribute *attr,
1141 return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1144 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1145 struct device_attribute *attr,
1149 return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1152 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1153 struct device_attribute *attr,
1156 return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1159 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1160 struct device_attribute *attr,
1164 return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1167 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1168 struct device_attribute *attr,
1171 return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1174 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1175 struct device_attribute *attr,
1179 return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1182 static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
1183 struct device_attribute *attr,
1186 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
1189 static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
1190 struct device_attribute *attr,
1194 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
1197 static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
1198 struct device_attribute *attr,
1201 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
1204 static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
1205 struct device_attribute *attr,
1209 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
1212 static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
1213 struct device_attribute *attr,
1216 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
1219 static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
1220 struct device_attribute *attr,
1224 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
1227 static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
1228 struct device_attribute *attr,
1231 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
1234 static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
1235 struct device_attribute *attr,
1239 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
1242 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1243 struct device_attribute *attr,
1246 return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1249 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1250 struct device_attribute *attr,
1254 return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1257 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1258 struct device_attribute *attr,
1261 return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1264 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1265 struct device_attribute *attr,
1269 return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1272 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1273 struct device_attribute *attr,
1276 struct drm_device *ddev = dev_get_drvdata(dev);
1277 struct amdgpu_device *adev = drm_to_adev(ddev);
1281 if (amdgpu_in_reset(adev))
1283 if (adev->in_suspend && !adev->in_runpm)
1286 ret = pm_runtime_get_sync(ddev->dev);
1288 pm_runtime_put_autosuspend(ddev->dev);
1292 value = amdgpu_dpm_get_sclk_od(adev);
1294 pm_runtime_mark_last_busy(ddev->dev);
1295 pm_runtime_put_autosuspend(ddev->dev);
1297 return sysfs_emit(buf, "%d\n", value);
1300 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1301 struct device_attribute *attr,
1305 struct drm_device *ddev = dev_get_drvdata(dev);
1306 struct amdgpu_device *adev = drm_to_adev(ddev);
1310 if (amdgpu_in_reset(adev))
1312 if (adev->in_suspend && !adev->in_runpm)
1315 ret = kstrtol(buf, 0, &value);
1320 ret = pm_runtime_get_sync(ddev->dev);
1322 pm_runtime_put_autosuspend(ddev->dev);
1326 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1328 pm_runtime_mark_last_busy(ddev->dev);
1329 pm_runtime_put_autosuspend(ddev->dev);
1334 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1335 struct device_attribute *attr,
1338 struct drm_device *ddev = dev_get_drvdata(dev);
1339 struct amdgpu_device *adev = drm_to_adev(ddev);
1343 if (amdgpu_in_reset(adev))
1345 if (adev->in_suspend && !adev->in_runpm)
1348 ret = pm_runtime_get_sync(ddev->dev);
1350 pm_runtime_put_autosuspend(ddev->dev);
1354 value = amdgpu_dpm_get_mclk_od(adev);
1356 pm_runtime_mark_last_busy(ddev->dev);
1357 pm_runtime_put_autosuspend(ddev->dev);
1359 return sysfs_emit(buf, "%d\n", value);
1362 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1363 struct device_attribute *attr,
1367 struct drm_device *ddev = dev_get_drvdata(dev);
1368 struct amdgpu_device *adev = drm_to_adev(ddev);
1372 if (amdgpu_in_reset(adev))
1374 if (adev->in_suspend && !adev->in_runpm)
1377 ret = kstrtol(buf, 0, &value);
1382 ret = pm_runtime_get_sync(ddev->dev);
1384 pm_runtime_put_autosuspend(ddev->dev);
1388 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1390 pm_runtime_mark_last_busy(ddev->dev);
1391 pm_runtime_put_autosuspend(ddev->dev);
1397 * DOC: pp_power_profile_mode
1399 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1400 * related to switching between power levels in a power state. The file
1401 * pp_power_profile_mode is used for this.
1403 * Reading this file outputs a list of all of the predefined power profiles
1404 * and the relevant heuristics settings for that profile.
1406 * To select a profile or create a custom profile, first select manual using
1407 * power_dpm_force_performance_level. Writing the number of a predefined
1408 * profile to pp_power_profile_mode will enable those heuristics. To
1409 * create a custom set of heuristics, write a string of numbers to the file
1410 * starting with the number of the custom profile along with a setting
1411 * for each heuristic parameter. Due to differences across asic families
1412 * the heuristic parameters vary from family to family.
1416 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1417 struct device_attribute *attr,
1420 struct drm_device *ddev = dev_get_drvdata(dev);
1421 struct amdgpu_device *adev = drm_to_adev(ddev);
1425 if (amdgpu_in_reset(adev))
1427 if (adev->in_suspend && !adev->in_runpm)
1430 ret = pm_runtime_get_sync(ddev->dev);
1432 pm_runtime_put_autosuspend(ddev->dev);
1436 size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1438 size = sysfs_emit(buf, "\n");
1440 pm_runtime_mark_last_busy(ddev->dev);
1441 pm_runtime_put_autosuspend(ddev->dev);
1447 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1448 struct device_attribute *attr,
1453 struct drm_device *ddev = dev_get_drvdata(dev);
1454 struct amdgpu_device *adev = drm_to_adev(ddev);
1455 uint32_t parameter_size = 0;
1457 char *sub_str, buf_cpy[128];
1461 long int profile_mode = 0;
1462 const char delimiter[3] = {' ', '\n', '\0'};
1464 if (amdgpu_in_reset(adev))
1466 if (adev->in_suspend && !adev->in_runpm)
1471 ret = kstrtol(tmp, 0, &profile_mode);
1475 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1476 if (count < 2 || count > 127)
1478 while (isspace(*++buf))
1480 memcpy(buf_cpy, buf, count-i);
1482 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1483 if (strlen(sub_str) == 0)
1485 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
1489 while (isspace(*tmp_str))
1493 parameter[parameter_size] = profile_mode;
1495 ret = pm_runtime_get_sync(ddev->dev);
1497 pm_runtime_put_autosuspend(ddev->dev);
1501 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1503 pm_runtime_mark_last_busy(ddev->dev);
1504 pm_runtime_put_autosuspend(ddev->dev);
1512 static int amdgpu_hwmon_get_sensor_generic(struct amdgpu_device *adev,
1513 enum amd_pp_sensors sensor,
1516 int r, size = sizeof(uint32_t);
1518 if (amdgpu_in_reset(adev))
1520 if (adev->in_suspend && !adev->in_runpm)
1523 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
1525 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1529 /* get the sensor value */
1530 r = amdgpu_dpm_read_sensor(adev, sensor, query, &size);
1532 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
1533 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1539 * DOC: gpu_busy_percent
1541 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1542 * is as a percentage. The file gpu_busy_percent is used for this.
1543 * The SMU firmware computes a percentage of load based on the
1544 * aggregate activity level in the IP cores.
1546 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1547 struct device_attribute *attr,
1550 struct drm_device *ddev = dev_get_drvdata(dev);
1551 struct amdgpu_device *adev = drm_to_adev(ddev);
1555 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value);
1559 return sysfs_emit(buf, "%d\n", value);
1563 * DOC: mem_busy_percent
1565 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1566 * is as a percentage. The file mem_busy_percent is used for this.
1567 * The SMU firmware computes a percentage of load based on the
1568 * aggregate activity level in the IP cores.
1570 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1571 struct device_attribute *attr,
1574 struct drm_device *ddev = dev_get_drvdata(dev);
1575 struct amdgpu_device *adev = drm_to_adev(ddev);
1579 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value);
1583 return sysfs_emit(buf, "%d\n", value);
1587 * DOC: vcn_busy_percent
1589 * The amdgpu driver provides a sysfs API for reading how busy the VCN
1590 * is as a percentage. The file vcn_busy_percent is used for this.
1591 * The SMU firmware computes a percentage of load based on the
1592 * aggregate activity level in the IP cores.
1594 static ssize_t amdgpu_get_vcn_busy_percent(struct device *dev,
1595 struct device_attribute *attr,
1598 struct drm_device *ddev = dev_get_drvdata(dev);
1599 struct amdgpu_device *adev = drm_to_adev(ddev);
1603 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VCN_LOAD, &value);
1607 return sysfs_emit(buf, "%d\n", value);
1613 * The amdgpu driver provides a sysfs API for estimating how much data
1614 * has been received and sent by the GPU in the last second through PCIe.
1615 * The file pcie_bw is used for this.
1616 * The Perf counters count the number of received and sent messages and return
1617 * those values, as well as the maximum payload size of a PCIe packet (mps).
1618 * Note that it is not possible to easily and quickly obtain the size of each
1619 * packet transmitted, so we output the max payload size (mps) to allow for
1620 * quick estimation of the PCIe bandwidth usage
1622 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1623 struct device_attribute *attr,
1626 struct drm_device *ddev = dev_get_drvdata(dev);
1627 struct amdgpu_device *adev = drm_to_adev(ddev);
1628 uint64_t count0 = 0, count1 = 0;
1631 if (amdgpu_in_reset(adev))
1633 if (adev->in_suspend && !adev->in_runpm)
1636 if (adev->flags & AMD_IS_APU)
1639 if (!adev->asic_funcs->get_pcie_usage)
1642 ret = pm_runtime_get_sync(ddev->dev);
1644 pm_runtime_put_autosuspend(ddev->dev);
1648 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1650 pm_runtime_mark_last_busy(ddev->dev);
1651 pm_runtime_put_autosuspend(ddev->dev);
1653 return sysfs_emit(buf, "%llu %llu %i\n",
1654 count0, count1, pcie_get_mps(adev->pdev));
1660 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1661 * The file unique_id is used for this.
1662 * This will provide a Unique ID that will persist from machine to machine
1664 * NOTE: This will only work for GFX9 and newer. This file will be absent
1665 * on unsupported ASICs (GFX8 and older)
1667 static ssize_t amdgpu_get_unique_id(struct device *dev,
1668 struct device_attribute *attr,
1671 struct drm_device *ddev = dev_get_drvdata(dev);
1672 struct amdgpu_device *adev = drm_to_adev(ddev);
1674 if (amdgpu_in_reset(adev))
1676 if (adev->in_suspend && !adev->in_runpm)
1679 if (adev->unique_id)
1680 return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1686 * DOC: thermal_throttling_logging
1688 * Thermal throttling pulls down the clock frequency and thus the performance.
1689 * It's an useful mechanism to protect the chip from overheating. Since it
1690 * impacts performance, the user controls whether it is enabled and if so,
1691 * the log frequency.
1693 * Reading back the file shows you the status(enabled or disabled) and
1694 * the interval(in seconds) between each thermal logging.
1696 * Writing an integer to the file, sets a new logging interval, in seconds.
1697 * The value should be between 1 and 3600. If the value is less than 1,
1698 * thermal logging is disabled. Values greater than 3600 are ignored.
1700 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1701 struct device_attribute *attr,
1704 struct drm_device *ddev = dev_get_drvdata(dev);
1705 struct amdgpu_device *adev = drm_to_adev(ddev);
1707 return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
1708 adev_to_drm(adev)->unique,
1709 atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1710 adev->throttling_logging_rs.interval / HZ + 1);
1713 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1714 struct device_attribute *attr,
1718 struct drm_device *ddev = dev_get_drvdata(dev);
1719 struct amdgpu_device *adev = drm_to_adev(ddev);
1720 long throttling_logging_interval;
1721 unsigned long flags;
1724 ret = kstrtol(buf, 0, &throttling_logging_interval);
1728 if (throttling_logging_interval > 3600)
1731 if (throttling_logging_interval > 0) {
1732 raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1734 * Reset the ratelimit timer internals.
1735 * This can effectively restart the timer.
1737 adev->throttling_logging_rs.interval =
1738 (throttling_logging_interval - 1) * HZ;
1739 adev->throttling_logging_rs.begin = 0;
1740 adev->throttling_logging_rs.printed = 0;
1741 adev->throttling_logging_rs.missed = 0;
1742 raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1744 atomic_set(&adev->throttling_logging_enabled, 1);
1746 atomic_set(&adev->throttling_logging_enabled, 0);
1753 * DOC: apu_thermal_cap
1755 * The amdgpu driver provides a sysfs API for retrieving/updating thermal
1756 * limit temperature in millidegrees Celsius
1758 * Reading back the file shows you core limit value
1760 * Writing an integer to the file, sets a new thermal limit. The value
1761 * should be between 0 and 100. If the value is less than 0 or greater
1762 * than 100, then the write request will be ignored.
1764 static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
1765 struct device_attribute *attr,
1770 struct drm_device *ddev = dev_get_drvdata(dev);
1771 struct amdgpu_device *adev = drm_to_adev(ddev);
1773 ret = pm_runtime_get_sync(ddev->dev);
1775 pm_runtime_put_autosuspend(ddev->dev);
1779 ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
1781 size = sysfs_emit(buf, "%u\n", limit);
1783 size = sysfs_emit(buf, "failed to get thermal limit\n");
1785 pm_runtime_mark_last_busy(ddev->dev);
1786 pm_runtime_put_autosuspend(ddev->dev);
1791 static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
1792 struct device_attribute *attr,
1798 struct drm_device *ddev = dev_get_drvdata(dev);
1799 struct amdgpu_device *adev = drm_to_adev(ddev);
1801 ret = kstrtou32(buf, 10, &value);
1806 dev_err(dev, "Invalid argument !\n");
1810 ret = pm_runtime_get_sync(ddev->dev);
1812 pm_runtime_put_autosuspend(ddev->dev);
1816 ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
1818 dev_err(dev, "failed to update thermal limit\n");
1822 pm_runtime_mark_last_busy(ddev->dev);
1823 pm_runtime_put_autosuspend(ddev->dev);
1828 static int amdgpu_pm_metrics_attr_update(struct amdgpu_device *adev,
1829 struct amdgpu_device_attr *attr,
1831 enum amdgpu_device_attr_states *states)
1833 if (amdgpu_dpm_get_pm_metrics(adev, NULL, 0) == -EOPNOTSUPP)
1834 *states = ATTR_STATE_UNSUPPORTED;
1839 static ssize_t amdgpu_get_pm_metrics(struct device *dev,
1840 struct device_attribute *attr, char *buf)
1842 struct drm_device *ddev = dev_get_drvdata(dev);
1843 struct amdgpu_device *adev = drm_to_adev(ddev);
1847 if (amdgpu_in_reset(adev))
1849 if (adev->in_suspend && !adev->in_runpm)
1852 ret = pm_runtime_get_sync(ddev->dev);
1854 pm_runtime_put_autosuspend(ddev->dev);
1858 size = amdgpu_dpm_get_pm_metrics(adev, buf, PAGE_SIZE);
1860 pm_runtime_mark_last_busy(ddev->dev);
1861 pm_runtime_put_autosuspend(ddev->dev);
1869 * The amdgpu driver provides a sysfs API for retrieving current gpu
1870 * metrics data. The file gpu_metrics is used for this. Reading the
1871 * file will dump all the current gpu metrics data.
1873 * These data include temperature, frequency, engines utilization,
1874 * power consume, throttler status, fan speed and cpu core statistics(
1875 * available for APU only). That's it will give a snapshot of all sensors
1878 static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1879 struct device_attribute *attr,
1882 struct drm_device *ddev = dev_get_drvdata(dev);
1883 struct amdgpu_device *adev = drm_to_adev(ddev);
1888 if (amdgpu_in_reset(adev))
1890 if (adev->in_suspend && !adev->in_runpm)
1893 ret = pm_runtime_get_sync(ddev->dev);
1895 pm_runtime_put_autosuspend(ddev->dev);
1899 size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1903 if (size >= PAGE_SIZE)
1904 size = PAGE_SIZE - 1;
1906 memcpy(buf, gpu_metrics, size);
1909 pm_runtime_mark_last_busy(ddev->dev);
1910 pm_runtime_put_autosuspend(ddev->dev);
1915 static int amdgpu_show_powershift_percent(struct device *dev,
1916 char *buf, enum amd_pp_sensors sensor)
1918 struct drm_device *ddev = dev_get_drvdata(dev);
1919 struct amdgpu_device *adev = drm_to_adev(ddev);
1923 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1924 if (r == -EOPNOTSUPP) {
1925 /* sensor not available on dGPU, try to read from APU */
1927 mutex_lock(&mgpu_info.mutex);
1928 for (i = 0; i < mgpu_info.num_gpu; i++) {
1929 if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
1930 adev = mgpu_info.gpu_ins[i].adev;
1934 mutex_unlock(&mgpu_info.mutex);
1936 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
1942 return sysfs_emit(buf, "%u%%\n", ss_power);
1946 * DOC: smartshift_apu_power
1948 * The amdgpu driver provides a sysfs API for reporting APU power
1949 * shift in percentage if platform supports smartshift. Value 0 means that
1950 * there is no powershift and values between [1-100] means that the power
1951 * is shifted to APU, the percentage of boost is with respect to APU power
1952 * limit on the platform.
1955 static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1958 return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE);
1962 * DOC: smartshift_dgpu_power
1964 * The amdgpu driver provides a sysfs API for reporting dGPU power
1965 * shift in percentage if platform supports smartshift. Value 0 means that
1966 * there is no powershift and values between [1-100] means that the power is
1967 * shifted to dGPU, the percentage of boost is with respect to dGPU power
1968 * limit on the platform.
1971 static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1974 return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE);
1978 * DOC: smartshift_bias
1980 * The amdgpu driver provides a sysfs API for reporting the
1981 * smartshift(SS2.0) bias level. The value ranges from -100 to 100
1982 * and the default is 0. -100 sets maximum preference to APU
1983 * and 100 sets max perference to dGPU.
1986 static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
1987 struct device_attribute *attr,
1992 r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
1997 static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
1998 struct device_attribute *attr,
1999 const char *buf, size_t count)
2001 struct drm_device *ddev = dev_get_drvdata(dev);
2002 struct amdgpu_device *adev = drm_to_adev(ddev);
2006 if (amdgpu_in_reset(adev))
2008 if (adev->in_suspend && !adev->in_runpm)
2011 r = pm_runtime_get_sync(ddev->dev);
2013 pm_runtime_put_autosuspend(ddev->dev);
2017 r = kstrtoint(buf, 10, &bias);
2021 if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
2022 bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
2023 else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
2024 bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
2026 amdgpu_smartshift_bias = bias;
2029 /* TODO: update bias level with SMU message */
2032 pm_runtime_mark_last_busy(ddev->dev);
2033 pm_runtime_put_autosuspend(ddev->dev);
2037 static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2038 uint32_t mask, enum amdgpu_device_attr_states *states)
2040 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
2041 *states = ATTR_STATE_UNSUPPORTED;
2046 static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2047 uint32_t mask, enum amdgpu_device_attr_states *states)
2051 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
2052 *states = ATTR_STATE_UNSUPPORTED;
2053 else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
2055 *states = ATTR_STATE_UNSUPPORTED;
2056 else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
2058 *states = ATTR_STATE_UNSUPPORTED;
2063 static int pp_od_clk_voltage_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2064 uint32_t mask, enum amdgpu_device_attr_states *states)
2066 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2068 *states = ATTR_STATE_SUPPORTED;
2070 if (!amdgpu_dpm_is_overdrive_supported(adev)) {
2071 *states = ATTR_STATE_UNSUPPORTED;
2075 /* Enable pp_od_clk_voltage node for gc 9.4.3 SRIOV/BM support */
2076 if (gc_ver == IP_VERSION(9, 4, 3)) {
2077 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
2078 *states = ATTR_STATE_UNSUPPORTED;
2082 if (!(attr->flags & mask))
2083 *states = ATTR_STATE_UNSUPPORTED;
2088 static int pp_dpm_dcefclk_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2089 uint32_t mask, enum amdgpu_device_attr_states *states)
2091 struct device_attribute *dev_attr = &attr->dev_attr;
2094 *states = ATTR_STATE_SUPPORTED;
2096 if (!(attr->flags & mask)) {
2097 *states = ATTR_STATE_UNSUPPORTED;
2101 gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2102 /* dcefclk node is not available on gfx 11.0.3 sriov */
2103 if ((gc_ver == IP_VERSION(11, 0, 3) && amdgpu_sriov_is_pp_one_vf(adev)) ||
2104 gc_ver < IP_VERSION(9, 0, 0) ||
2105 !amdgpu_device_has_display_hardware(adev))
2106 *states = ATTR_STATE_UNSUPPORTED;
2108 /* SMU MP1 does not support dcefclk level setting,
2109 * setting should not be allowed from VF if not in one VF mode.
2111 if (gc_ver >= IP_VERSION(10, 0, 0) ||
2112 (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))) {
2113 dev_attr->attr.mode &= ~S_IWUGO;
2114 dev_attr->store = NULL;
2120 static int pp_dpm_clk_default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2121 uint32_t mask, enum amdgpu_device_attr_states *states)
2123 struct device_attribute *dev_attr = &attr->dev_attr;
2124 enum amdgpu_device_attr_id attr_id = attr->attr_id;
2125 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
2126 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2128 *states = ATTR_STATE_SUPPORTED;
2130 if (!(attr->flags & mask)) {
2131 *states = ATTR_STATE_UNSUPPORTED;
2135 if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2136 if (gc_ver < IP_VERSION(9, 0, 0))
2137 *states = ATTR_STATE_UNSUPPORTED;
2138 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2139 if (mp1_ver < IP_VERSION(10, 0, 0))
2140 *states = ATTR_STATE_UNSUPPORTED;
2141 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
2142 if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2143 gc_ver == IP_VERSION(10, 3, 3) ||
2144 gc_ver == IP_VERSION(10, 3, 6) ||
2145 gc_ver == IP_VERSION(10, 3, 7) ||
2146 gc_ver == IP_VERSION(10, 3, 0) ||
2147 gc_ver == IP_VERSION(10, 1, 2) ||
2148 gc_ver == IP_VERSION(11, 0, 0) ||
2149 gc_ver == IP_VERSION(11, 0, 1) ||
2150 gc_ver == IP_VERSION(11, 0, 4) ||
2151 gc_ver == IP_VERSION(11, 5, 0) ||
2152 gc_ver == IP_VERSION(11, 0, 2) ||
2153 gc_ver == IP_VERSION(11, 0, 3) ||
2154 gc_ver == IP_VERSION(9, 4, 3)))
2155 *states = ATTR_STATE_UNSUPPORTED;
2156 } else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
2157 if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2158 gc_ver == IP_VERSION(10, 3, 0) ||
2159 gc_ver == IP_VERSION(11, 0, 2) ||
2160 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2161 *states = ATTR_STATE_UNSUPPORTED;
2162 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
2163 if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2164 gc_ver == IP_VERSION(10, 3, 3) ||
2165 gc_ver == IP_VERSION(10, 3, 6) ||
2166 gc_ver == IP_VERSION(10, 3, 7) ||
2167 gc_ver == IP_VERSION(10, 3, 0) ||
2168 gc_ver == IP_VERSION(10, 1, 2) ||
2169 gc_ver == IP_VERSION(11, 0, 0) ||
2170 gc_ver == IP_VERSION(11, 0, 1) ||
2171 gc_ver == IP_VERSION(11, 0, 4) ||
2172 gc_ver == IP_VERSION(11, 5, 0) ||
2173 gc_ver == IP_VERSION(11, 0, 2) ||
2174 gc_ver == IP_VERSION(11, 0, 3) ||
2175 gc_ver == IP_VERSION(9, 4, 3)))
2176 *states = ATTR_STATE_UNSUPPORTED;
2177 } else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
2178 if (!((gc_ver == IP_VERSION(10, 3, 1) ||
2179 gc_ver == IP_VERSION(10, 3, 0) ||
2180 gc_ver == IP_VERSION(11, 0, 2) ||
2181 gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
2182 *states = ATTR_STATE_UNSUPPORTED;
2183 } else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
2184 if (gc_ver == IP_VERSION(9, 4, 2) ||
2185 gc_ver == IP_VERSION(9, 4, 3))
2186 *states = ATTR_STATE_UNSUPPORTED;
2190 case IP_VERSION(9, 4, 1):
2191 case IP_VERSION(9, 4, 2):
2192 /* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2193 if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2194 DEVICE_ATTR_IS(pp_dpm_socclk) ||
2195 DEVICE_ATTR_IS(pp_dpm_fclk)) {
2196 dev_attr->attr.mode &= ~S_IWUGO;
2197 dev_attr->store = NULL;
2204 /* setting should not be allowed from VF if not in one VF mode */
2205 if (amdgpu_sriov_vf(adev) && amdgpu_sriov_is_pp_one_vf(adev)) {
2206 dev_attr->attr.mode &= ~S_IWUGO;
2207 dev_attr->store = NULL;
2213 /* Following items will be read out to indicate current plpd policy:
2219 static ssize_t amdgpu_get_xgmi_plpd_policy(struct device *dev,
2220 struct device_attribute *attr,
2223 struct drm_device *ddev = dev_get_drvdata(dev);
2224 struct amdgpu_device *adev = drm_to_adev(ddev);
2225 char *mode_desc = "none";
2228 if (amdgpu_in_reset(adev))
2230 if (adev->in_suspend && !adev->in_runpm)
2233 mode = amdgpu_dpm_get_xgmi_plpd_mode(adev, &mode_desc);
2235 return sysfs_emit(buf, "%d: %s\n", mode, mode_desc);
2238 /* Following argument value is expected from user to change plpd policy
2239 * - arg 0: disallow plpd
2240 * - arg 1: default policy
2241 * - arg 2: optimized policy
2243 static ssize_t amdgpu_set_xgmi_plpd_policy(struct device *dev,
2244 struct device_attribute *attr,
2245 const char *buf, size_t count)
2247 struct drm_device *ddev = dev_get_drvdata(dev);
2248 struct amdgpu_device *adev = drm_to_adev(ddev);
2251 if (amdgpu_in_reset(adev))
2253 if (adev->in_suspend && !adev->in_runpm)
2256 ret = kstrtos32(buf, 0, &mode);
2260 ret = pm_runtime_get_sync(ddev->dev);
2262 pm_runtime_put_autosuspend(ddev->dev);
2266 ret = amdgpu_dpm_set_xgmi_plpd_mode(adev, mode);
2268 pm_runtime_mark_last_busy(ddev->dev);
2269 pm_runtime_put_autosuspend(ddev->dev);
2277 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2278 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2279 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2280 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2281 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2282 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2283 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2284 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2285 .attr_update = pp_dpm_clk_default_attr_update),
2286 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2287 .attr_update = pp_dpm_clk_default_attr_update),
2288 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2289 .attr_update = pp_dpm_clk_default_attr_update),
2290 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2291 .attr_update = pp_dpm_clk_default_attr_update),
2292 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2293 .attr_update = pp_dpm_clk_default_attr_update),
2294 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2295 .attr_update = pp_dpm_clk_default_attr_update),
2296 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2297 .attr_update = pp_dpm_clk_default_attr_update),
2298 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2299 .attr_update = pp_dpm_clk_default_attr_update),
2300 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2301 .attr_update = pp_dpm_dcefclk_attr_update),
2302 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
2303 .attr_update = pp_dpm_clk_default_attr_update),
2304 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC),
2305 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC),
2306 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2307 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC,
2308 .attr_update = pp_od_clk_voltage_attr_update),
2309 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2310 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2311 AMDGPU_DEVICE_ATTR_RO(vcn_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2312 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC),
2313 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2314 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2315 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2316 AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2317 AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2318 AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC,
2319 .attr_update = ss_power_attr_update),
2320 AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC,
2321 .attr_update = ss_power_attr_update),
2322 AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC,
2323 .attr_update = ss_bias_attr_update),
2324 AMDGPU_DEVICE_ATTR_RW(xgmi_plpd_policy, ATTR_FLAG_BASIC),
2325 AMDGPU_DEVICE_ATTR_RO(pm_metrics, ATTR_FLAG_BASIC,
2326 .attr_update = amdgpu_pm_metrics_attr_update),
2329 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2330 uint32_t mask, enum amdgpu_device_attr_states *states)
2332 struct device_attribute *dev_attr = &attr->dev_attr;
2333 enum amdgpu_device_attr_id attr_id = attr->attr_id;
2334 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
2336 if (!(attr->flags & mask)) {
2337 *states = ATTR_STATE_UNSUPPORTED;
2341 if (DEVICE_ATTR_IS(mem_busy_percent)) {
2342 if ((adev->flags & AMD_IS_APU &&
2343 gc_ver != IP_VERSION(9, 4, 3)) ||
2344 gc_ver == IP_VERSION(9, 0, 1))
2345 *states = ATTR_STATE_UNSUPPORTED;
2346 } else if (DEVICE_ATTR_IS(vcn_busy_percent)) {
2347 if (!(gc_ver == IP_VERSION(10, 3, 1) ||
2348 gc_ver == IP_VERSION(10, 3, 3) ||
2349 gc_ver == IP_VERSION(10, 3, 6) ||
2350 gc_ver == IP_VERSION(10, 3, 7) ||
2351 gc_ver == IP_VERSION(11, 0, 1) ||
2352 gc_ver == IP_VERSION(11, 0, 4) ||
2353 gc_ver == IP_VERSION(11, 5, 0)))
2354 *states = ATTR_STATE_UNSUPPORTED;
2355 } else if (DEVICE_ATTR_IS(pcie_bw)) {
2356 /* PCIe Perf counters won't work on APU nodes */
2357 if (adev->flags & AMD_IS_APU ||
2358 !adev->asic_funcs->get_pcie_usage)
2359 *states = ATTR_STATE_UNSUPPORTED;
2360 } else if (DEVICE_ATTR_IS(unique_id)) {
2362 case IP_VERSION(9, 0, 1):
2363 case IP_VERSION(9, 4, 0):
2364 case IP_VERSION(9, 4, 1):
2365 case IP_VERSION(9, 4, 2):
2366 case IP_VERSION(9, 4, 3):
2367 case IP_VERSION(10, 3, 0):
2368 case IP_VERSION(11, 0, 0):
2369 case IP_VERSION(11, 0, 1):
2370 case IP_VERSION(11, 0, 2):
2371 case IP_VERSION(11, 0, 3):
2372 *states = ATTR_STATE_SUPPORTED;
2375 *states = ATTR_STATE_UNSUPPORTED;
2377 } else if (DEVICE_ATTR_IS(pp_features)) {
2378 if ((adev->flags & AMD_IS_APU &&
2379 gc_ver != IP_VERSION(9, 4, 3)) ||
2380 gc_ver < IP_VERSION(9, 0, 0))
2381 *states = ATTR_STATE_UNSUPPORTED;
2382 } else if (DEVICE_ATTR_IS(gpu_metrics)) {
2383 if (gc_ver < IP_VERSION(9, 1, 0))
2384 *states = ATTR_STATE_UNSUPPORTED;
2385 } else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
2386 if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2387 *states = ATTR_STATE_UNSUPPORTED;
2388 else if ((gc_ver == IP_VERSION(10, 3, 0) ||
2389 gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev))
2390 *states = ATTR_STATE_UNSUPPORTED;
2391 } else if (DEVICE_ATTR_IS(xgmi_plpd_policy)) {
2392 if (amdgpu_dpm_get_xgmi_plpd_mode(adev, NULL) == XGMI_PLPD_NONE)
2393 *states = ATTR_STATE_UNSUPPORTED;
2394 } else if (DEVICE_ATTR_IS(pp_mclk_od)) {
2395 if (amdgpu_dpm_get_mclk_od(adev) == -EOPNOTSUPP)
2396 *states = ATTR_STATE_UNSUPPORTED;
2397 } else if (DEVICE_ATTR_IS(pp_sclk_od)) {
2398 if (amdgpu_dpm_get_sclk_od(adev) == -EOPNOTSUPP)
2399 *states = ATTR_STATE_UNSUPPORTED;
2400 } else if (DEVICE_ATTR_IS(apu_thermal_cap)) {
2403 if (amdgpu_dpm_get_apu_thermal_limit(adev, &limit) ==
2405 *states = ATTR_STATE_UNSUPPORTED;
2409 case IP_VERSION(10, 3, 0):
2410 if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
2411 amdgpu_sriov_vf(adev)) {
2412 dev_attr->attr.mode &= ~0222;
2413 dev_attr->store = NULL;
2424 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2425 struct amdgpu_device_attr *attr,
2426 uint32_t mask, struct list_head *attr_list)
2429 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2430 struct amdgpu_device_attr_entry *attr_entry;
2431 struct device_attribute *dev_attr;
2434 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2435 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2440 dev_attr = &attr->dev_attr;
2441 name = dev_attr->attr.name;
2443 attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2445 ret = attr_update(adev, attr, mask, &attr_states);
2447 dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2452 if (attr_states == ATTR_STATE_UNSUPPORTED)
2455 ret = device_create_file(adev->dev, dev_attr);
2457 dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2461 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2465 attr_entry->attr = attr;
2466 INIT_LIST_HEAD(&attr_entry->entry);
2468 list_add_tail(&attr_entry->entry, attr_list);
2473 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2475 struct device_attribute *dev_attr = &attr->dev_attr;
2477 device_remove_file(adev->dev, dev_attr);
2480 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2481 struct list_head *attr_list);
2483 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2484 struct amdgpu_device_attr *attrs,
2487 struct list_head *attr_list)
2492 for (i = 0; i < counts; i++) {
2493 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2501 amdgpu_device_attr_remove_groups(adev, attr_list);
2506 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2507 struct list_head *attr_list)
2509 struct amdgpu_device_attr_entry *entry, *entry_tmp;
2511 if (list_empty(attr_list))
2514 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2515 amdgpu_device_attr_remove(adev, entry->attr);
2516 list_del(&entry->entry);
2521 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2522 struct device_attribute *attr,
2525 struct amdgpu_device *adev = dev_get_drvdata(dev);
2526 int channel = to_sensor_dev_attr(attr)->index;
2529 if (channel >= PP_TEMP_MAX)
2533 case PP_TEMP_JUNCTION:
2534 /* get current junction temperature */
2535 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2539 /* get current edge temperature */
2540 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2544 /* get current memory temperature */
2545 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2556 return sysfs_emit(buf, "%d\n", temp);
2559 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2560 struct device_attribute *attr,
2563 struct amdgpu_device *adev = dev_get_drvdata(dev);
2564 int hyst = to_sensor_dev_attr(attr)->index;
2568 temp = adev->pm.dpm.thermal.min_temp;
2570 temp = adev->pm.dpm.thermal.max_temp;
2572 return sysfs_emit(buf, "%d\n", temp);
2575 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2576 struct device_attribute *attr,
2579 struct amdgpu_device *adev = dev_get_drvdata(dev);
2580 int hyst = to_sensor_dev_attr(attr)->index;
2584 temp = adev->pm.dpm.thermal.min_hotspot_temp;
2586 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2588 return sysfs_emit(buf, "%d\n", temp);
2591 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2592 struct device_attribute *attr,
2595 struct amdgpu_device *adev = dev_get_drvdata(dev);
2596 int hyst = to_sensor_dev_attr(attr)->index;
2600 temp = adev->pm.dpm.thermal.min_mem_temp;
2602 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2604 return sysfs_emit(buf, "%d\n", temp);
2607 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2608 struct device_attribute *attr,
2611 int channel = to_sensor_dev_attr(attr)->index;
2613 if (channel >= PP_TEMP_MAX)
2616 return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2619 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2620 struct device_attribute *attr,
2623 struct amdgpu_device *adev = dev_get_drvdata(dev);
2624 int channel = to_sensor_dev_attr(attr)->index;
2627 if (channel >= PP_TEMP_MAX)
2631 case PP_TEMP_JUNCTION:
2632 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2635 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2638 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2642 return sysfs_emit(buf, "%d\n", temp);
2645 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2646 struct device_attribute *attr,
2649 struct amdgpu_device *adev = dev_get_drvdata(dev);
2653 if (amdgpu_in_reset(adev))
2655 if (adev->in_suspend && !adev->in_runpm)
2658 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2660 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2664 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2666 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2667 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2672 return sysfs_emit(buf, "%u\n", pwm_mode);
2675 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2676 struct device_attribute *attr,
2680 struct amdgpu_device *adev = dev_get_drvdata(dev);
2685 if (amdgpu_in_reset(adev))
2687 if (adev->in_suspend && !adev->in_runpm)
2690 err = kstrtoint(buf, 10, &value);
2695 pwm_mode = AMD_FAN_CTRL_NONE;
2696 else if (value == 1)
2697 pwm_mode = AMD_FAN_CTRL_MANUAL;
2698 else if (value == 2)
2699 pwm_mode = AMD_FAN_CTRL_AUTO;
2703 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2705 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2709 ret = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2711 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2712 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2720 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2721 struct device_attribute *attr,
2724 return sysfs_emit(buf, "%i\n", 0);
2727 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2728 struct device_attribute *attr,
2731 return sysfs_emit(buf, "%i\n", 255);
2734 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2735 struct device_attribute *attr,
2736 const char *buf, size_t count)
2738 struct amdgpu_device *adev = dev_get_drvdata(dev);
2743 if (amdgpu_in_reset(adev))
2745 if (adev->in_suspend && !adev->in_runpm)
2748 err = kstrtou32(buf, 10, &value);
2752 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2754 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2758 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2762 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2763 pr_info("manual fan speed control should be enabled first\n");
2768 err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
2771 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2772 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2780 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2781 struct device_attribute *attr,
2784 struct amdgpu_device *adev = dev_get_drvdata(dev);
2788 if (amdgpu_in_reset(adev))
2790 if (adev->in_suspend && !adev->in_runpm)
2793 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2795 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2799 err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2801 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2802 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2807 return sysfs_emit(buf, "%i\n", speed);
2810 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2811 struct device_attribute *attr,
2814 struct amdgpu_device *adev = dev_get_drvdata(dev);
2818 if (amdgpu_in_reset(adev))
2820 if (adev->in_suspend && !adev->in_runpm)
2823 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2825 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2829 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2831 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2832 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2837 return sysfs_emit(buf, "%i\n", speed);
2840 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2841 struct device_attribute *attr,
2844 struct amdgpu_device *adev = dev_get_drvdata(dev);
2848 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2854 return sysfs_emit(buf, "%d\n", min_rpm);
2857 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2858 struct device_attribute *attr,
2861 struct amdgpu_device *adev = dev_get_drvdata(dev);
2865 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2871 return sysfs_emit(buf, "%d\n", max_rpm);
2874 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2875 struct device_attribute *attr,
2878 struct amdgpu_device *adev = dev_get_drvdata(dev);
2882 if (amdgpu_in_reset(adev))
2884 if (adev->in_suspend && !adev->in_runpm)
2887 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2889 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2893 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2895 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2896 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2901 return sysfs_emit(buf, "%i\n", rpm);
2904 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2905 struct device_attribute *attr,
2906 const char *buf, size_t count)
2908 struct amdgpu_device *adev = dev_get_drvdata(dev);
2913 if (amdgpu_in_reset(adev))
2915 if (adev->in_suspend && !adev->in_runpm)
2918 err = kstrtou32(buf, 10, &value);
2922 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2924 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2928 err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2932 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2937 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2940 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2941 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2949 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2950 struct device_attribute *attr,
2953 struct amdgpu_device *adev = dev_get_drvdata(dev);
2957 if (amdgpu_in_reset(adev))
2959 if (adev->in_suspend && !adev->in_runpm)
2962 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2964 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2968 ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
2970 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2971 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2976 return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2979 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2980 struct device_attribute *attr,
2984 struct amdgpu_device *adev = dev_get_drvdata(dev);
2989 if (amdgpu_in_reset(adev))
2991 if (adev->in_suspend && !adev->in_runpm)
2994 err = kstrtoint(buf, 10, &value);
2999 pwm_mode = AMD_FAN_CTRL_AUTO;
3000 else if (value == 1)
3001 pwm_mode = AMD_FAN_CTRL_MANUAL;
3005 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3007 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3011 err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
3013 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3014 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3022 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
3023 struct device_attribute *attr,
3026 struct amdgpu_device *adev = dev_get_drvdata(dev);
3030 /* get the voltage */
3031 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
3036 return sysfs_emit(buf, "%d\n", vddgfx);
3039 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
3040 struct device_attribute *attr,
3043 return sysfs_emit(buf, "vddgfx\n");
3046 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
3047 struct device_attribute *attr,
3050 struct amdgpu_device *adev = dev_get_drvdata(dev);
3054 /* only APUs have vddnb */
3055 if (!(adev->flags & AMD_IS_APU))
3058 /* get the voltage */
3059 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
3064 return sysfs_emit(buf, "%d\n", vddnb);
3067 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
3068 struct device_attribute *attr,
3071 return sysfs_emit(buf, "vddnb\n");
3074 static int amdgpu_hwmon_get_power(struct device *dev,
3075 enum amd_pp_sensors sensor)
3077 struct amdgpu_device *adev = dev_get_drvdata(dev);
3082 r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
3086 /* convert to microwatts */
3087 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
3092 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
3093 struct device_attribute *attr,
3098 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
3102 return sysfs_emit(buf, "%zd\n", val);
3105 static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
3106 struct device_attribute *attr,
3111 val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
3115 return sysfs_emit(buf, "%zd\n", val);
3118 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
3119 struct device_attribute *attr,
3121 enum pp_power_limit_level pp_limit_level)
3123 struct amdgpu_device *adev = dev_get_drvdata(dev);
3124 enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
3129 if (amdgpu_in_reset(adev))
3131 if (adev->in_suspend && !adev->in_runpm)
3134 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3136 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3140 r = amdgpu_dpm_get_power_limit(adev, &limit,
3141 pp_limit_level, power_type);
3144 size = sysfs_emit(buf, "%u\n", limit * 1000000);
3146 size = sysfs_emit(buf, "\n");
3148 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3149 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3154 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
3155 struct device_attribute *attr,
3158 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MIN);
3161 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
3162 struct device_attribute *attr,
3165 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
3169 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
3170 struct device_attribute *attr,
3173 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
3177 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
3178 struct device_attribute *attr,
3181 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
3185 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
3186 struct device_attribute *attr,
3189 struct amdgpu_device *adev = dev_get_drvdata(dev);
3190 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
3192 if (gc_ver == IP_VERSION(10, 3, 1))
3193 return sysfs_emit(buf, "%s\n",
3194 to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
3195 "fastPPT" : "slowPPT");
3197 return sysfs_emit(buf, "PPT\n");
3200 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3201 struct device_attribute *attr,
3205 struct amdgpu_device *adev = dev_get_drvdata(dev);
3206 int limit_type = to_sensor_dev_attr(attr)->index;
3210 if (amdgpu_in_reset(adev))
3212 if (adev->in_suspend && !adev->in_runpm)
3215 if (amdgpu_sriov_vf(adev))
3218 err = kstrtou32(buf, 10, &value);
3222 value = value / 1000000; /* convert to Watt */
3223 value |= limit_type << 24;
3225 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3227 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3231 err = amdgpu_dpm_set_power_limit(adev, value);
3233 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3234 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3242 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3243 struct device_attribute *attr,
3246 struct amdgpu_device *adev = dev_get_drvdata(dev);
3251 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3256 return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
3259 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3260 struct device_attribute *attr,
3263 return sysfs_emit(buf, "sclk\n");
3266 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3267 struct device_attribute *attr,
3270 struct amdgpu_device *adev = dev_get_drvdata(dev);
3275 r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3280 return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3283 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3284 struct device_attribute *attr,
3287 return sysfs_emit(buf, "mclk\n");
3293 * The amdgpu driver exposes the following sensor interfaces:
3295 * - GPU temperature (via the on-die sensor)
3299 * - Northbridge voltage (APUs only)
3305 * - GPU gfx/compute engine clock
3307 * - GPU memory clock (dGPU only)
3309 * hwmon interfaces for GPU temperature:
3311 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3312 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
3314 * - temp[1-3]_label: temperature channel label
3315 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
3317 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3318 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3320 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3321 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3323 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3324 * - these are supported on SOC15 dGPUs only
3326 * hwmon interfaces for GPU voltage:
3328 * - in0_input: the voltage on the GPU in millivolts
3330 * - in1_input: the voltage on the Northbridge in millivolts
3332 * hwmon interfaces for GPU power:
3334 * - power1_average: average power used by the SoC in microWatts. On APUs this includes the CPU.
3336 * - power1_input: instantaneous power used by the SoC in microWatts. On APUs this includes the CPU.
3338 * - power1_cap_min: minimum cap supported in microWatts
3340 * - power1_cap_max: maximum cap supported in microWatts
3342 * - power1_cap: selected power cap in microWatts
3344 * hwmon interfaces for GPU fan:
3346 * - pwm1: pulse width modulation fan level (0-255)
3348 * - 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)
3350 * - pwm1_min: pulse width modulation fan control minimum level (0)
3352 * - pwm1_max: pulse width modulation fan control maximum level (255)
3354 * - fan1_min: a minimum value Unit: revolution/min (RPM)
3356 * - fan1_max: a maximum value Unit: revolution/max (RPM)
3358 * - fan1_input: fan speed in RPM
3360 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3362 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3364 * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
3365 * That will get the former one overridden.
3367 * hwmon interfaces for GPU clocks:
3369 * - freq1_input: the gfx/compute clock in hertz
3371 * - freq2_input: the memory clock in hertz
3373 * You can use hwmon tools like sensors to view this information on your system.
3377 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3378 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3379 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3380 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3381 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3382 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3383 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3384 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3385 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3386 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3387 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3388 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3389 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3390 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3391 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3392 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3393 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3394 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3395 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3396 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3397 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3398 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3399 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3400 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3401 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3402 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3403 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3404 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3405 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3406 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
3407 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3408 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3409 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3410 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3411 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3412 static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3413 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3414 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3415 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
3416 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3417 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3418 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3419 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3420 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3421 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3423 static struct attribute *hwmon_attributes[] = {
3424 &sensor_dev_attr_temp1_input.dev_attr.attr,
3425 &sensor_dev_attr_temp1_crit.dev_attr.attr,
3426 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3427 &sensor_dev_attr_temp2_input.dev_attr.attr,
3428 &sensor_dev_attr_temp2_crit.dev_attr.attr,
3429 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3430 &sensor_dev_attr_temp3_input.dev_attr.attr,
3431 &sensor_dev_attr_temp3_crit.dev_attr.attr,
3432 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3433 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
3434 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
3435 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
3436 &sensor_dev_attr_temp1_label.dev_attr.attr,
3437 &sensor_dev_attr_temp2_label.dev_attr.attr,
3438 &sensor_dev_attr_temp3_label.dev_attr.attr,
3439 &sensor_dev_attr_pwm1.dev_attr.attr,
3440 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3441 &sensor_dev_attr_pwm1_min.dev_attr.attr,
3442 &sensor_dev_attr_pwm1_max.dev_attr.attr,
3443 &sensor_dev_attr_fan1_input.dev_attr.attr,
3444 &sensor_dev_attr_fan1_min.dev_attr.attr,
3445 &sensor_dev_attr_fan1_max.dev_attr.attr,
3446 &sensor_dev_attr_fan1_target.dev_attr.attr,
3447 &sensor_dev_attr_fan1_enable.dev_attr.attr,
3448 &sensor_dev_attr_in0_input.dev_attr.attr,
3449 &sensor_dev_attr_in0_label.dev_attr.attr,
3450 &sensor_dev_attr_in1_input.dev_attr.attr,
3451 &sensor_dev_attr_in1_label.dev_attr.attr,
3452 &sensor_dev_attr_power1_average.dev_attr.attr,
3453 &sensor_dev_attr_power1_input.dev_attr.attr,
3454 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
3455 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
3456 &sensor_dev_attr_power1_cap.dev_attr.attr,
3457 &sensor_dev_attr_power1_cap_default.dev_attr.attr,
3458 &sensor_dev_attr_power1_label.dev_attr.attr,
3459 &sensor_dev_attr_power2_average.dev_attr.attr,
3460 &sensor_dev_attr_power2_cap_max.dev_attr.attr,
3461 &sensor_dev_attr_power2_cap_min.dev_attr.attr,
3462 &sensor_dev_attr_power2_cap.dev_attr.attr,
3463 &sensor_dev_attr_power2_cap_default.dev_attr.attr,
3464 &sensor_dev_attr_power2_label.dev_attr.attr,
3465 &sensor_dev_attr_freq1_input.dev_attr.attr,
3466 &sensor_dev_attr_freq1_label.dev_attr.attr,
3467 &sensor_dev_attr_freq2_input.dev_attr.attr,
3468 &sensor_dev_attr_freq2_label.dev_attr.attr,
3472 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3473 struct attribute *attr, int index)
3475 struct device *dev = kobj_to_dev(kobj);
3476 struct amdgpu_device *adev = dev_get_drvdata(dev);
3477 umode_t effective_mode = attr->mode;
3478 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
3481 /* under pp one vf mode manage of hwmon attributes is not supported */
3482 if (amdgpu_sriov_is_pp_one_vf(adev))
3483 effective_mode &= ~S_IWUSR;
3485 /* Skip fan attributes if fan is not present */
3486 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3487 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3488 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3489 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3490 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3491 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3492 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3493 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3494 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3497 /* Skip fan attributes on APU */
3498 if ((adev->flags & AMD_IS_APU) &&
3499 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3500 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3501 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3502 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3503 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3504 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3505 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3506 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3507 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3510 /* Skip crit temp on APU */
3511 if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
3512 (gc_ver == IP_VERSION(9, 4, 3))) &&
3513 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3514 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3517 /* Skip limit attributes if DPM is not enabled */
3518 if (!adev->pm.dpm_enabled &&
3519 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3520 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3521 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3522 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3523 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3524 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3525 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3526 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3527 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3528 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3529 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3532 /* mask fan attributes if we have no bindings for this asic to expose */
3533 if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3534 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3535 ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
3536 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3537 effective_mode &= ~S_IRUGO;
3539 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3540 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3541 ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
3542 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3543 effective_mode &= ~S_IWUSR;
3545 /* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
3546 if (((adev->family == AMDGPU_FAMILY_SI) ||
3547 ((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(10, 3, 1)) &&
3548 (gc_ver != IP_VERSION(9, 4, 3)))) &&
3549 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3550 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
3551 attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
3552 attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3555 /* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
3556 if (((adev->family == AMDGPU_FAMILY_SI) ||
3557 ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
3558 (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3561 /* not all products support both average and instantaneous */
3562 if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
3563 amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
3565 if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
3566 amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
3569 /* hide max/min values if we can't both query and manage the fan */
3570 if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
3571 (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
3572 (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3573 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
3574 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3575 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3578 if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
3579 (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
3580 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3581 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3584 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
3585 adev->family == AMDGPU_FAMILY_KV || /* not implemented yet */
3586 (gc_ver == IP_VERSION(9, 4, 3))) &&
3587 (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3588 attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3591 /* only APUs other than gc 9,4,3 have vddnb */
3592 if ((!(adev->flags & AMD_IS_APU) || (gc_ver == IP_VERSION(9, 4, 3))) &&
3593 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3594 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3597 /* no mclk on APUs other than gc 9,4,3*/
3598 if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
3599 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3600 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3603 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3604 (gc_ver != IP_VERSION(9, 4, 3)) &&
3605 (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3606 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3607 attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3608 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3609 attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
3610 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
3613 /* hotspot temperature for gc 9,4,3*/
3614 if (gc_ver == IP_VERSION(9, 4, 3)) {
3615 if (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
3616 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3617 attr == &sensor_dev_attr_temp1_label.dev_attr.attr)
3620 if (attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3621 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)
3625 /* only SOC15 dGPUs support hotspot and mem temperatures */
3626 if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
3627 (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3628 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3629 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3630 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3631 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
3634 /* only Vangogh has fast PPT limit and power labels */
3635 if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
3636 (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3637 attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3638 attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3639 attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
3640 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3641 attr == &sensor_dev_attr_power2_label.dev_attr.attr))
3644 return effective_mode;
3647 static const struct attribute_group hwmon_attrgroup = {
3648 .attrs = hwmon_attributes,
3649 .is_visible = hwmon_attributes_visible,
3652 static const struct attribute_group *hwmon_groups[] = {
3657 static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
3658 enum pp_clock_type od_type,
3664 if (amdgpu_in_reset(adev))
3666 if (adev->in_suspend && !adev->in_runpm)
3669 ret = pm_runtime_get_sync(adev->dev);
3671 pm_runtime_put_autosuspend(adev->dev);
3675 size = amdgpu_dpm_print_clock_levels(adev, od_type, buf);
3677 size = sysfs_emit(buf, "\n");
3679 pm_runtime_mark_last_busy(adev->dev);
3680 pm_runtime_put_autosuspend(adev->dev);
3685 static int parse_input_od_command_lines(const char *buf,
3689 uint32_t *num_of_params)
3691 const char delimiter[3] = {' ', '\n', '\0'};
3692 uint32_t parameter_size = 0;
3693 char buf_cpy[128] = {0};
3694 char *tmp_str, *sub_str;
3697 if (count > sizeof(buf_cpy) - 1)
3700 memcpy(buf_cpy, buf, count);
3703 /* skip heading spaces */
3704 while (isspace(*tmp_str))
3709 *type = PP_OD_COMMIT_DPM_TABLE;
3712 params[parameter_size] = *type;
3714 *type = PP_OD_RESTORE_DEFAULT_TABLE;
3720 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
3721 if (strlen(sub_str) == 0)
3724 ret = kstrtol(sub_str, 0, ¶ms[parameter_size]);
3729 while (isspace(*tmp_str))
3733 *num_of_params = parameter_size;
3739 amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
3740 enum PP_OD_DPM_TABLE_COMMAND cmd_type,
3744 uint32_t parameter_size = 0;
3748 if (amdgpu_in_reset(adev))
3750 if (adev->in_suspend && !adev->in_runpm)
3753 ret = parse_input_od_command_lines(in_buf,
3761 ret = pm_runtime_get_sync(adev->dev);
3765 ret = amdgpu_dpm_odn_edit_dpm_table(adev,
3772 if (cmd_type == PP_OD_COMMIT_DPM_TABLE) {
3773 ret = amdgpu_dpm_dispatch_task(adev,
3774 AMD_PP_TASK_READJUST_POWER_STATE,
3780 pm_runtime_mark_last_busy(adev->dev);
3781 pm_runtime_put_autosuspend(adev->dev);
3786 pm_runtime_mark_last_busy(adev->dev);
3788 pm_runtime_put_autosuspend(adev->dev);
3796 * The amdgpu driver provides a sysfs API for checking and adjusting the fan
3797 * control curve line.
3799 * Reading back the file shows you the current settings(temperature in Celsius
3800 * degree and fan speed in pwm) applied to every anchor point of the curve line
3801 * and their permitted ranges if changable.
3803 * Writing a desired string(with the format like "anchor_point_index temperature
3804 * fan_speed_in_pwm") to the file, change the settings for the specific anchor
3805 * point accordingly.
3807 * When you have finished the editing, write "c" (commit) to the file to commit
3810 * If you want to reset to the default value, write "r" (reset) to the file to
3813 * There are two fan control modes supported: auto and manual. With auto mode,
3814 * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature).
3815 * While with manual mode, users can set their own fan curve line as what
3816 * described here. Normally the ASIC is booted up with auto mode. Any
3817 * settings via this interface will switch the fan control to manual mode
3820 static ssize_t fan_curve_show(struct kobject *kobj,
3821 struct kobj_attribute *attr,
3824 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3825 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3827 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf);
3830 static ssize_t fan_curve_store(struct kobject *kobj,
3831 struct kobj_attribute *attr,
3835 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3836 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3838 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3839 PP_OD_EDIT_FAN_CURVE,
3844 static umode_t fan_curve_visible(struct amdgpu_device *adev)
3846 umode_t umode = 0000;
3848 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE)
3849 umode |= S_IRUSR | S_IRGRP | S_IROTH;
3851 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET)
3858 * DOC: acoustic_limit_rpm_threshold
3860 * The amdgpu driver provides a sysfs API for checking and adjusting the
3861 * acoustic limit in RPM for fan control.
3863 * Reading back the file shows you the current setting and the permitted
3864 * ranges if changable.
3866 * Writing an integer to the file, change the setting accordingly.
3868 * When you have finished the editing, write "c" (commit) to the file to commit
3871 * If you want to reset to the default value, write "r" (reset) to the file to
3874 * This setting works under auto fan control mode only. It adjusts the PMFW's
3875 * behavior about the maximum speed in RPM the fan can spin. Setting via this
3876 * interface will switch the fan control to auto mode implicitly.
3878 static ssize_t acoustic_limit_threshold_show(struct kobject *kobj,
3879 struct kobj_attribute *attr,
3882 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3883 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3885 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf);
3888 static ssize_t acoustic_limit_threshold_store(struct kobject *kobj,
3889 struct kobj_attribute *attr,
3893 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3894 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3896 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3897 PP_OD_EDIT_ACOUSTIC_LIMIT,
3902 static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev)
3904 umode_t umode = 0000;
3906 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE)
3907 umode |= S_IRUSR | S_IRGRP | S_IROTH;
3909 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET)
3916 * DOC: acoustic_target_rpm_threshold
3918 * The amdgpu driver provides a sysfs API for checking and adjusting the
3919 * acoustic target in RPM for fan control.
3921 * Reading back the file shows you the current setting and the permitted
3922 * ranges if changable.
3924 * Writing an integer to the file, change the setting accordingly.
3926 * When you have finished the editing, write "c" (commit) to the file to commit
3929 * If you want to reset to the default value, write "r" (reset) to the file to
3932 * This setting works under auto fan control mode only. It can co-exist with
3933 * other settings which can work also under auto mode. It adjusts the PMFW's
3934 * behavior about the maximum speed in RPM the fan can spin when ASIC
3935 * temperature is not greater than target temperature. Setting via this
3936 * interface will switch the fan control to auto mode implicitly.
3938 static ssize_t acoustic_target_threshold_show(struct kobject *kobj,
3939 struct kobj_attribute *attr,
3942 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3943 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3945 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf);
3948 static ssize_t acoustic_target_threshold_store(struct kobject *kobj,
3949 struct kobj_attribute *attr,
3953 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
3954 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
3956 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
3957 PP_OD_EDIT_ACOUSTIC_TARGET,
3962 static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev)
3964 umode_t umode = 0000;
3966 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE)
3967 umode |= S_IRUSR | S_IRGRP | S_IROTH;
3969 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET)
3976 * DOC: fan_target_temperature
3978 * The amdgpu driver provides a sysfs API for checking and adjusting the
3979 * target tempeature in Celsius degree for fan control.
3981 * Reading back the file shows you the current setting and the permitted
3982 * ranges if changable.
3984 * Writing an integer to the file, change the setting accordingly.
3986 * When you have finished the editing, write "c" (commit) to the file to commit
3989 * If you want to reset to the default value, write "r" (reset) to the file to
3992 * This setting works under auto fan control mode only. It can co-exist with
3993 * other settings which can work also under auto mode. Paring with the
3994 * acoustic_target_rpm_threshold setting, they define the maximum speed in
3995 * RPM the fan can spin when ASIC temperature is not greater than target
3996 * temperature. Setting via this interface will switch the fan control to
3997 * auto mode implicitly.
3999 static ssize_t fan_target_temperature_show(struct kobject *kobj,
4000 struct kobj_attribute *attr,
4003 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4004 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4006 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf);
4009 static ssize_t fan_target_temperature_store(struct kobject *kobj,
4010 struct kobj_attribute *attr,
4014 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4015 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4017 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4018 PP_OD_EDIT_FAN_TARGET_TEMPERATURE,
4023 static umode_t fan_target_temperature_visible(struct amdgpu_device *adev)
4025 umode_t umode = 0000;
4027 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE)
4028 umode |= S_IRUSR | S_IRGRP | S_IROTH;
4030 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET)
4037 * DOC: fan_minimum_pwm
4039 * The amdgpu driver provides a sysfs API for checking and adjusting the
4040 * minimum fan speed in PWM.
4042 * Reading back the file shows you the current setting and the permitted
4043 * ranges if changable.
4045 * Writing an integer to the file, change the setting accordingly.
4047 * When you have finished the editing, write "c" (commit) to the file to commit
4050 * If you want to reset to the default value, write "r" (reset) to the file to
4053 * This setting works under auto fan control mode only. It can co-exist with
4054 * other settings which can work also under auto mode. It adjusts the PMFW's
4055 * behavior about the minimum fan speed in PWM the fan should spin. Setting
4056 * via this interface will switch the fan control to auto mode implicitly.
4058 static ssize_t fan_minimum_pwm_show(struct kobject *kobj,
4059 struct kobj_attribute *attr,
4062 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4063 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4065 return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf);
4068 static ssize_t fan_minimum_pwm_store(struct kobject *kobj,
4069 struct kobj_attribute *attr,
4073 struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
4074 struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
4076 return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
4077 PP_OD_EDIT_FAN_MINIMUM_PWM,
4082 static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev)
4084 umode_t umode = 0000;
4086 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE)
4087 umode |= S_IRUSR | S_IRGRP | S_IROTH;
4089 if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET)
4095 static struct od_feature_set amdgpu_od_set = {
4101 .name = "fan_curve",
4103 .is_visible = fan_curve_visible,
4104 .show = fan_curve_show,
4105 .store = fan_curve_store,
4109 .name = "acoustic_limit_rpm_threshold",
4111 .is_visible = acoustic_limit_threshold_visible,
4112 .show = acoustic_limit_threshold_show,
4113 .store = acoustic_limit_threshold_store,
4117 .name = "acoustic_target_rpm_threshold",
4119 .is_visible = acoustic_target_threshold_visible,
4120 .show = acoustic_target_threshold_show,
4121 .store = acoustic_target_threshold_store,
4125 .name = "fan_target_temperature",
4127 .is_visible = fan_target_temperature_visible,
4128 .show = fan_target_temperature_show,
4129 .store = fan_target_temperature_store,
4133 .name = "fan_minimum_pwm",
4135 .is_visible = fan_minimum_pwm_visible,
4136 .show = fan_minimum_pwm_show,
4137 .store = fan_minimum_pwm_store,
4145 static void od_kobj_release(struct kobject *kobj)
4147 struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj);
4152 static const struct kobj_type od_ktype = {
4153 .release = od_kobj_release,
4154 .sysfs_ops = &kobj_sysfs_ops,
4157 static void amdgpu_od_set_fini(struct amdgpu_device *adev)
4159 struct od_kobj *container, *container_next;
4160 struct od_attribute *attribute, *attribute_next;
4162 if (list_empty(&adev->pm.od_kobj_list))
4165 list_for_each_entry_safe(container, container_next,
4166 &adev->pm.od_kobj_list, entry) {
4167 list_del(&container->entry);
4169 list_for_each_entry_safe(attribute, attribute_next,
4170 &container->attribute, entry) {
4171 list_del(&attribute->entry);
4172 sysfs_remove_file(&container->kobj,
4173 &attribute->attribute.attr);
4177 kobject_put(&container->kobj);
4181 static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev,
4182 struct od_feature_ops *feature_ops)
4186 if (!feature_ops->is_visible)
4190 * If the feature has no user read and write mode set,
4191 * we can assume the feature is actually not supported.(?)
4192 * And the revelant sysfs interface should not be exposed.
4194 mode = feature_ops->is_visible(adev);
4195 if (mode & (S_IRUSR | S_IWUSR))
4201 static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev,
4202 struct od_feature_container *container)
4207 * If there is no valid entry within the container, the container
4208 * is recognized as a self contained container. And the valid entry
4209 * here means it has a valid naming and it is visible/supported by
4212 for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) {
4213 if (container->sub_feature[i].name &&
4214 amdgpu_is_od_feature_supported(adev,
4215 &container->sub_feature[i].ops))
4222 static int amdgpu_od_set_init(struct amdgpu_device *adev)
4224 struct od_kobj *top_set, *sub_set;
4225 struct od_attribute *attribute;
4226 struct od_feature_container *container;
4227 struct od_feature_item *feature;
4231 /* Setup the top `gpu_od` directory which holds all other OD interfaces */
4232 top_set = kzalloc(sizeof(*top_set), GFP_KERNEL);
4235 list_add(&top_set->entry, &adev->pm.od_kobj_list);
4237 ret = kobject_init_and_add(&top_set->kobj,
4244 INIT_LIST_HEAD(&top_set->attribute);
4245 top_set->priv = adev;
4247 for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) {
4248 container = &amdgpu_od_set.containers[i];
4250 if (!container->name)
4254 * If there is valid entries within the container, the container
4255 * will be presented as a sub directory and all its holding entries
4256 * will be presented as plain files under it.
4257 * While if there is no valid entry within the container, the container
4258 * itself will be presented as a plain file under top `gpu_od` directory.
4260 if (amdgpu_od_is_self_contained(adev, container)) {
4261 if (!amdgpu_is_od_feature_supported(adev,
4266 * The container is presented as a plain file under top `gpu_od`
4269 attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
4274 list_add(&attribute->entry, &top_set->attribute);
4276 attribute->attribute.attr.mode =
4277 container->ops.is_visible(adev);
4278 attribute->attribute.attr.name = container->name;
4279 attribute->attribute.show =
4280 container->ops.show;
4281 attribute->attribute.store =
4282 container->ops.store;
4283 ret = sysfs_create_file(&top_set->kobj,
4284 &attribute->attribute.attr);
4288 /* The container is presented as a sub directory. */
4289 sub_set = kzalloc(sizeof(*sub_set), GFP_KERNEL);
4294 list_add(&sub_set->entry, &adev->pm.od_kobj_list);
4296 ret = kobject_init_and_add(&sub_set->kobj,
4303 INIT_LIST_HEAD(&sub_set->attribute);
4304 sub_set->priv = adev;
4306 for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) {
4307 feature = &container->sub_feature[j];
4311 if (!amdgpu_is_od_feature_supported(adev,
4316 * With the container presented as a sub directory, the entry within
4317 * it is presented as a plain file under the sub directory.
4319 attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
4324 list_add(&attribute->entry, &sub_set->attribute);
4326 attribute->attribute.attr.mode =
4327 feature->ops.is_visible(adev);
4328 attribute->attribute.attr.name = feature->name;
4329 attribute->attribute.show =
4331 attribute->attribute.store =
4333 ret = sysfs_create_file(&sub_set->kobj,
4334 &attribute->attribute.attr);
4342 * If gpu_od is the only member in the list, that means gpu_od is an
4343 * empty directory, so remove it.
4345 if (list_is_singular(&adev->pm.od_kobj_list))
4351 amdgpu_od_set_fini(adev);
4356 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
4358 enum amdgpu_sriov_vf_mode mode;
4362 if (adev->pm.sysfs_initialized)
4365 INIT_LIST_HEAD(&adev->pm.pm_attr_list);
4367 if (adev->pm.dpm_enabled == 0)
4370 mode = amdgpu_virt_get_sriov_vf_mode(adev);
4372 /* under multi-vf mode, the hwmon attributes are all not supported */
4373 if (mode != SRIOV_VF_MODE_MULTI_VF) {
4374 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
4377 if (IS_ERR(adev->pm.int_hwmon_dev)) {
4378 ret = PTR_ERR(adev->pm.int_hwmon_dev);
4379 dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret);
4385 case SRIOV_VF_MODE_ONE_VF:
4386 mask = ATTR_FLAG_ONEVF;
4388 case SRIOV_VF_MODE_MULTI_VF:
4391 case SRIOV_VF_MODE_BARE_METAL:
4393 mask = ATTR_FLAG_MASK_ALL;
4397 ret = amdgpu_device_attr_create_groups(adev,
4398 amdgpu_device_attrs,
4399 ARRAY_SIZE(amdgpu_device_attrs),
4401 &adev->pm.pm_attr_list);
4405 if (amdgpu_dpm_is_overdrive_supported(adev)) {
4406 ret = amdgpu_od_set_init(adev);
4409 } else if (adev->pm.pp_feature & PP_OVERDRIVE_MASK) {
4410 dev_info(adev->dev, "overdrive feature is not supported\n");
4413 adev->pm.sysfs_initialized = true;
4418 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4420 if (adev->pm.int_hwmon_dev)
4421 hwmon_device_unregister(adev->pm.int_hwmon_dev);
4426 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
4428 amdgpu_od_set_fini(adev);
4430 if (adev->pm.int_hwmon_dev)
4431 hwmon_device_unregister(adev->pm.int_hwmon_dev);
4433 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
4439 #if defined(CONFIG_DEBUG_FS)
4441 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
4442 struct amdgpu_device *adev)
4447 uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
4449 if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
4450 p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
4453 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
4454 (void *)p_val, &size)) {
4455 for (i = 0; i < num_cpu_cores; i++)
4456 seq_printf(m, "\t%u MHz (CPU%d)\n",
4464 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
4466 uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
4467 uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
4469 uint64_t value64 = 0;
4474 size = sizeof(value);
4475 seq_printf(m, "GFX Clocks and Power:\n");
4477 amdgpu_debugfs_prints_cpu_info(m, adev);
4479 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
4480 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
4481 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
4482 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
4483 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
4484 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
4485 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
4486 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
4487 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
4488 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
4489 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
4490 seq_printf(m, "\t%u mV (VDDNB)\n", value);
4491 size = sizeof(uint32_t);
4492 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size)) {
4493 if (adev->flags & AMD_IS_APU)
4494 seq_printf(m, "\t%u.%02u W (average SoC including CPU)\n", query >> 8, query & 0xff);
4496 seq_printf(m, "\t%u.%02u W (average SoC)\n", query >> 8, query & 0xff);
4498 size = sizeof(uint32_t);
4499 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size)) {
4500 if (adev->flags & AMD_IS_APU)
4501 seq_printf(m, "\t%u.%02u W (current SoC including CPU)\n", query >> 8, query & 0xff);
4503 seq_printf(m, "\t%u.%02u W (current SoC)\n", query >> 8, query & 0xff);
4505 size = sizeof(value);
4506 seq_printf(m, "\n");
4509 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
4510 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
4513 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
4514 seq_printf(m, "GPU Load: %u %%\n", value);
4516 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
4517 seq_printf(m, "MEM Load: %u %%\n", value);
4519 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_LOAD, (void *)&value, &size))
4520 seq_printf(m, "VCN Load: %u %%\n", value);
4522 seq_printf(m, "\n");
4524 /* SMC feature mask */
4525 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
4526 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
4528 /* ASICs greater than CHIP_VEGA20 supports these sensors */
4529 if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
4531 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
4533 seq_printf(m, "VCN: Powered down\n");
4535 seq_printf(m, "VCN: Powered up\n");
4536 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4537 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4538 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4539 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4542 seq_printf(m, "\n");
4545 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
4547 seq_printf(m, "UVD: Powered down\n");
4549 seq_printf(m, "UVD: Powered up\n");
4550 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
4551 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
4552 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
4553 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
4556 seq_printf(m, "\n");
4559 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
4561 seq_printf(m, "VCE: Powered down\n");
4563 seq_printf(m, "VCE: Powered up\n");
4564 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
4565 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
4573 static const struct cg_flag_name clocks[] = {
4574 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
4575 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
4576 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
4577 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
4578 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
4579 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
4580 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
4581 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
4582 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
4583 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
4584 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
4585 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
4586 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
4587 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
4588 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
4589 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
4590 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
4591 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
4592 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
4593 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
4594 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
4595 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
4596 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
4597 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
4598 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
4599 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
4600 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
4601 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
4602 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
4603 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
4604 {AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
4605 {AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
4606 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
4607 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
4611 static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
4615 for (i = 0; clocks[i].flag; i++)
4616 seq_printf(m, "\t%s: %s\n", clocks[i].name,
4617 (flags & clocks[i].flag) ? "On" : "Off");
4620 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
4622 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
4623 struct drm_device *dev = adev_to_drm(adev);
4627 if (amdgpu_in_reset(adev))
4629 if (adev->in_suspend && !adev->in_runpm)
4632 r = pm_runtime_get_sync(dev->dev);
4634 pm_runtime_put_autosuspend(dev->dev);
4638 if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
4639 r = amdgpu_debugfs_pm_info_pp(m, adev);
4644 amdgpu_device_ip_get_clockgating_state(adev, &flags);
4646 seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
4647 amdgpu_parse_cg_state(m, flags);
4648 seq_printf(m, "\n");
4651 pm_runtime_mark_last_busy(dev->dev);
4652 pm_runtime_put_autosuspend(dev->dev);
4657 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
4660 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
4662 * Reads debug memory region allocated to PMFW
4664 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
4665 size_t size, loff_t *pos)
4667 struct amdgpu_device *adev = file_inode(f)->i_private;
4668 size_t smu_prv_buf_size;
4672 if (amdgpu_in_reset(adev))
4674 if (adev->in_suspend && !adev->in_runpm)
4677 ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
4681 if (!smu_prv_buf || !smu_prv_buf_size)
4684 return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
4688 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
4689 .owner = THIS_MODULE,
4690 .open = simple_open,
4691 .read = amdgpu_pm_prv_buffer_read,
4692 .llseek = default_llseek,
4697 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
4699 #if defined(CONFIG_DEBUG_FS)
4700 struct drm_minor *minor = adev_to_drm(adev)->primary;
4701 struct dentry *root = minor->debugfs_root;
4703 if (!adev->pm.dpm_enabled)
4706 debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
4707 &amdgpu_debugfs_pm_info_fops);
4709 if (adev->pm.smu_prv_buffer_size > 0)
4710 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
4712 &amdgpu_debugfs_pm_prv_buffer_fops,
4713 adev->pm.smu_prv_buffer_size);
4715 amdgpu_dpm_stb_debug_fs_init(adev);