2 * Copyright 2017 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "amdgpu_drv.h"
28 #include "amdgpu_pm.h"
29 #include "amdgpu_dpm.h"
31 #include <linux/pci.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/nospec.h>
35 #include <linux/pm_runtime.h>
36 #include <asm/processor.h>
39 static const struct cg_flag_name clocks[] = {
40 {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
41 {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
42 {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
43 {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
44 {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
45 {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
46 {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
47 {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
48 {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
49 {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
50 {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
51 {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
52 {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
53 {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
54 {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
55 {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
56 {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
57 {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
58 {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
59 {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
60 {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
61 {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
62 {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
63 {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
64 {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
65 {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
66 {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
67 {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
68 {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
69 {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
71 {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
72 {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
76 static const struct hwmon_temp_label {
77 enum PP_HWMON_TEMP channel;
80 {PP_TEMP_EDGE, "edge"},
81 {PP_TEMP_JUNCTION, "junction"},
85 const char * const amdgpu_pp_profile_name[] = {
96 * DOC: power_dpm_state
98 * The power_dpm_state file is a legacy interface and is only provided for
99 * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
100 * certain power related parameters. The file power_dpm_state is used for this.
101 * It accepts the following arguments:
111 * On older GPUs, the vbios provided a special power state for battery
112 * operation. Selecting battery switched to this state. This is no
113 * longer provided on newer GPUs so the option does nothing in that case.
117 * On older GPUs, the vbios provided a special power state for balanced
118 * operation. Selecting balanced switched to this state. This is no
119 * longer provided on newer GPUs so the option does nothing in that case.
123 * On older GPUs, the vbios provided a special power state for performance
124 * operation. Selecting performance switched to this state. This is no
125 * longer provided on newer GPUs so the option does nothing in that case.
129 static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
130 struct device_attribute *attr,
133 struct drm_device *ddev = dev_get_drvdata(dev);
134 struct amdgpu_device *adev = drm_to_adev(ddev);
135 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
136 enum amd_pm_state_type pm;
139 if (amdgpu_in_reset(adev))
141 if (adev->in_suspend && !adev->in_runpm)
144 ret = pm_runtime_get_sync(ddev->dev);
146 pm_runtime_put_autosuspend(ddev->dev);
150 if (pp_funcs->get_current_power_state) {
151 pm = amdgpu_dpm_get_current_power_state(adev);
153 pm = adev->pm.dpm.user_state;
156 pm_runtime_mark_last_busy(ddev->dev);
157 pm_runtime_put_autosuspend(ddev->dev);
159 return sysfs_emit(buf, "%s\n",
160 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
161 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
164 static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
165 struct device_attribute *attr,
169 struct drm_device *ddev = dev_get_drvdata(dev);
170 struct amdgpu_device *adev = drm_to_adev(ddev);
171 enum amd_pm_state_type state;
174 if (amdgpu_in_reset(adev))
176 if (adev->in_suspend && !adev->in_runpm)
179 if (strncmp("battery", buf, strlen("battery")) == 0)
180 state = POWER_STATE_TYPE_BATTERY;
181 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
182 state = POWER_STATE_TYPE_BALANCED;
183 else if (strncmp("performance", buf, strlen("performance")) == 0)
184 state = POWER_STATE_TYPE_PERFORMANCE;
188 ret = pm_runtime_get_sync(ddev->dev);
190 pm_runtime_put_autosuspend(ddev->dev);
194 if (is_support_sw_smu(adev)) {
195 mutex_lock(&adev->pm.mutex);
196 adev->pm.dpm.user_state = state;
197 mutex_unlock(&adev->pm.mutex);
198 } else if (adev->powerplay.pp_funcs->dispatch_tasks) {
199 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_ENABLE_USER_STATE, &state);
201 mutex_lock(&adev->pm.mutex);
202 adev->pm.dpm.user_state = state;
203 mutex_unlock(&adev->pm.mutex);
205 amdgpu_pm_compute_clocks(adev);
207 pm_runtime_mark_last_busy(ddev->dev);
208 pm_runtime_put_autosuspend(ddev->dev);
215 * DOC: power_dpm_force_performance_level
217 * The amdgpu driver provides a sysfs API for adjusting certain power
218 * related parameters. The file power_dpm_force_performance_level is
219 * used for this. It accepts the following arguments:
239 * When auto is selected, the driver will attempt to dynamically select
240 * the optimal power profile for current conditions in the driver.
244 * When low is selected, the clocks are forced to the lowest power state.
248 * When high is selected, the clocks are forced to the highest power state.
252 * When manual is selected, the user can manually adjust which power states
253 * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
254 * and pp_dpm_pcie files and adjust the power state transition heuristics
255 * via the pp_power_profile_mode sysfs file.
262 * When the profiling modes are selected, clock and power gating are
263 * disabled and the clocks are set for different profiling cases. This
264 * mode is recommended for profiling specific work loads where you do
265 * not want clock or power gating for clock fluctuation to interfere
266 * with your results. profile_standard sets the clocks to a fixed clock
267 * level which varies from asic to asic. profile_min_sclk forces the sclk
268 * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
269 * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
273 static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
274 struct device_attribute *attr,
277 struct drm_device *ddev = dev_get_drvdata(dev);
278 struct amdgpu_device *adev = drm_to_adev(ddev);
279 enum amd_dpm_forced_level level = 0xff;
282 if (amdgpu_in_reset(adev))
284 if (adev->in_suspend && !adev->in_runpm)
287 ret = pm_runtime_get_sync(ddev->dev);
289 pm_runtime_put_autosuspend(ddev->dev);
293 if (adev->powerplay.pp_funcs->get_performance_level)
294 level = amdgpu_dpm_get_performance_level(adev);
296 level = adev->pm.dpm.forced_level;
298 pm_runtime_mark_last_busy(ddev->dev);
299 pm_runtime_put_autosuspend(ddev->dev);
301 return sysfs_emit(buf, "%s\n",
302 (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
303 (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
304 (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
305 (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
306 (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
307 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
308 (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
309 (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
310 (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
314 static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
315 struct device_attribute *attr,
319 struct drm_device *ddev = dev_get_drvdata(dev);
320 struct amdgpu_device *adev = drm_to_adev(ddev);
321 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
322 enum amd_dpm_forced_level level;
323 enum amd_dpm_forced_level current_level;
326 if (amdgpu_in_reset(adev))
328 if (adev->in_suspend && !adev->in_runpm)
331 if (strncmp("low", buf, strlen("low")) == 0) {
332 level = AMD_DPM_FORCED_LEVEL_LOW;
333 } else if (strncmp("high", buf, strlen("high")) == 0) {
334 level = AMD_DPM_FORCED_LEVEL_HIGH;
335 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
336 level = AMD_DPM_FORCED_LEVEL_AUTO;
337 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
338 level = AMD_DPM_FORCED_LEVEL_MANUAL;
339 } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
340 level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
341 } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
342 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
343 } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
344 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
345 } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
346 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
347 } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
348 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
349 } else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
350 level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
355 ret = pm_runtime_get_sync(ddev->dev);
357 pm_runtime_put_autosuspend(ddev->dev);
361 if (pp_funcs->get_performance_level)
362 current_level = amdgpu_dpm_get_performance_level(adev);
364 current_level = adev->pm.dpm.forced_level;
366 if (current_level == level) {
367 pm_runtime_mark_last_busy(ddev->dev);
368 pm_runtime_put_autosuspend(ddev->dev);
372 if (adev->asic_type == CHIP_RAVEN) {
373 if (!(adev->apu_flags & AMD_APU_IS_RAVEN2)) {
374 if (current_level != AMD_DPM_FORCED_LEVEL_MANUAL && level == AMD_DPM_FORCED_LEVEL_MANUAL)
375 amdgpu_gfx_off_ctrl(adev, false);
376 else if (current_level == AMD_DPM_FORCED_LEVEL_MANUAL && level != AMD_DPM_FORCED_LEVEL_MANUAL)
377 amdgpu_gfx_off_ctrl(adev, true);
381 /* profile_exit setting is valid only when current mode is in profile mode */
382 if (!(current_level & (AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD |
383 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK |
384 AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK |
385 AMD_DPM_FORCED_LEVEL_PROFILE_PEAK)) &&
386 (level == AMD_DPM_FORCED_LEVEL_PROFILE_EXIT)) {
387 pr_err("Currently not in any profile mode!\n");
388 pm_runtime_mark_last_busy(ddev->dev);
389 pm_runtime_put_autosuspend(ddev->dev);
393 if (pp_funcs->force_performance_level) {
394 mutex_lock(&adev->pm.mutex);
395 if (adev->pm.dpm.thermal_active) {
396 mutex_unlock(&adev->pm.mutex);
397 pm_runtime_mark_last_busy(ddev->dev);
398 pm_runtime_put_autosuspend(ddev->dev);
401 ret = amdgpu_dpm_force_performance_level(adev, level);
403 mutex_unlock(&adev->pm.mutex);
404 pm_runtime_mark_last_busy(ddev->dev);
405 pm_runtime_put_autosuspend(ddev->dev);
408 adev->pm.dpm.forced_level = level;
410 mutex_unlock(&adev->pm.mutex);
412 pm_runtime_mark_last_busy(ddev->dev);
413 pm_runtime_put_autosuspend(ddev->dev);
418 static ssize_t amdgpu_get_pp_num_states(struct device *dev,
419 struct device_attribute *attr,
422 struct drm_device *ddev = dev_get_drvdata(dev);
423 struct amdgpu_device *adev = drm_to_adev(ddev);
424 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
425 struct pp_states_info data;
429 if (amdgpu_in_reset(adev))
431 if (adev->in_suspend && !adev->in_runpm)
434 ret = pm_runtime_get_sync(ddev->dev);
436 pm_runtime_put_autosuspend(ddev->dev);
440 if (pp_funcs->get_pp_num_states) {
441 amdgpu_dpm_get_pp_num_states(adev, &data);
443 memset(&data, 0, sizeof(data));
446 pm_runtime_mark_last_busy(ddev->dev);
447 pm_runtime_put_autosuspend(ddev->dev);
449 buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
450 for (i = 0; i < data.nums; i++)
451 buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
452 (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
453 (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
454 (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
455 (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
460 static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
461 struct device_attribute *attr,
464 struct drm_device *ddev = dev_get_drvdata(dev);
465 struct amdgpu_device *adev = drm_to_adev(ddev);
466 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
467 struct pp_states_info data = {0};
468 enum amd_pm_state_type pm = 0;
471 if (amdgpu_in_reset(adev))
473 if (adev->in_suspend && !adev->in_runpm)
476 ret = pm_runtime_get_sync(ddev->dev);
478 pm_runtime_put_autosuspend(ddev->dev);
482 if (pp_funcs->get_current_power_state
483 && pp_funcs->get_pp_num_states) {
484 pm = amdgpu_dpm_get_current_power_state(adev);
485 amdgpu_dpm_get_pp_num_states(adev, &data);
488 pm_runtime_mark_last_busy(ddev->dev);
489 pm_runtime_put_autosuspend(ddev->dev);
491 for (i = 0; i < data.nums; i++) {
492 if (pm == data.states[i])
499 return sysfs_emit(buf, "%d\n", i);
502 static ssize_t amdgpu_get_pp_force_state(struct device *dev,
503 struct device_attribute *attr,
506 struct drm_device *ddev = dev_get_drvdata(dev);
507 struct amdgpu_device *adev = drm_to_adev(ddev);
509 if (amdgpu_in_reset(adev))
511 if (adev->in_suspend && !adev->in_runpm)
514 if (adev->pp_force_state_enabled)
515 return amdgpu_get_pp_cur_state(dev, attr, buf);
517 return sysfs_emit(buf, "\n");
520 static ssize_t amdgpu_set_pp_force_state(struct device *dev,
521 struct device_attribute *attr,
525 struct drm_device *ddev = dev_get_drvdata(dev);
526 struct amdgpu_device *adev = drm_to_adev(ddev);
527 enum amd_pm_state_type state = 0;
531 if (amdgpu_in_reset(adev))
533 if (adev->in_suspend && !adev->in_runpm)
536 if (strlen(buf) == 1)
537 adev->pp_force_state_enabled = false;
538 else if (is_support_sw_smu(adev))
539 adev->pp_force_state_enabled = false;
540 else if (adev->powerplay.pp_funcs->dispatch_tasks &&
541 adev->powerplay.pp_funcs->get_pp_num_states) {
542 struct pp_states_info data;
544 ret = kstrtoul(buf, 0, &idx);
545 if (ret || idx >= ARRAY_SIZE(data.states))
548 idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
550 amdgpu_dpm_get_pp_num_states(adev, &data);
551 state = data.states[idx];
553 ret = pm_runtime_get_sync(ddev->dev);
555 pm_runtime_put_autosuspend(ddev->dev);
559 /* only set user selected power states */
560 if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
561 state != POWER_STATE_TYPE_DEFAULT) {
562 amdgpu_dpm_dispatch_task(adev,
563 AMD_PP_TASK_ENABLE_USER_STATE, &state);
564 adev->pp_force_state_enabled = true;
566 pm_runtime_mark_last_busy(ddev->dev);
567 pm_runtime_put_autosuspend(ddev->dev);
576 * The amdgpu driver provides a sysfs API for uploading new powerplay
577 * tables. The file pp_table is used for this. Reading the file
578 * will dump the current power play table. Writing to the file
579 * will attempt to upload a new powerplay table and re-initialize
580 * powerplay using that new table.
584 static ssize_t amdgpu_get_pp_table(struct device *dev,
585 struct device_attribute *attr,
588 struct drm_device *ddev = dev_get_drvdata(dev);
589 struct amdgpu_device *adev = drm_to_adev(ddev);
593 if (amdgpu_in_reset(adev))
595 if (adev->in_suspend && !adev->in_runpm)
598 ret = pm_runtime_get_sync(ddev->dev);
600 pm_runtime_put_autosuspend(ddev->dev);
604 if (adev->powerplay.pp_funcs->get_pp_table) {
605 size = amdgpu_dpm_get_pp_table(adev, &table);
606 pm_runtime_mark_last_busy(ddev->dev);
607 pm_runtime_put_autosuspend(ddev->dev);
611 pm_runtime_mark_last_busy(ddev->dev);
612 pm_runtime_put_autosuspend(ddev->dev);
616 if (size >= PAGE_SIZE)
617 size = PAGE_SIZE - 1;
619 memcpy(buf, table, size);
624 static ssize_t amdgpu_set_pp_table(struct device *dev,
625 struct device_attribute *attr,
629 struct drm_device *ddev = dev_get_drvdata(dev);
630 struct amdgpu_device *adev = drm_to_adev(ddev);
633 if (amdgpu_in_reset(adev))
635 if (adev->in_suspend && !adev->in_runpm)
638 ret = pm_runtime_get_sync(ddev->dev);
640 pm_runtime_put_autosuspend(ddev->dev);
644 ret = amdgpu_dpm_set_pp_table(adev, buf, count);
646 pm_runtime_mark_last_busy(ddev->dev);
647 pm_runtime_put_autosuspend(ddev->dev);
651 pm_runtime_mark_last_busy(ddev->dev);
652 pm_runtime_put_autosuspend(ddev->dev);
658 * DOC: pp_od_clk_voltage
660 * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
661 * in each power level within a power state. The pp_od_clk_voltage is used for
664 * Note that the actual memory controller clock rate are exposed, not
665 * the effective memory clock of the DRAMs. To translate it, use the
668 * Clock conversion (Mhz):
670 * HBM: effective_memory_clock = memory_controller_clock * 1
672 * G5: effective_memory_clock = memory_controller_clock * 1
674 * G6: effective_memory_clock = memory_controller_clock * 2
676 * DRAM data rate (MT/s):
678 * HBM: effective_memory_clock * 2 = data_rate
680 * G5: effective_memory_clock * 4 = data_rate
682 * G6: effective_memory_clock * 8 = data_rate
686 * data_rate * vram_bit_width / 8 = memory_bandwidth
692 * memory_controller_clock = 1750 Mhz
694 * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
696 * data rate = 1750 * 4 = 7000 MT/s
698 * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
702 * memory_controller_clock = 875 Mhz
704 * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
706 * data rate = 1750 * 8 = 14000 MT/s
708 * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
710 * < For Vega10 and previous ASICs >
712 * Reading the file will display:
714 * - a list of engine clock levels and voltages labeled OD_SCLK
716 * - a list of memory clock levels and voltages labeled OD_MCLK
718 * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
720 * To manually adjust these settings, first select manual using
721 * power_dpm_force_performance_level. Enter a new value for each
722 * level by writing a string that contains "s/m level clock voltage" to
723 * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
724 * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
725 * 810 mV. When you have edited all of the states as needed, write
726 * "c" (commit) to the file to commit your changes. If you want to reset to the
727 * default power levels, write "r" (reset) to the file to reset them.
730 * < For Vega20 and newer ASICs >
732 * Reading the file will display:
734 * - minimum and maximum engine clock labeled OD_SCLK
736 * - minimum(not available for Vega20 and Navi1x) and maximum memory
737 * clock labeled OD_MCLK
739 * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
740 * They can be used to calibrate the sclk voltage curve.
742 * - voltage offset(in mV) applied on target voltage calculation.
743 * This is available for Sienna Cichlid, Navy Flounder and Dimgrey
744 * Cavefish. For these ASICs, the target voltage calculation can be
745 * illustrated by "voltage = voltage calculated from v/f curve +
746 * overdrive vddgfx offset"
748 * - a list of valid ranges for sclk, mclk, and voltage curve points
753 * Reading the file will display:
755 * - minimum and maximum engine clock labeled OD_SCLK
757 * - a list of valid ranges for sclk labeled OD_RANGE
761 * Reading the file will display:
763 * - minimum and maximum engine clock labeled OD_SCLK
764 * - minimum and maximum core clocks labeled OD_CCLK
766 * - a list of valid ranges for sclk and cclk labeled OD_RANGE
768 * To manually adjust these settings:
770 * - First select manual using power_dpm_force_performance_level
772 * - For clock frequency setting, enter a new value by writing a
773 * string that contains "s/m index clock" to the file. The index
774 * should be 0 if to set minimum clock. And 1 if to set maximum
775 * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
776 * "m 1 800" will update maximum mclk to be 800Mhz. For core
777 * clocks on VanGogh, the string contains "p core index clock".
778 * E.g., "p 2 0 800" would set the minimum core clock on core
781 * For sclk voltage curve, enter the new values by writing a
782 * string that contains "vc point clock voltage" to the file. The
783 * points are indexed by 0, 1 and 2. E.g., "vc 0 300 600" will
784 * update point1 with clock set as 300Mhz and voltage as
785 * 600mV. "vc 2 1000 1000" will update point3 with clock set
786 * as 1000Mhz and voltage 1000mV.
788 * To update the voltage offset applied for gfxclk/voltage calculation,
789 * enter the new value by writing a string that contains "vo offset".
790 * This is supported by Sienna Cichlid, Navy Flounder and Dimgrey Cavefish.
791 * And the offset can be a positive or negative value.
793 * - When you have edited all of the states as needed, write "c" (commit)
794 * to the file to commit your changes
796 * - If you want to reset to the default power levels, write "r" (reset)
797 * to the file to reset them
801 static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
802 struct device_attribute *attr,
806 struct drm_device *ddev = dev_get_drvdata(dev);
807 struct amdgpu_device *adev = drm_to_adev(ddev);
809 uint32_t parameter_size = 0;
814 const char delimiter[3] = {' ', '\n', '\0'};
817 if (amdgpu_in_reset(adev))
819 if (adev->in_suspend && !adev->in_runpm)
826 type = PP_OD_EDIT_SCLK_VDDC_TABLE;
827 else if (*buf == 'p')
828 type = PP_OD_EDIT_CCLK_VDDC_TABLE;
829 else if (*buf == 'm')
830 type = PP_OD_EDIT_MCLK_VDDC_TABLE;
832 type = PP_OD_RESTORE_DEFAULT_TABLE;
833 else if (*buf == 'c')
834 type = PP_OD_COMMIT_DPM_TABLE;
835 else if (!strncmp(buf, "vc", 2))
836 type = PP_OD_EDIT_VDDC_CURVE;
837 else if (!strncmp(buf, "vo", 2))
838 type = PP_OD_EDIT_VDDGFX_OFFSET;
842 memcpy(buf_cpy, buf, count+1);
846 if ((type == PP_OD_EDIT_VDDC_CURVE) ||
847 (type == PP_OD_EDIT_VDDGFX_OFFSET))
849 while (isspace(*++tmp_str));
851 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
852 if (strlen(sub_str) == 0)
854 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
859 while (isspace(*tmp_str))
863 ret = pm_runtime_get_sync(ddev->dev);
865 pm_runtime_put_autosuspend(ddev->dev);
869 if (adev->powerplay.pp_funcs->set_fine_grain_clk_vol) {
870 ret = amdgpu_dpm_set_fine_grain_clk_vol(adev, type,
874 pm_runtime_mark_last_busy(ddev->dev);
875 pm_runtime_put_autosuspend(ddev->dev);
880 if (adev->powerplay.pp_funcs->odn_edit_dpm_table) {
881 ret = amdgpu_dpm_odn_edit_dpm_table(adev, type,
882 parameter, parameter_size);
884 pm_runtime_mark_last_busy(ddev->dev);
885 pm_runtime_put_autosuspend(ddev->dev);
890 if (type == PP_OD_COMMIT_DPM_TABLE) {
891 if (adev->powerplay.pp_funcs->dispatch_tasks) {
892 amdgpu_dpm_dispatch_task(adev,
893 AMD_PP_TASK_READJUST_POWER_STATE,
895 pm_runtime_mark_last_busy(ddev->dev);
896 pm_runtime_put_autosuspend(ddev->dev);
899 pm_runtime_mark_last_busy(ddev->dev);
900 pm_runtime_put_autosuspend(ddev->dev);
905 pm_runtime_mark_last_busy(ddev->dev);
906 pm_runtime_put_autosuspend(ddev->dev);
911 static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
912 struct device_attribute *attr,
915 struct drm_device *ddev = dev_get_drvdata(dev);
916 struct amdgpu_device *adev = drm_to_adev(ddev);
920 if (amdgpu_in_reset(adev))
922 if (adev->in_suspend && !adev->in_runpm)
925 ret = pm_runtime_get_sync(ddev->dev);
927 pm_runtime_put_autosuspend(ddev->dev);
931 if (adev->powerplay.pp_funcs->print_clock_levels) {
932 size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
933 size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf+size);
934 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf+size);
935 size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf+size);
936 size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf+size);
937 size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf+size);
939 size = sysfs_emit(buf, "\n");
941 pm_runtime_mark_last_busy(ddev->dev);
942 pm_runtime_put_autosuspend(ddev->dev);
950 * The amdgpu driver provides a sysfs API for adjusting what powerplay
951 * features to be enabled. The file pp_features is used for this. And
952 * this is only available for Vega10 and later dGPUs.
954 * Reading back the file will show you the followings:
955 * - Current ppfeature masks
956 * - List of the all supported powerplay features with their naming,
957 * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
959 * To manually enable or disable a specific feature, just set or clear
960 * the corresponding bit from original ppfeature masks and input the
961 * new ppfeature masks.
963 static ssize_t amdgpu_set_pp_features(struct device *dev,
964 struct device_attribute *attr,
968 struct drm_device *ddev = dev_get_drvdata(dev);
969 struct amdgpu_device *adev = drm_to_adev(ddev);
970 uint64_t featuremask;
973 if (amdgpu_in_reset(adev))
975 if (adev->in_suspend && !adev->in_runpm)
978 ret = kstrtou64(buf, 0, &featuremask);
982 ret = pm_runtime_get_sync(ddev->dev);
984 pm_runtime_put_autosuspend(ddev->dev);
988 if (adev->powerplay.pp_funcs->set_ppfeature_status) {
989 ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
991 pm_runtime_mark_last_busy(ddev->dev);
992 pm_runtime_put_autosuspend(ddev->dev);
996 pm_runtime_mark_last_busy(ddev->dev);
997 pm_runtime_put_autosuspend(ddev->dev);
1002 static ssize_t amdgpu_get_pp_features(struct device *dev,
1003 struct device_attribute *attr,
1006 struct drm_device *ddev = dev_get_drvdata(dev);
1007 struct amdgpu_device *adev = drm_to_adev(ddev);
1011 if (amdgpu_in_reset(adev))
1013 if (adev->in_suspend && !adev->in_runpm)
1016 ret = pm_runtime_get_sync(ddev->dev);
1018 pm_runtime_put_autosuspend(ddev->dev);
1022 if (adev->powerplay.pp_funcs->get_ppfeature_status)
1023 size = amdgpu_dpm_get_ppfeature_status(adev, buf);
1025 size = sysfs_emit(buf, "\n");
1027 pm_runtime_mark_last_busy(ddev->dev);
1028 pm_runtime_put_autosuspend(ddev->dev);
1034 * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
1036 * The amdgpu driver provides a sysfs API for adjusting what power levels
1037 * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
1038 * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
1041 * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
1042 * Vega10 and later ASICs.
1043 * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
1045 * Reading back the files will show you the available power levels within
1046 * the power state and the clock information for those levels.
1048 * To manually adjust these states, first select manual using
1049 * power_dpm_force_performance_level.
1050 * Secondly, enter a new value for each level by inputing a string that
1051 * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
1054 * .. code-block:: bash
1056 * echo "4 5 6" > pp_dpm_sclk
1058 * will enable sclk levels 4, 5, and 6.
1060 * NOTE: change to the dcefclk max dpm level is not supported now
1063 static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
1064 enum pp_clock_type type,
1067 struct drm_device *ddev = dev_get_drvdata(dev);
1068 struct amdgpu_device *adev = drm_to_adev(ddev);
1072 if (amdgpu_in_reset(adev))
1074 if (adev->in_suspend && !adev->in_runpm)
1077 ret = pm_runtime_get_sync(ddev->dev);
1079 pm_runtime_put_autosuspend(ddev->dev);
1083 if (adev->powerplay.pp_funcs->print_clock_levels)
1084 size = amdgpu_dpm_print_clock_levels(adev, type, buf);
1086 size = sysfs_emit(buf, "\n");
1088 pm_runtime_mark_last_busy(ddev->dev);
1089 pm_runtime_put_autosuspend(ddev->dev);
1095 * Worst case: 32 bits individually specified, in octal at 12 characters
1096 * per line (+1 for \n).
1098 #define AMDGPU_MASK_BUF_MAX (32 * 13)
1100 static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
1103 unsigned long level;
1104 char *sub_str = NULL;
1106 char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
1107 const char delimiter[3] = {' ', '\n', '\0'};
1112 bytes = min(count, sizeof(buf_cpy) - 1);
1113 memcpy(buf_cpy, buf, bytes);
1114 buf_cpy[bytes] = '\0';
1116 while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
1117 if (strlen(sub_str)) {
1118 ret = kstrtoul(sub_str, 0, &level);
1119 if (ret || level > 31)
1121 *mask |= 1 << level;
1129 static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
1130 enum pp_clock_type type,
1134 struct drm_device *ddev = dev_get_drvdata(dev);
1135 struct amdgpu_device *adev = drm_to_adev(ddev);
1139 if (amdgpu_in_reset(adev))
1141 if (adev->in_suspend && !adev->in_runpm)
1144 ret = amdgpu_read_mask(buf, count, &mask);
1148 ret = pm_runtime_get_sync(ddev->dev);
1150 pm_runtime_put_autosuspend(ddev->dev);
1154 if (adev->powerplay.pp_funcs->force_clock_level)
1155 ret = amdgpu_dpm_force_clock_level(adev, type, mask);
1159 pm_runtime_mark_last_busy(ddev->dev);
1160 pm_runtime_put_autosuspend(ddev->dev);
1168 static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
1169 struct device_attribute *attr,
1172 return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
1175 static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
1176 struct device_attribute *attr,
1180 return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
1183 static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
1184 struct device_attribute *attr,
1187 return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
1190 static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
1191 struct device_attribute *attr,
1195 return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
1198 static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
1199 struct device_attribute *attr,
1202 return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
1205 static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
1206 struct device_attribute *attr,
1210 return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
1213 static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
1214 struct device_attribute *attr,
1217 return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
1220 static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
1221 struct device_attribute *attr,
1225 return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
1228 static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
1229 struct device_attribute *attr,
1232 return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
1235 static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
1236 struct device_attribute *attr,
1240 return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
1243 static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
1244 struct device_attribute *attr,
1247 return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
1250 static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
1251 struct device_attribute *attr,
1255 return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
1258 static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
1259 struct device_attribute *attr,
1262 return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
1265 static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
1266 struct device_attribute *attr,
1270 return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
1273 static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
1274 struct device_attribute *attr,
1277 return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
1280 static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
1281 struct device_attribute *attr,
1285 return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
1288 static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
1289 struct device_attribute *attr,
1292 struct drm_device *ddev = dev_get_drvdata(dev);
1293 struct amdgpu_device *adev = drm_to_adev(ddev);
1297 if (amdgpu_in_reset(adev))
1299 if (adev->in_suspend && !adev->in_runpm)
1302 ret = pm_runtime_get_sync(ddev->dev);
1304 pm_runtime_put_autosuspend(ddev->dev);
1308 if (is_support_sw_smu(adev))
1310 else if (adev->powerplay.pp_funcs->get_sclk_od)
1311 value = amdgpu_dpm_get_sclk_od(adev);
1313 pm_runtime_mark_last_busy(ddev->dev);
1314 pm_runtime_put_autosuspend(ddev->dev);
1316 return sysfs_emit(buf, "%d\n", value);
1319 static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
1320 struct device_attribute *attr,
1324 struct drm_device *ddev = dev_get_drvdata(dev);
1325 struct amdgpu_device *adev = drm_to_adev(ddev);
1329 if (amdgpu_in_reset(adev))
1331 if (adev->in_suspend && !adev->in_runpm)
1334 ret = kstrtol(buf, 0, &value);
1339 ret = pm_runtime_get_sync(ddev->dev);
1341 pm_runtime_put_autosuspend(ddev->dev);
1345 if (is_support_sw_smu(adev)) {
1348 if (adev->powerplay.pp_funcs->set_sclk_od)
1349 amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
1351 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1352 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1354 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1355 amdgpu_pm_compute_clocks(adev);
1359 pm_runtime_mark_last_busy(ddev->dev);
1360 pm_runtime_put_autosuspend(ddev->dev);
1365 static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
1366 struct device_attribute *attr,
1369 struct drm_device *ddev = dev_get_drvdata(dev);
1370 struct amdgpu_device *adev = drm_to_adev(ddev);
1374 if (amdgpu_in_reset(adev))
1376 if (adev->in_suspend && !adev->in_runpm)
1379 ret = pm_runtime_get_sync(ddev->dev);
1381 pm_runtime_put_autosuspend(ddev->dev);
1385 if (is_support_sw_smu(adev))
1387 else if (adev->powerplay.pp_funcs->get_mclk_od)
1388 value = amdgpu_dpm_get_mclk_od(adev);
1390 pm_runtime_mark_last_busy(ddev->dev);
1391 pm_runtime_put_autosuspend(ddev->dev);
1393 return sysfs_emit(buf, "%d\n", value);
1396 static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
1397 struct device_attribute *attr,
1401 struct drm_device *ddev = dev_get_drvdata(dev);
1402 struct amdgpu_device *adev = drm_to_adev(ddev);
1406 if (amdgpu_in_reset(adev))
1408 if (adev->in_suspend && !adev->in_runpm)
1411 ret = kstrtol(buf, 0, &value);
1416 ret = pm_runtime_get_sync(ddev->dev);
1418 pm_runtime_put_autosuspend(ddev->dev);
1422 if (is_support_sw_smu(adev)) {
1425 if (adev->powerplay.pp_funcs->set_mclk_od)
1426 amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
1428 if (adev->powerplay.pp_funcs->dispatch_tasks) {
1429 amdgpu_dpm_dispatch_task(adev, AMD_PP_TASK_READJUST_POWER_STATE, NULL);
1431 adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
1432 amdgpu_pm_compute_clocks(adev);
1436 pm_runtime_mark_last_busy(ddev->dev);
1437 pm_runtime_put_autosuspend(ddev->dev);
1443 * DOC: pp_power_profile_mode
1445 * The amdgpu driver provides a sysfs API for adjusting the heuristics
1446 * related to switching between power levels in a power state. The file
1447 * pp_power_profile_mode is used for this.
1449 * Reading this file outputs a list of all of the predefined power profiles
1450 * and the relevant heuristics settings for that profile.
1452 * To select a profile or create a custom profile, first select manual using
1453 * power_dpm_force_performance_level. Writing the number of a predefined
1454 * profile to pp_power_profile_mode will enable those heuristics. To
1455 * create a custom set of heuristics, write a string of numbers to the file
1456 * starting with the number of the custom profile along with a setting
1457 * for each heuristic parameter. Due to differences across asic families
1458 * the heuristic parameters vary from family to family.
1462 static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
1463 struct device_attribute *attr,
1466 struct drm_device *ddev = dev_get_drvdata(dev);
1467 struct amdgpu_device *adev = drm_to_adev(ddev);
1471 if (amdgpu_in_reset(adev))
1473 if (adev->in_suspend && !adev->in_runpm)
1476 ret = pm_runtime_get_sync(ddev->dev);
1478 pm_runtime_put_autosuspend(ddev->dev);
1482 if (adev->powerplay.pp_funcs->get_power_profile_mode)
1483 size = amdgpu_dpm_get_power_profile_mode(adev, buf);
1485 size = sysfs_emit(buf, "\n");
1487 pm_runtime_mark_last_busy(ddev->dev);
1488 pm_runtime_put_autosuspend(ddev->dev);
1494 static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
1495 struct device_attribute *attr,
1500 struct drm_device *ddev = dev_get_drvdata(dev);
1501 struct amdgpu_device *adev = drm_to_adev(ddev);
1502 uint32_t parameter_size = 0;
1504 char *sub_str, buf_cpy[128];
1508 long int profile_mode = 0;
1509 const char delimiter[3] = {' ', '\n', '\0'};
1511 if (amdgpu_in_reset(adev))
1513 if (adev->in_suspend && !adev->in_runpm)
1518 ret = kstrtol(tmp, 0, &profile_mode);
1522 if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
1523 if (count < 2 || count > 127)
1525 while (isspace(*++buf))
1527 memcpy(buf_cpy, buf, count-i);
1529 while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
1530 if (strlen(sub_str) == 0)
1532 ret = kstrtol(sub_str, 0, ¶meter[parameter_size]);
1536 while (isspace(*tmp_str))
1540 parameter[parameter_size] = profile_mode;
1542 ret = pm_runtime_get_sync(ddev->dev);
1544 pm_runtime_put_autosuspend(ddev->dev);
1548 if (adev->powerplay.pp_funcs->set_power_profile_mode)
1549 ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
1551 pm_runtime_mark_last_busy(ddev->dev);
1552 pm_runtime_put_autosuspend(ddev->dev);
1561 * DOC: gpu_busy_percent
1563 * The amdgpu driver provides a sysfs API for reading how busy the GPU
1564 * is as a percentage. The file gpu_busy_percent is used for this.
1565 * The SMU firmware computes a percentage of load based on the
1566 * aggregate activity level in the IP cores.
1568 static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
1569 struct device_attribute *attr,
1572 struct drm_device *ddev = dev_get_drvdata(dev);
1573 struct amdgpu_device *adev = drm_to_adev(ddev);
1574 int r, value, size = sizeof(value);
1576 if (amdgpu_in_reset(adev))
1578 if (adev->in_suspend && !adev->in_runpm)
1581 r = pm_runtime_get_sync(ddev->dev);
1583 pm_runtime_put_autosuspend(ddev->dev);
1587 /* read the IP busy sensor */
1588 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD,
1589 (void *)&value, &size);
1591 pm_runtime_mark_last_busy(ddev->dev);
1592 pm_runtime_put_autosuspend(ddev->dev);
1597 return sysfs_emit(buf, "%d\n", value);
1601 * DOC: mem_busy_percent
1603 * The amdgpu driver provides a sysfs API for reading how busy the VRAM
1604 * is as a percentage. The file mem_busy_percent is used for this.
1605 * The SMU firmware computes a percentage of load based on the
1606 * aggregate activity level in the IP cores.
1608 static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
1609 struct device_attribute *attr,
1612 struct drm_device *ddev = dev_get_drvdata(dev);
1613 struct amdgpu_device *adev = drm_to_adev(ddev);
1614 int r, value, size = sizeof(value);
1616 if (amdgpu_in_reset(adev))
1618 if (adev->in_suspend && !adev->in_runpm)
1621 r = pm_runtime_get_sync(ddev->dev);
1623 pm_runtime_put_autosuspend(ddev->dev);
1627 /* read the IP busy sensor */
1628 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD,
1629 (void *)&value, &size);
1631 pm_runtime_mark_last_busy(ddev->dev);
1632 pm_runtime_put_autosuspend(ddev->dev);
1637 return sysfs_emit(buf, "%d\n", value);
1643 * The amdgpu driver provides a sysfs API for estimating how much data
1644 * has been received and sent by the GPU in the last second through PCIe.
1645 * The file pcie_bw is used for this.
1646 * The Perf counters count the number of received and sent messages and return
1647 * those values, as well as the maximum payload size of a PCIe packet (mps).
1648 * Note that it is not possible to easily and quickly obtain the size of each
1649 * packet transmitted, so we output the max payload size (mps) to allow for
1650 * quick estimation of the PCIe bandwidth usage
1652 static ssize_t amdgpu_get_pcie_bw(struct device *dev,
1653 struct device_attribute *attr,
1656 struct drm_device *ddev = dev_get_drvdata(dev);
1657 struct amdgpu_device *adev = drm_to_adev(ddev);
1658 uint64_t count0 = 0, count1 = 0;
1661 if (amdgpu_in_reset(adev))
1663 if (adev->in_suspend && !adev->in_runpm)
1666 if (adev->flags & AMD_IS_APU)
1669 if (!adev->asic_funcs->get_pcie_usage)
1672 ret = pm_runtime_get_sync(ddev->dev);
1674 pm_runtime_put_autosuspend(ddev->dev);
1678 amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
1680 pm_runtime_mark_last_busy(ddev->dev);
1681 pm_runtime_put_autosuspend(ddev->dev);
1683 return sysfs_emit(buf, "%llu %llu %i\n",
1684 count0, count1, pcie_get_mps(adev->pdev));
1690 * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
1691 * The file unique_id is used for this.
1692 * This will provide a Unique ID that will persist from machine to machine
1694 * NOTE: This will only work for GFX9 and newer. This file will be absent
1695 * on unsupported ASICs (GFX8 and older)
1697 static ssize_t amdgpu_get_unique_id(struct device *dev,
1698 struct device_attribute *attr,
1701 struct drm_device *ddev = dev_get_drvdata(dev);
1702 struct amdgpu_device *adev = drm_to_adev(ddev);
1704 if (amdgpu_in_reset(adev))
1706 if (adev->in_suspend && !adev->in_runpm)
1709 if (adev->unique_id)
1710 return sysfs_emit(buf, "%016llx\n", adev->unique_id);
1716 * DOC: thermal_throttling_logging
1718 * Thermal throttling pulls down the clock frequency and thus the performance.
1719 * It's an useful mechanism to protect the chip from overheating. Since it
1720 * impacts performance, the user controls whether it is enabled and if so,
1721 * the log frequency.
1723 * Reading back the file shows you the status(enabled or disabled) and
1724 * the interval(in seconds) between each thermal logging.
1726 * Writing an integer to the file, sets a new logging interval, in seconds.
1727 * The value should be between 1 and 3600. If the value is less than 1,
1728 * thermal logging is disabled. Values greater than 3600 are ignored.
1730 static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
1731 struct device_attribute *attr,
1734 struct drm_device *ddev = dev_get_drvdata(dev);
1735 struct amdgpu_device *adev = drm_to_adev(ddev);
1737 return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
1738 adev_to_drm(adev)->unique,
1739 atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
1740 adev->throttling_logging_rs.interval / HZ + 1);
1743 static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
1744 struct device_attribute *attr,
1748 struct drm_device *ddev = dev_get_drvdata(dev);
1749 struct amdgpu_device *adev = drm_to_adev(ddev);
1750 long throttling_logging_interval;
1751 unsigned long flags;
1754 ret = kstrtol(buf, 0, &throttling_logging_interval);
1758 if (throttling_logging_interval > 3600)
1761 if (throttling_logging_interval > 0) {
1762 raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
1764 * Reset the ratelimit timer internals.
1765 * This can effectively restart the timer.
1767 adev->throttling_logging_rs.interval =
1768 (throttling_logging_interval - 1) * HZ;
1769 adev->throttling_logging_rs.begin = 0;
1770 adev->throttling_logging_rs.printed = 0;
1771 adev->throttling_logging_rs.missed = 0;
1772 raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
1774 atomic_set(&adev->throttling_logging_enabled, 1);
1776 atomic_set(&adev->throttling_logging_enabled, 0);
1785 * The amdgpu driver provides a sysfs API for retrieving current gpu
1786 * metrics data. The file gpu_metrics is used for this. Reading the
1787 * file will dump all the current gpu metrics data.
1789 * These data include temperature, frequency, engines utilization,
1790 * power consume, throttler status, fan speed and cpu core statistics(
1791 * available for APU only). That's it will give a snapshot of all sensors
1794 static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
1795 struct device_attribute *attr,
1798 struct drm_device *ddev = dev_get_drvdata(dev);
1799 struct amdgpu_device *adev = drm_to_adev(ddev);
1804 if (amdgpu_in_reset(adev))
1806 if (adev->in_suspend && !adev->in_runpm)
1809 ret = pm_runtime_get_sync(ddev->dev);
1811 pm_runtime_put_autosuspend(ddev->dev);
1815 if (adev->powerplay.pp_funcs->get_gpu_metrics)
1816 size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
1821 if (size >= PAGE_SIZE)
1822 size = PAGE_SIZE - 1;
1824 memcpy(buf, gpu_metrics, size);
1827 pm_runtime_mark_last_busy(ddev->dev);
1828 pm_runtime_put_autosuspend(ddev->dev);
1834 * DOC: smartshift_apu_power
1836 * The amdgpu driver provides a sysfs API for reporting APU power
1837 * share if it supports smartshift. The value is expressed as
1838 * the proportion of stapm limit where stapm limit is the total APU
1839 * power limit. The result is in percentage. If APU power is 130% of
1840 * STAPM, then APU is using 30% of the dGPU's headroom.
1843 static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
1846 struct drm_device *ddev = dev_get_drvdata(dev);
1847 struct amdgpu_device *adev = drm_to_adev(ddev);
1848 uint32_t ss_power, size;
1851 if (amdgpu_in_reset(adev))
1853 if (adev->in_suspend && !adev->in_runpm)
1856 r = pm_runtime_get_sync(ddev->dev);
1858 pm_runtime_put_autosuspend(ddev->dev);
1862 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1863 (void *)&ss_power, &size);
1867 r = sysfs_emit(buf, "%u%%\n", ss_power);
1870 pm_runtime_mark_last_busy(ddev->dev);
1871 pm_runtime_put_autosuspend(ddev->dev);
1876 * DOC: smartshift_dgpu_power
1878 * The amdgpu driver provides a sysfs API for reporting the dGPU power
1879 * share if the device is in HG and supports smartshift. The value
1880 * is expressed as the proportion of stapm limit where stapm limit
1881 * is the total APU power limit. The value is in percentage. If dGPU
1882 * power is 20% higher than STAPM power(120%), it's using 20% of the
1883 * APU's power headroom.
1886 static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
1889 struct drm_device *ddev = dev_get_drvdata(dev);
1890 struct amdgpu_device *adev = drm_to_adev(ddev);
1891 uint32_t ss_power, size;
1894 if (amdgpu_in_reset(adev))
1896 if (adev->in_suspend && !adev->in_runpm)
1899 r = pm_runtime_get_sync(ddev->dev);
1901 pm_runtime_put_autosuspend(ddev->dev);
1905 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1906 (void *)&ss_power, &size);
1911 r = sysfs_emit(buf, "%u%%\n", ss_power);
1914 pm_runtime_mark_last_busy(ddev->dev);
1915 pm_runtime_put_autosuspend(ddev->dev);
1920 * DOC: smartshift_bias
1922 * The amdgpu driver provides a sysfs API for reporting the
1923 * smartshift(SS2.0) bias level. The value ranges from -100 to 100
1924 * and the default is 0. -100 sets maximum preference to APU
1925 * and 100 sets max perference to dGPU.
1928 static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
1929 struct device_attribute *attr,
1934 r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
1939 static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
1940 struct device_attribute *attr,
1941 const char *buf, size_t count)
1943 struct drm_device *ddev = dev_get_drvdata(dev);
1944 struct amdgpu_device *adev = drm_to_adev(ddev);
1948 if (amdgpu_in_reset(adev))
1950 if (adev->in_suspend && !adev->in_runpm)
1953 r = pm_runtime_get_sync(ddev->dev);
1955 pm_runtime_put_autosuspend(ddev->dev);
1959 r = kstrtoint(buf, 10, &bias);
1963 if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
1964 bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
1965 else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
1966 bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
1968 amdgpu_smartshift_bias = bias;
1971 /* TODO: upadte bias level with SMU message */
1974 pm_runtime_mark_last_busy(ddev->dev);
1975 pm_runtime_put_autosuspend(ddev->dev);
1980 static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
1981 uint32_t mask, enum amdgpu_device_attr_states *states)
1983 uint32_t ss_power, size;
1985 if (!amdgpu_acpi_is_power_shift_control_supported())
1986 *states = ATTR_STATE_UNSUPPORTED;
1987 else if ((adev->flags & AMD_IS_PX) &&
1988 !amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
1989 *states = ATTR_STATE_UNSUPPORTED;
1990 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
1991 (void *)&ss_power, &size))
1992 *states = ATTR_STATE_UNSUPPORTED;
1993 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
1994 (void *)&ss_power, &size))
1995 *states = ATTR_STATE_UNSUPPORTED;
2000 static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2001 uint32_t mask, enum amdgpu_device_attr_states *states)
2003 uint32_t ss_power, size;
2005 if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
2006 *states = ATTR_STATE_UNSUPPORTED;
2007 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
2008 (void *)&ss_power, &size))
2009 *states = ATTR_STATE_UNSUPPORTED;
2010 else if (amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
2011 (void *)&ss_power, &size))
2012 *states = ATTR_STATE_UNSUPPORTED;
2017 static struct amdgpu_device_attr amdgpu_device_attrs[] = {
2018 AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2019 AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2020 AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2021 AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2022 AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2023 AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2024 AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2025 AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2026 AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2027 AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2028 AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2029 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2030 AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC),
2031 AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC),
2032 AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC),
2033 AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC),
2034 AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2035 AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC),
2036 AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2037 AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2038 AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC),
2039 AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2040 AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2041 AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2042 AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
2043 AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC,
2044 .attr_update = ss_power_attr_update),
2045 AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC,
2046 .attr_update = ss_power_attr_update),
2047 AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC,
2048 .attr_update = ss_bias_attr_update),
2051 static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2052 uint32_t mask, enum amdgpu_device_attr_states *states)
2054 struct device_attribute *dev_attr = &attr->dev_attr;
2055 const char *attr_name = dev_attr->attr.name;
2056 struct pp_hwmgr *hwmgr = adev->powerplay.pp_handle;
2057 enum amd_asic_type asic_type = adev->asic_type;
2059 if (!(attr->flags & mask)) {
2060 *states = ATTR_STATE_UNSUPPORTED;
2064 #define DEVICE_ATTR_IS(_name) (!strcmp(attr_name, #_name))
2066 if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
2067 if (asic_type < CHIP_VEGA10)
2068 *states = ATTR_STATE_UNSUPPORTED;
2069 } else if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2070 if (asic_type < CHIP_VEGA10 ||
2071 asic_type == CHIP_ARCTURUS ||
2072 asic_type == CHIP_ALDEBARAN)
2073 *states = ATTR_STATE_UNSUPPORTED;
2074 } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
2075 if (asic_type < CHIP_VEGA20)
2076 *states = ATTR_STATE_UNSUPPORTED;
2077 } else if (DEVICE_ATTR_IS(pp_od_clk_voltage)) {
2078 *states = ATTR_STATE_UNSUPPORTED;
2079 if ((is_support_sw_smu(adev) && adev->smu.od_enabled) ||
2080 (is_support_sw_smu(adev) && adev->smu.is_apu) ||
2081 (!is_support_sw_smu(adev) && hwmgr->od_enabled))
2082 *states = ATTR_STATE_SUPPORTED;
2083 } else if (DEVICE_ATTR_IS(mem_busy_percent)) {
2084 if (adev->flags & AMD_IS_APU || asic_type == CHIP_VEGA10)
2085 *states = ATTR_STATE_UNSUPPORTED;
2086 } else if (DEVICE_ATTR_IS(pcie_bw)) {
2087 /* PCIe Perf counters won't work on APU nodes */
2088 if (adev->flags & AMD_IS_APU)
2089 *states = ATTR_STATE_UNSUPPORTED;
2090 } else if (DEVICE_ATTR_IS(unique_id)) {
2091 if (asic_type != CHIP_VEGA10 &&
2092 asic_type != CHIP_VEGA20 &&
2093 asic_type != CHIP_ARCTURUS &&
2094 asic_type != CHIP_ALDEBARAN)
2095 *states = ATTR_STATE_UNSUPPORTED;
2096 } else if (DEVICE_ATTR_IS(pp_features)) {
2097 if (adev->flags & AMD_IS_APU || asic_type < CHIP_VEGA10)
2098 *states = ATTR_STATE_UNSUPPORTED;
2099 } else if (DEVICE_ATTR_IS(gpu_metrics)) {
2100 if (asic_type < CHIP_VEGA12)
2101 *states = ATTR_STATE_UNSUPPORTED;
2102 } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
2103 if (!(asic_type == CHIP_VANGOGH || asic_type == CHIP_SIENNA_CICHLID))
2104 *states = ATTR_STATE_UNSUPPORTED;
2105 } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
2106 if (!(asic_type == CHIP_VANGOGH || asic_type == CHIP_SIENNA_CICHLID))
2107 *states = ATTR_STATE_UNSUPPORTED;
2108 } else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
2109 if (!adev->powerplay.pp_funcs->get_power_profile_mode ||
2110 amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
2111 *states = ATTR_STATE_UNSUPPORTED;
2114 switch (asic_type) {
2116 case CHIP_ALDEBARAN:
2117 /* the Mi series card does not support standalone mclk/socclk/fclk level setting */
2118 if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
2119 DEVICE_ATTR_IS(pp_dpm_socclk) ||
2120 DEVICE_ATTR_IS(pp_dpm_fclk)) {
2121 dev_attr->attr.mode &= ~S_IWUGO;
2122 dev_attr->store = NULL;
2129 if (DEVICE_ATTR_IS(pp_dpm_dcefclk)) {
2130 /* SMU MP1 does not support dcefclk level setting */
2131 if (asic_type >= CHIP_NAVI10) {
2132 dev_attr->attr.mode &= ~S_IWUGO;
2133 dev_attr->store = NULL;
2137 /* setting should not be allowed from VF */
2138 if (amdgpu_sriov_vf(adev)) {
2139 dev_attr->attr.mode &= ~S_IWUGO;
2140 dev_attr->store = NULL;
2143 #undef DEVICE_ATTR_IS
2149 static int amdgpu_device_attr_create(struct amdgpu_device *adev,
2150 struct amdgpu_device_attr *attr,
2151 uint32_t mask, struct list_head *attr_list)
2154 struct device_attribute *dev_attr = &attr->dev_attr;
2155 const char *name = dev_attr->attr.name;
2156 enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
2157 struct amdgpu_device_attr_entry *attr_entry;
2159 int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
2160 uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
2164 attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
2166 ret = attr_update(adev, attr, mask, &attr_states);
2168 dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
2173 if (attr_states == ATTR_STATE_UNSUPPORTED)
2176 ret = device_create_file(adev->dev, dev_attr);
2178 dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
2182 attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
2186 attr_entry->attr = attr;
2187 INIT_LIST_HEAD(&attr_entry->entry);
2189 list_add_tail(&attr_entry->entry, attr_list);
2194 static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
2196 struct device_attribute *dev_attr = &attr->dev_attr;
2198 device_remove_file(adev->dev, dev_attr);
2201 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2202 struct list_head *attr_list);
2204 static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
2205 struct amdgpu_device_attr *attrs,
2208 struct list_head *attr_list)
2213 for (i = 0; i < counts; i++) {
2214 ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
2222 amdgpu_device_attr_remove_groups(adev, attr_list);
2227 static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
2228 struct list_head *attr_list)
2230 struct amdgpu_device_attr_entry *entry, *entry_tmp;
2232 if (list_empty(attr_list))
2235 list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
2236 amdgpu_device_attr_remove(adev, entry->attr);
2237 list_del(&entry->entry);
2242 static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
2243 struct device_attribute *attr,
2246 struct amdgpu_device *adev = dev_get_drvdata(dev);
2247 int channel = to_sensor_dev_attr(attr)->index;
2248 int r, temp = 0, size = sizeof(temp);
2250 if (amdgpu_in_reset(adev))
2252 if (adev->in_suspend && !adev->in_runpm)
2255 if (channel >= PP_TEMP_MAX)
2258 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2260 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2265 case PP_TEMP_JUNCTION:
2266 /* get current junction temperature */
2267 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
2268 (void *)&temp, &size);
2271 /* get current edge temperature */
2272 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
2273 (void *)&temp, &size);
2276 /* get current memory temperature */
2277 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
2278 (void *)&temp, &size);
2285 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2286 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2291 return sysfs_emit(buf, "%d\n", temp);
2294 static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
2295 struct device_attribute *attr,
2298 struct amdgpu_device *adev = dev_get_drvdata(dev);
2299 int hyst = to_sensor_dev_attr(attr)->index;
2303 temp = adev->pm.dpm.thermal.min_temp;
2305 temp = adev->pm.dpm.thermal.max_temp;
2307 return sysfs_emit(buf, "%d\n", temp);
2310 static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
2311 struct device_attribute *attr,
2314 struct amdgpu_device *adev = dev_get_drvdata(dev);
2315 int hyst = to_sensor_dev_attr(attr)->index;
2319 temp = adev->pm.dpm.thermal.min_hotspot_temp;
2321 temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
2323 return sysfs_emit(buf, "%d\n", temp);
2326 static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
2327 struct device_attribute *attr,
2330 struct amdgpu_device *adev = dev_get_drvdata(dev);
2331 int hyst = to_sensor_dev_attr(attr)->index;
2335 temp = adev->pm.dpm.thermal.min_mem_temp;
2337 temp = adev->pm.dpm.thermal.max_mem_crit_temp;
2339 return sysfs_emit(buf, "%d\n", temp);
2342 static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
2343 struct device_attribute *attr,
2346 int channel = to_sensor_dev_attr(attr)->index;
2348 if (channel >= PP_TEMP_MAX)
2351 return sysfs_emit(buf, "%s\n", temp_label[channel].label);
2354 static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
2355 struct device_attribute *attr,
2358 struct amdgpu_device *adev = dev_get_drvdata(dev);
2359 int channel = to_sensor_dev_attr(attr)->index;
2362 if (channel >= PP_TEMP_MAX)
2366 case PP_TEMP_JUNCTION:
2367 temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
2370 temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
2373 temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
2377 return sysfs_emit(buf, "%d\n", temp);
2380 static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
2381 struct device_attribute *attr,
2384 struct amdgpu_device *adev = dev_get_drvdata(dev);
2388 if (amdgpu_in_reset(adev))
2390 if (adev->in_suspend && !adev->in_runpm)
2393 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2395 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2399 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2400 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2401 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2405 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2407 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2408 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2410 return sysfs_emit(buf, "%u\n", pwm_mode);
2413 static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
2414 struct device_attribute *attr,
2418 struct amdgpu_device *adev = dev_get_drvdata(dev);
2422 if (amdgpu_in_reset(adev))
2424 if (adev->in_suspend && !adev->in_runpm)
2427 err = kstrtoint(buf, 10, &value);
2431 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2433 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2437 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2438 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2439 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2443 amdgpu_dpm_set_fan_control_mode(adev, value);
2445 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2446 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2451 static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
2452 struct device_attribute *attr,
2455 return sysfs_emit(buf, "%i\n", 0);
2458 static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
2459 struct device_attribute *attr,
2462 return sysfs_emit(buf, "%i\n", 255);
2465 static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
2466 struct device_attribute *attr,
2467 const char *buf, size_t count)
2469 struct amdgpu_device *adev = dev_get_drvdata(dev);
2474 if (amdgpu_in_reset(adev))
2476 if (adev->in_suspend && !adev->in_runpm)
2479 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2481 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2485 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2486 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2487 pr_info("manual fan speed control should be enabled first\n");
2488 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2489 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2493 err = kstrtou32(buf, 10, &value);
2495 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2496 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2500 if (adev->powerplay.pp_funcs->set_fan_speed_pwm)
2501 err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
2505 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2506 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2514 static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
2515 struct device_attribute *attr,
2518 struct amdgpu_device *adev = dev_get_drvdata(dev);
2522 if (amdgpu_in_reset(adev))
2524 if (adev->in_suspend && !adev->in_runpm)
2527 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2529 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2533 if (adev->powerplay.pp_funcs->get_fan_speed_pwm)
2534 err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
2538 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2539 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2544 return sysfs_emit(buf, "%i\n", speed);
2547 static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
2548 struct device_attribute *attr,
2551 struct amdgpu_device *adev = dev_get_drvdata(dev);
2555 if (amdgpu_in_reset(adev))
2557 if (adev->in_suspend && !adev->in_runpm)
2560 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2562 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2566 if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2567 err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
2571 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2572 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2577 return sysfs_emit(buf, "%i\n", speed);
2580 static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
2581 struct device_attribute *attr,
2584 struct amdgpu_device *adev = dev_get_drvdata(dev);
2586 u32 size = sizeof(min_rpm);
2589 if (amdgpu_in_reset(adev))
2591 if (adev->in_suspend && !adev->in_runpm)
2594 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2596 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2600 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
2601 (void *)&min_rpm, &size);
2603 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2604 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2609 return sysfs_emit(buf, "%d\n", min_rpm);
2612 static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
2613 struct device_attribute *attr,
2616 struct amdgpu_device *adev = dev_get_drvdata(dev);
2618 u32 size = sizeof(max_rpm);
2621 if (amdgpu_in_reset(adev))
2623 if (adev->in_suspend && !adev->in_runpm)
2626 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2628 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2632 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
2633 (void *)&max_rpm, &size);
2635 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2636 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2641 return sysfs_emit(buf, "%d\n", max_rpm);
2644 static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
2645 struct device_attribute *attr,
2648 struct amdgpu_device *adev = dev_get_drvdata(dev);
2652 if (amdgpu_in_reset(adev))
2654 if (adev->in_suspend && !adev->in_runpm)
2657 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2659 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2663 if (adev->powerplay.pp_funcs->get_fan_speed_rpm)
2664 err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
2668 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2669 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2674 return sysfs_emit(buf, "%i\n", rpm);
2677 static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
2678 struct device_attribute *attr,
2679 const char *buf, size_t count)
2681 struct amdgpu_device *adev = dev_get_drvdata(dev);
2686 if (amdgpu_in_reset(adev))
2688 if (adev->in_suspend && !adev->in_runpm)
2691 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2693 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2697 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2699 if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
2700 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2701 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2705 err = kstrtou32(buf, 10, &value);
2707 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2708 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2712 if (adev->powerplay.pp_funcs->set_fan_speed_rpm)
2713 err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
2717 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2718 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2726 static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
2727 struct device_attribute *attr,
2730 struct amdgpu_device *adev = dev_get_drvdata(dev);
2734 if (amdgpu_in_reset(adev))
2736 if (adev->in_suspend && !adev->in_runpm)
2739 ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2741 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2745 if (!adev->powerplay.pp_funcs->get_fan_control_mode) {
2746 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2747 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2751 pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
2753 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2754 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2756 return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
2759 static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
2760 struct device_attribute *attr,
2764 struct amdgpu_device *adev = dev_get_drvdata(dev);
2769 if (amdgpu_in_reset(adev))
2771 if (adev->in_suspend && !adev->in_runpm)
2774 err = kstrtoint(buf, 10, &value);
2779 pwm_mode = AMD_FAN_CTRL_AUTO;
2780 else if (value == 1)
2781 pwm_mode = AMD_FAN_CTRL_MANUAL;
2785 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2787 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2791 if (!adev->powerplay.pp_funcs->set_fan_control_mode) {
2792 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2793 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2796 amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
2798 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2799 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2804 static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
2805 struct device_attribute *attr,
2808 struct amdgpu_device *adev = dev_get_drvdata(dev);
2810 int r, size = sizeof(vddgfx);
2812 if (amdgpu_in_reset(adev))
2814 if (adev->in_suspend && !adev->in_runpm)
2817 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2819 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2823 /* get the voltage */
2824 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX,
2825 (void *)&vddgfx, &size);
2827 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2828 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2833 return sysfs_emit(buf, "%d\n", vddgfx);
2836 static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
2837 struct device_attribute *attr,
2840 return sysfs_emit(buf, "vddgfx\n");
2843 static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
2844 struct device_attribute *attr,
2847 struct amdgpu_device *adev = dev_get_drvdata(dev);
2849 int r, size = sizeof(vddnb);
2851 if (amdgpu_in_reset(adev))
2853 if (adev->in_suspend && !adev->in_runpm)
2856 /* only APUs have vddnb */
2857 if (!(adev->flags & AMD_IS_APU))
2860 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2862 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2866 /* get the voltage */
2867 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB,
2868 (void *)&vddnb, &size);
2870 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2871 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2876 return sysfs_emit(buf, "%d\n", vddnb);
2879 static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
2880 struct device_attribute *attr,
2883 return sysfs_emit(buf, "vddnb\n");
2886 static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
2887 struct device_attribute *attr,
2890 struct amdgpu_device *adev = dev_get_drvdata(dev);
2892 int r, size = sizeof(u32);
2895 if (amdgpu_in_reset(adev))
2897 if (adev->in_suspend && !adev->in_runpm)
2900 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2902 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2906 /* get the voltage */
2907 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER,
2908 (void *)&query, &size);
2910 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2911 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2916 /* convert to microwatts */
2917 uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
2919 return sysfs_emit(buf, "%u\n", uw);
2922 static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
2923 struct device_attribute *attr,
2926 return sysfs_emit(buf, "%i\n", 0);
2930 static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
2931 struct device_attribute *attr,
2933 enum pp_power_limit_level pp_limit_level)
2935 struct amdgpu_device *adev = dev_get_drvdata(dev);
2936 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
2937 enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
2942 if (amdgpu_in_reset(adev))
2944 if (adev->in_suspend && !adev->in_runpm)
2947 if ( !(pp_funcs && pp_funcs->get_power_limit))
2950 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
2952 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2956 r = pp_funcs->get_power_limit(adev->powerplay.pp_handle, &limit,
2957 pp_limit_level, power_type);
2960 size = sysfs_emit(buf, "%u\n", limit * 1000000);
2962 size = sysfs_emit(buf, "\n");
2964 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
2965 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
2971 static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
2972 struct device_attribute *attr,
2975 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
2979 static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
2980 struct device_attribute *attr,
2983 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
2987 static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
2988 struct device_attribute *attr,
2991 return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
2995 static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
2996 struct device_attribute *attr,
2999 int limit_type = to_sensor_dev_attr(attr)->index;
3001 return sysfs_emit(buf, "%s\n",
3002 limit_type == SMU_FAST_PPT_LIMIT ? "fastPPT" : "slowPPT");
3005 static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
3006 struct device_attribute *attr,
3010 struct amdgpu_device *adev = dev_get_drvdata(dev);
3011 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
3012 int limit_type = to_sensor_dev_attr(attr)->index;
3016 if (amdgpu_in_reset(adev))
3018 if (adev->in_suspend && !adev->in_runpm)
3021 if (amdgpu_sriov_vf(adev))
3024 err = kstrtou32(buf, 10, &value);
3028 value = value / 1000000; /* convert to Watt */
3029 value |= limit_type << 24;
3031 err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3033 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3037 if (pp_funcs && pp_funcs->set_power_limit)
3038 err = pp_funcs->set_power_limit(adev->powerplay.pp_handle, value);
3042 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3043 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3051 static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
3052 struct device_attribute *attr,
3055 struct amdgpu_device *adev = dev_get_drvdata(dev);
3057 int r, size = sizeof(sclk);
3059 if (amdgpu_in_reset(adev))
3061 if (adev->in_suspend && !adev->in_runpm)
3064 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3066 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3071 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
3072 (void *)&sclk, &size);
3074 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3075 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3080 return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
3083 static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
3084 struct device_attribute *attr,
3087 return sysfs_emit(buf, "sclk\n");
3090 static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
3091 struct device_attribute *attr,
3094 struct amdgpu_device *adev = dev_get_drvdata(dev);
3096 int r, size = sizeof(mclk);
3098 if (amdgpu_in_reset(adev))
3100 if (adev->in_suspend && !adev->in_runpm)
3103 r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
3105 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3110 r = amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
3111 (void *)&mclk, &size);
3113 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
3114 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
3119 return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
3122 static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
3123 struct device_attribute *attr,
3126 return sysfs_emit(buf, "mclk\n");
3132 * The amdgpu driver exposes the following sensor interfaces:
3134 * - GPU temperature (via the on-die sensor)
3138 * - Northbridge voltage (APUs only)
3144 * - GPU gfx/compute engine clock
3146 * - GPU memory clock (dGPU only)
3148 * hwmon interfaces for GPU temperature:
3150 * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
3151 * - temp2_input and temp3_input are supported on SOC15 dGPUs only
3153 * - temp[1-3]_label: temperature channel label
3154 * - temp2_label and temp3_label are supported on SOC15 dGPUs only
3156 * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
3157 * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
3159 * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
3160 * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
3162 * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
3163 * - these are supported on SOC15 dGPUs only
3165 * hwmon interfaces for GPU voltage:
3167 * - in0_input: the voltage on the GPU in millivolts
3169 * - in1_input: the voltage on the Northbridge in millivolts
3171 * hwmon interfaces for GPU power:
3173 * - power1_average: average power used by the GPU in microWatts
3175 * - power1_cap_min: minimum cap supported in microWatts
3177 * - power1_cap_max: maximum cap supported in microWatts
3179 * - power1_cap: selected power cap in microWatts
3181 * hwmon interfaces for GPU fan:
3183 * - pwm1: pulse width modulation fan level (0-255)
3185 * - 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)
3187 * - pwm1_min: pulse width modulation fan control minimum level (0)
3189 * - pwm1_max: pulse width modulation fan control maximum level (255)
3191 * - fan1_min: a minimum value Unit: revolution/min (RPM)
3193 * - fan1_max: a maximum value Unit: revolution/max (RPM)
3195 * - fan1_input: fan speed in RPM
3197 * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
3199 * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
3201 * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
3202 * That will get the former one overridden.
3204 * hwmon interfaces for GPU clocks:
3206 * - freq1_input: the gfx/compute clock in hertz
3208 * - freq2_input: the memory clock in hertz
3210 * You can use hwmon tools like sensors to view this information on your system.
3214 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
3215 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
3216 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
3217 static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
3218 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
3219 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
3220 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
3221 static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
3222 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
3223 static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
3224 static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
3225 static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
3226 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
3227 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
3228 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
3229 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
3230 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
3231 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
3232 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
3233 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
3234 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
3235 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
3236 static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
3237 static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
3238 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
3239 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
3240 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
3241 static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
3242 static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
3243 static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
3244 static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
3245 static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
3246 static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
3247 static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
3248 static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
3249 static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
3250 static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
3251 static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
3252 static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
3253 static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
3254 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
3255 static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
3256 static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
3257 static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
3259 static struct attribute *hwmon_attributes[] = {
3260 &sensor_dev_attr_temp1_input.dev_attr.attr,
3261 &sensor_dev_attr_temp1_crit.dev_attr.attr,
3262 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
3263 &sensor_dev_attr_temp2_input.dev_attr.attr,
3264 &sensor_dev_attr_temp2_crit.dev_attr.attr,
3265 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
3266 &sensor_dev_attr_temp3_input.dev_attr.attr,
3267 &sensor_dev_attr_temp3_crit.dev_attr.attr,
3268 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
3269 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
3270 &sensor_dev_attr_temp2_emergency.dev_attr.attr,
3271 &sensor_dev_attr_temp3_emergency.dev_attr.attr,
3272 &sensor_dev_attr_temp1_label.dev_attr.attr,
3273 &sensor_dev_attr_temp2_label.dev_attr.attr,
3274 &sensor_dev_attr_temp3_label.dev_attr.attr,
3275 &sensor_dev_attr_pwm1.dev_attr.attr,
3276 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3277 &sensor_dev_attr_pwm1_min.dev_attr.attr,
3278 &sensor_dev_attr_pwm1_max.dev_attr.attr,
3279 &sensor_dev_attr_fan1_input.dev_attr.attr,
3280 &sensor_dev_attr_fan1_min.dev_attr.attr,
3281 &sensor_dev_attr_fan1_max.dev_attr.attr,
3282 &sensor_dev_attr_fan1_target.dev_attr.attr,
3283 &sensor_dev_attr_fan1_enable.dev_attr.attr,
3284 &sensor_dev_attr_in0_input.dev_attr.attr,
3285 &sensor_dev_attr_in0_label.dev_attr.attr,
3286 &sensor_dev_attr_in1_input.dev_attr.attr,
3287 &sensor_dev_attr_in1_label.dev_attr.attr,
3288 &sensor_dev_attr_power1_average.dev_attr.attr,
3289 &sensor_dev_attr_power1_cap_max.dev_attr.attr,
3290 &sensor_dev_attr_power1_cap_min.dev_attr.attr,
3291 &sensor_dev_attr_power1_cap.dev_attr.attr,
3292 &sensor_dev_attr_power1_cap_default.dev_attr.attr,
3293 &sensor_dev_attr_power1_label.dev_attr.attr,
3294 &sensor_dev_attr_power2_average.dev_attr.attr,
3295 &sensor_dev_attr_power2_cap_max.dev_attr.attr,
3296 &sensor_dev_attr_power2_cap_min.dev_attr.attr,
3297 &sensor_dev_attr_power2_cap.dev_attr.attr,
3298 &sensor_dev_attr_power2_cap_default.dev_attr.attr,
3299 &sensor_dev_attr_power2_label.dev_attr.attr,
3300 &sensor_dev_attr_freq1_input.dev_attr.attr,
3301 &sensor_dev_attr_freq1_label.dev_attr.attr,
3302 &sensor_dev_attr_freq2_input.dev_attr.attr,
3303 &sensor_dev_attr_freq2_label.dev_attr.attr,
3307 static umode_t hwmon_attributes_visible(struct kobject *kobj,
3308 struct attribute *attr, int index)
3310 struct device *dev = kobj_to_dev(kobj);
3311 struct amdgpu_device *adev = dev_get_drvdata(dev);
3312 umode_t effective_mode = attr->mode;
3314 /* under multi-vf mode, the hwmon attributes are all not supported */
3315 if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
3318 /* there is no fan under pp one vf mode */
3319 if (amdgpu_sriov_is_pp_one_vf(adev) &&
3320 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3321 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3322 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3323 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3324 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3325 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3326 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3327 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3328 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3331 /* Skip fan attributes if fan is not present */
3332 if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3333 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3334 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3335 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3336 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3337 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3338 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3339 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3340 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3343 /* Skip fan attributes on APU */
3344 if ((adev->flags & AMD_IS_APU) &&
3345 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3346 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3347 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3348 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3349 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3350 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3351 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3352 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3353 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3356 /* Skip crit temp on APU */
3357 if ((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ) &&
3358 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3359 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
3362 /* Skip limit attributes if DPM is not enabled */
3363 if (!adev->pm.dpm_enabled &&
3364 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
3365 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
3366 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
3367 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
3368 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3369 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
3370 attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
3371 attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
3372 attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3373 attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
3374 attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
3377 if (!is_support_sw_smu(adev)) {
3378 /* mask fan attributes if we have no bindings for this asic to expose */
3379 if ((!adev->powerplay.pp_funcs->get_fan_speed_pwm &&
3380 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
3381 (!adev->powerplay.pp_funcs->get_fan_control_mode &&
3382 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
3383 effective_mode &= ~S_IRUGO;
3385 if ((!adev->powerplay.pp_funcs->set_fan_speed_pwm &&
3386 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
3387 (!adev->powerplay.pp_funcs->set_fan_control_mode &&
3388 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
3389 effective_mode &= ~S_IWUSR;
3392 if (((adev->family == AMDGPU_FAMILY_SI) ||
3393 ((adev->flags & AMD_IS_APU) &&
3394 (adev->asic_type != CHIP_VANGOGH))) && /* not implemented yet */
3395 (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
3396 attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr||
3397 attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
3398 attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr))
3401 if (((adev->family == AMDGPU_FAMILY_SI) ||
3402 ((adev->flags & AMD_IS_APU) &&
3403 (adev->asic_type < CHIP_RENOIR))) && /* not implemented yet */
3404 (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
3407 if (!is_support_sw_smu(adev)) {
3408 /* hide max/min values if we can't both query and manage the fan */
3409 if ((!adev->powerplay.pp_funcs->set_fan_speed_pwm &&
3410 !adev->powerplay.pp_funcs->get_fan_speed_pwm) &&
3411 (!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3412 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3413 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
3414 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
3417 if ((!adev->powerplay.pp_funcs->set_fan_speed_rpm &&
3418 !adev->powerplay.pp_funcs->get_fan_speed_rpm) &&
3419 (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
3420 attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
3424 if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
3425 adev->family == AMDGPU_FAMILY_KV) && /* not implemented yet */
3426 (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
3427 attr == &sensor_dev_attr_in0_label.dev_attr.attr))
3430 /* only APUs have vddnb */
3431 if (!(adev->flags & AMD_IS_APU) &&
3432 (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
3433 attr == &sensor_dev_attr_in1_label.dev_attr.attr))
3436 /* no mclk on APUs */
3437 if ((adev->flags & AMD_IS_APU) &&
3438 (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
3439 attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
3442 /* only SOC15 dGPUs support hotspot and mem temperatures */
3443 if (((adev->flags & AMD_IS_APU) ||
3444 adev->asic_type < CHIP_VEGA10) &&
3445 (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
3446 attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
3447 attr == &sensor_dev_attr_temp3_crit.dev_attr.attr ||
3448 attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
3449 attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
3450 attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
3451 attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr ||
3452 attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
3453 attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
3454 attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
3455 attr == &sensor_dev_attr_temp3_label.dev_attr.attr))
3458 /* only Vangogh has fast PPT limit and power labels */
3459 if (!(adev->asic_type == CHIP_VANGOGH) &&
3460 (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
3461 attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
3462 attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
3463 attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
3464 attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
3465 attr == &sensor_dev_attr_power2_label.dev_attr.attr ||
3466 attr == &sensor_dev_attr_power1_label.dev_attr.attr))
3469 return effective_mode;
3472 static const struct attribute_group hwmon_attrgroup = {
3473 .attrs = hwmon_attributes,
3474 .is_visible = hwmon_attributes_visible,
3477 static const struct attribute_group *hwmon_groups[] = {
3482 int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
3487 if (adev->pm.sysfs_initialized)
3490 if (adev->pm.dpm_enabled == 0)
3493 INIT_LIST_HEAD(&adev->pm.pm_attr_list);
3495 adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
3498 if (IS_ERR(adev->pm.int_hwmon_dev)) {
3499 ret = PTR_ERR(adev->pm.int_hwmon_dev);
3501 "Unable to register hwmon device: %d\n", ret);
3505 switch (amdgpu_virt_get_sriov_vf_mode(adev)) {
3506 case SRIOV_VF_MODE_ONE_VF:
3507 mask = ATTR_FLAG_ONEVF;
3509 case SRIOV_VF_MODE_MULTI_VF:
3512 case SRIOV_VF_MODE_BARE_METAL:
3514 mask = ATTR_FLAG_MASK_ALL;
3518 ret = amdgpu_device_attr_create_groups(adev,
3519 amdgpu_device_attrs,
3520 ARRAY_SIZE(amdgpu_device_attrs),
3522 &adev->pm.pm_attr_list);
3526 adev->pm.sysfs_initialized = true;
3531 void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
3533 if (adev->pm.dpm_enabled == 0)
3536 if (adev->pm.int_hwmon_dev)
3537 hwmon_device_unregister(adev->pm.int_hwmon_dev);
3539 amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
3545 #if defined(CONFIG_DEBUG_FS)
3547 static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
3548 struct amdgpu_device *adev) {
3553 if (is_support_cclk_dpm(adev)) {
3554 p_val = kcalloc(adev->smu.cpu_core_num, sizeof(uint16_t),
3557 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
3558 (void *)p_val, &size)) {
3559 for (i = 0; i < adev->smu.cpu_core_num; i++)
3560 seq_printf(m, "\t%u MHz (CPU%d)\n",
3568 static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
3571 uint64_t value64 = 0;
3576 size = sizeof(value);
3577 seq_printf(m, "GFX Clocks and Power:\n");
3579 amdgpu_debugfs_prints_cpu_info(m, adev);
3581 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
3582 seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
3583 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
3584 seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
3585 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
3586 seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
3587 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
3588 seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
3589 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
3590 seq_printf(m, "\t%u mV (VDDGFX)\n", value);
3591 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
3592 seq_printf(m, "\t%u mV (VDDNB)\n", value);
3593 size = sizeof(uint32_t);
3594 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size))
3595 seq_printf(m, "\t%u.%u W (average GPU)\n", query >> 8, query & 0xff);
3596 size = sizeof(value);
3597 seq_printf(m, "\n");
3600 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
3601 seq_printf(m, "GPU Temperature: %u C\n", value/1000);
3604 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
3605 seq_printf(m, "GPU Load: %u %%\n", value);
3607 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
3608 seq_printf(m, "MEM Load: %u %%\n", value);
3610 seq_printf(m, "\n");
3612 /* SMC feature mask */
3613 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
3614 seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
3616 if (adev->asic_type > CHIP_VEGA20) {
3618 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
3620 seq_printf(m, "VCN: Disabled\n");
3622 seq_printf(m, "VCN: Enabled\n");
3623 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3624 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3625 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3626 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3629 seq_printf(m, "\n");
3632 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
3634 seq_printf(m, "UVD: Disabled\n");
3636 seq_printf(m, "UVD: Enabled\n");
3637 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
3638 seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
3639 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
3640 seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
3643 seq_printf(m, "\n");
3646 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
3648 seq_printf(m, "VCE: Disabled\n");
3650 seq_printf(m, "VCE: Enabled\n");
3651 if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
3652 seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
3660 static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
3664 for (i = 0; clocks[i].flag; i++)
3665 seq_printf(m, "\t%s: %s\n", clocks[i].name,
3666 (flags & clocks[i].flag) ? "On" : "Off");
3669 static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
3671 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
3672 struct drm_device *dev = adev_to_drm(adev);
3676 if (amdgpu_in_reset(adev))
3678 if (adev->in_suspend && !adev->in_runpm)
3681 r = pm_runtime_get_sync(dev->dev);
3683 pm_runtime_put_autosuspend(dev->dev);
3687 if (!adev->pm.dpm_enabled) {
3688 seq_printf(m, "dpm not enabled\n");
3689 pm_runtime_mark_last_busy(dev->dev);
3690 pm_runtime_put_autosuspend(dev->dev);
3694 if (!is_support_sw_smu(adev) &&
3695 adev->powerplay.pp_funcs->debugfs_print_current_performance_level) {
3696 mutex_lock(&adev->pm.mutex);
3697 if (adev->powerplay.pp_funcs->debugfs_print_current_performance_level)
3698 adev->powerplay.pp_funcs->debugfs_print_current_performance_level(adev, m);
3700 seq_printf(m, "Debugfs support not implemented for this asic\n");
3701 mutex_unlock(&adev->pm.mutex);
3704 r = amdgpu_debugfs_pm_info_pp(m, adev);
3709 amdgpu_device_ip_get_clockgating_state(adev, &flags);
3711 seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
3712 amdgpu_parse_cg_state(m, flags);
3713 seq_printf(m, "\n");
3716 pm_runtime_mark_last_busy(dev->dev);
3717 pm_runtime_put_autosuspend(dev->dev);
3722 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
3725 * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
3727 * Reads debug memory region allocated to PMFW
3729 static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
3730 size_t size, loff_t *pos)
3732 struct amdgpu_device *adev = file_inode(f)->i_private;
3733 const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
3734 void *pp_handle = adev->powerplay.pp_handle;
3735 size_t smu_prv_buf_size;
3738 if (amdgpu_in_reset(adev))
3740 if (adev->in_suspend && !adev->in_runpm)
3743 if (pp_funcs && pp_funcs->get_smu_prv_buf_details)
3744 pp_funcs->get_smu_prv_buf_details(pp_handle, &smu_prv_buf,
3749 if (!smu_prv_buf || !smu_prv_buf_size)
3752 return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
3756 static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
3757 .owner = THIS_MODULE,
3758 .open = simple_open,
3759 .read = amdgpu_pm_prv_buffer_read,
3760 .llseek = default_llseek,
3765 void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
3767 #if defined(CONFIG_DEBUG_FS)
3768 struct drm_minor *minor = adev_to_drm(adev)->primary;
3769 struct dentry *root = minor->debugfs_root;
3771 debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
3772 &amdgpu_debugfs_pm_info_fops);
3774 if (adev->pm.smu_prv_buffer_size > 0)
3775 debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
3777 &amdgpu_debugfs_pm_prv_buffer_fops,
3778 adev->pm.smu_prv_buffer_size);
3780 amdgpu_smu_stb_debug_fs_init(adev);