/rc5.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. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  24. NAME=loadcpufreq
  25. # Get lsb functions
  26. . /lib/lsb/init-functions
  27. . /etc/default/rcS
  28. # Set a default value
  29. ENABLE="true"
  30. [ -f /etc/default/loadcpufreq ] && . /etc/default/loadcpufreq
  31. set -e
  32. # if not enabled then exit gracefully
  33. [ "$ENABLE" = "true" ] || exit 0
  34. load_detected_cpufreq_modules() {
  35. #if /usr/sbin/laptop-detect; then LAPTOP=1; fi
  36. CPUINFO=/proc/cpuinfo
  37. IOPORTS=/proc/ioports
  38. if [ ! -f $CPUINFO ] ; then
  39. echo "$CPUINFO not detected..." >&2
  40. return
  41. fi
  42. MODEL_NAME=$(grep '^model name' "$CPUINFO" | head -1 | sed -e 's/^.*: //;')
  43. MODEL_ID=$(grep -E '^model[[:space:]]+:' "$CPUINFO" | head -1 | sed -e 's/^.*: //;')
  44. CPU=$(grep -E '^cpud[^:]+:' "$CPUINFO" | head -1 | sed -e 's/^.*: //;')
  45. VENDOR_ID=$(grep -E '^vendor_id[^:]+:' "$CPUINFO" | head -1 | sed -e 's/^.*: //;')
  46. CPU_FAMILY=$(sed -e '/^cpu family/ {s/.*: //;p;Q};d' $CPUINFO)
  47. MODULE=
  48. MODULE_FALLBACK=acpi-cpufreq
  49. # Two modules for PIII-M depending the chipset.
  50. # modprobe speedstep-ich$EXT || modprobe speestep-smi$EXT
  51. # would be another way
  52. if [ -f $IOPORTS ] && grep -q 'Intel .*ICH' $IOPORTS ; then
  53. PIII_MODULE=speedstep-ich
  54. else
  55. PIII_MODULE=speedstep-smi
  56. fi
  57. case "$VENDOR_ID" in
  58. GenuineIntel*)
  59. # If the CPU has the est flag, it supports enhanced
  60. # speedstep and should use the acpi-cpufreq driver
  61. if [ "$(grep est $CPUINFO)" ]; then
  62. MODULE=acpi-cpufreq
  63. elif [ $CPU_FAMILY = 15 ]; then
  64. # Right. Check if it's a P4 without est.
  65. # Could be speedstep-ich, or could be p4-clockmod.
  66. MODULE=speedstep-ich;
  67. # Disabled for now - the latency tends to be bad
  68. # enough to make it fairly pointless.
  69. # echo "FREQDRIVER=p4-clockmod" >/etc/default/powernowd
  70. # to override this
  71. # if [ $LAPTOP = "1" ]; then
  72. # MODULE_FALLBACK=p4-clockmod;
  73. # fi
  74. else
  75. # So it doesn't have Enhanced Speedstep, and it's not a
  76. # P4. It could be a Speedstep PIII, or it may be
  77. # unsupported. There's no terribly good programmatic way
  78. # of telling.
  79. case "$MODEL_NAME" in
  80. Intel\(R\)\ Pentium\(R\)\ III\ Mobile\ CPU*)
  81. MODULE=$PIII_MODULE
  82. ;;
  83. # JD: says this works with cpufreq_userspace
  84. Mobile\ Intel\(R\)\ Pentium\(R\)\ III\ CPU\ -\ M*)
  85. MODULE=$PIII_MODULE
  86. ;;
  87. # https://bugzilla.ubuntu.com/show_bug.cgi?id=4262
  88. # UNCONFIRMED
  89. Pentium\ III\ \(Coppermine\)*)
  90. MODULE=$PIII_MODULE
  91. ;;
  92. esac
  93. fi
  94. ;;
  95. AuthenticAMD*)
  96. # Hurrah. This is nice and easy.
  97. case $CPU_FAMILY in
  98. 5)
  99. # K6
  100. MODULE=powernow-k6
  101. ;;
  102. 6)
  103. # K7
  104. MODULE=powernow-k7
  105. ;;
  106. 15|16|17)
  107. # K8
  108. MODULE=powernow-k8
  109. ;;
  110. esac
  111. ;;
  112. CentaurHauls*)
  113. # VIA
  114. if [ $CPU_FAMILY = 6 ]; then
  115. case $MODEL_ID in
  116. 10) # VIA C7 VIA Esther
  117. MODULE=e_powersaver
  118. ;;
  119. *)
  120. MODULE=longhaul
  121. ;;
  122. esac
  123. fi
  124. ;;
  125. GenuineTMx86*)
  126. # Transmeta
  127. if [ "$(grep longrun $CPUINFO)" ]; then
  128. MODULE=longrun
  129. fi
  130. ;;
  131. esac
  132. }
  133. load_modules() {
  134. #stop the kernel printk'ing at all while we load.
  135. PRINTK=$(cat /proc/sys/kernel/printk)
  136. [ "$VERBOSE" = no ] && echo "1 1 1 1" > /proc/sys/kernel/printk
  137. #build a list of current modules so we don't load a module twice
  138. LIST=$(/sbin/lsmod|awk '!/Module/ {print $1}')
  139. #get list of available modules (governors and helpers)
  140. LOC="/lib/modules/$(uname -r)/kernel/drivers/cpufreq"
  141. if [ -d $LOC ]; then
  142. MODAVAIL=$( ( find $LOC -type f -name "*.o" -printf "basename %f .o\n"; \
  143. find $LOC -type f -name "*.ko" -printf "basename %f .ko\n" ) | /bin/sh)
  144. else
  145. MODAVAIL=""
  146. fi
  147. #echo "Loading cpufreq modules:"
  148. for mod in $MODAVAIL; do
  149. # echo " $mod"
  150. echo $LIST| grep -q -w "$mod" || modprobe $mod >/dev/null || /bin/true
  151. done
  152. #cpufreq is built in on powerpc; just return
  153. if [ "$(uname -m)" = "ppc" ]; then
  154. echo "$PRINTK" > /proc/sys/kernel/printk
  155. return 0
  156. fi
  157. #new style detection system
  158. if [ ! "$FREQDRIVER" = "" ]; then
  159. # user overridden value in /etc/default/loadcpufreq
  160. modprobe "$FREQDRIVER"
  161. MODULE="$FREQDRIVER"
  162. else
  163. load_detected_cpufreq_modules
  164. if [ ! -z "$MODULE" ] || [ ! -z "$MODULE_FALLBACK" ] ; then
  165. if [ ! -z "$MODULE" ] && modprobe "$MODULE" 2>/dev/null ; then
  166. :
  167. elif modprobe "$MODULE_FALLBACK" 2>/dev/null ; then
  168. MODULE="$MODULE_FALLBACK"
  169. else
  170. unset MODULE
  171. fi
  172. fi
  173. fi
  174. echo "$PRINTK" > /proc/sys/kernel/printk
  175. }
  176. check_kernel() {
  177. CPUFREQ=/sys/devices/system/cpu/cpu0/cpufreq
  178. if [ -z "$MODULE" ] || ([ -f "$CPUFREQ/scaling_governor" ] && \
  179. [ -f "$CPUFREQ/scaling_available_governors" ])
  180. then
  181. return 0
  182. else
  183. return 1
  184. fi
  185. }
  186. case "$1" in
  187. start)
  188. log_action_begin_msg "Loading cpufreq kernel modules"
  189. [ -f /proc/modules ] && load_modules
  190. if check_kernel ; then
  191. [ -z "$MODULE" ] && MODULE="none"
  192. log_action_end_msg 0 "$MODULE"
  193. else
  194. log_action_end_msg 1
  195. fi
  196. ;;
  197. stop)
  198. ;;
  199. restart|force-reload)
  200. $0 stop
  201. sleep 1
  202. $0 start
  203. #echo "$NAME."
  204. ;;
  205. *)
  206. N=/etc/init.d/$NAME
  207. log_success_msg "Usage: $N {start|stop|restart|force-reload}" >&2
  208. exit 1
  209. ;;
  210. esac
  211. exit 0