PageRenderTime 45ms CodeModel.GetById 26ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/rc3.d/S05loadcpufreq

http://github.com/brinkman83/bashrc
Shell | 236 lines | 149 code | 25 blank | 62 comment | 25 complexity | 05a411504f770886e4feb8df8a2d4339 MD5 | raw file
  1#!/bin/sh
  2### BEGIN INIT INFO
  3# Provides:          loadcpufreq
  4# Required-Start:    $remote_fs
  5# Required-Stop:
  6# Default-Start:     2 3 4 5
  7# Default-Stop:
  8# Short-Description: Load kernel modules needed to enable cpufreq scaling
  9# Description:       Make it possible to save power by reducing
 10#                    the CPU speed when there is little to do.
 11### END INIT INFO
 12# License: GNU General Public License.
 13#
 14# Based on scripts found in the powernowd package version
 15# 0.97-1ubuntu6 on Ubuntu.
 16#
 17# This script is an interim solution until the default Debian packages
 18# will load the proper kernel modules at boot time.  Track #396117,
 19# #342014 and #367307 to see status on this.
 20# <URL:http://0pointer.de/blog/projects/dmi-based-module-autoloading.html>
 21# claim the later kernels support autoloading of these modules, so I
 22# guess In the future this script can be dropped. [pere 2007-05-12]
 23
 24PATH=/sbin:/bin:/usr/sbin:/usr/bin
 25NAME=loadcpufreq
 26
 27# Get lsb functions
 28. /lib/lsb/init-functions
 29. /etc/default/rcS
 30
 31# Set a default value
 32ENABLE="true"
 33[ -f /etc/default/loadcpufreq ] && . /etc/default/loadcpufreq
 34
 35set -e
 36
 37# if not enabled then exit gracefully
 38[ "$ENABLE" = "true" ] || exit 0
 39
 40load_detected_cpufreq_modules() {
 41    #if /usr/sbin/laptop-detect; then LAPTOP=1; fi
 42    CPUINFO=/proc/cpuinfo
 43    IOPORTS=/proc/ioports
 44
 45    if [ ! -f $CPUINFO ] ; then
 46	echo "$CPUINFO not detected..." >&2
 47	return
 48    fi
 49
 50    MODEL_NAME=$(grep '^model name' "$CPUINFO" | head -1 | sed -e 's/^.*: //;')
 51    MODEL_ID=$(grep -E '^model[[:space:]]+:' "$CPUINFO" | head -1 | sed -e 's/^.*: //;')
 52    CPU=$(grep -E '^cpud[^:]+:' "$CPUINFO" | head -1 | sed -e 's/^.*: //;')
 53    VENDOR_ID=$(grep -E '^vendor_id[^:]+:' "$CPUINFO" | head -1 | sed -e 's/^.*: //;')
 54    CPU_FAMILY=$(sed -e '/^cpu family/ {s/.*: //;p;Q};d' $CPUINFO)
 55
 56    MODULE=
 57    MODULE_FALLBACK=acpi-cpufreq
 58
 59    # Two modules for PIII-M depending the chipset.
 60    # modprobe speedstep-ich$EXT || modprobe speestep-smi$EXT
 61    # would be another way
 62    if [ -f $IOPORTS ] && grep -q 'Intel .*ICH' $IOPORTS ; then
 63	PIII_MODULE=speedstep-ich
 64    else
 65	PIII_MODULE=speedstep-smi
 66    fi
 67
 68    case "$VENDOR_ID" in
 69        GenuineIntel*)
 70            # If the CPU has the est flag, it supports enhanced
 71            # speedstep and should use the acpi-cpufreq driver
 72            if [ "$(grep est $CPUINFO)" ]; then
 73		MODULE=acpi-cpufreq
 74            elif [ $CPU_FAMILY = 15 ]; then
 75            # Right. Check if it's a P4 without est.
 76                # Could be speedstep-ich, or could be p4-clockmod. 
 77                MODULE=speedstep-ich;
 78                # Disabled for now - the latency tends to be bad
 79                # enough to make it fairly pointless.
 80                # echo "FREQDRIVER=p4-clockmod" >/etc/default/powernowd
 81                # to override this
 82#               if [ $LAPTOP = "1" ]; then
 83#                   MODULE_FALLBACK=p4-clockmod;
 84#               fi
 85            else
 86        # So it doesn't have Enhanced Speedstep, and it's not a
 87        # P4. It could be a Speedstep PIII, or it may be
 88        # unsupported. There's no terribly good programmatic way
 89        # of telling.
 90                case "$MODEL_NAME" in
 91                    Intel\(R\)\ Pentium\(R\)\ III\ Mobile\ CPU*)
 92                        MODULE=$PIII_MODULE
 93                        ;;
 94        
 95        # JD: says this works with cpufreq_userspace
 96
 97                    Mobile\ Intel\(R\)\ Pentium\(R\)\ III\ CPU\ -\ M*)
 98                        MODULE=$PIII_MODULE
 99                        ;;
