/
45-tlp-func-gpu
177 lines (144 loc) · 5.81 KB
/
45-tlp-func-gpu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/sh
# tlp-func-gpu - Intel GPU Functions
#
# Copyright (c) 2019 Thomas Koch <linrunner at gmx.net> and others.
# This software is licensed under the GPL v2 or later.
# Needs: tlp-func-base
# ----------------------------------------------------------------------------
# Constants
readonly BASE_MODD=/sys/module
readonly BASE_DRMD=/sys/class/drm
readonly BASE_DEBUGD=/sys/kernel/debug/dri
readonly IGPU_MIN_FREQ=gt_min_freq_mhz
readonly IGPU_MAX_FREQ=gt_max_freq_mhz
readonly IGPU_BOOST_FREQ=gt_boost_freq_mhz
readonly IGPU_FREQ_TABLE=i915_ring_freq_table
readonly RADD=/sys/module/radeon
readonly DEFAULT_RADEON_DPM_PERF_LEVEL_ON_AC=auto
readonly DEFAULT_RADEON_DPM_PERF_LEVEL_ON_BAT=auto
# ----------------------------------------------------------------------------
# Functions
# --- Intel GPU
check_intel_gpu () { # detect Intel GPU presence and determine sysdirs
# rc: 0=present/1=absent
# retval: $_intel_gpu_parm: card parameter sysdir;
# $_intel_gpu_drm: card drm sysdir;
# $_intel_gpu_dbg: card debug sysdir
local cardno driver gpu
_intel_gpu_parm=""
_intel_gpu_drm=""
_intel_gpu_dbg=""
for gpu in ${BASE_DRMD}/card?; do
driver=$(readlink ${gpu}/device/driver)
driver=${driver##*/}
case $driver in
i915*) # Intel GPU found
cardno=${gpu##${BASE_DRMD}/card}
_intel_gpu_parm=${BASE_MODD}/${driver}/parameters
_intel_gpu_drm=${gpu}
_intel_gpu_dbg=${BASE_DEBUGD}/${cardno}
echo_debug "pm" "check_intel_gpu.present: parm=$_intel_gpu_parm; drm=$_intel_gpu_drm; dbg=$_intel_gpu_dbg"
return 0
;;
esac
done
# no Intel GPU found
echo_debug "pm" "check_intel_gpu.no_card"
return 1
}
set_intel_gpu_min_max_boost_freq () { # set gpu frequency limits -- $1: 0=ac mode, 1=battery mode
local minfreq maxfreq boostfreq
local conf=0
check_intel_gpu || return 0
if [ "$1" = "1" ]; then
minfreq=${INTEL_GPU_MIN_FREQ_ON_BAT:-}
maxfreq=${INTEL_GPU_MAX_FREQ_ON_BAT:-}
boostfreq=${INTEL_GPU_BOOST_FREQ_ON_BAT:-}
else
minfreq=${INTEL_GPU_MIN_FREQ_ON_AC:-}
maxfreq=${INTEL_GPU_MAX_FREQ_ON_AC:-}
boostfreq=${INTEL_GPU_BOOST_FREQ_ON_AC:-}
fi
if [ -n "$minfreq" ] && [ "$minfreq" != "0" ]; then
if { printf '%s\n' "$minfreq" > $_intel_gpu_drm/$IGPU_MIN_FREQ; } 2> /dev/null; then
echo_debug "pm" "set_intel_gpu_min_max_boost_freq($1).min: $minfreq"
else
echo_debug "pm" "set_intel_gpu_min_max_boost_freq($1).min.write_error"
fi
conf=1
fi
if [ -n "$maxfreq" ] && [ "$maxfreq" != "0" ]; then
if { printf '%s\n' "$maxfreq" > $_intel_gpu_drm/$IGPU_MAX_FREQ; } 2> /dev/null; then
echo_debug "pm" "set_intel_gpu_min_max_boost_freq($1).max: $maxfreq"
else
echo_debug "pm" "set_intel_gpu_min_max_boost_freq($1).max.write_error"
fi
conf=1
fi
if [ -n "$boostfreq" ] && [ "$boostfreq" != "0" ]; then
if { printf '%s\n' "$boostfreq" > $_intel_gpu_drm/$IGPU_BOOST_FREQ; } 2> /dev/null; then
echo_debug "pm" "set_intel_gpu_min_max_boost_freq($1).boost: $boostfreq"
else
echo_debug "pm" "set_intel_gpu_min_max_boost_freq($1).boost.write_error"
fi
conf=1
fi
[ $conf -eq 1 ] || echo_debug "pm" "set_intel_gpu_min_max_boost_freq($1).not_configured"
return 0
}
# --- AMD Radeon GPU
set_radeon_profile () { # set radeon power profile
# $1: 0=ac mode, 1=battery mode
local card level pwr rc1 rc2
local sdone=0 # 1=radeon present
if [ ! -d $RADD ]; then
# No card present --> exit
echo_debug "pm" "set_radeon_profile($1).no_card"
return 0
fi
for card in /sys/class/drm/card[0-9]/device ; do
if [ -f $card/power_dpm_state ] && [ -f $card/power_dpm_force_performance_level ]; then
# Use new radeon dynamic power management method (dpm)
if [ "$1" = "1" ]; then
pwr=${RADEON_DPM_STATE_ON_BAT:-}
level=${RADEON_DPM_PERF_LEVEL_ON_BAT:-${DEFAULT_RADEON_DPM_PERF_LEVEL_ON_BAT}}
else
pwr=${RADEON_DPM_STATE_ON_AC:-}
level=${RADEON_DPM_PERF_LEVEL_ON_AC:-${DEFAULT_RADEON_DPM_PERF_LEVEL_ON_AC}}
fi
if [ -z "$pwr" ]; then
# do nothing if unconfigured
echo_debug "pm" "set_radeon_profile($1).not_configured: $card"
return 0
fi
if [ -n "$pwr" ]; then
{ printf '%s\n' "$pwr" > $card/power_dpm_state; } 2> /dev/null; rc1=$?
{ printf '%s\n' "$level" > $card/power_dpm_force_performance_level; } 2> /dev/null; rc2=$?
echo_debug "pm" "set_radeon_profile($1): $card state=$pwr [rc=$rc1] perf=$level [rc=$rc2]"
fi
sdone=1
elif [ -f $card/power_method ] && [ -f $card/power_profile ]; then
# Use old radeon power profile method
if [ "$1" = "1" ]; then
pwr=${RADEON_POWER_PROFILE_ON_BAT:-}
else
pwr=${RADEON_POWER_PROFILE_ON_AC:-}
fi
if [ -z "$pwr" ]; then
# do nothing if unconfigured
echo_debug "pm" "set_radeon_profile($1).not_configured: $card"
return 0
fi
if [ -n "$pwr" ]; then
echo_debug "pm" "set_radeon_profile($1): $card profile=$pwr"
{ printf '%s\n' "profile" > $card/power_method; } 2> /dev/null
{ printf '%s\n' "$pwr" > $card/power_profile; } 2> /dev/null
fi
sdone=1
fi
done
if [ $sdone -eq 0 ]; then
echo_debug "pm" "set_radeon_profile($1).not_available"
fi
return 0
}