From 87928136b630403b8ed29986a59f9b9f935b3b48 Mon Sep 17 00:00:00 2001
From: Koichiro Iwao
Date: Thu, 20 Jun 2024 08:01:56 +0000
Subject: [PATCH] Add kernel-tools to optimize CPU clock (cpupower.service)
(cherry picked from commit 021ebb7be30e61a6ff2abfa2dc29eb6e2763eb04)
---
SOURCES/cpupower.config | 3 +
SOURCES/cpupower.service | 13 +++
SOURCES/kvm_stat.logrotate | 11 +++
SPECS/raspberrypi2.spec | 162 ++++++++++++++++++++++++++++++++++++-
4 files changed, 187 insertions(+), 2 deletions(-)
create mode 100644 SOURCES/cpupower.config
create mode 100644 SOURCES/cpupower.service
create mode 100644 SOURCES/kvm_stat.logrotate
diff --git a/SOURCES/cpupower.config b/SOURCES/cpupower.config
new file mode 100644
index 0000000..4c08d9f
--- /dev/null
+++ b/SOURCES/cpupower.config
@@ -0,0 +1,3 @@
+# See 'cpupower help' and cpupower(1) for more info
+CPUPOWER_START_OPTS="frequency-set -g ondemand"
+CPUPOWER_STOP_OPTS="frequency-set -g powersave"
diff --git a/SOURCES/cpupower.service b/SOURCES/cpupower.service
new file mode 100644
index 0000000..5f10ab7
--- /dev/null
+++ b/SOURCES/cpupower.service
@@ -0,0 +1,13 @@
+[Unit]
+Description=Configure CPU power related settings
+After=syslog.target
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+EnvironmentFile=/etc/sysconfig/cpupower
+ExecStart=/usr/bin/cpupower $CPUPOWER_START_OPTS
+ExecStop=/usr/bin/cpupower $CPUPOWER_STOP_OPTS
+
+[Install]
+WantedBy=multi-user.target
diff --git a/SOURCES/kvm_stat.logrotate b/SOURCES/kvm_stat.logrotate
new file mode 100644
index 0000000..105e15e
--- /dev/null
+++ b/SOURCES/kvm_stat.logrotate
@@ -0,0 +1,11 @@
+/var/log/kvm_stat.csv {
+ size 10M
+ missingok
+ compress
+ maxage 30
+ rotate 5
+ nodateext
+ postrotate
+ /usr/bin/systemctl try-restart kvm_stat.service
+ endscript
+}
diff --git a/SPECS/raspberrypi2.spec b/SPECS/raspberrypi2.spec
index 4a16c74..9df5d9b 100644
--- a/SPECS/raspberrypi2.spec
+++ b/SPECS/raspberrypi2.spec
@@ -11,7 +11,7 @@ ExclusiveArch: aarch64
%define local_version v8
%define bcmmodel 2711
-%define extra_version 1
+%define extra_version 2
# This originally implies Kernel 4.x for RPi 2 and is not appropriate now.
# Be careful to change this not to disturb the seamless package update.
@@ -21,6 +21,13 @@ ExclusiveArch: aarch64
%define kversion 6.6
%define patchlevel 31
+# standard kernel
+%define with_up %{?_without_up: 0} %{?!_without_up: 1}
+# tools
+%define with_tools %{?_without_tools: 0} %{?!_without_tools: 1}
+# firmware
+%define with_firmware %{?_without_firmware: 0} %{?!_without_firmware: 1}
+
Name: raspberrypi%{rpisuffix}
Version: %{kversion}.%{patchlevel}
Release: %{version_tag}.%{local_version}.%{extra_version}%{?dist}
@@ -32,6 +39,10 @@ Source0: https://github.com/raspberrypi/linux/archive/refs/tags/stable_%{
Source1: https://github.com/raspberrypi/firmware/archive/%{commit_firmware_long}.tar.gz
Patch100: config_2711.patch
Patch101: config_2712.patch
+# Sources for kernel-tools
+Source2000: cpupower.service
+Source2001: cpupower.config
+Source2002: kvm_stat.logrotate
BuildRequires: kmod, patch, bash, coreutils, tar
BuildRequires: bzip2, xz, findutils, gzip, m4, perl, perl-Carp, make, diffutils, gawk
@@ -41,6 +52,10 @@ BuildRequires: elfutils-devel zlib-devel binutils-devel newt-devel python3-devel
BuildRequires: audit-libs-devel
BuildRequires: pciutils-devel gettext ncurses-devel
BuildRequires: openssl-devel
+%if %{with_tools}
+# kernel-tools
+BuildRequires: asciidoc
+%endif
%description
Specific kernel and bootcode for Raspberry Pi
@@ -74,7 +89,41 @@ Requires: perl-interpreter
This package provides kernel headers and makefiles sufficient to build modules
against the kernel package.
+%if %{with_tools}
+%package kernel%{?ksuffix}-tools
+Summary: Assortment of tools for the Linux kernel
+Provides: cpupowerutils = 1:009-0.6.p1
+Obsoletes: cpupowerutils < 1:009-0.6.p1
+Provides: cpufreq-utils = 1:009-0.6.p1
+Provides: cpufrequtils = 1:009-0.6.p1
+Obsoletes: cpufreq-utils < 1:009-0.6.p1
+Obsoletes: cpufrequtils < 1:009-0.6.p1
+Obsoletes: cpuspeed < 1:1.5-16
+Requires: kernel-tools-libs = %{version}-%{release}
+%define __requires_exclude ^%{_bindir}/python
+%description kernel%{?ksuffix}-tools
+This package contains the tools/ directory from the kernel source
+and the supporting documentation.
+%package kernel%{?ksuffix}-tools-libs
+Summary: Libraries for the kernels-tools
+%description kernel%{?ksuffix}-tools-libs
+This package contains the libraries built from the tools/ directory
+from the kernel source.
+
+%package kernel%{?ksuffix}-tools-libs-devel
+Summary: Assortment of tools for the Linux kernel
+Requires: kernel%{?ksuffix}-tools = %{version}-%{release}
+Provides: cpupowerutils-devel = 1:009-0.6.p1
+Obsoletes: cpupowerutils-devel < 1:009-0.6.p1
+Requires: kernel%{?ksuffix}-tools-libs = %{version}-%{release}
+Provides: kernel-tools-devel
+%description kernel%{?ksuffix}-tools-libs-devel
+This package contains the development files for the tools/ directory from
+the kernel source.
+%endif
+
+%if %{with_firmware}
%package firmware
Summary: GPU firmware for the Raspberry Pi computer
License: Redistributable, with restrictions; see LICENSE.broadcom
@@ -86,7 +135,7 @@ Provides: grubby=8.40-10
%description firmware
This package contains the GPU firmware for the Raspberry Pi BCM2835 SOC
including the kernel bootloader.
-
+%endif
%prep
%setup -q -n linux-stable_%{version_tag}
@@ -112,9 +161,29 @@ pathfix.py -pni "%{__python3} %{py3_shbang_opts}" tools/ tools/perf/scripts/pyth
# to support both RPi 4 and 5.
export KERNEL=kernel%{armtarget}
make bcm%{bcmmodel}_defconfig
+%if %{with_up}
make %{?_smp_mflags} HOSTCFLAGS="%{?build_cflags}" HOSTLDFLAGS="%{?build_ldflags}" %{build_image} modules dtbs
+%endif
+
+# kernel-tools
+%if %{with_tools}
+make %{?_smp_mflags} -C tools/power/cpupower CPUFREQ_BENCH=false DEBUG=false
+pushd tools/thermal/tmon/
+make %{?_smp_mflags} HOSTCFLAGS="%{?build_cflags}" HOSTLDFLAGS="%{?build_ldflags}"
+popd
+pushd tools/iio/
+make %{?_smp_mflags} HOSTCFLAGS="%{?build_cflags}" HOSTLDFLAGS="%{?build_ldflags}"
+popd
+pushd tools/gpio/
+make %{?_smp_mflags} HOSTCFLAGS="%{?build_cflags}" HOSTLDFLAGS="%{?build_ldflags}"
+popd
+pushd tools/mm/
+make %{?_smp_mflags} HOSTCFLAGS="%{?build_cflags}" HOSTLDFLAGS="%{?build_ldflags}" slabinfo page_owner_sort
+popd
+%endif
%install
+%if %{with_up}
# kernel
mkdir -p %{buildroot}/boot/overlays/
mkdir -p %{buildroot}/usr/share/%{name}-kernel/%{version}-%{release}/boot/overlays
@@ -172,6 +241,9 @@ touch -r %{buildroot}$DevelDir/Makefile %{buildroot}$DevelDir/include/linux/vers
ln -T -s $DevelDir %{buildroot}/lib/modules/%{version}-%{release}/build --force
ln -T -s build %{buildroot}/lib/modules/%{version}-%{release}/source --force
+%endif
+
+%if %{with_firmware}
# firmware
# precompiled GPU firmware and bootloader
pushd %{buildroot}
@@ -182,7 +254,55 @@ tar -xf %{_sourcedir}/%{commit_firmware_long}.tar.gz \
firmware-%{commit_firmware_long}/boot/bootcode.bin \
--strip-components=1
popd
+%endif
+%if %{with_tools}
+# kernel-tools
+make -C tools/power/cpupower DESTDIR=$RPM_BUILD_ROOT libdir=%{_libdir} mandir=%{_mandir} CPUFREQ_BENCH=false install
+rm -f %{buildroot}%{_libdir}/*.{a,la}
+%find_lang cpupower
+
+install -D -m644 %{SOURCE2000} %{buildroot}%{_unitdir}/cpupower.service
+install -D -m644 %{SOURCE2001} %{buildroot}%{_sysconfdir}/sysconfig/cpupower
+pushd tools/thermal/tmon
+make INSTALL_ROOT=%{buildroot} install
+popd
+pushd tools/iio
+make DESTDIR=%{buildroot} install
+popd
+pushd tools/gpio
+make DESTDIR=%{buildroot} install
+popd
+install -m644 -D %{SOURCE2002} %{buildroot}%{_sysconfdir}/logrotate.d/kvm_stat
+pushd tools/kvm/kvm_stat
+%{__make} INSTALL_ROOT=%{buildroot} install-tools
+%{__make} INSTALL_ROOT=%{buildroot} install-man
+install -m644 -D kvm_stat.service %{buildroot}%{_unitdir}/kvm_stat.service
+popd
+pushd tools/mm/
+install -m755 slabinfo %{buildroot}%{_bindir}/slabinfo
+install -m755 page_owner_sort %{buildroot}%{_bindir}/page_owner_sort
+popd
+%endif
+
+%if %{with_tools}
+%post kernel%{?ksuffix}-tools
+%systemd_post cpupower.service
+
+%preun kernel%{?ksuffix}-tools
+%systemd_preun cpupower.service
+
+%postun kernel%{?ksuffix}-tools
+%systemd_postun cpupower.service
+
+%post kernel%{?ksuffix}-tools-libs
+/sbin/ldconfig
+
+%postun kernel%{?ksuffix}-tools-libs
+/sbin/ldconfig
+%endif
+
+%if %{with_up}
%files kernel%{?ksuffix}
%defattr(-,root,root,-)
/lib/modules/%{version}-%{release}
@@ -224,16 +344,54 @@ cp $(ls -1 /boot/config-kernel-*-*|sort -V|tail -1) /boot/config-kernel.inc
%files kernel%{?ksuffix}-devel
%defattr(-,root,root)
/usr/src/kernels/%{version}-%{release}
+%endif
+%if %{with_tools}
+%files kernel%{?ksuffix}-tools -f cpupower.lang
+%{_bindir}/cpupower
+%{_datadir}/bash-completion/completions/cpupower
+%{_unitdir}/cpupower.service
+%{_mandir}/man[1-8]/cpupower*
+%config(noreplace) %{_sysconfdir}/sysconfig/cpupower
+%{_bindir}/tmon
+%{_bindir}/iio_event_monitor
+%{_bindir}/iio_generic_buffer
+%{_bindir}/lsiio
+%{_bindir}/lsgpio
+%{_bindir}/gpio-hammer
+%{_bindir}/gpio-event-mon
+%{_bindir}/gpio-watch
+%{_mandir}/man1/kvm_stat*
+%{_bindir}/kvm_stat
+%{_unitdir}/kvm_stat.service
+%config(noreplace) %{_sysconfdir}/logrotate.d/kvm_stat
+%{_bindir}/page_owner_sort
+%{_bindir}/slabinfo
+%files kernel%{?ksuffix}-tools-libs
+%{_libdir}/libcpupower.so.1
+%{_libdir}/libcpupower.so.0.0.1
+
+%files kernel%{?ksuffix}-tools-libs-devel
+%{_libdir}/libcpupower.so
+%{_includedir}/cpufreq.h
+%{_includedir}/cpuidle.h
+%{_includedir}/powercap.h
+%endif
+
+%if %{with_firmware}
%files firmware
%defattr(-,root,root,-)
/boot/bootcode.bin
/boot/fixup*
/boot/start*
%doc /boot/LICENCE.broadcom
+%endif
%changelog
+* Thu Jun 20 2024 Koichiro Iwao - 6.6.31-20240529.v8.2
+- Add kernel-tools to optimize CPU clock (cpupower.service)
+
* Mon Jun 10 2024 Koichiro Iwao - 6.6.31-20240529.v8.1
- Update to v6.6.31 stable_20240529