Skip to content

Commit 4f3b86f

Browse files
committed
cpufreq: Add simple userspace CPU-boost driver
This is a simple CPU-boost driver designed to be controlled by the power HAL's power hints. Usage: When 1 is written to /sys/module/userspace_cpu_boost/parameters/boost_now by the power HAL, all online CPUs are boosted to a specified boost frequency for a specified duration of time. The default boost frequency is 1134MHz and the default boost duration is 3 seconds. This is meant to be an open-source solution for the closed-source CPU-boosting behavior that is present on stock AOSP for the Nexus 5. Signed-off-by: Sultanxda <sultanxda@gmail.com>
1 parent cd049c3 commit 4f3b86f

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

‎drivers/cpufreq/Kconfig‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,10 @@ depends on X86
217217
source "drivers/cpufreq/Kconfig.x86"
218218
endmenu
219219

220+
config USERSPACE_CPU_BOOST
221+
bool "Userspace-controlled CPU boosting"
222+
help
223+
Simple CPU-boost driver that is controlled by userspace.
224+
220225
endif
221226
endmenu

‎drivers/cpufreq/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# CPUfreq core
22
obj-$(CONFIG_CPU_FREQ) += cpufreq.o
3+
obj-$(CONFIG_USERSPACE_CPU_BOOST) += userspace_cpu-boost.o
34
# CPUfreq stats
45
obj-$(CONFIG_CPU_FREQ_STAT) += cpufreq_stats.o
56

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2014, Sultanxda <sultanxda@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 and
6+
* only version 2 as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*/
13+
14+
#include <linux/cpu.h>
15+
#include <linux/cpufreq.h>
16+
#include <linux/init.h>
17+
#include <linux/module.h>
18+
#include <linux/moduleparam.h>
19+
20+
/* CPU boost freq in KHz. All online CPUs
21+
* are boosted. */
22+
static int boost_freq_khz = 1134000;
23+
module_param(boost_freq_khz, int, 0644);
24+
25+
/* Duration to boost CPUs in millisecs. */
26+
static int boost_duration_ms = 3000;
27+
module_param(boost_duration_ms, int, 0644);
28+
29+
/* File for userspace to write to in
30+
* order to request boost. */
31+
static int boost_now = 0;
32+
module_param(boost_now, int, 0644);
33+
34+
/* Time in ms to poll for boost request from
35+
* userspace. */
36+
#define BOOST_CHECK_MS 20
37+
38+
/* CPU's default minfreq in KHz. */
39+
#define DEFAULT_MINFREQ_KHZ 192000
40+
41+
static struct delayed_work check_input_boost;
42+
43+
static void input_boost_main(struct work_struct *work)
44+
{
45+
struct cpufreq_policy *cpu_policy = NULL;
46+
static int cpu_boosted = 0;
47+
unsigned int minfreq = DEFAULT_MINFREQ_KHZ;
48+
unsigned int wait_ms = BOOST_CHECK_MS;
49+
int cpu = 0;
50+
51+
if (cpu_boosted) {
52+
/* restore original minfreq */
53+
get_online_cpus();
54+
for_each_online_cpu(cpu) {
55+
cpu_policy = cpufreq_cpu_get(cpu);
56+
cpufreq_verify_within_limits(cpu_policy, minfreq, UINT_MAX);
57+
cpu_policy->user_policy.min = minfreq;
58+
cpufreq_update_policy(cpu);
59+
cpufreq_cpu_put(cpu_policy);
60+
}
61+
put_online_cpus();
62+
cpu_boosted = 0;
63+
wait_ms = 0;
64+
boost_now = 0;
65+
goto end;
66+
}
67+
68+
if (boost_now) {
69+
minfreq = boost_freq_khz;
70+
71+
/* boost online CPUs */
72+
get_online_cpus();
73+
for_each_online_cpu(cpu) {
74+
cpu_policy = cpufreq_cpu_get(cpu);
75+
cpufreq_verify_within_limits(cpu_policy, minfreq, UINT_MAX);
76+
cpu_policy->user_policy.min = minfreq;
77+
cpufreq_update_policy(cpu);
78+
cpufreq_cpu_put(cpu_policy);
79+
}
80+
put_online_cpus();
81+
cpu_boosted = 1;
82+
wait_ms = boost_duration_ms;
83+
}
84+
85+
end:
86+
schedule_delayed_work(&check_input_boost,
87+
msecs_to_jiffies(wait_ms));
88+
}
89+
90+
static int userspace_boost_init(void)
91+
{
92+
INIT_DELAYED_WORK(&check_input_boost, input_boost_main);
93+
schedule_delayed_work(&check_input_boost, 0);
94+
95+
return 0;
96+
}
97+
late_initcall(userspace_boost_init);
98+
99+
MODULE_AUTHOR("Sultanxda <sultanxda@gmail.com>");
100+
MODULE_DESCRIPTION("Userspace CPU-Boost Driver");
101+
MODULE_LICENSE("GPLv2");

0 commit comments

Comments
 (0)