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"
30 #include "amdgpu_display.h"
31 #include "amdgpu_smu.h"
33 #include <linux/power_supply.h>
34 #include <linux/hwmon.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/nospec.h>
40 static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
42 static const struct cg_flag_name clocks[] = {
43 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
44 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
45 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
46 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
47 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
48 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
49 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
50 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
51 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
52 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
53 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
54 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
55 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
56 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
57 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
58 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
59 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
60 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
61 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
62 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
63 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
64 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
65 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
66 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
70 static const struct hwmon_temp_label {
71 enum PP_HWMON_TEMP channel;
74 {PP_TEMP_EDGE, "edge"},
75 {PP_TEMP_JUNCTION, "junction"},
79 void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
81 if (adev->pm.dpm_enabled) {
82 mutex_lock(&adev->pm.mutex);
83 if (power_supply_is_system_supplied() > 0)
84 adev->pm.ac_power = true;
86 adev->pm.ac_power = false;
87 if (adev->powerplay.pp_funcs->enable_bapm)
88 amdgpu_dpm_enable_bapm(adev, adev->pm.ac_power);
89 mutex_unlock(&adev->pm.mutex);
93 int amdgpu_dpm_read_sensor(struct amdgpu_device *adev, enum amd_pp_sensors sensor,
94 void *data, uint32_t *size)
101 if (is_support_sw_smu(adev))
102 ret = smu_read_sensor(&adev->smu, sensor, data, size);
104 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
105 ret = adev->powerplay.pp_funcs->read_sensor((adev)->powerplay.pp_handle,
115 * DOC: power_dpm_state
117 * The power_dpm_state file is a legacy interface and is only provided for
118 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
119 * certain power related parameters. The file power_dpm_state is used for this.
120 * It accepts the following arguments:
130 * On older GPUs, the vbios provided a special power state for battery
131 * operation. Selecting battery switched to this state. This is no
132 * longer provided on newer GPUs so the option does nothing in that case.
136 * On older GPUs, the vbios provided a special power state for balanced
137 * operation. Selecting balanced switched to this state. This is no
138 * longer provided on newer GPUs so the option does nothing in that case.
142 * On older GPUs, the vbios provided a special power state for performance
143 * operation. Selecting performance switched to this state. This is no
144 * longer provided on newer GPUs so the option does nothing in that case.
148 static ssize_t amdgpu_get_dpm_state(struct device *dev,
149 struct device_attribute *attr,
152 struct drm_device *ddev = dev_get_drvdata(dev);
153 struct amdgpu_device *adev = ddev->dev_private;
154 enum amd_pm_state_type pm;
156 if (is_support_sw_smu(adev) && adev->smu.ppt_funcs->get_current_power_state)
157 pm = amdgpu_smu_get_current_power_state(adev);
158 else if (adev->powerplay.pp_funcs->get_current_power_state)
159 pm = amdgpu_dpm_get_current_power_state(adev);
161 pm = adev->pm.dpm.user_state;
163 return snprintf(buf, PAGE_SIZE, "%s\n",
164 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
165 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
168 static ssize_t amdgpu_set_dpm_state(struct device *dev,
169 struct device_attribute *attr,
173 struct drm_device *ddev = dev_get_drvdata(dev);
174 struct amdgpu_device *adev = ddev->dev_private;
175 enum amd_pm_state_type state;
177 if (strncmp("battery", buf, strlen("battery")) == 0)
178 state = POWER_STATE_TYPE_BATTERY;
179 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
180 state = POWER_STATE_TYPE_BALANCED;
181 else if (strncmp("performance", buf, strlen("performance")) == 0)
182 state = POWER_STATE_TYPE_PERFORMANCE;
188 if (adev->powerplay.pp_funcs->dispatch_tasks) {
189 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
191 mutex_lock(&adev->pm.mutex);
192 adev->pm.dpm.user_state = state;
193 mutex_unlock(&adev->pm.mutex);
195 /* Can't set dpm state when the card is off */
196 if (!(adev->flags & AMD_IS_PX) ||
197 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
198 amdgpu_pm_compute_clocks(adev);
206 * DOC: power_dpm_force_performance_level
208 * The amdgpu driver provides a sysfs API for adjusting certain power
209 * related parameters. The file power_dpm_force_performance_level is
210 * used for this. It accepts the following arguments:
230 * When auto is selected, the driver will attempt to dynamically select
231 * the optimal power profile for current conditions in the driver.
235 * When low is selected, the clocks are forced to the lowest power state.
239 * When high is selected, the clocks are forced to the highest power state.
243 * When manual is selected, the user can manually adjust which power states
244 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
245 * and pp_dpm_pcie files and adjust the power state transition heuristics
246 * via the pp_power_profile_mode sysfs file.
253 * When the profiling modes are selected, clock and power gating are
254 * disabled and the clocks are set for different profiling cases. This
255 * mode is recommended for profiling specific work loads where you do
256 * not want clock or power gating for clock fluctuation to interfere
257 * with your results. profile_standard sets the clocks to a fixed clock
258 * level which varies from asic to asic. profile_min_sclk forces the sclk
259 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
260 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
264 static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
265 struct device_attribute *attr,
268 struct drm_device *ddev = dev_get_drvdata(dev);
269 struct amdgpu_device *adev = ddev->dev_private;
270 enum amd_dpm_forced_level level = 0xff;
272 if ((adev->flags & AMD_IS_PX) &&
273 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
274 return snprintf(buf, PAGE_SIZE, "off\n");
276 if (is_support_sw_smu(adev))
277 level = smu_get_performance_level(&adev->smu);
278 else if (adev->powerplay.pp_funcs->get_performance_level)
279 level = amdgpu_dpm_get_performance_level(adev);
281 level = adev->pm.dpm.forced_level;
283 return snprintf(buf, PAGE_SIZE, "%s\n",
284 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
285 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
286 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
287 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
288 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
289 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
290 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
291 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
295 static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
296 struct device_attribute *attr,
300 struct drm_device *ddev = dev_get_drvdata(dev);
301 struct amdgpu_device *adev = ddev->dev_private;
302 enum amd_dpm_forced_level level;
303 enum amd_dpm_forced_level current_level = 0xff;
306 /* Can't force performance level when the card is off */
307 if ((adev->flags & AMD_IS_PX) &&
308 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
311 if (is_support_sw_smu(adev))
312 current_level = smu_get_performance_level(&adev->smu);
313 else if (adev->powerplay.pp_funcs->get_performance_level)
314 current_level = amdgpu_dpm_get_performance_level(adev);
316 if (strncmp("low", buf, strlen("low")) == 0) {
317 level = AMD_DPM_FORCED_LEVEL_LOW;
318 } else if (strncmp("high", buf, strlen("high")) == 0) {
319 level = AMD_DPM_FORCED_LEVEL_HIGH;
320 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
321 level = AMD_DPM_FORCED_LEVEL_AUTO;
322 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
323 level = AMD_DPM_FORCED_LEVEL_MANUAL;
324 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
325 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
326 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
327 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
328 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
329 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
330 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
331 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
332 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
333 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
339 if (amdgpu_sriov_vf(adev)) {
340 if (amdgim_is_hwperf(adev) &&
341 adev->virt.ops->force_dpm_level) {
342 mutex_lock(&adev->pm.mutex);
343 adev->virt.ops->force_dpm_level(adev, level);
344 mutex_unlock(&adev->pm.mutex);
351 if (current_level == level)
354 /* profile_exit setting is valid only when current mode is in profile mode */
355 if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
356 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
357 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
358 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
359 (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
360 pr_err("Currently not in any profile mode!\n");
364 if (is_support_sw_smu(adev)) {
365 mutex_lock(&adev->pm.mutex);
366 if (adev->pm.dpm.thermal_active) {
368 mutex_unlock(&adev->pm.mutex);
371 ret = smu_force_performance_level(&adev->smu, level);
375 adev->pm.dpm.forced_level = level;
376 mutex_unlock(&adev->pm.mutex);
377 } else if (adev->powerplay.pp_funcs->force_performance_level) {
378 mutex_lock(&adev->pm.mutex);
379 if (adev->pm.dpm.thermal_active) {
381 mutex_unlock(&adev->pm.mutex);
384 ret = amdgpu_dpm_force_performance_level(adev, level);
388 adev->pm.dpm.forced_level = level;
389 mutex_unlock(&adev->pm.mutex);
396 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
397 struct device_attribute *attr,
400 struct drm_device *ddev = dev_get_drvdata(dev);
401 struct amdgpu_device *adev = ddev->dev_private;
402 struct pp_states_info data;
405 if (is_support_sw_smu(adev)) {
406 ret = smu_get_power_num_states(&adev->smu, &data);
409 } else if (adev->powerplay.pp_funcs->get_pp_num_states)
410 amdgpu_dpm_get_pp_num_states(adev, &data);
412 buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
413 for (i = 0; i < data.nums; i++)
414 buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
415 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
416 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
417 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
418 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
423 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
424 struct device_attribute *attr,
427 struct drm_device *ddev = dev_get_drvdata(dev);
428 struct amdgpu_device *adev = ddev->dev_private;
429 struct pp_states_info data;
430 struct smu_context *smu = &adev->smu;
431 enum amd_pm_state_type pm = 0;
434 if (is_support_sw_smu(adev)) {
435 pm = smu_get_current_power_state(smu);
436 ret = smu_get_power_num_states(smu, &data);
439 } else if (adev->powerplay.pp_funcs->get_current_power_state
440 && adev->powerplay.pp_funcs->get_pp_num_states) {
441 pm = amdgpu_dpm_get_current_power_state(adev);
442 amdgpu_dpm_get_pp_num_states(adev, &data);
445 for (i = 0; i < data.nums; i++) {
446 if (pm == data.states[i])
453 return snprintf(buf, PAGE_SIZE, "%d\n", i);
456 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
457 struct device_attribute *attr,
460 struct drm_device *ddev = dev_get_drvdata(dev);
461 struct amdgpu_device *adev = ddev->dev_private;
463 if (adev->pp_force_state_enabled)
464 return amdgpu_get_pp_cur_state(dev, attr, buf);
466 return snprintf(buf, PAGE_SIZE, "\n");
469 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
470 struct device_attribute *attr,
474 struct drm_device *ddev = dev_get_drvdata(dev);
475 struct amdgpu_device *adev = ddev->dev_private;
476 enum amd_pm_state_type state = 0;
480 if (strlen(buf) == 1)
481 adev->pp_force_state_enabled = false;
482 else if (is_support_sw_smu(adev))
483 adev->pp_force_state_enabled = false;
484 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
485 adev->powerplay.pp_funcs->get_pp_num_states) {
486 struct pp_states_info data;
488 ret = kstrtoul(buf, 0, &idx);
489 if (ret || idx >= ARRAY_SIZE(data.states)) {
493 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
495 amdgpu_dpm_get_pp_num_states(adev, &data);
496 state = data.states[idx];
497 /* only set user selected power states */
498 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
499 state != POWER_STATE_TYPE_DEFAULT) {
500 amdgpu_dpm_dispatch_task(adev,
501 AMD_PP_TASK_ENABLE_USER_STATE, &state);
502 adev->pp_force_state_enabled = true;
512 * The amdgpu driver provides a sysfs API for uploading new powerplay
513 * tables. The file pp_table is used for this. Reading the file
514 * will dump the current power play table. Writing to the file
515 * will attempt to upload a new powerplay table and re-initialize
516 * powerplay using that new table.
520 static ssize_t amdgpu_get_pp_table(struct device *dev,
521 struct device_attribute *attr,
524 struct drm_device *ddev = dev_get_drvdata(dev);
525 struct amdgpu_device *adev = ddev->dev_private;
529 if (is_support_sw_smu(adev)) {
530 size = smu_sys_get_pp_table(&adev->smu, (void **)&table);
534 else if (adev->powerplay.pp_funcs->get_pp_table)
535 size = amdgpu_dpm_get_pp_table(adev, &table);
539 if (size >= PAGE_SIZE)
540 size = PAGE_SIZE - 1;
542 memcpy(buf, table, size);
547 static ssize_t amdgpu_set_pp_table(struct device *dev,
548 struct device_attribute *attr,
552 struct drm_device *ddev = dev_get_drvdata(dev);
553 struct amdgpu_device *adev = ddev->dev_private;
556 if (is_support_sw_smu(adev)) {
557 ret = smu_sys_set_pp_table(&adev->smu, (void *)buf, count);
560 } else if (adev->powerplay.pp_funcs->set_pp_table)
561 amdgpu_dpm_set_pp_table(adev, buf, count);
567 * DOC: pp_od_clk_voltage
569 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
570 * in each power level within a power state. The pp_od_clk_voltage is used for
573 * < For Vega10 and previous ASICs >
575 * Reading the file will display:
577 * - a list of engine clock levels and voltages labeled OD_SCLK
579 * - a list of memory clock levels and voltages labeled OD_MCLK
581 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
583 * To manually adjust these settings, first select manual using
584 * power_dpm_force_performance_level. Enter a new value for each
585 * level by writing a string that contains "s/m level clock voltage" to
586 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
587 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
588 * 810 mV. When you have edited all of the states as needed, write
589 * "c" (commit) to the file to commit your changes. If you want to reset to the
590 * default power levels, write "r" (reset) to the file to reset them.
595 * Reading the file will display:
597 * - minimum and maximum engine clock labeled OD_SCLK
599 * - maximum memory clock labeled OD_MCLK
601 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
602 * They can be used to calibrate the sclk voltage curve.
604 * - a list of valid ranges for sclk, mclk, and voltage curve points
607 * To manually adjust these settings:
609 * - First select manual using power_dpm_force_performance_level
611 * - For clock frequency setting, enter a new value by writing a
612 * string that contains "s/m index clock" to the file. The index
613 * should be 0 if to set minimum clock. And 1 if to set maximum
614 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
615 * "m 1 800" will update maximum mclk to be 800Mhz.
617 * For sclk voltage curve, enter the new values by writing a
618 * string that contains "vc point clock voltage" to the file. The
619 * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
620 * update point1 with clock set as 300Mhz and voltage as
621 * 600mV. "vc 2 1000 1000" will update point3 with clock set
622 * as 1000Mhz and voltage 1000mV.
624 * - When you have edited all of the states as needed, write "c" (commit)
625 * to the file to commit your changes
627 * - If you want to reset to the default power levels, write "r" (reset)
628 * to the file to reset them
632 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
633 struct device_attribute *attr,
637 struct drm_device *ddev = dev_get_drvdata(dev);
638 struct amdgpu_device *adev = ddev->dev_private;
640 uint32_t parameter_size = 0;
645 const char delimiter[3] = {' ', '\n', '\0'};
652 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
653 else if (*buf == 'm')
654 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
656 type = PP_OD_RESTORE_DEFAULT_TABLE;
657 else if (*buf == 'c')
658 type = PP_OD_COMMIT_DPM_TABLE;
659 else if (!strncmp(buf, "vc", 2))
660 type = PP_OD_EDIT_VDDC_CURVE;
664 memcpy(buf_cpy, buf, count+1);
668 if (type == PP_OD_EDIT_VDDC_CURVE)
670 while (isspace(*++tmp_str));
673 sub_str = strsep(&tmp_str, delimiter);
674 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
679 while (isspace(*tmp_str))
683 if (is_support_sw_smu(adev)) {
684 ret = smu_od_edit_dpm_table(&adev->smu, type,
685 parameter, parameter_size);
690 if (adev->powerplay.pp_funcs->odn_edit_dpm_table)
691 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
692 parameter, parameter_size);
697 if (type == PP_OD_COMMIT_DPM_TABLE) {
698 if (adev->powerplay.pp_funcs->dispatch_tasks) {
699 amdgpu_dpm_dispatch_task(adev,
700 AMD_PP_TASK_READJUST_POWER_STATE,
712 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
713 struct device_attribute *attr,
716 struct drm_device *ddev = dev_get_drvdata(dev);
717 struct amdgpu_device *adev = ddev->dev_private;
720 if (is_support_sw_smu(adev)) {
721 size = smu_print_clk_levels(&adev->smu, OD_SCLK, buf);
722 size += smu_print_clk_levels(&adev->smu, OD_MCLK, buf+size);
723 size += smu_print_clk_levels(&adev->smu, OD_VDDC_CURVE, buf+size);
724 size += smu_print_clk_levels(&adev->smu, OD_RANGE, buf+size);
726 } else if (adev->powerplay.pp_funcs->print_clock_levels) {
727 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
728 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
729 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
730 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
733 return snprintf(buf, PAGE_SIZE, "\n");
741 * The amdgpu driver provides a sysfs API for adjusting what powerplay
742 * features to be enabled. The file ppfeatures is used for this. And
743 * this is only available for Vega10 and later dGPUs.
745 * Reading back the file will show you the followings:
746 * - Current ppfeature masks
747 * - List of the all supported powerplay features with their naming,
748 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
750 * To manually enable or disable a specific feature, just set or clear
751 * the corresponding bit from original ppfeature masks and input the
752 * new ppfeature masks.
754 static ssize_t amdgpu_set_ppfeature_status(struct device *dev,
755 struct device_attribute *attr,
759 struct drm_device *ddev = dev_get_drvdata(dev);
760 struct amdgpu_device *adev = ddev->dev_private;
761 uint64_t featuremask;
764 ret = kstrtou64(buf, 0, &featuremask);
768 pr_debug("featuremask = 0x%llx\n", featuremask);
770 if (is_support_sw_smu(adev)) {
771 ret = smu_set_ppfeature_status(&adev->smu, featuremask);
774 } else if (adev->powerplay.pp_funcs->set_ppfeature_status) {
775 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
783 static ssize_t amdgpu_get_ppfeature_status(struct device *dev,
784 struct device_attribute *attr,
787 struct drm_device *ddev = dev_get_drvdata(dev);
788 struct amdgpu_device *adev = ddev->dev_private;
790 if (is_support_sw_smu(adev)) {
791 return smu_get_ppfeature_status(&adev->smu, buf);
792 } else if (adev->powerplay.pp_funcs->get_ppfeature_status)
793 return amdgpu_dpm_get_ppfeature_status(adev, buf);
795 return snprintf(buf, PAGE_SIZE, "\n");
799 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk
802 * The amdgpu driver provides a sysfs API for adjusting what power levels
803 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
804 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
807 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
808 * Vega10 and later ASICs.
809 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
811 * Reading back the files will show you the available power levels within
812 * the power state and the clock information for those levels.
814 * To manually adjust these states, first select manual using
815 * power_dpm_force_performance_level.
816 * Secondly,Enter a new value for each level by inputing a string that
817 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
818 * E.g., echo 4 5 6 to > pp_dpm_sclk will enable sclk levels 4, 5, and 6.
820 * NOTE: change to the dcefclk max dpm level is not supported now
823 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
824 struct device_attribute *attr,
827 struct drm_device *ddev = dev_get_drvdata(dev);
828 struct amdgpu_device *adev = ddev->dev_private;
830 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev) &&
831 adev->virt.ops->get_pp_clk)
832 return adev->virt.ops->get_pp_clk(adev, PP_SCLK, buf);
834 if (is_support_sw_smu(adev))
835 return smu_print_clk_levels(&adev->smu, PP_SCLK, buf);
836 else if (adev->powerplay.pp_funcs->print_clock_levels)
837 return amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
839 return snprintf(buf, PAGE_SIZE, "\n");
843 * Worst case: 32 bits individually specified, in octal at 12 characters
844 * per line (+1 for \n).
846 #define AMDGPU_MASK_BUF_MAX (32 * 13)
848 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
852 char *sub_str = NULL;
854 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
855 const char delimiter[3] = {' ', '\n', '\0'};
860 bytes = min(count, sizeof(buf_cpy) - 1);
861 memcpy(buf_cpy, buf, bytes);
862 buf_cpy[bytes] = '\0';
865 sub_str = strsep(&tmp, delimiter);
866 if (strlen(sub_str)) {
867 ret = kstrtol(sub_str, 0, &level);
878 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
879 struct device_attribute *attr,
883 struct drm_device *ddev = dev_get_drvdata(dev);
884 struct amdgpu_device *adev = ddev->dev_private;
888 ret = amdgpu_read_mask(buf, count, &mask);
892 if (is_support_sw_smu(adev))
893 ret = smu_force_clk_levels(&adev->smu, PP_SCLK, mask);
894 else if (adev->powerplay.pp_funcs->force_clock_level)
895 ret = amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
903 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
904 struct device_attribute *attr,
907 struct drm_device *ddev = dev_get_drvdata(dev);
908 struct amdgpu_device *adev = ddev->dev_private;
910 if (is_support_sw_smu(adev))
911 return smu_print_clk_levels(&adev->smu, PP_MCLK, buf);
912 else if (adev->powerplay.pp_funcs->print_clock_levels)
913 return amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
915 return snprintf(buf, PAGE_SIZE, "\n");
918 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
919 struct device_attribute *attr,
923 struct drm_device *ddev = dev_get_drvdata(dev);
924 struct amdgpu_device *adev = ddev->dev_private;
928 ret = amdgpu_read_mask(buf, count, &mask);
932 if (is_support_sw_smu(adev))
933 ret = smu_force_clk_levels(&adev->smu, PP_MCLK, mask);
934 else if (adev->powerplay.pp_funcs->force_clock_level)
935 ret = amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
943 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
944 struct device_attribute *attr,
947 struct drm_device *ddev = dev_get_drvdata(dev);
948 struct amdgpu_device *adev = ddev->dev_private;
950 if (is_support_sw_smu(adev))
951 return smu_print_clk_levels(&adev->smu, PP_SOCCLK, buf);
952 else if (adev->powerplay.pp_funcs->print_clock_levels)
953 return amdgpu_dpm_print_clock_levels(adev, PP_SOCCLK, buf);
955 return snprintf(buf, PAGE_SIZE, "\n");
958 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
959 struct device_attribute *attr,
963 struct drm_device *ddev = dev_get_drvdata(dev);
964 struct amdgpu_device *adev = ddev->dev_private;
968 ret = amdgpu_read_mask(buf, count, &mask);
972 if (is_support_sw_smu(adev))
973 ret = smu_force_clk_levels(&adev->smu, PP_SOCCLK, mask);
974 else if (adev->powerplay.pp_funcs->force_clock_level)
975 ret = amdgpu_dpm_force_clock_level(adev, PP_SOCCLK, mask);
983 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
984 struct device_attribute *attr,
987 struct drm_device *ddev = dev_get_drvdata(dev);
988 struct amdgpu_device *adev = ddev->dev_private;
990 if (is_support_sw_smu(adev))
991 return smu_print_clk_levels(&adev->smu, PP_FCLK, buf);
992 else if (adev->powerplay.pp_funcs->print_clock_levels)
993 return amdgpu_dpm_print_clock_levels(adev, PP_FCLK, buf);
995 return snprintf(buf, PAGE_SIZE, "\n");
998 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
999 struct device_attribute *attr,
1003 struct drm_device *ddev = dev_get_drvdata(dev);
1004 struct amdgpu_device *adev = ddev->dev_private;
1008 ret = amdgpu_read_mask(buf, count, &mask);
1012 if (is_support_sw_smu(adev))
1013 ret = smu_force_clk_levels(&adev->smu, PP_FCLK, mask);
1014 else if (adev->powerplay.pp_funcs->force_clock_level)
1015 ret = amdgpu_dpm_force_clock_level(adev, PP_FCLK, mask);
1023 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1024 struct device_attribute *attr,
1027 struct drm_device *ddev = dev_get_drvdata(dev);
1028 struct amdgpu_device *adev = ddev->dev_private;
1030 if (is_support_sw_smu(adev))
1031 return smu_print_clk_levels(&adev->smu, PP_DCEFCLK, buf);
1032 else if (adev->powerplay.pp_funcs->print_clock_levels)
1033 return amdgpu_dpm_print_clock_levels(adev, PP_DCEFCLK, buf);
1035 return snprintf(buf, PAGE_SIZE, "\n");
1038 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1039 struct device_attribute *attr,
1043 struct drm_device *ddev = dev_get_drvdata(dev);
1044 struct amdgpu_device *adev = ddev->dev_private;
1048 ret = amdgpu_read_mask(buf, count, &mask);
1052 if (is_support_sw_smu(adev))
1053 ret = smu_force_clk_levels(&adev->smu, PP_DCEFCLK, mask);
1054 else if (adev->powerplay.pp_funcs->force_clock_level)
1055 ret = amdgpu_dpm_force_clock_level(adev, PP_DCEFCLK, mask);
1063 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1064 struct device_attribute *attr,
1067 struct drm_device *ddev = dev_get_drvdata(dev);
1068 struct amdgpu_device *adev = ddev->dev_private;
1070 if (is_support_sw_smu(adev))
1071 return smu_print_clk_levels(&adev->smu, PP_PCIE, buf);
1072 else if (adev->powerplay.pp_funcs->print_clock_levels)
1073 return amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
1075 return snprintf(buf, PAGE_SIZE, "\n");
1078 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1079 struct device_attribute *attr,
1083 struct drm_device *ddev = dev_get_drvdata(dev);
1084 struct amdgpu_device *adev = ddev->dev_private;
1088 ret = amdgpu_read_mask(buf, count, &mask);
1092 if (is_support_sw_smu(adev))
1093 ret = smu_force_clk_levels(&adev->smu, PP_PCIE, mask);
1094 else if (adev->powerplay.pp_funcs->force_clock_level)
1095 ret = amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
1103 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1104 struct device_attribute *attr,
1107 struct drm_device *ddev = dev_get_drvdata(dev);
1108 struct amdgpu_device *adev = ddev->dev_private;
1111 if (is_support_sw_smu(adev))
1112 value = smu_get_od_percentage(&(adev->smu), OD_SCLK);
1113 else if (adev->powerplay.pp_funcs->get_sclk_od)
1114 value = amdgpu_dpm_get_sclk_od(adev);
1116 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1119 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1120 struct device_attribute *attr,
1124 struct drm_device *ddev = dev_get_drvdata(dev);
1125 struct amdgpu_device *adev = ddev->dev_private;
1129 ret = kstrtol(buf, 0, &value);
1136 if (is_support_sw_smu(adev)) {
1137 value = smu_set_od_percentage(&(adev->smu), OD_SCLK, (uint32_t)value);
1139 if (adev->powerplay.pp_funcs->set_sclk_od)
1140 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1142 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1143 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1145 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1146 amdgpu_pm_compute_clocks(adev);
1154 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1155 struct device_attribute *attr,
1158 struct drm_device *ddev = dev_get_drvdata(dev);
1159 struct amdgpu_device *adev = ddev->dev_private;
1162 if (is_support_sw_smu(adev))
1163 value = smu_get_od_percentage(&(adev->smu), OD_MCLK);
1164 else if (adev->powerplay.pp_funcs->get_mclk_od)
1165 value = amdgpu_dpm_get_mclk_od(adev);
1167 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1170 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1171 struct device_attribute *attr,
1175 struct drm_device *ddev = dev_get_drvdata(dev);
1176 struct amdgpu_device *adev = ddev->dev_private;
1180 ret = kstrtol(buf, 0, &value);
1187 if (is_support_sw_smu(adev)) {
1188 value = smu_set_od_percentage(&(adev->smu), OD_MCLK, (uint32_t)value);
1190 if (adev->powerplay.pp_funcs->set_mclk_od)
1191 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1193 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1194 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1196 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1197 amdgpu_pm_compute_clocks(adev);
1206 * DOC: pp_power_profile_mode
1208 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1209 * related to switching between power levels in a power state. The file
1210 * pp_power_profile_mode is used for this.
1212 * Reading this file outputs a list of all of the predefined power profiles
1213 * and the relevant heuristics settings for that profile.
1215 * To select a profile or create a custom profile, first select manual using
1216 * power_dpm_force_performance_level. Writing the number of a predefined
1217 * profile to pp_power_profile_mode will enable those heuristics. To
1218 * create a custom set of heuristics, write a string of numbers to the file
1219 * starting with the number of the custom profile along with a setting
1220 * for each heuristic parameter. Due to differences across asic families
1221 * the heuristic parameters vary from family to family.
1225 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1226 struct device_attribute *attr,
1229 struct drm_device *ddev = dev_get_drvdata(dev);
1230 struct amdgpu_device *adev = ddev->dev_private;
1232 if (is_support_sw_smu(adev))
1233 return smu_get_power_profile_mode(&adev->smu, buf);
1234 else if (adev->powerplay.pp_funcs->get_power_profile_mode)
1235 return amdgpu_dpm_get_power_profile_mode(adev, buf);
1237 return snprintf(buf, PAGE_SIZE, "\n");
1241 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1242 struct device_attribute *attr,
1247 struct drm_device *ddev = dev_get_drvdata(dev);
1248 struct amdgpu_device *adev = ddev->dev_private;
1249 uint32_t parameter_size = 0;
1251 char *sub_str, buf_cpy[128];
1255 long int profile_mode = 0;
1256 const char delimiter[3] = {' ', '\n', '\0'};
1260 ret = kstrtol(tmp, 0, &profile_mode);
1264 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1265 if (count < 2 || count > 127)
1267 while (isspace(*++buf))
1269 memcpy(buf_cpy, buf, count-i);
1271 while (tmp_str[0]) {
1272 sub_str = strsep(&tmp_str, delimiter);
1273 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
1279 while (isspace(*tmp_str))
1283 parameter[parameter_size] = profile_mode;
1284 if (is_support_sw_smu(adev))
1285 ret = smu_set_power_profile_mode(&adev->smu, parameter, parameter_size);
1286 else if (adev->powerplay.pp_funcs->set_power_profile_mode)
1287 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1297 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1298 * is as a percentage. The file gpu_busy_percent is used for this.
1299 * The SMU firmware computes a percentage of load based on the
1300 * aggregate activity level in the IP cores.
1302 static ssize_t amdgpu_get_busy_percent(struct device *dev,
1303 struct device_attribute *attr,
1306 struct drm_device *ddev = dev_get_drvdata(dev);
1307 struct amdgpu_device *adev = ddev->dev_private;
1308 int r, value, size = sizeof(value);
1310 /* read the IP busy sensor */
1311 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1312 (void *)&value, &size);
1317 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1321 * DOC: mem_busy_percent
1323 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1324 * is as a percentage. The file mem_busy_percent is used for this.
1325 * The SMU firmware computes a percentage of load based on the
1326 * aggregate activity level in the IP cores.
1328 static ssize_t amdgpu_get_memory_busy_percent(struct device *dev,
1329 struct device_attribute *attr,
1332 struct drm_device *ddev = dev_get_drvdata(dev);
1333 struct amdgpu_device *adev = ddev->dev_private;
1334 int r, value, size = sizeof(value);
1336 /* read the IP busy sensor */
1337 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1338 (void *)&value, &size);
1343 return snprintf(buf, PAGE_SIZE, "%d\n", value);
1349 * The amdgpu driver provides a sysfs API for estimating how much data
1350 * has been received and sent by the GPU in the last second through PCIe.
1351 * The file pcie_bw is used for this.
1352 * The Perf counters count the number of received and sent messages and return
1353 * those values, as well as the maximum payload size of a PCIe packet (mps).
1354 * Note that it is not possible to easily and quickly obtain the size of each
1355 * packet transmitted, so we output the max payload size (mps) to allow for
1356 * quick estimation of the PCIe bandwidth usage
1358 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1359 struct device_attribute *attr,
1362 struct drm_device *ddev = dev_get_drvdata(dev);
1363 struct amdgpu_device *adev = ddev->dev_private;
1364 uint64_t count0, count1;
1366 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1367 return snprintf(buf, PAGE_SIZE, "%llu %llu %i\n",
1368 count0, count1, pcie_get_mps(adev->pdev));
1374 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1375 * The file unique_id is used for this.
1376 * This will provide a Unique ID that will persist from machine to machine
1378 * NOTE: This will only work for GFX9 and newer. This file will be absent
1379 * on unsupported ASICs (GFX8 and older)
1381 static ssize_t amdgpu_get_unique_id(struct device *dev,
1382 struct device_attribute *attr,
1385 struct drm_device *ddev = dev_get_drvdata(dev);
1386 struct amdgpu_device *adev = ddev->dev_private;
1388 if (adev->unique_id)
1389 return snprintf(buf, PAGE_SIZE, "%016llx\n", adev->unique_id);
1394 static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
1395 static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
1396 amdgpu_get_dpm_forced_performance_level,
1397 amdgpu_set_dpm_forced_performance_level);
1398 static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
1399 static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
1400 static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
1401 amdgpu_get_pp_force_state,
1402 amdgpu_set_pp_force_state);
1403 static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
1404 amdgpu_get_pp_table,
1405 amdgpu_set_pp_table);
1406 static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
1407 amdgpu_get_pp_dpm_sclk,
1408 amdgpu_set_pp_dpm_sclk);
1409 static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
1410 amdgpu_get_pp_dpm_mclk,
1411 amdgpu_set_pp_dpm_mclk);
1412 static DEVICE_ATTR(pp_dpm_socclk, S_IRUGO | S_IWUSR,
1413 amdgpu_get_pp_dpm_socclk,
1414 amdgpu_set_pp_dpm_socclk);
1415 static DEVICE_ATTR(pp_dpm_fclk, S_IRUGO | S_IWUSR,
1416 amdgpu_get_pp_dpm_fclk,
1417 amdgpu_set_pp_dpm_fclk);
1418 static DEVICE_ATTR(pp_dpm_dcefclk, S_IRUGO | S_IWUSR,
1419 amdgpu_get_pp_dpm_dcefclk,
1420 amdgpu_set_pp_dpm_dcefclk);
1421 static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
1422 amdgpu_get_pp_dpm_pcie,
1423 amdgpu_set_pp_dpm_pcie);
1424 static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
1425 amdgpu_get_pp_sclk_od,
1426 amdgpu_set_pp_sclk_od);
1427 static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
1428 amdgpu_get_pp_mclk_od,
1429 amdgpu_set_pp_mclk_od);
1430 static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
1431 amdgpu_get_pp_power_profile_mode,
1432 amdgpu_set_pp_power_profile_mode);
1433 static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
1434 amdgpu_get_pp_od_clk_voltage,
1435 amdgpu_set_pp_od_clk_voltage);
1436 static DEVICE_ATTR(gpu_busy_percent, S_IRUGO,
1437 amdgpu_get_busy_percent, NULL);
1438 static DEVICE_ATTR(mem_busy_percent, S_IRUGO,
1439 amdgpu_get_memory_busy_percent, NULL);
1440 static DEVICE_ATTR(pcie_bw, S_IRUGO, amdgpu_get_pcie_bw, NULL);
1441 static DEVICE_ATTR(ppfeatures, S_IRUGO | S_IWUSR,
1442 amdgpu_get_ppfeature_status,
1443 amdgpu_set_ppfeature_status);
1444 static DEVICE_ATTR(unique_id, S_IRUGO, amdgpu_get_unique_id, NULL);
1446 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
1447 struct device_attribute *attr,
1450 struct amdgpu_device *adev = dev_get_drvdata(dev);
1451 struct drm_device *ddev = adev->ddev;
1452 int channel = to_sensor_dev_attr(attr)->index;
1453 int r, temp, size = sizeof(temp);
1455 /* Can't get temperature when the card is off */
1456 if ((adev->flags & AMD_IS_PX) &&
1457 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1460 if (channel >= PP_TEMP_MAX)
1464 case PP_TEMP_JUNCTION:
1465 /* get current junction temperature */
1466 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
1467 (void *)&temp, &size);
1472 /* get current edge temperature */
1473 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
1474 (void *)&temp, &size);
1479 /* get current memory temperature */
1480 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
1481 (void *)&temp, &size);
1487 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1490 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
1491 struct device_attribute *attr,
1494 struct amdgpu_device *adev = dev_get_drvdata(dev);
1495 int hyst = to_sensor_dev_attr(attr)->index;
1499 temp = adev->pm.dpm.thermal.min_temp;
1501 temp = adev->pm.dpm.thermal.max_temp;
1503 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1506 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
1507 struct device_attribute *attr,
1510 struct amdgpu_device *adev = dev_get_drvdata(dev);
1511 int hyst = to_sensor_dev_attr(attr)->index;
1515 temp = adev->pm.dpm.thermal.min_hotspot_temp;
1517 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
1519 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1522 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
1523 struct device_attribute *attr,
1526 struct amdgpu_device *adev = dev_get_drvdata(dev);
1527 int hyst = to_sensor_dev_attr(attr)->index;
1531 temp = adev->pm.dpm.thermal.min_mem_temp;
1533 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
1535 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1538 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
1539 struct device_attribute *attr,
1542 int channel = to_sensor_dev_attr(attr)->index;
1544 if (channel >= PP_TEMP_MAX)
1547 return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label);
1550 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
1551 struct device_attribute *attr,
1554 struct amdgpu_device *adev = dev_get_drvdata(dev);
1555 int channel = to_sensor_dev_attr(attr)->index;
1558 if (channel >= PP_TEMP_MAX)
1562 case PP_TEMP_JUNCTION:
1563 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
1566 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
1569 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
1573 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1576 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
1577 struct device_attribute *attr,
1580 struct amdgpu_device *adev = dev_get_drvdata(dev);
1582 if (is_support_sw_smu(adev)) {
1583 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1585 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
1588 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1591 return sprintf(buf, "%i\n", pwm_mode);
1594 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
1595 struct device_attribute *attr,
1599 struct amdgpu_device *adev = dev_get_drvdata(dev);
1603 /* Can't adjust fan when the card is off */
1604 if ((adev->flags & AMD_IS_PX) &&
1605 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1608 if (is_support_sw_smu(adev)) {
1609 err = kstrtoint(buf, 10, &value);
1613 smu_set_fan_control_mode(&adev->smu, value);
1615 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1618 err = kstrtoint(buf, 10, &value);
1622 amdgpu_dpm_set_fan_control_mode(adev, value);
1628 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
1629 struct device_attribute *attr,
1632 return sprintf(buf, "%i\n", 0);
1635 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
1636 struct device_attribute *attr,
1639 return sprintf(buf, "%i\n", 255);
1642 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
1643 struct device_attribute *attr,
1644 const char *buf, size_t count)
1646 struct amdgpu_device *adev = dev_get_drvdata(dev);
1651 /* Can't adjust fan when the card is off */
1652 if ((adev->flags & AMD_IS_PX) &&
1653 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1655 if (is_support_sw_smu(adev))
1656 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1658 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1659 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
1660 pr_info("manual fan speed control should be enabled first\n");
1664 err = kstrtou32(buf, 10, &value);
1668 value = (value * 100) / 255;
1670 if (is_support_sw_smu(adev)) {
1671 err = smu_set_fan_speed_percent(&adev->smu, value);
1674 } else if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
1675 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
1683 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
1684 struct device_attribute *attr,
1687 struct amdgpu_device *adev = dev_get_drvdata(dev);
1691 /* Can't adjust fan when the card is off */
1692 if ((adev->flags & AMD_IS_PX) &&
1693 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1696 if (is_support_sw_smu(adev)) {
1697 err = smu_get_fan_speed_percent(&adev->smu, &speed);
1700 } else if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
1701 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
1706 speed = (speed * 255) / 100;
1708 return sprintf(buf, "%i\n", speed);
1711 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
1712 struct device_attribute *attr,
1715 struct amdgpu_device *adev = dev_get_drvdata(dev);
1719 /* Can't adjust fan when the card is off */
1720 if ((adev->flags & AMD_IS_PX) &&
1721 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1724 if (is_support_sw_smu(adev)) {
1725 err = smu_get_current_rpm(&adev->smu, &speed);
1728 } else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1729 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
1734 return sprintf(buf, "%i\n", speed);
1737 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
1738 struct device_attribute *attr,
1741 struct amdgpu_device *adev = dev_get_drvdata(dev);
1743 u32 size = sizeof(min_rpm);
1746 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
1747 (void *)&min_rpm, &size);
1751 return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
1754 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
1755 struct device_attribute *attr,
1758 struct amdgpu_device *adev = dev_get_drvdata(dev);
1760 u32 size = sizeof(max_rpm);
1763 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
1764 (void *)&max_rpm, &size);
1768 return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
1771 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
1772 struct device_attribute *attr,
1775 struct amdgpu_device *adev = dev_get_drvdata(dev);
1779 /* Can't adjust fan when the card is off */
1780 if ((adev->flags & AMD_IS_PX) &&
1781 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1784 if (is_support_sw_smu(adev)) {
1785 err = smu_get_current_rpm(&adev->smu, &rpm);
1788 } else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1789 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
1794 return sprintf(buf, "%i\n", rpm);
1797 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
1798 struct device_attribute *attr,
1799 const char *buf, size_t count)
1801 struct amdgpu_device *adev = dev_get_drvdata(dev);
1806 if (is_support_sw_smu(adev))
1807 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1809 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1811 if (pwm_mode != AMD_FAN_CTRL_MANUAL)
1814 /* Can't adjust fan when the card is off */
1815 if ((adev->flags & AMD_IS_PX) &&
1816 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1819 err = kstrtou32(buf, 10, &value);
1823 if (is_support_sw_smu(adev)) {
1824 err = smu_set_fan_speed_rpm(&adev->smu, value);
1827 } else if (adev->powerplay.pp_funcs->set_fan_speed_rpm) {
1828 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
1836 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
1837 struct device_attribute *attr,
1840 struct amdgpu_device *adev = dev_get_drvdata(dev);
1843 if (is_support_sw_smu(adev)) {
1844 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1846 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
1849 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1851 return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
1854 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
1855 struct device_attribute *attr,
1859 struct amdgpu_device *adev = dev_get_drvdata(dev);
1864 /* Can't adjust fan when the card is off */
1865 if ((adev->flags & AMD_IS_PX) &&
1866 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1870 err = kstrtoint(buf, 10, &value);
1875 pwm_mode = AMD_FAN_CTRL_AUTO;
1876 else if (value == 1)
1877 pwm_mode = AMD_FAN_CTRL_MANUAL;
1881 if (is_support_sw_smu(adev)) {
1882 smu_set_fan_control_mode(&adev->smu, pwm_mode);
1884 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1886 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
1892 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
1893 struct device_attribute *attr,
1896 struct amdgpu_device *adev = dev_get_drvdata(dev);
1897 struct drm_device *ddev = adev->ddev;
1899 int r, size = sizeof(vddgfx);
1901 /* Can't get voltage when the card is off */
1902 if ((adev->flags & AMD_IS_PX) &&
1903 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1906 /* get the voltage */
1907 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
1908 (void *)&vddgfx, &size);
1912 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
1915 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
1916 struct device_attribute *attr,
1919 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
1922 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
1923 struct device_attribute *attr,
1926 struct amdgpu_device *adev = dev_get_drvdata(dev);
1927 struct drm_device *ddev = adev->ddev;
1929 int r, size = sizeof(vddnb);
1931 /* only APUs have vddnb */
1932 if (!(adev->flags & AMD_IS_APU))
1935 /* Can't get voltage when the card is off */
1936 if ((adev->flags & AMD_IS_PX) &&
1937 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1940 /* get the voltage */
1941 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
1942 (void *)&vddnb, &size);
1946 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
1949 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
1950 struct device_attribute *attr,
1953 return snprintf(buf, PAGE_SIZE, "vddnb\n");
1956 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
1957 struct device_attribute *attr,
1960 struct amdgpu_device *adev = dev_get_drvdata(dev);
1961 struct drm_device *ddev = adev->ddev;
1963 int r, size = sizeof(u32);
1966 /* Can't get power when the card is off */
1967 if ((adev->flags & AMD_IS_PX) &&
1968 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1971 /* get the voltage */
1972 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
1973 (void *)&query, &size);
1977 /* convert to microwatts */
1978 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
1980 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
1983 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
1984 struct device_attribute *attr,
1987 return sprintf(buf, "%i\n", 0);
1990 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
1991 struct device_attribute *attr,
1994 struct amdgpu_device *adev = dev_get_drvdata(dev);
1997 if (is_support_sw_smu(adev)) {
1998 smu_get_power_limit(&adev->smu, &limit, true);
1999 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2000 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2001 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
2002 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2004 return snprintf(buf, PAGE_SIZE, "\n");
2008 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2009 struct device_attribute *attr,
2012 struct amdgpu_device *adev = dev_get_drvdata(dev);
2015 if (is_support_sw_smu(adev)) {
2016 smu_get_power_limit(&adev->smu, &limit, false);
2017 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2018 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
2019 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
2020 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
2022 return snprintf(buf, PAGE_SIZE, "\n");
2027 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2028 struct device_attribute *attr,
2032 struct amdgpu_device *adev = dev_get_drvdata(dev);
2036 err = kstrtou32(buf, 10, &value);
2040 value = value / 1000000; /* convert to Watt */
2041 if (is_support_sw_smu(adev)) {
2042 adev->smu.funcs->set_power_limit(&adev->smu, value);
2043 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {
2044 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
2054 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2055 struct device_attribute *attr,
2058 struct amdgpu_device *adev = dev_get_drvdata(dev);
2059 struct drm_device *ddev = adev->ddev;
2061 int r, size = sizeof(sclk);
2063 /* Can't get voltage when the card is off */
2064 if ((adev->flags & AMD_IS_PX) &&
2065 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
2068 /* sanity check PP is enabled */
2069 if (!(adev->powerplay.pp_funcs &&
2070 adev->powerplay.pp_funcs->read_sensor))
2074 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2075 (void *)&sclk, &size);
2079 return snprintf(buf, PAGE_SIZE, "%d\n", sclk * 10 * 1000);
2082 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2083 struct device_attribute *attr,
2086 return snprintf(buf, PAGE_SIZE, "sclk\n");
2089 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2090 struct device_attribute *attr,
2093 struct amdgpu_device *adev = dev_get_drvdata(dev);
2094 struct drm_device *ddev = adev->ddev;
2096 int r, size = sizeof(mclk);
2098 /* Can't get voltage when the card is off */
2099 if ((adev->flags & AMD_IS_PX) &&
2100 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
2103 /* sanity check PP is enabled */
2104 if (!(adev->powerplay.pp_funcs &&
2105 adev->powerplay.pp_funcs->read_sensor))
2109 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2110 (void *)&mclk, &size);
2114 return snprintf(buf, PAGE_SIZE, "%d\n", mclk * 10 * 1000);
2117 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
2118 struct device_attribute *attr,
2121 return snprintf(buf, PAGE_SIZE, "mclk\n");
2127 * The amdgpu driver exposes the following sensor interfaces:
2129 * - GPU temperature (via the on-die sensor)
2133 * - Northbridge voltage (APUs only)
2139 * - GPU gfx/compute engine clock
2141 * - GPU memory clock (dGPU only)
2143 * hwmon interfaces for GPU temperature:
2145 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
2146 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
2148 * - temp[1-3]_label: temperature channel label
2149 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
2151 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
2152 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
2154 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
2155 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
2157 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
2158 * - these are supported on SOC15 dGPUs only
2160 * hwmon interfaces for GPU voltage:
2162 * - in0_input: the voltage on the GPU in millivolts
2164 * - in1_input: the voltage on the Northbridge in millivolts
2166 * hwmon interfaces for GPU power:
2168 * - power1_average: average power used by the GPU in microWatts
2170 * - power1_cap_min: minimum cap supported in microWatts
2172 * - power1_cap_max: maximum cap supported in microWatts
2174 * - power1_cap: selected power cap in microWatts
2176 * hwmon interfaces for GPU fan:
2178 * - pwm1: pulse width modulation fan level (0-255)
2180 * - 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)
2182 * - pwm1_min: pulse width modulation fan control minimum level (0)
2184 * - pwm1_max: pulse width modulation fan control maximum level (255)
2186 * - fan1_min: an minimum value Unit: revolution/min (RPM)
2188 * - fan1_max: an maxmum value Unit: revolution/max (RPM)
2190 * - fan1_input: fan speed in RPM
2192 * - fan[1-*]_target: Desired fan speed Unit: revolution/min (RPM)
2194 * - fan[1-*]_enable: Enable or disable the sensors.1: Enable 0: Disable
2196 * hwmon interfaces for GPU clocks:
2198 * - freq1_input: the gfx/compute clock in hertz
2200 * - freq2_input: the memory clock in hertz
2202 * You can use hwmon tools like sensors to view this information on your system.
2206 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
2207 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
2208 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
2209 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
2210 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
2211 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
2212 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
2213 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
2214 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
2215 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
2216 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
2217 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
2218 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
2219 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
2220 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
2221 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
2222 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
2223 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
2224 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
2225 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
2226 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
2227 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
2228 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
2229 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
2230 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
2231 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
2232 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
2233 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
2234 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
2235 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
2236 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
2237 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
2238 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
2239 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
2240 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
2241 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
2243 static struct attribute *hwmon_attributes[] = {
2244 &sensor_dev_attr_temp1_input.dev_attr.attr,
2245 &sensor_dev_attr_temp1_crit.dev_attr.attr,
2246 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
2247 &sensor_dev_attr_temp2_input.dev_attr.attr,
2248 &sensor_dev_attr_temp2_crit.dev_attr.attr,
2249 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
2250 &sensor_dev_attr_temp3_input.dev_attr.attr,
2251 &sensor_dev_attr_temp3_crit.dev_attr.attr,
2252 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
2253 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
2254 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
2255 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
2256 &sensor_dev_attr_temp1_label.dev_attr.attr,
2257 &sensor_dev_attr_temp2_label.dev_attr.attr,
2258 &sensor_dev_attr_temp3_label.dev_attr.attr,
2259 &sensor_dev_attr_pwm1.dev_attr.attr,
2260 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2261 &sensor_dev_attr_pwm1_min.dev_attr.attr,
2262 &sensor_dev_attr_pwm1_max.dev_attr.attr,
2263 &sensor_dev_attr_fan1_input.dev_attr.attr,
2264 &sensor_dev_attr_fan1_min.dev_attr.attr,
2265 &sensor_dev_attr_fan1_max.dev_attr.attr,
2266 &sensor_dev_attr_fan1_target.dev_attr.attr,
2267 &sensor_dev_attr_fan1_enable.dev_attr.attr,
2268 &sensor_dev_attr_in0_input.dev_attr.attr,
2269 &sensor_dev_attr_in0_label.dev_attr.attr,
2270 &sensor_dev_attr_in1_input.dev_attr.attr,
2271 &sensor_dev_attr_in1_label.dev_attr.attr,
2272 &sensor_dev_attr_power1_average.dev_attr.attr,
2273 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
2274 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
2275 &sensor_dev_attr_power1_cap.dev_attr.attr,
2276 &sensor_dev_attr_freq1_input.dev_attr.attr,
2277 &sensor_dev_attr_freq1_label.dev_attr.attr,
2278 &sensor_dev_attr_freq2_input.dev_attr.attr,
2279 &sensor_dev_attr_freq2_label.dev_attr.attr,
2283 static umode_t hwmon_attributes_visible(struct kobject *kobj,
2284 struct attribute *attr, int index)
2286 struct device *dev = kobj_to_dev(kobj);
2287 struct amdgpu_device *adev = dev_get_drvdata(dev);
2288 umode_t effective_mode = attr->mode;
2290 /* Skip fan attributes if fan is not present */
2291 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2292 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2293 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2294 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2295 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2296 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2297 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2298 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2299 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2302 /* Skip fan attributes on APU */
2303 if ((adev->flags & AMD_IS_APU) &&
2304 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2305 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2306 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2307 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2308 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2309 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2310 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2311 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2312 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2315 /* Skip limit attributes if DPM is not enabled */
2316 if (!adev->pm.dpm_enabled &&
2317 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
2318 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
2319 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2320 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2321 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2322 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2323 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2324 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2325 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2326 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2327 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2330 if (!is_support_sw_smu(adev)) {
2331 /* mask fan attributes if we have no bindings for this asic to expose */
2332 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
2333 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
2334 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
2335 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
2336 effective_mode &= ~S_IRUGO;
2338 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2339 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
2340 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
2341 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
2342 effective_mode &= ~S_IWUSR;
2345 if ((adev->flags & AMD_IS_APU) &&
2346 (attr == &sensor_dev_attr_power1_average.dev_attr.attr ||
2347 attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
2348 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
2349 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
2352 if (!is_support_sw_smu(adev)) {
2353 /* hide max/min values if we can't both query and manage the fan */
2354 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2355 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
2356 (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2357 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2358 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2359 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
2362 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2363 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2364 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2365 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
2369 /* only APUs have vddnb */
2370 if (!(adev->flags & AMD_IS_APU) &&
2371 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
2372 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
2375 /* no mclk on APUs */
2376 if ((adev->flags & AMD_IS_APU) &&
2377 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
2378 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
2381 /* only SOC15 dGPUs support hotspot and mem temperatures */
2382 if (((adev->flags & AMD_IS_APU) ||
2383 adev->asic_type < CHIP_VEGA10) &&
2384 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
2385 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
2386 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
2387 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
2388 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
2389 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
2390 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
2391 attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
2392 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
2393 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
2394 attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
2397 return effective_mode;
2400 static const struct attribute_group hwmon_attrgroup = {
2401 .attrs = hwmon_attributes,
2402 .is_visible = hwmon_attributes_visible,
2405 static const struct attribute_group *hwmon_groups[] = {
2410 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
2412 struct amdgpu_device *adev =
2413 container_of(work, struct amdgpu_device,
2414 pm.dpm.thermal.work);
2415 /* switch to the thermal state */
2416 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
2417 int temp, size = sizeof(temp);
2419 if (!adev->pm.dpm_enabled)
2422 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
2423 (void *)&temp, &size)) {
2424 if (temp < adev->pm.dpm.thermal.min_temp)
2425 /* switch back the user state */
2426 dpm_state = adev->pm.dpm.user_state;
2428 if (adev->pm.dpm.thermal.high_to_low)
2429 /* switch back the user state */
2430 dpm_state = adev->pm.dpm.user_state;
2432 mutex_lock(&adev->pm.mutex);
2433 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
2434 adev->pm.dpm.thermal_active = true;
2436 adev->pm.dpm.thermal_active = false;
2437 adev->pm.dpm.state = dpm_state;
2438 mutex_unlock(&adev->pm.mutex);
2440 amdgpu_pm_compute_clocks(adev);
2443 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
2444 enum amd_pm_state_type dpm_state)
2447 struct amdgpu_ps *ps;
2449 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
2452 /* check if the vblank period is too short to adjust the mclk */
2453 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
2454 if (amdgpu_dpm_vblank_too_short(adev))
2455 single_display = false;
2458 /* certain older asics have a separare 3D performance state,
2459 * so try that first if the user selected performance
2461 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
2462 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
2463 /* balanced states don't exist at the moment */
2464 if (dpm_state == POWER_STATE_TYPE_BALANCED)
2465 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2468 /* Pick the best power state based on current conditions */
2469 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
2470 ps = &adev->pm.dpm.ps[i];
2471 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
2472 switch (dpm_state) {
2474 case POWER_STATE_TYPE_BATTERY:
2475 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
2476 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2483 case POWER_STATE_TYPE_BALANCED:
2484 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
2485 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2492 case POWER_STATE_TYPE_PERFORMANCE:
2493 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
2494 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2501 /* internal states */
2502 case POWER_STATE_TYPE_INTERNAL_UVD:
2503 if (adev->pm.dpm.uvd_ps)
2504 return adev->pm.dpm.uvd_ps;
2507 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
2508 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
2511 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
2512 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
2515 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
2516 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
2519 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
2520 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
2523 case POWER_STATE_TYPE_INTERNAL_BOOT:
2524 return adev->pm.dpm.boot_ps;
2525 case POWER_STATE_TYPE_INTERNAL_THERMAL:
2526 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
2529 case POWER_STATE_TYPE_INTERNAL_ACPI:
2530 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
2533 case POWER_STATE_TYPE_INTERNAL_ULV:
2534 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
2537 case POWER_STATE_TYPE_INTERNAL_3DPERF:
2538 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
2545 /* use a fallback state if we didn't match */
2546 switch (dpm_state) {
2547 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
2548 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
2549 goto restart_search;
2550 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
2551 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
2552 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
2553 if (adev->pm.dpm.uvd_ps) {
2554 return adev->pm.dpm.uvd_ps;
2556 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2557 goto restart_search;
2559 case POWER_STATE_TYPE_INTERNAL_THERMAL:
2560 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
2561 goto restart_search;
2562 case POWER_STATE_TYPE_INTERNAL_ACPI:
2563 dpm_state = POWER_STATE_TYPE_BATTERY;
2564 goto restart_search;
2565 case POWER_STATE_TYPE_BATTERY:
2566 case POWER_STATE_TYPE_BALANCED:
2567 case POWER_STATE_TYPE_INTERNAL_3DPERF:
2568 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2569 goto restart_search;
2577 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
2579 struct amdgpu_ps *ps;
2580 enum amd_pm_state_type dpm_state;
2584 /* if dpm init failed */
2585 if (!adev->pm.dpm_enabled)
2588 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
2589 /* add other state override checks here */
2590 if ((!adev->pm.dpm.thermal_active) &&
2591 (!adev->pm.dpm.uvd_active))
2592 adev->pm.dpm.state = adev->pm.dpm.user_state;
2594 dpm_state = adev->pm.dpm.state;
2596 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
2598 adev->pm.dpm.requested_ps = ps;
2602 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
2603 printk("switching from power state:\n");
2604 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
2605 printk("switching to power state:\n");
2606 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
2609 /* update whether vce is active */
2610 ps->vce_active = adev->pm.dpm.vce_active;
2611 if (adev->powerplay.pp_funcs->display_configuration_changed)
2612 amdgpu_dpm_display_configuration_changed(adev);
2614 ret = amdgpu_dpm_pre_set_power_state(adev);
2618 if (adev->powerplay.pp_funcs->check_state_equal) {
2619 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
2626 amdgpu_dpm_set_power_state(adev);
2627 amdgpu_dpm_post_set_power_state(adev);
2629 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
2630 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
2632 if (adev->powerplay.pp_funcs->force_performance_level) {
2633 if (adev->pm.dpm.thermal_active) {
2634 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
2635 /* force low perf level for thermal */
2636 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
2637 /* save the user's level */
2638 adev->pm.dpm.forced_level = level;
2640 /* otherwise, user selected level */
2641 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
2646 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
2649 if (is_support_sw_smu(adev)) {
2650 ret = smu_dpm_set_power_gate(&adev->smu, AMD_IP_BLOCK_TYPE_UVD, enable);
2652 DRM_ERROR("[SW SMU]: dpm enable uvd failed, state = %s, ret = %d. \n",
2653 enable ? "true" : "false", ret);
2654 } else if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
2655 /* enable/disable UVD */
2656 mutex_lock(&adev->pm.mutex);
2657 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_UVD, !enable);
2658 mutex_unlock(&adev->pm.mutex);
2660 /* enable/disable Low Memory PState for UVD (4k videos) */
2661 if (adev->asic_type == CHIP_STONEY &&
2662 adev->uvd.decode_image_width >= WIDTH_4K) {
2663 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2665 if (hwmgr && hwmgr->hwmgr_func &&
2666 hwmgr->hwmgr_func->update_nbdpm_pstate)
2667 hwmgr->hwmgr_func->update_nbdpm_pstate(hwmgr,
2673 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
2676 if (is_support_sw_smu(adev)) {
2677 ret = smu_dpm_set_power_gate(&adev->smu, AMD_IP_BLOCK_TYPE_VCE, enable);
2679 DRM_ERROR("[SW SMU]: dpm enable vce failed, state = %s, ret = %d. \n",
2680 enable ? "true" : "false", ret);
2681 } else if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
2682 /* enable/disable VCE */
2683 mutex_lock(&adev->pm.mutex);
2684 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_VCE, !enable);
2685 mutex_unlock(&adev->pm.mutex);
2689 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
2693 if (adev->powerplay.pp_funcs->print_power_state == NULL)
2696 for (i = 0; i < adev->pm.dpm.num_ps; i++)
2697 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
2701 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
2703 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2706 if (adev->pm.sysfs_initialized)
2709 if (adev->pm.dpm_enabled == 0)
2712 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
2715 if (IS_ERR(adev->pm.int_hwmon_dev)) {
2716 ret = PTR_ERR(adev->pm.int_hwmon_dev);
2718 "Unable to register hwmon device: %d\n", ret);
2722 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
2724 DRM_ERROR("failed to create device file for dpm state\n");
2727 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2729 DRM_ERROR("failed to create device file for dpm state\n");
2734 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
2736 DRM_ERROR("failed to create device file pp_num_states\n");
2739 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
2741 DRM_ERROR("failed to create device file pp_cur_state\n");
2744 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
2746 DRM_ERROR("failed to create device file pp_force_state\n");
2749 ret = device_create_file(adev->dev, &dev_attr_pp_table);
2751 DRM_ERROR("failed to create device file pp_table\n");
2755 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
2757 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
2760 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
2762 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
2765 if (adev->asic_type >= CHIP_VEGA10) {
2766 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_socclk);
2768 DRM_ERROR("failed to create device file pp_dpm_socclk\n");
2771 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_dcefclk);
2773 DRM_ERROR("failed to create device file pp_dpm_dcefclk\n");
2777 if (adev->asic_type >= CHIP_VEGA20) {
2778 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_fclk);
2780 DRM_ERROR("failed to create device file pp_dpm_fclk\n");
2784 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
2786 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
2789 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
2791 DRM_ERROR("failed to create device file pp_sclk_od\n");
2794 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
2796 DRM_ERROR("failed to create device file pp_mclk_od\n");
2799 ret = device_create_file(adev->dev,
2800 &dev_attr_pp_power_profile_mode);
2802 DRM_ERROR("failed to create device file "
2803 "pp_power_profile_mode\n");
2806 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2807 (!is_support_sw_smu(adev) && hwmgr->od_enabled)) {
2808 ret = device_create_file(adev->dev,
2809 &dev_attr_pp_od_clk_voltage);
2811 DRM_ERROR("failed to create device file "
2812 "pp_od_clk_voltage\n");
2816 ret = device_create_file(adev->dev,
2817 &dev_attr_gpu_busy_percent);
2819 DRM_ERROR("failed to create device file "
2820 "gpu_busy_level\n");
2823 /* APU does not have its own dedicated memory */
2824 if (!(adev->flags & AMD_IS_APU)) {
2825 ret = device_create_file(adev->dev,
2826 &dev_attr_mem_busy_percent);
2828 DRM_ERROR("failed to create device file "
2829 "mem_busy_percent\n");
2833 /* PCIe Perf counters won't work on APU nodes */
2834 if (!(adev->flags & AMD_IS_APU)) {
2835 ret = device_create_file(adev->dev, &dev_attr_pcie_bw);
2837 DRM_ERROR("failed to create device file pcie_bw\n");
2841 if (adev->unique_id)
2842 ret = device_create_file(adev->dev, &dev_attr_unique_id);
2844 DRM_ERROR("failed to create device file unique_id\n");
2847 ret = amdgpu_debugfs_pm_init(adev);
2849 DRM_ERROR("Failed to register debugfs file for dpm!\n");
2853 if ((adev->asic_type >= CHIP_VEGA10) &&
2854 !(adev->flags & AMD_IS_APU)) {
2855 ret = device_create_file(adev->dev,
2856 &dev_attr_ppfeatures);
2858 DRM_ERROR("failed to create device file "
2864 adev->pm.sysfs_initialized = true;
2869 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
2871 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2873 if (adev->pm.dpm_enabled == 0)
2876 if (adev->pm.int_hwmon_dev)
2877 hwmon_device_unregister(adev->pm.int_hwmon_dev);
2878 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
2879 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2881 device_remove_file(adev->dev, &dev_attr_pp_num_states);
2882 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
2883 device_remove_file(adev->dev, &dev_attr_pp_force_state);
2884 device_remove_file(adev->dev, &dev_attr_pp_table);
2886 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
2887 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
2888 if (adev->asic_type >= CHIP_VEGA10) {
2889 device_remove_file(adev->dev, &dev_attr_pp_dpm_socclk);
2890 device_remove_file(adev->dev, &dev_attr_pp_dpm_dcefclk);
2892 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
2893 if (adev->asic_type >= CHIP_VEGA20)
2894 device_remove_file(adev->dev, &dev_attr_pp_dpm_fclk);
2895 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
2896 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
2897 device_remove_file(adev->dev,
2898 &dev_attr_pp_power_profile_mode);
2899 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2900 (!is_support_sw_smu(adev) && hwmgr->od_enabled))
2901 device_remove_file(adev->dev,
2902 &dev_attr_pp_od_clk_voltage);
2903 device_remove_file(adev->dev, &dev_attr_gpu_busy_percent);
2904 if (!(adev->flags & AMD_IS_APU))
2905 device_remove_file(adev->dev, &dev_attr_mem_busy_percent);
2906 if (!(adev->flags & AMD_IS_APU))
2907 device_remove_file(adev->dev, &dev_attr_pcie_bw);
2908 if (adev->unique_id)
2909 device_remove_file(adev->dev, &dev_attr_unique_id);
2910 if ((adev->asic_type >= CHIP_VEGA10) &&
2911 !(adev->flags & AMD_IS_APU))
2912 device_remove_file(adev->dev, &dev_attr_ppfeatures);
2915 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
2919 if (!adev->pm.dpm_enabled)
2922 if (adev->mode_info.num_crtc)
2923 amdgpu_display_bandwidth_update(adev);
2925 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
2926 struct amdgpu_ring *ring = adev->rings[i];
2927 if (ring && ring->sched.ready)
2928 amdgpu_fence_wait_empty(ring);
2931 if (is_support_sw_smu(adev)) {
2932 struct smu_context *smu = &adev->smu;
2933 struct smu_dpm_context *smu_dpm = &adev->smu.smu_dpm;
2934 mutex_lock(&(smu->mutex));
2935 smu_handle_task(&adev->smu,
2937 AMD_PP_TASK_DISPLAY_CONFIG_CHANGE);
2938 mutex_unlock(&(smu->mutex));
2940 if (adev->powerplay.pp_funcs->dispatch_tasks) {
2941 if (!amdgpu_device_has_dc_support(adev)) {
2942 mutex_lock(&adev->pm.mutex);
2943 amdgpu_dpm_get_active_displays(adev);
2944 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
2945 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
2946 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
2947 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
2948 if (adev->pm.pm_display_cfg.vrefresh > 120)
2949 adev->pm.pm_display_cfg.min_vblank_time = 0;
2950 if (adev->powerplay.pp_funcs->display_configuration_change)
2951 adev->powerplay.pp_funcs->display_configuration_change(
2952 adev->powerplay.pp_handle,
2953 &adev->pm.pm_display_cfg);
2954 mutex_unlock(&adev->pm.mutex);
2956 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
2958 mutex_lock(&adev->pm.mutex);
2959 amdgpu_dpm_get_active_displays(adev);
2960 amdgpu_dpm_change_power_state_locked(adev);
2961 mutex_unlock(&adev->pm.mutex);
2969 #if defined(CONFIG_DEBUG_FS)
2971 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
2979 size = sizeof(value);
2980 seq_printf(m, "GFX Clocks and Power:\n");
2981 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
2982 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
2983 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
2984 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
2985 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
2986 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
2987 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
2988 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
2989 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
2990 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
2991 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
2992 seq_printf(m, "\t%u mV (VDDNB)\n", value);
2993 size = sizeof(uint32_t);
2994 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
2995 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
2996 size = sizeof(value);
2997 seq_printf(m, "\n");
3000 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3001 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3004 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3005 seq_printf(m, "GPU Load: %u %%\n", value);
3007 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3008 seq_printf(m, "MEM Load: %u %%\n", value);
3010 seq_printf(m, "\n");
3012 /* SMC feature mask */
3013 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3014 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3017 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3019 seq_printf(m, "UVD: Disabled\n");
3021 seq_printf(m, "UVD: Enabled\n");
3022 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3023 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3024 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3025 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3028 seq_printf(m, "\n");
3031 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3033 seq_printf(m, "VCE: Disabled\n");
3035 seq_printf(m, "VCE: Enabled\n");
3036 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3037 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3044 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3048 for (i = 0; clocks[i].flag; i++)
3049 seq_printf(m, "\t%s: %s\n", clocks[i].name,
3050 (flags & clocks[i].flag) ? "On" : "Off");
3053 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
3055 struct drm_info_node *node = (struct drm_info_node *) m->private;
3056 struct drm_device *dev = node->minor->dev;
3057 struct amdgpu_device *adev = dev->dev_private;
3058 struct drm_device *ddev = adev->ddev;
3061 amdgpu_device_ip_get_clockgating_state(adev, &flags);
3062 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3063 amdgpu_parse_cg_state(m, flags);
3064 seq_printf(m, "\n");
3066 if (!adev->pm.dpm_enabled) {
3067 seq_printf(m, "dpm not enabled\n");
3070 if ((adev->flags & AMD_IS_PX) &&
3071 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
3072 seq_printf(m, "PX asic powered off\n");
3073 } else if (!is_support_sw_smu(adev) && adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3074 mutex_lock(&adev->pm.mutex);
3075 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3076 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3078 seq_printf(m, "Debugfs support not implemented for this asic\n");
3079 mutex_unlock(&adev->pm.mutex);
3081 return amdgpu_debugfs_pm_info_pp(m, adev);
3087 static const struct drm_info_list amdgpu_pm_info_list[] = {
3088 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
3092 static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3094 #if defined(CONFIG_DEBUG_FS)
3095 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));