1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software: you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation, either version 3 of the License, or
9 ;;; (at your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu services pm)
20 #:use-module (guix gexp)
21 #:use-module (guix packages)
22 #:use-module (guix records)
23 #:use-module (gnu packages linux)
24 #:use-module (gnu services)
25 #:use-module (gnu services base)
26 #:use-module (gnu services configuration)
27 #:use-module (gnu services shepherd)
28 #:use-module (gnu system shadow)
29 #:export (tlp-service-type
32 (define (uglify-field-name field-name)
33 (let ((str (symbol->string field-name)))
34 (string-join (string-split
36 (if (string-suffix? "?" str)
37 (substring str 0 (1- (string-length str)))
42 (define (serialize-field field-name val)
43 (format #t "~a=~a\n" (uglify-field-name field-name) val))
45 (define (serialize-boolean field-name val)
46 (serialize-field field-name (if val "1" "0")))
47 (define-maybe boolean)
49 (define (serialize-string field-name val)
50 (serialize-field field-name val))
53 (define (space-separated-string-list? val)
56 (and (string? x) (not (string-index x #\space))))
58 (define (serialize-space-separated-string-list field-name val)
59 (serialize-field field-name
61 (string-join val " "))))
62 (define-maybe space-separated-string-list)
64 (define (non-negative-integer? val)
65 (and (exact-integer? val) (not (negative? val))))
66 (define (serialize-non-negative-integer field-name val)
67 (serialize-field field-name val))
68 (define-maybe non-negative-integer)
70 (define (on-off-boolean? val)
72 (define (serialize-on-off-boolean field-name val)
73 (serialize-field field-name (if val "on" "off")))
74 (define-maybe on-off-boolean)
76 (define (y-n-boolean? val)
78 (define (serialize-y-n-boolean field-name val)
79 (serialize-field field-name (if val "Y" "N")))
81 (define-configuration tlp-configuration
88 "Set to true if you wish to enable TLP.")
92 "Default mode when no power supply can be detected. Alternatives are
96 (non-negative-integer 0)
97 "Number of seconds Linux kernel has to wait after the disk goes idle,
98 before syncing on AC.")
100 (disk-idle-secs-on-bat
101 (non-negative-integer 2)
102 "Same as @code{disk-idle-ac} but on BAT mode.")
104 (max-lost-work-secs-on-ac
105 (non-negative-integer 15)
106 "Dirty pages flushing periodicity, expressed in seconds.")
108 (max-lost-work-secs-on-bat
109 (non-negative-integer 60)
110 "Same as @code{max-lost-work-secs-on-ac} but on BAT mode.")
112 (cpu-scaling-governor-on-ac
113 (maybe-space-separated-string-list 'disabled)
114 "CPU frequency scaling governor on AC mode. With intel_pstate
115 driver, alternatives are powersave and performance. With acpi-cpufreq driver,
116 alternatives are ondemand, powersave, performance and conservative.")
118 (cpu-scaling-governor-on-bat
119 (maybe-space-separated-string-list 'disabled)
120 "Same as @code{cpu-scaling-governor-on-ac} but on BAT mode.")
122 (cpu-scaling-min-freq-on-ac
123 (maybe-non-negative-integer 'disabled)
124 "Set the min available frequency for the scaling governor on AC.")
126 (cpu-scaling-max-freq-on-ac
127 (maybe-non-negative-integer 'disabled)
128 "Set the max available frequency for the scaling governor on AC.")
130 (cpu-scaling-min-freq-on-bat
131 (maybe-non-negative-integer 'disabled)
132 "Set the min available frequency for the scaling governor on BAT.")
134 (cpu-scaling-max-freq-on-bat
135 (maybe-non-negative-integer 'disabled)
136 "Set the max available frequency for the scaling governor on BAT.")
139 (maybe-non-negative-integer 'disabled)
140 "Limit the min P-state to control the power dissipation of the CPU,
141 in AC mode. Values are stated as a percentage of the available performance.")
144 (maybe-non-negative-integer 'disabled)
145 "Limit the max P-state to control the power dissipation of the CPU,
146 in AC mode. Values are stated as a percentage of the available performance.")
149 (maybe-non-negative-integer 'disabled)
150 "Same as @code{cpu-min-perf-on-ac} on BAT mode.")
153 (maybe-non-negative-integer 'disabled)
154 "Same as @code{cpu-max-perf-on-ac} on BAT mode.")
157 (maybe-boolean 'disabled)
158 "Enable CPU turbo boost feature on AC mode.")
161 (maybe-boolean 'disabled)
162 "Same as @code{cpu-boost-on-ac?} on BAT mode.")
164 (sched-powersave-on-ac?
166 "Allow Linux kernel to minimize the number of CPU cores/hyper-threads
167 used under light load conditions.")
169 (sched-powersave-on-bat?
171 "Same as @code{sched-powersave-on-ac?} but on BAT mode.")
175 "Enable Linux kernel NMI watchdog.")
178 (maybe-string 'disabled)
179 "For Linux kernels with PHC patch applied, change CPU voltages.
180 An example value would be @samp{\"F:V F:V F:V F:V\"}.")
182 (energy-perf-policy-on-ac
183 (string "performance")
184 "Set CPU performance versus energy saving policy on AC. Alternatives are
185 performance, normal, powersave.")
187 (energy-perf-policy-on-bat
189 "Same as @code{energy-perf-policy-ac} but on BAT mode.")
192 (space-separated-string-list '("sda"))
193 "Hard disk devices.")
195 (disk-apm-level-on-ac
196 (space-separated-string-list '("254" "254"))
197 "Hard disk advanced power management level.")
199 (disk-apm-level-on-bat
200 (space-separated-string-list '("128" "128"))
201 "Same as @code{disk-apm-bat} but on BAT mode.")
203 (disk-spindown-timeout-on-ac
204 (maybe-space-separated-string-list 'disabled)
205 "Hard disk spin down timeout. One value has to be specified for
206 each declared hard disk.")
208 (disk-spindown-timeout-on-bat
209 (maybe-space-separated-string-list 'disabled)
210 "Same as @code{disk-spindown-timeout-on-ac} but on BAT mode.")
213 (maybe-space-separated-string-list 'disabled)
214 "Select IO scheduler for disk devices. One value has to be specified
215 for each declared hard disk. Example alternatives are cfq, deadline and noop.")
218 (string "max_performance")
219 "SATA aggressive link power management (ALPM) level. Alternatives are
220 min_power, medium_power, max_performance.")
224 "Same as @code{sata-linkpwr-ac} but on BAT mode.")
226 (sata-linkpwr-blacklist
227 (maybe-string 'disabled)
228 "Exclude specified SATA host devices for link power management.")
230 (ahci-runtime-pm-on-ac?
231 (maybe-on-off-boolean 'disabled)
232 "Enable Runtime Power Management for AHCI controller and disks
235 (ahci-runtime-pm-on-bat?
236 (maybe-on-off-boolean 'disabled)
237 "Same as @code{ahci-runtime-pm-on-ac} on BAT mode.")
239 (ahci-runtime-pm-timeout
240 (non-negative-integer 15)
241 "Seconds of inactivity before disk is suspended.")
244 (string "performance")
245 "PCI Express Active State Power Management level. Alternatives are
246 default, performance, powersave.")
250 "Same as @code{pcie-aspm-ac} but on BAT mode.")
252 (radeon-power-profile-on-ac
254 "Radeon graphics clock speed level. Alternatives are
255 low, mid, high, auto, default.")
257 (radeon-power-profile-on-bat
259 "Same as @code{radeon-power-ac} but on BAT mode.")
261 (radeon-dpm-state-on-ac
262 (string "performance")
263 "Radeon dynamic power management method (DPM). Alternatives are
264 battery, performance.")
266 (radeon-dpm-state-on-bat
268 "Same as @code{radeon-dpm-state-ac} but on BAT mode.")
270 (radeon-dpm-perf-level-on-ac
272 "Radeon DPM performance level. Alternatives are
275 (radeon-dpm-perf-level-on-bat
277 "Same as @code{radeon-dpm-perf-ac} but on BAT mode.")
281 "Wifi power saving mode.")
285 "Same as @code{wifi-power-ac?} but on BAT mode.")
289 "Disable wake on LAN.")
291 (sound-power-save-on-ac
292 (non-negative-integer 0)
293 "Timeout duration in seconds before activating audio power saving
294 on Intel HDA and AC97 devices. A value of 0 disables power saving.")
296 (sound-power-save-on-bat
297 (non-negative-integer 1)
298 "Same as @code{sound-powersave-ac} but on BAT mode.")
300 (sound-power-save-controller?
302 "Disable controller in powersaving mode on Intel HDA devices.")
304 (bay-poweroff-on-bat?
306 "Enable optical drive in UltraBay/MediaBay on BAT mode.
307 Drive can be powered on again by releasing (and reinserting) the eject lever
308 or by pressing the disc eject button on newer models.")
312 "Name of the optical drive device to power off.")
316 "Runtime Power Management for PCI(e) bus devices. Alternatives are
321 "Same as @code{runtime-pm-ac} but on BAT mode.")
325 "Runtime Power Management for all PCI(e) bus devices, except
328 (runtime-pm-blacklist
329 (maybe-space-separated-string-list 'disabled)
330 "Exclude specified PCI(e) device addresses from Runtime Power Management.")
332 (runtime-pm-driver-blacklist
333 (space-separated-string-list '("radeon" "nouveau"))
334 "Exclude PCI(e) devices assigned to the specified drivers from
335 Runtime Power Management.")
339 "Enable USB autosuspend feature.")
342 (maybe-string 'disabled)
343 "Exclude specified devices from USB autosuspend.")
347 "Exclude WWAN devices from USB autosuspend.")
350 (maybe-string 'disabled)
351 "Include specified devices into USB autosuspend, even if they are
352 already excluded by the driver or via @code{usb-blacklist-wwan?}.")
354 (usb-autosuspend-disable-on-shutdown?
355 (maybe-boolean 'disabled)
356 "Enable USB autosuspend before shutdown.")
358 (restore-device-state-on-startup?
360 "Restore radio device state (bluetooth, wifi, wwan) from previous
361 shutdown on system startup."))
364 (define (tlp-shepherd-service config)
365 (let* ((tlp-bin (file-append
366 (tlp-configuration-tlp config) "/bin/tlp"))
367 (tlp-action (lambda args
369 (zero? (system* #$tlp-bin #$@args))))))
370 (list (shepherd-service
371 (documentation "Run TLP script.")
373 (requirement '(user-processes))
374 (start (tlp-action "init" "start"))
375 (stop (tlp-action "init" "stop"))))))
377 (define (tlp-activation config)
378 (let* ((config-str (with-output-to-string
380 (serialize-configuration
382 tlp-configuration-fields))))
383 (config-file (plain-file "tlp" config-str)))
384 (with-imported-modules '((guix build utils))
386 (use-modules (guix build utils))
387 (copy-file #$config-file "/etc/tlp")))))
389 (define tlp-service-type
394 (service-extension shepherd-root-service-type
395 tlp-shepherd-service)
396 (service-extension udev-service-type
397 (compose list tlp-configuration-tlp))
398 (service-extension activation-service-type
400 (default-value (tlp-configuration))))
402 (define (generate-tlp-documentation)
403 (generate-documentation
404 `((tlp-configuration ,tlp-configuration-fields))