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.
22 * Authors: Rafał Miłecki <zajec5@gmail.com>
23 * Alex Deucher <alexdeucher@gmail.com>
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));
1371 static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
1372 static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
1373 amdgpu_get_dpm_forced_performance_level,
1374 amdgpu_set_dpm_forced_performance_level);
1375 static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
1376 static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
1377 static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
1378 amdgpu_get_pp_force_state,
1379 amdgpu_set_pp_force_state);
1380 static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
1381 amdgpu_get_pp_table,
1382 amdgpu_set_pp_table);
1383 static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
1384 amdgpu_get_pp_dpm_sclk,
1385 amdgpu_set_pp_dpm_sclk);
1386 static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
1387 amdgpu_get_pp_dpm_mclk,
1388 amdgpu_set_pp_dpm_mclk);
1389 static DEVICE_ATTR(pp_dpm_socclk, S_IRUGO | S_IWUSR,
1390 amdgpu_get_pp_dpm_socclk,
1391 amdgpu_set_pp_dpm_socclk);
1392 static DEVICE_ATTR(pp_dpm_fclk, S_IRUGO | S_IWUSR,
1393 amdgpu_get_pp_dpm_fclk,
1394 amdgpu_set_pp_dpm_fclk);
1395 static DEVICE_ATTR(pp_dpm_dcefclk, S_IRUGO | S_IWUSR,
1396 amdgpu_get_pp_dpm_dcefclk,
1397 amdgpu_set_pp_dpm_dcefclk);
1398 static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
1399 amdgpu_get_pp_dpm_pcie,
1400 amdgpu_set_pp_dpm_pcie);
1401 static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
1402 amdgpu_get_pp_sclk_od,
1403 amdgpu_set_pp_sclk_od);
1404 static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
1405 amdgpu_get_pp_mclk_od,
1406 amdgpu_set_pp_mclk_od);
1407 static DEVICE_ATTR(pp_power_profile_mode, S_IRUGO | S_IWUSR,
1408 amdgpu_get_pp_power_profile_mode,
1409 amdgpu_set_pp_power_profile_mode);
1410 static DEVICE_ATTR(pp_od_clk_voltage, S_IRUGO | S_IWUSR,
1411 amdgpu_get_pp_od_clk_voltage,
1412 amdgpu_set_pp_od_clk_voltage);
1413 static DEVICE_ATTR(gpu_busy_percent, S_IRUGO,
1414 amdgpu_get_busy_percent, NULL);
1415 static DEVICE_ATTR(mem_busy_percent, S_IRUGO,
1416 amdgpu_get_memory_busy_percent, NULL);
1417 static DEVICE_ATTR(pcie_bw, S_IRUGO, amdgpu_get_pcie_bw, NULL);
1418 static DEVICE_ATTR(ppfeatures, S_IRUGO | S_IWUSR,
1419 amdgpu_get_ppfeature_status,
1420 amdgpu_set_ppfeature_status);
1422 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
1423 struct device_attribute *attr,
1426 struct amdgpu_device *adev = dev_get_drvdata(dev);
1427 struct drm_device *ddev = adev->ddev;
1428 int channel = to_sensor_dev_attr(attr)->index;
1429 int r, temp, size = sizeof(temp);
1431 /* Can't get temperature when the card is off */
1432 if ((adev->flags & AMD_IS_PX) &&
1433 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1436 if (channel >= PP_TEMP_MAX)
1440 case PP_TEMP_JUNCTION:
1441 /* get current junction temperature */
1442 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
1443 (void *)&temp, &size);
1448 /* get current edge temperature */
1449 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
1450 (void *)&temp, &size);
1455 /* get current memory temperature */
1456 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
1457 (void *)&temp, &size);
1463 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1466 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
1467 struct device_attribute *attr,
1470 struct amdgpu_device *adev = dev_get_drvdata(dev);
1471 int hyst = to_sensor_dev_attr(attr)->index;
1475 temp = adev->pm.dpm.thermal.min_temp;
1477 temp = adev->pm.dpm.thermal.max_temp;
1479 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1482 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
1483 struct device_attribute *attr,
1486 struct amdgpu_device *adev = dev_get_drvdata(dev);
1487 int hyst = to_sensor_dev_attr(attr)->index;
1491 temp = adev->pm.dpm.thermal.min_hotspot_temp;
1493 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
1495 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1498 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
1499 struct device_attribute *attr,
1502 struct amdgpu_device *adev = dev_get_drvdata(dev);
1503 int hyst = to_sensor_dev_attr(attr)->index;
1507 temp = adev->pm.dpm.thermal.min_mem_temp;
1509 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
1511 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1514 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
1515 struct device_attribute *attr,
1518 int channel = to_sensor_dev_attr(attr)->index;
1520 if (channel >= PP_TEMP_MAX)
1523 return snprintf(buf, PAGE_SIZE, "%s\n", temp_label[channel].label);
1526 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
1527 struct device_attribute *attr,
1530 struct amdgpu_device *adev = dev_get_drvdata(dev);
1531 int channel = to_sensor_dev_attr(attr)->index;
1534 if (channel >= PP_TEMP_MAX)
1538 case PP_TEMP_JUNCTION:
1539 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
1542 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
1545 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
1549 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
1552 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
1553 struct device_attribute *attr,
1556 struct amdgpu_device *adev = dev_get_drvdata(dev);
1558 if (is_support_sw_smu(adev)) {
1559 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1561 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
1564 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1567 return sprintf(buf, "%i\n", pwm_mode);
1570 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
1571 struct device_attribute *attr,
1575 struct amdgpu_device *adev = dev_get_drvdata(dev);
1579 /* Can't adjust fan when the card is off */
1580 if ((adev->flags & AMD_IS_PX) &&
1581 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1584 if (is_support_sw_smu(adev)) {
1585 err = kstrtoint(buf, 10, &value);
1589 smu_set_fan_control_mode(&adev->smu, value);
1591 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1594 err = kstrtoint(buf, 10, &value);
1598 amdgpu_dpm_set_fan_control_mode(adev, value);
1604 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
1605 struct device_attribute *attr,
1608 return sprintf(buf, "%i\n", 0);
1611 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
1612 struct device_attribute *attr,
1615 return sprintf(buf, "%i\n", 255);
1618 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
1619 struct device_attribute *attr,
1620 const char *buf, size_t count)
1622 struct amdgpu_device *adev = dev_get_drvdata(dev);
1627 /* Can't adjust fan when the card is off */
1628 if ((adev->flags & AMD_IS_PX) &&
1629 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1631 if (is_support_sw_smu(adev))
1632 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1634 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1635 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
1636 pr_info("manual fan speed control should be enabled first\n");
1640 err = kstrtou32(buf, 10, &value);
1644 value = (value * 100) / 255;
1646 if (is_support_sw_smu(adev)) {
1647 err = smu_set_fan_speed_percent(&adev->smu, value);
1650 } else if (adev->powerplay.pp_funcs->set_fan_speed_percent) {
1651 err = amdgpu_dpm_set_fan_speed_percent(adev, value);
1659 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
1660 struct device_attribute *attr,
1663 struct amdgpu_device *adev = dev_get_drvdata(dev);
1667 /* Can't adjust fan when the card is off */
1668 if ((adev->flags & AMD_IS_PX) &&
1669 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1672 if (is_support_sw_smu(adev)) {
1673 err = smu_get_fan_speed_percent(&adev->smu, &speed);
1676 } else if (adev->powerplay.pp_funcs->get_fan_speed_percent) {
1677 err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
1682 speed = (speed * 255) / 100;
1684 return sprintf(buf, "%i\n", speed);
1687 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
1688 struct device_attribute *attr,
1691 struct amdgpu_device *adev = dev_get_drvdata(dev);
1695 /* Can't adjust fan when the card is off */
1696 if ((adev->flags & AMD_IS_PX) &&
1697 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1700 if (is_support_sw_smu(adev)) {
1701 err = smu_get_current_rpm(&adev->smu, &speed);
1704 } else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1705 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
1710 return sprintf(buf, "%i\n", speed);
1713 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
1714 struct device_attribute *attr,
1717 struct amdgpu_device *adev = dev_get_drvdata(dev);
1719 u32 size = sizeof(min_rpm);
1722 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
1723 (void *)&min_rpm, &size);
1727 return snprintf(buf, PAGE_SIZE, "%d\n", min_rpm);
1730 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
1731 struct device_attribute *attr,
1734 struct amdgpu_device *adev = dev_get_drvdata(dev);
1736 u32 size = sizeof(max_rpm);
1739 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
1740 (void *)&max_rpm, &size);
1744 return snprintf(buf, PAGE_SIZE, "%d\n", max_rpm);
1747 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
1748 struct device_attribute *attr,
1751 struct amdgpu_device *adev = dev_get_drvdata(dev);
1755 /* Can't adjust fan when the card is off */
1756 if ((adev->flags & AMD_IS_PX) &&
1757 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1760 if (is_support_sw_smu(adev)) {
1761 err = smu_get_current_rpm(&adev->smu, &rpm);
1764 } else if (adev->powerplay.pp_funcs->get_fan_speed_rpm) {
1765 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
1770 return sprintf(buf, "%i\n", rpm);
1773 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
1774 struct device_attribute *attr,
1775 const char *buf, size_t count)
1777 struct amdgpu_device *adev = dev_get_drvdata(dev);
1782 if (is_support_sw_smu(adev))
1783 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1785 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1787 if (pwm_mode != AMD_FAN_CTRL_MANUAL)
1790 /* Can't adjust fan when the card is off */
1791 if ((adev->flags & AMD_IS_PX) &&
1792 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1795 err = kstrtou32(buf, 10, &value);
1799 if (is_support_sw_smu(adev)) {
1800 err = smu_set_fan_speed_rpm(&adev->smu, value);
1803 } else if (adev->powerplay.pp_funcs->set_fan_speed_rpm) {
1804 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
1812 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
1813 struct device_attribute *attr,
1816 struct amdgpu_device *adev = dev_get_drvdata(dev);
1819 if (is_support_sw_smu(adev)) {
1820 pwm_mode = smu_get_fan_control_mode(&adev->smu);
1822 if (!adev->powerplay.pp_funcs->get_fan_control_mode)
1825 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
1827 return sprintf(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
1830 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
1831 struct device_attribute *attr,
1835 struct amdgpu_device *adev = dev_get_drvdata(dev);
1840 /* Can't adjust fan when the card is off */
1841 if ((adev->flags & AMD_IS_PX) &&
1842 (adev->ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1846 err = kstrtoint(buf, 10, &value);
1851 pwm_mode = AMD_FAN_CTRL_AUTO;
1852 else if (value == 1)
1853 pwm_mode = AMD_FAN_CTRL_MANUAL;
1857 if (is_support_sw_smu(adev)) {
1858 smu_set_fan_control_mode(&adev->smu, pwm_mode);
1860 if (!adev->powerplay.pp_funcs->set_fan_control_mode)
1862 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
1868 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
1869 struct device_attribute *attr,
1872 struct amdgpu_device *adev = dev_get_drvdata(dev);
1873 struct drm_device *ddev = adev->ddev;
1875 int r, size = sizeof(vddgfx);
1877 /* Can't get voltage when the card is off */
1878 if ((adev->flags & AMD_IS_PX) &&
1879 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1882 /* get the voltage */
1883 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
1884 (void *)&vddgfx, &size);
1888 return snprintf(buf, PAGE_SIZE, "%d\n", vddgfx);
1891 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
1892 struct device_attribute *attr,
1895 return snprintf(buf, PAGE_SIZE, "vddgfx\n");
1898 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
1899 struct device_attribute *attr,
1902 struct amdgpu_device *adev = dev_get_drvdata(dev);
1903 struct drm_device *ddev = adev->ddev;
1905 int r, size = sizeof(vddnb);
1907 /* only APUs have vddnb */
1908 if (!(adev->flags & AMD_IS_APU))
1911 /* Can't get voltage when the card is off */
1912 if ((adev->flags & AMD_IS_PX) &&
1913 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1916 /* get the voltage */
1917 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
1918 (void *)&vddnb, &size);
1922 return snprintf(buf, PAGE_SIZE, "%d\n", vddnb);
1925 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
1926 struct device_attribute *attr,
1929 return snprintf(buf, PAGE_SIZE, "vddnb\n");
1932 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
1933 struct device_attribute *attr,
1936 struct amdgpu_device *adev = dev_get_drvdata(dev);
1937 struct drm_device *ddev = adev->ddev;
1939 int r, size = sizeof(u32);
1942 /* Can't get power when the card is off */
1943 if ((adev->flags & AMD_IS_PX) &&
1944 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
1947 /* get the voltage */
1948 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
1949 (void *)&query, &size);
1953 /* convert to microwatts */
1954 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
1956 return snprintf(buf, PAGE_SIZE, "%u\n", uw);
1959 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
1960 struct device_attribute *attr,
1963 return sprintf(buf, "%i\n", 0);
1966 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
1967 struct device_attribute *attr,
1970 struct amdgpu_device *adev = dev_get_drvdata(dev);
1973 if (is_support_sw_smu(adev)) {
1974 smu_get_power_limit(&adev->smu, &limit, true);
1975 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1976 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1977 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, true);
1978 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1980 return snprintf(buf, PAGE_SIZE, "\n");
1984 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
1985 struct device_attribute *attr,
1988 struct amdgpu_device *adev = dev_get_drvdata(dev);
1991 if (is_support_sw_smu(adev)) {
1992 smu_get_power_limit(&adev->smu, &limit, false);
1993 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1994 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->get_power_limit) {
1995 adev->powerplay.pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit, false);
1996 return snprintf(buf, PAGE_SIZE, "%u\n", limit * 1000000);
1998 return snprintf(buf, PAGE_SIZE, "\n");
2003 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
2004 struct device_attribute *attr,
2008 struct amdgpu_device *adev = dev_get_drvdata(dev);
2012 err = kstrtou32(buf, 10, &value);
2016 value = value / 1000000; /* convert to Watt */
2017 if (is_support_sw_smu(adev)) {
2018 adev->smu.funcs->set_power_limit(&adev->smu, value);
2019 } else if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->set_power_limit) {
2020 err = adev->powerplay.pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
2030 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
2031 struct device_attribute *attr,
2034 struct amdgpu_device *adev = dev_get_drvdata(dev);
2035 struct drm_device *ddev = adev->ddev;
2037 int r, size = sizeof(sclk);
2039 /* Can't get voltage when the card is off */
2040 if ((adev->flags & AMD_IS_PX) &&
2041 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
2044 /* sanity check PP is enabled */
2045 if (!(adev->powerplay.pp_funcs &&
2046 adev->powerplay.pp_funcs->read_sensor))
2050 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
2051 (void *)&sclk, &size);
2055 return snprintf(buf, PAGE_SIZE, "%d\n", sclk * 10 * 1000);
2058 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
2059 struct device_attribute *attr,
2062 return snprintf(buf, PAGE_SIZE, "sclk\n");
2065 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
2066 struct device_attribute *attr,
2069 struct amdgpu_device *adev = dev_get_drvdata(dev);
2070 struct drm_device *ddev = adev->ddev;
2072 int r, size = sizeof(mclk);
2074 /* Can't get voltage when the card is off */
2075 if ((adev->flags & AMD_IS_PX) &&
2076 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
2079 /* sanity check PP is enabled */
2080 if (!(adev->powerplay.pp_funcs &&
2081 adev->powerplay.pp_funcs->read_sensor))
2085 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
2086 (void *)&mclk, &size);
2090 return snprintf(buf, PAGE_SIZE, "%d\n", mclk * 10 * 1000);
2093 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
2094 struct device_attribute *attr,
2097 return snprintf(buf, PAGE_SIZE, "mclk\n");
2103 * The amdgpu driver exposes the following sensor interfaces:
2105 * - GPU temperature (via the on-die sensor)
2109 * - Northbridge voltage (APUs only)
2115 * - GPU gfx/compute engine clock
2117 * - GPU memory clock (dGPU only)
2119 * hwmon interfaces for GPU temperature:
2121 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
2122 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
2124 * - temp[1-3]_label: temperature channel label
2125 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
2127 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
2128 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
2130 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
2131 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
2133 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
2134 * - these are supported on SOC15 dGPUs only
2136 * hwmon interfaces for GPU voltage:
2138 * - in0_input: the voltage on the GPU in millivolts
2140 * - in1_input: the voltage on the Northbridge in millivolts
2142 * hwmon interfaces for GPU power:
2144 * - power1_average: average power used by the GPU in microWatts
2146 * - power1_cap_min: minimum cap supported in microWatts
2148 * - power1_cap_max: maximum cap supported in microWatts
2150 * - power1_cap: selected power cap in microWatts
2152 * hwmon interfaces for GPU fan:
2154 * - pwm1: pulse width modulation fan level (0-255)
2156 * - 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)
2158 * - pwm1_min: pulse width modulation fan control minimum level (0)
2160 * - pwm1_max: pulse width modulation fan control maximum level (255)
2162 * - fan1_min: an minimum value Unit: revolution/min (RPM)
2164 * - fan1_max: an maxmum value Unit: revolution/max (RPM)
2166 * - fan1_input: fan speed in RPM
2168 * - fan[1-*]_target: Desired fan speed Unit: revolution/min (RPM)
2170 * - fan[1-*]_enable: Enable or disable the sensors.1: Enable 0: Disable
2172 * hwmon interfaces for GPU clocks:
2174 * - freq1_input: the gfx/compute clock in hertz
2176 * - freq2_input: the memory clock in hertz
2178 * You can use hwmon tools like sensors to view this information on your system.
2182 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
2183 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
2184 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
2185 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
2186 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
2187 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
2188 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
2189 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
2190 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
2191 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
2192 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
2193 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
2194 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
2195 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
2196 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
2197 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
2198 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
2199 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
2200 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
2201 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
2202 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
2203 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
2204 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
2205 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
2206 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
2207 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
2208 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
2209 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
2210 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
2211 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
2212 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
2213 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
2214 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
2215 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
2216 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
2217 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
2219 static struct attribute *hwmon_attributes[] = {
2220 &sensor_dev_attr_temp1_input.dev_attr.attr,
2221 &sensor_dev_attr_temp1_crit.dev_attr.attr,
2222 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
2223 &sensor_dev_attr_temp2_input.dev_attr.attr,
2224 &sensor_dev_attr_temp2_crit.dev_attr.attr,
2225 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
2226 &sensor_dev_attr_temp3_input.dev_attr.attr,
2227 &sensor_dev_attr_temp3_crit.dev_attr.attr,
2228 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
2229 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
2230 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
2231 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
2232 &sensor_dev_attr_temp1_label.dev_attr.attr,
2233 &sensor_dev_attr_temp2_label.dev_attr.attr,
2234 &sensor_dev_attr_temp3_label.dev_attr.attr,
2235 &sensor_dev_attr_pwm1.dev_attr.attr,
2236 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2237 &sensor_dev_attr_pwm1_min.dev_attr.attr,
2238 &sensor_dev_attr_pwm1_max.dev_attr.attr,
2239 &sensor_dev_attr_fan1_input.dev_attr.attr,
2240 &sensor_dev_attr_fan1_min.dev_attr.attr,
2241 &sensor_dev_attr_fan1_max.dev_attr.attr,
2242 &sensor_dev_attr_fan1_target.dev_attr.attr,
2243 &sensor_dev_attr_fan1_enable.dev_attr.attr,
2244 &sensor_dev_attr_in0_input.dev_attr.attr,
2245 &sensor_dev_attr_in0_label.dev_attr.attr,
2246 &sensor_dev_attr_in1_input.dev_attr.attr,
2247 &sensor_dev_attr_in1_label.dev_attr.attr,
2248 &sensor_dev_attr_power1_average.dev_attr.attr,
2249 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
2250 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
2251 &sensor_dev_attr_power1_cap.dev_attr.attr,
2252 &sensor_dev_attr_freq1_input.dev_attr.attr,
2253 &sensor_dev_attr_freq1_label.dev_attr.attr,
2254 &sensor_dev_attr_freq2_input.dev_attr.attr,
2255 &sensor_dev_attr_freq2_label.dev_attr.attr,
2259 static umode_t hwmon_attributes_visible(struct kobject *kobj,
2260 struct attribute *attr, int index)
2262 struct device *dev = kobj_to_dev(kobj);
2263 struct amdgpu_device *adev = dev_get_drvdata(dev);
2264 umode_t effective_mode = attr->mode;
2266 /* Skip fan attributes if fan is not present */
2267 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2268 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2269 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2270 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2271 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2272 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2273 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2274 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2275 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2278 /* Skip fan attributes on APU */
2279 if ((adev->flags & AMD_IS_APU) &&
2280 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2281 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2282 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2283 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2284 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2285 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2286 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2287 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2288 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2291 /* Skip limit attributes if DPM is not enabled */
2292 if (!adev->pm.dpm_enabled &&
2293 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
2294 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
2295 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
2296 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
2297 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2298 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
2299 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
2300 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
2301 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2302 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
2303 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
2306 if (!is_support_sw_smu(adev)) {
2307 /* mask fan attributes if we have no bindings for this asic to expose */
2308 if ((!adev->powerplay.pp_funcs->get_fan_speed_percent &&
2309 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
2310 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
2311 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
2312 effective_mode &= ~S_IRUGO;
2314 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2315 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
2316 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
2317 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
2318 effective_mode &= ~S_IWUSR;
2321 if ((adev->flags & AMD_IS_APU) &&
2322 (attr == &sensor_dev_attr_power1_average.dev_attr.attr ||
2323 attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
2324 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
2325 attr == &sensor_dev_attr_power1_cap.dev_attr.attr))
2328 if (!is_support_sw_smu(adev)) {
2329 /* hide max/min values if we can't both query and manage the fan */
2330 if ((!adev->powerplay.pp_funcs->set_fan_speed_percent &&
2331 !adev->powerplay.pp_funcs->get_fan_speed_percent) &&
2332 (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2333 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2334 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
2335 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
2338 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
2339 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
2340 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
2341 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
2345 /* only APUs have vddnb */
2346 if (!(adev->flags & AMD_IS_APU) &&
2347 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
2348 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
2351 /* no mclk on APUs */
2352 if ((adev->flags & AMD_IS_APU) &&
2353 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
2354 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
2357 /* only SOC15 dGPUs support hotspot and mem temperatures */
2358 if (((adev->flags & AMD_IS_APU) ||
2359 adev->asic_type < CHIP_VEGA10) &&
2360 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
2361 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
2362 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
2363 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
2364 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
2365 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
2366 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
2367 attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
2368 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
2369 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
2370 attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
2373 return effective_mode;
2376 static const struct attribute_group hwmon_attrgroup = {
2377 .attrs = hwmon_attributes,
2378 .is_visible = hwmon_attributes_visible,
2381 static const struct attribute_group *hwmon_groups[] = {
2386 void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
2388 struct amdgpu_device *adev =
2389 container_of(work, struct amdgpu_device,
2390 pm.dpm.thermal.work);
2391 /* switch to the thermal state */
2392 enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
2393 int temp, size = sizeof(temp);
2395 if (!adev->pm.dpm_enabled)
2398 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP,
2399 (void *)&temp, &size)) {
2400 if (temp < adev->pm.dpm.thermal.min_temp)
2401 /* switch back the user state */
2402 dpm_state = adev->pm.dpm.user_state;
2404 if (adev->pm.dpm.thermal.high_to_low)
2405 /* switch back the user state */
2406 dpm_state = adev->pm.dpm.user_state;
2408 mutex_lock(&adev->pm.mutex);
2409 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
2410 adev->pm.dpm.thermal_active = true;
2412 adev->pm.dpm.thermal_active = false;
2413 adev->pm.dpm.state = dpm_state;
2414 mutex_unlock(&adev->pm.mutex);
2416 amdgpu_pm_compute_clocks(adev);
2419 static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
2420 enum amd_pm_state_type dpm_state)
2423 struct amdgpu_ps *ps;
2425 bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
2428 /* check if the vblank period is too short to adjust the mclk */
2429 if (single_display && adev->powerplay.pp_funcs->vblank_too_short) {
2430 if (amdgpu_dpm_vblank_too_short(adev))
2431 single_display = false;
2434 /* certain older asics have a separare 3D performance state,
2435 * so try that first if the user selected performance
2437 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
2438 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
2439 /* balanced states don't exist at the moment */
2440 if (dpm_state == POWER_STATE_TYPE_BALANCED)
2441 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2444 /* Pick the best power state based on current conditions */
2445 for (i = 0; i < adev->pm.dpm.num_ps; i++) {
2446 ps = &adev->pm.dpm.ps[i];
2447 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
2448 switch (dpm_state) {
2450 case POWER_STATE_TYPE_BATTERY:
2451 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
2452 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2459 case POWER_STATE_TYPE_BALANCED:
2460 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
2461 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2468 case POWER_STATE_TYPE_PERFORMANCE:
2469 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
2470 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
2477 /* internal states */
2478 case POWER_STATE_TYPE_INTERNAL_UVD:
2479 if (adev->pm.dpm.uvd_ps)
2480 return adev->pm.dpm.uvd_ps;
2483 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
2484 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
2487 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
2488 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
2491 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
2492 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
2495 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
2496 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
2499 case POWER_STATE_TYPE_INTERNAL_BOOT:
2500 return adev->pm.dpm.boot_ps;
2501 case POWER_STATE_TYPE_INTERNAL_THERMAL:
2502 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
2505 case POWER_STATE_TYPE_INTERNAL_ACPI:
2506 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
2509 case POWER_STATE_TYPE_INTERNAL_ULV:
2510 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
2513 case POWER_STATE_TYPE_INTERNAL_3DPERF:
2514 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
2521 /* use a fallback state if we didn't match */
2522 switch (dpm_state) {
2523 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
2524 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
2525 goto restart_search;
2526 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
2527 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
2528 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
2529 if (adev->pm.dpm.uvd_ps) {
2530 return adev->pm.dpm.uvd_ps;
2532 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2533 goto restart_search;
2535 case POWER_STATE_TYPE_INTERNAL_THERMAL:
2536 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
2537 goto restart_search;
2538 case POWER_STATE_TYPE_INTERNAL_ACPI:
2539 dpm_state = POWER_STATE_TYPE_BATTERY;
2540 goto restart_search;
2541 case POWER_STATE_TYPE_BATTERY:
2542 case POWER_STATE_TYPE_BALANCED:
2543 case POWER_STATE_TYPE_INTERNAL_3DPERF:
2544 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
2545 goto restart_search;
2553 static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
2555 struct amdgpu_ps *ps;
2556 enum amd_pm_state_type dpm_state;
2560 /* if dpm init failed */
2561 if (!adev->pm.dpm_enabled)
2564 if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
2565 /* add other state override checks here */
2566 if ((!adev->pm.dpm.thermal_active) &&
2567 (!adev->pm.dpm.uvd_active))
2568 adev->pm.dpm.state = adev->pm.dpm.user_state;
2570 dpm_state = adev->pm.dpm.state;
2572 ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
2574 adev->pm.dpm.requested_ps = ps;
2578 if (amdgpu_dpm == 1 && adev->powerplay.pp_funcs->print_power_state) {
2579 printk("switching from power state:\n");
2580 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
2581 printk("switching to power state:\n");
2582 amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
2585 /* update whether vce is active */
2586 ps->vce_active = adev->pm.dpm.vce_active;
2587 if (adev->powerplay.pp_funcs->display_configuration_changed)
2588 amdgpu_dpm_display_configuration_changed(adev);
2590 ret = amdgpu_dpm_pre_set_power_state(adev);
2594 if (adev->powerplay.pp_funcs->check_state_equal) {
2595 if (0 != amdgpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal))
2602 amdgpu_dpm_set_power_state(adev);
2603 amdgpu_dpm_post_set_power_state(adev);
2605 adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
2606 adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
2608 if (adev->powerplay.pp_funcs->force_performance_level) {
2609 if (adev->pm.dpm.thermal_active) {
2610 enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
2611 /* force low perf level for thermal */
2612 amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
2613 /* save the user's level */
2614 adev->pm.dpm.forced_level = level;
2616 /* otherwise, user selected level */
2617 amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
2622 void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
2625 if (is_support_sw_smu(adev)) {
2626 ret = smu_dpm_set_power_gate(&adev->smu, AMD_IP_BLOCK_TYPE_UVD, enable);
2628 DRM_ERROR("[SW SMU]: dpm enable uvd failed, state = %s, ret = %d. \n",
2629 enable ? "true" : "false", ret);
2630 } else if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
2631 /* enable/disable UVD */
2632 mutex_lock(&adev->pm.mutex);
2633 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_UVD, !enable);
2634 mutex_unlock(&adev->pm.mutex);
2636 /* enable/disable Low Memory PState for UVD (4k videos) */
2637 if (adev->asic_type == CHIP_STONEY &&
2638 adev->uvd.decode_image_width >= WIDTH_4K) {
2639 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2641 if (hwmgr && hwmgr->hwmgr_func &&
2642 hwmgr->hwmgr_func->update_nbdpm_pstate)
2643 hwmgr->hwmgr_func->update_nbdpm_pstate(hwmgr,
2649 void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
2652 if (is_support_sw_smu(adev)) {
2653 ret = smu_dpm_set_power_gate(&adev->smu, AMD_IP_BLOCK_TYPE_VCE, enable);
2655 DRM_ERROR("[SW SMU]: dpm enable vce failed, state = %s, ret = %d. \n",
2656 enable ? "true" : "false", ret);
2657 } else if (adev->powerplay.pp_funcs->set_powergating_by_smu) {
2658 /* enable/disable VCE */
2659 mutex_lock(&adev->pm.mutex);
2660 amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_VCE, !enable);
2661 mutex_unlock(&adev->pm.mutex);
2665 void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
2669 if (adev->powerplay.pp_funcs->print_power_state == NULL)
2672 for (i = 0; i < adev->pm.dpm.num_ps; i++)
2673 amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
2677 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
2679 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2682 if (adev->pm.sysfs_initialized)
2685 if (adev->pm.dpm_enabled == 0)
2688 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
2691 if (IS_ERR(adev->pm.int_hwmon_dev)) {
2692 ret = PTR_ERR(adev->pm.int_hwmon_dev);
2694 "Unable to register hwmon device: %d\n", ret);
2698 ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
2700 DRM_ERROR("failed to create device file for dpm state\n");
2703 ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2705 DRM_ERROR("failed to create device file for dpm state\n");
2710 ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
2712 DRM_ERROR("failed to create device file pp_num_states\n");
2715 ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
2717 DRM_ERROR("failed to create device file pp_cur_state\n");
2720 ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
2722 DRM_ERROR("failed to create device file pp_force_state\n");
2725 ret = device_create_file(adev->dev, &dev_attr_pp_table);
2727 DRM_ERROR("failed to create device file pp_table\n");
2731 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
2733 DRM_ERROR("failed to create device file pp_dpm_sclk\n");
2736 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
2738 DRM_ERROR("failed to create device file pp_dpm_mclk\n");
2741 if (adev->asic_type >= CHIP_VEGA10) {
2742 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_socclk);
2744 DRM_ERROR("failed to create device file pp_dpm_socclk\n");
2747 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_dcefclk);
2749 DRM_ERROR("failed to create device file pp_dpm_dcefclk\n");
2753 if (adev->asic_type >= CHIP_VEGA20) {
2754 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_fclk);
2756 DRM_ERROR("failed to create device file pp_dpm_fclk\n");
2760 ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
2762 DRM_ERROR("failed to create device file pp_dpm_pcie\n");
2765 ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
2767 DRM_ERROR("failed to create device file pp_sclk_od\n");
2770 ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
2772 DRM_ERROR("failed to create device file pp_mclk_od\n");
2775 ret = device_create_file(adev->dev,
2776 &dev_attr_pp_power_profile_mode);
2778 DRM_ERROR("failed to create device file "
2779 "pp_power_profile_mode\n");
2782 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2783 (!is_support_sw_smu(adev) && hwmgr->od_enabled)) {
2784 ret = device_create_file(adev->dev,
2785 &dev_attr_pp_od_clk_voltage);
2787 DRM_ERROR("failed to create device file "
2788 "pp_od_clk_voltage\n");
2792 ret = device_create_file(adev->dev,
2793 &dev_attr_gpu_busy_percent);
2795 DRM_ERROR("failed to create device file "
2796 "gpu_busy_level\n");
2799 /* APU does not have its own dedicated memory */
2800 if (!(adev->flags & AMD_IS_APU)) {
2801 ret = device_create_file(adev->dev,
2802 &dev_attr_mem_busy_percent);
2804 DRM_ERROR("failed to create device file "
2805 "mem_busy_percent\n");
2809 /* PCIe Perf counters won't work on APU nodes */
2810 if (!(adev->flags & AMD_IS_APU)) {
2811 ret = device_create_file(adev->dev, &dev_attr_pcie_bw);
2813 DRM_ERROR("failed to create device file pcie_bw\n");
2817 ret = amdgpu_debugfs_pm_init(adev);
2819 DRM_ERROR("Failed to register debugfs file for dpm!\n");
2823 if ((adev->asic_type >= CHIP_VEGA10) &&
2824 !(adev->flags & AMD_IS_APU)) {
2825 ret = device_create_file(adev->dev,
2826 &dev_attr_ppfeatures);
2828 DRM_ERROR("failed to create device file "
2834 adev->pm.sysfs_initialized = true;
2839 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
2841 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2843 if (adev->pm.dpm_enabled == 0)
2846 if (adev->pm.int_hwmon_dev)
2847 hwmon_device_unregister(adev->pm.int_hwmon_dev);
2848 device_remove_file(adev->dev, &dev_attr_power_dpm_state);
2849 device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
2851 device_remove_file(adev->dev, &dev_attr_pp_num_states);
2852 device_remove_file(adev->dev, &dev_attr_pp_cur_state);
2853 device_remove_file(adev->dev, &dev_attr_pp_force_state);
2854 device_remove_file(adev->dev, &dev_attr_pp_table);
2856 device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
2857 device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
2858 if (adev->asic_type >= CHIP_VEGA10) {
2859 device_remove_file(adev->dev, &dev_attr_pp_dpm_socclk);
2860 device_remove_file(adev->dev, &dev_attr_pp_dpm_dcefclk);
2862 device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
2863 if (adev->asic_type >= CHIP_VEGA20)
2864 device_remove_file(adev->dev, &dev_attr_pp_dpm_fclk);
2865 device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
2866 device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
2867 device_remove_file(adev->dev,
2868 &dev_attr_pp_power_profile_mode);
2869 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2870 (!is_support_sw_smu(adev) && hwmgr->od_enabled))
2871 device_remove_file(adev->dev,
2872 &dev_attr_pp_od_clk_voltage);
2873 device_remove_file(adev->dev, &dev_attr_gpu_busy_percent);
2874 if (!(adev->flags & AMD_IS_APU))
2875 device_remove_file(adev->dev, &dev_attr_mem_busy_percent);
2876 if (!(adev->flags & AMD_IS_APU))
2877 device_remove_file(adev->dev, &dev_attr_pcie_bw);
2878 if ((adev->asic_type >= CHIP_VEGA10) &&
2879 !(adev->flags & AMD_IS_APU))
2880 device_remove_file(adev->dev, &dev_attr_ppfeatures);
2883 void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
2887 if (!adev->pm.dpm_enabled)
2890 if (adev->mode_info.num_crtc)
2891 amdgpu_display_bandwidth_update(adev);
2893 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
2894 struct amdgpu_ring *ring = adev->rings[i];
2895 if (ring && ring->sched.ready)
2896 amdgpu_fence_wait_empty(ring);
2899 if (is_support_sw_smu(adev)) {
2900 struct smu_context *smu = &adev->smu;
2901 struct smu_dpm_context *smu_dpm = &adev->smu.smu_dpm;
2902 mutex_lock(&(smu->mutex));
2903 smu_handle_task(&adev->smu,
2905 AMD_PP_TASK_DISPLAY_CONFIG_CHANGE);
2906 mutex_unlock(&(smu->mutex));
2908 if (adev->powerplay.pp_funcs->dispatch_tasks) {
2909 if (!amdgpu_device_has_dc_support(adev)) {
2910 mutex_lock(&adev->pm.mutex);
2911 amdgpu_dpm_get_active_displays(adev);
2912 adev->pm.pm_display_cfg.num_display = adev->pm.dpm.new_active_crtc_count;
2913 adev->pm.pm_display_cfg.vrefresh = amdgpu_dpm_get_vrefresh(adev);
2914 adev->pm.pm_display_cfg.min_vblank_time = amdgpu_dpm_get_vblank_time(adev);
2915 /* we have issues with mclk switching with refresh rates over 120 hz on the non-DC code. */
2916 if (adev->pm.pm_display_cfg.vrefresh > 120)
2917 adev->pm.pm_display_cfg.min_vblank_time = 0;
2918 if (adev->powerplay.pp_funcs->display_configuration_change)
2919 adev->powerplay.pp_funcs->display_configuration_change(
2920 adev->powerplay.pp_handle,
2921 &adev->pm.pm_display_cfg);
2922 mutex_unlock(&adev->pm.mutex);
2924 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_DISPLAY_CONFIG_CHANGE, NULL);
2926 mutex_lock(&adev->pm.mutex);
2927 amdgpu_dpm_get_active_displays(adev);
2928 amdgpu_dpm_change_power_state_locked(adev);
2929 mutex_unlock(&adev->pm.mutex);
2937 #if defined(CONFIG_DEBUG_FS)
2939 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
2947 size = sizeof(value);
2948 seq_printf(m, "GFX Clocks and Power:\n");
2949 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
2950 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
2951 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
2952 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
2953 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
2954 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
2955 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
2956 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
2957 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
2958 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
2959 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
2960 seq_printf(m, "\t%u mV (VDDNB)\n", value);
2961 size = sizeof(uint32_t);
2962 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
2963 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
2964 size = sizeof(value);
2965 seq_printf(m, "\n");
2968 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
2969 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
2972 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
2973 seq_printf(m, "GPU Load: %u %%\n", value);
2975 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
2976 seq_printf(m, "MEM Load: %u %%\n", value);
2978 seq_printf(m, "\n");
2980 /* SMC feature mask */
2981 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
2982 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
2985 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
2987 seq_printf(m, "UVD: Disabled\n");
2989 seq_printf(m, "UVD: Enabled\n");
2990 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
2991 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
2992 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
2993 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
2996 seq_printf(m, "\n");
2999 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3001 seq_printf(m, "VCE: Disabled\n");
3003 seq_printf(m, "VCE: Enabled\n");
3004 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3005 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3012 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3016 for (i = 0; clocks[i].flag; i++)
3017 seq_printf(m, "\t%s: %s\n", clocks[i].name,
3018 (flags & clocks[i].flag) ? "On" : "Off");
3021 static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
3023 struct drm_info_node *node = (struct drm_info_node *) m->private;
3024 struct drm_device *dev = node->minor->dev;
3025 struct amdgpu_device *adev = dev->dev_private;
3026 struct drm_device *ddev = adev->ddev;
3029 amdgpu_device_ip_get_clockgating_state(adev, &flags);
3030 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3031 amdgpu_parse_cg_state(m, flags);
3032 seq_printf(m, "\n");
3034 if (!adev->pm.dpm_enabled) {
3035 seq_printf(m, "dpm not enabled\n");
3038 if ((adev->flags & AMD_IS_PX) &&
3039 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
3040 seq_printf(m, "PX asic powered off\n");
3041 } else if (!is_support_sw_smu(adev) && adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3042 mutex_lock(&adev->pm.mutex);
3043 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3044 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3046 seq_printf(m, "Debugfs support not implemented for this asic\n");
3047 mutex_unlock(&adev->pm.mutex);
3049 return amdgpu_debugfs_pm_info_pp(m, adev);
3055 static const struct drm_info_list amdgpu_pm_info_list[] = {
3056 {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
3060 static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3062 #if defined(CONFIG_DEBUG_FS)
3063 return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));