|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Select a low-latency pstate mode early in boot when the user did not choose |
| 4 | +# one explicitly on the kernel command line. |
| 5 | + |
| 6 | +proc_cmdline="${BIGLINUX_PROC_CMDLINE:-/proc/cmdline}" |
| 7 | +proc_cpuinfo="${BIGLINUX_PROC_CPUINFO:-/proc/cpuinfo}" |
| 8 | +sys_cpu="${BIGLINUX_SYS_CPU:-/sys/devices/system/cpu}" |
| 9 | + |
| 10 | +log() { |
| 11 | + if command -v logger >/dev/null 2>&1; then |
| 12 | + logger -t biglinux-auto-pstate "$*" 2>/dev/null || true |
| 13 | + fi |
| 14 | +} |
| 15 | + |
| 16 | +manual_pstate_is_configured() { |
| 17 | + [ -r "$proc_cmdline" ] || return 1 |
| 18 | + tr ' ' '\n' < "$proc_cmdline" | grep -Eq '^(amd_pstate|intel_pstate)([.=]|$)' |
| 19 | +} |
| 20 | + |
| 21 | +cpu_vendor() { |
| 22 | + [ -r "$proc_cpuinfo" ] || return 1 |
| 23 | + awk -F ': *' '/^vendor_id[[:space:]]*:/{ print $2; exit }' "$proc_cpuinfo" |
| 24 | +} |
| 25 | + |
| 26 | +set_pstate_status() { |
| 27 | + local driver="$1" |
| 28 | + local desired_status="$2" |
| 29 | + local status_file="$sys_cpu/$driver/status" |
| 30 | + local current_status |
| 31 | + |
| 32 | + [ -r "$status_file" ] || return 0 |
| 33 | + |
| 34 | + current_status="$(cat "$status_file" 2>/dev/null || true)" |
| 35 | + [ "$current_status" = "$desired_status" ] && return 0 |
| 36 | + |
| 37 | + if ! [ -w "$status_file" ]; then |
| 38 | + log "Cannot set $driver to $desired_status: $status_file is not writable" |
| 39 | + return 0 |
| 40 | + fi |
| 41 | + |
| 42 | + if printf '%s\n' "$desired_status" > "$status_file" 2>/dev/null; then |
| 43 | + log "Configured $driver status=$desired_status" |
| 44 | + else |
| 45 | + log "Failed to configure $driver status=$desired_status" |
| 46 | + fi |
| 47 | +} |
| 48 | + |
| 49 | +main() { |
| 50 | + local vendor |
| 51 | + |
| 52 | + if manual_pstate_is_configured; then |
| 53 | + log "Keeping pstate mode from kernel command line" |
| 54 | + return 0 |
| 55 | + fi |
| 56 | + |
| 57 | + vendor="$(cpu_vendor || true)" |
| 58 | + case "$vendor" in |
| 59 | + GenuineIntel) |
| 60 | + set_pstate_status intel_pstate passive |
| 61 | + ;; |
| 62 | + AuthenticAMD) |
| 63 | + set_pstate_status amd_pstate guided |
| 64 | + ;; |
| 65 | + esac |
| 66 | +} |
| 67 | + |
| 68 | +main "$@" |
0 commit comments