100        
101        # https://bugzilla.ubuntu.com/show_bug.cgi?id=4262
102        # UNCONFIRMED
103                    Pentium\ III\ \(Coppermine\)*)
104                        MODULE=$PIII_MODULE
105                        ;;
106                esac
107            fi
108            ;;
109        AuthenticAMD*)
110# Hurrah. This is nice and easy.
111            case $CPU_FAMILY in
112                5)
113                    # K6
114                    MODULE=powernow-k6
115                    ;;
116                6)
117                    # K7
118                    MODULE=powernow-k7
119                    ;;
120                15|16|17)
121                    # K8
122                    MODULE=powernow-k8
123                    ;;
124            esac
125            ;;
126        CentaurHauls*)
127        # VIA
128            if [ $CPU_FAMILY = 6 ]; then
129					case $MODEL_ID in
130						10) # VIA C7 VIA Esther
131							MODULE=e_powersaver
132						;;
133						*)
134							MODULE=longhaul
135						;;
136					esac
137            fi
138            ;;    
139        GenuineTMx86*)
140        # Transmeta
141            if [ "$(grep longrun $CPUINFO)" ]; then
142                MODULE=longrun
143            fi
144            ;;
145    esac
146}
147
148load_modules() {
149        #stop the kernel printk'ing at all while we load.
150        PRINTK=$(cat /proc/sys/kernel/printk)
151        [ "$VERBOSE" = no ] && echo "1 1 1 1" > /proc/sys/kernel/printk
152
153        #build a list of current modules so we don't load a module twice
154        LIST=$(/sbin/lsmod|awk '!/Module/ {print $1}')
155
156        #get list of available modules (governors and helpers)
157        LOC="/lib/modules/$(uname -r)/kernel/drivers/cpufreq"
158        if [ -d $LOC ]; then
159          MODAVAIL=$( ( find $LOC -type f -name "*.o" -printf "basename %f .o\n"; \
160	      find $LOC -type f -name "*.ko" -printf "basename %f .ko\n" ) | /bin/sh)
161        else
162          MODAVAIL=""
163        fi
164
165        #echo "Loading cpufreq modules:"
166        for mod in $MODAVAIL; do
167        #        echo "     $mod"
168                echo $LIST| grep -q -w "$mod" || modprobe $mod >/dev/null || /bin/true
169        done
170
171        #cpufreq is built in on powerpc; just return
172        if [ "$(uname -m)" = "ppc" ]; then
173                echo "$PRINTK" > /proc/sys/kernel/printk
174                return 0
175        fi
176
177        #new style detection system
178        if [ ! "$FREQDRIVER" = "" ]; then 
179		# user overridden value in /etc/default/loadcpufreq
180                modprobe "$FREQDRIVER"
181		MODULE="$FREQDRIVER"
182        else
183	        load_detected_cpufreq_modules
184		if [ ! -z "$MODULE" ] || [ ! -z "$MODULE_FALLBACK" ] ; then
185		    if [ ! -z "$MODULE" ] && modprobe "$MODULE" 2>/dev/null ; then
186			:
187		    elif modprobe "$MODULE_FALLBACK" 2>/dev/null ; then
188			MODULE="$MODULE_FALLBACK"
189		    else
190			unset MODULE
191		    fi
192		fi
193        fi
194
195        echo "$PRINTK" > /proc/sys/kernel/printk
196}
197
198check_kernel() {
199	CPUFREQ=/sys/devices/system/cpu/cpu0/cpufreq
200
201	if [ -z "$MODULE" ] || ([ -f "$CPUFREQ/scaling_governor" ] && \
202		[ -f "$CPUFREQ/scaling_available_governors" ])
203	then
204		return 0
205	else
206		return 1
207	fi
208}
209
210case "$1" in
211  start)
212       	log_action_begin_msg "Loading cpufreq kernel modules"
213        [ -f /proc/modules ] && load_modules
214	if check_kernel ; then
215	    [ -z "$MODULE" ] && MODULE="none"
216	    log_action_end_msg 0 "$MODULE"
217	else
218	    log_action_end_msg 1
219	fi
220	;;
221  stop)
222	;;
223  restart|force-reload)
224        $0 stop
225	sleep 1
226	$0 start
227	#echo "$NAME."
228	;;
229  *)
230	N=/etc/init.d/$NAME
231	log_success_msg "Usage: $N {start|stop|restart|force-reload}" >&2
232	exit 1
233	;;
234esac
235
236exit 0