summarylogtreecommitdiffstats
path: root/phc-intel.sh
blob: a62ad5839c569339b00973205b587d3eb0e93285 (plain)
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
#!/bin/bash -e

## This file is licensed under the GNU General Public License, version 2.

VERSION=0.4.0
_conf='/etc/default/phc-intel'

msg() {
  printf '%s\n' "$@"
}

errmsg() {
  msg "$@" > /dev/stderr
}

check_conf_and_set() {
  if [ -r "${_conf}" ]; then
    . "${_conf}"
    if [ -z "$VIDS" ]; then
      errmsg "=> Please edit '${_conf}'."
      exit 1
    fi
    msg ':: Setting PHC VIDs'
    for i in /sys/devices/system/cpu/cpu*/cpufreq/phc_vids; do
      echo $VIDS > "$i"
    done
  else
    errmsg "$0: Error: Config file '${_conf}' not present."
    exit 2
  fi
}

check_kernel_cmdline() {
  for i in $(< /proc/cmdline); do
    if [ $i = nophc ]; then
      errmsg "=> 'nophc' kernel option set, not setting PHC VIDs."
      errmsg "   Use '$0 force-set' to override."
      exit 0
      return 1
    fi
  done
}

shopt -s nullglob

case "$1" in

  start)
    check_kernel_cmdline "$@" || exit 0
    check_conf_and_set "$@"
  ;;

  stop|reset)
    msg ':: Resetting default PHC VIDs'
    for i in /sys/devices/system/cpu/cpu*/cpufreq/phc_vids; do
      cp "${i%vids}default_vids" "$i"
    done
  ;;

  status)
    check_off () {
      for i in /sys/devices/system/cpu/cpu*/cpufreq/phc_vids; do
        [ "$(< "$i")" = "$(< "${i%vids}default_vids")" ] || return;
      done
    }
    check_on () {
      for i in /sys/devices/system/cpu/cpu*/cpufreq/phc_vids; do
        [[ "$(< "$i")" =~ "$VIDS" ]] || return;
      done
    }
    printf '%s' 'PHC status: '
    . "${_conf}"
    if check_off; then
      msg 'inactive'
    elif check_on; then
      msg 'active'
    else
      msg 'unknown'
    fi
  ;;

  set)
    "$0" start
  ;;

  force-set)
    check_conf_and_set "$@"
  ;;

  reset)
    "$0" stop
  ;;

  --version)
    printf '%s\n' "${VERSION}"
  ;;

  *)
    echo "usage: $0 {start|stop|status|set|force-set|reset|--version}"
  ;;

esac