blob: 327cf06595b1c25872304f39204cd4fc8254963d [file] [log] [blame]
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <errno.h>
17#include <string.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -070021#include <sys/socket.h>
22#include <cutils/uevent.h>
23#include <sys/poll.h>
24#include <pthread.h>
25#include <linux/netlink.h>
26#include <stdlib.h>
27#include <stdbool.h>
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -070028
29#define LOG_TAG "Grouper PowerHAL"
30#include <utils/Log.h>
31
32#include <hardware/hardware.h>
33#include <hardware/power.h>
34
35#define BOOST_PATH "/sys/devices/system/cpu/cpufreq/interactive/boost"
Ruchi Kandoid53a9ab2014-07-02 16:30:50 -070036#define UEVENT_MSG_LEN 2048
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -070037#define TOTAL_CPUS 4
38#define RETRY_TIME_CHANGING_FREQ 20
39#define SLEEP_USEC_BETWN_RETRY 200
40#define LOW_POWER_MAX_FREQ "640000"
41#define LOW_POWER_MIN_FREQ "51000"
42#define NORMAL_MAX_FREQ "1300000"
43#define UEVENT_STRING "online@/devices/system/cpu/"
44
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -070045static int boost_fd = -1;
46static int boost_warned;
47
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -070048static struct pollfd pfd;
49static char *cpu_path_min[] = {
50 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq",
51 "/sys/devices/system/cpu/cpu1/cpufreq/scaling_min_freq",
52 "/sys/devices/system/cpu/cpu2/cpufreq/scaling_min_freq",
53 "/sys/devices/system/cpu/cpu3/cpufreq/scaling_min_freq",
54};
55static char *cpu_path_max[] = {
56 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq",
57 "/sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq",
58 "/sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq",
59 "/sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq",
60};
61static bool freq_set[TOTAL_CPUS];
62static bool low_power_mode = false;
63static pthread_mutex_t low_power_mode_lock = PTHREAD_MUTEX_INITIALIZER;
64
65static int sysfs_write(char *path, char *s)
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -070066{
67 char buf[80];
68 int len;
69 int fd = open(path, O_WRONLY);
70
71 if (fd < 0) {
72 strerror_r(errno, buf, sizeof(buf));
73 ALOGE("Error opening %s: %s\n", path, buf);
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -070074 return -1;
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -070075 }
76
77 len = write(fd, s, strlen(s));
78 if (len < 0) {
79 strerror_r(errno, buf, sizeof(buf));
80 ALOGE("Error writing to %s: %s\n", path, buf);
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -070081 return -1;
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -070082 }
83
84 close(fd);
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -070085 return 0;
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -070086}
87
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -070088static int uevent_event()
89{
90 char msg[UEVENT_MSG_LEN];
91 char *cp;
92 int n, cpu, ret, retry = RETRY_TIME_CHANGING_FREQ;
93
94 n = recv(pfd.fd, msg, UEVENT_MSG_LEN, MSG_DONTWAIT);
95 if (n <= 0) {
96 return -1;
97 }
98 if (n >= UEVENT_MSG_LEN) { /* overflow -- discard */
99 return -1;
100 }
101
102 cp = msg;
103
104 if (strstr(cp, UEVENT_STRING)) {
105 n = strlen(cp);
106 errno = 0;
107 cpu = strtol(cp + n - 1, NULL, 10);
108
109 if (errno == EINVAL || errno == ERANGE || cpu < 0 || cpu >= TOTAL_CPUS) {
110 return -1;
111 }
112
113 pthread_mutex_lock(&low_power_mode_lock);
114 if (low_power_mode && !freq_set[cpu]) {
115 while (retry) {
116 sysfs_write(cpu_path_min[cpu], LOW_POWER_MIN_FREQ);
117 ret = sysfs_write(cpu_path_max[cpu], LOW_POWER_MAX_FREQ);
118 if (!ret) {
119 freq_set[cpu] = true;
120 break;
121 }
122 usleep(SLEEP_USEC_BETWN_RETRY);
123 retry--;
124 }
125 } else if (!low_power_mode && freq_set[cpu]) {
126 while (retry) {
127 ret = sysfs_write(cpu_path_max[cpu], NORMAL_MAX_FREQ);
128 if (!ret) {
129 freq_set[cpu] = false;
130 break;
131 }
132 usleep(SLEEP_USEC_BETWN_RETRY);
133 retry--;
134 }
135 }
136 pthread_mutex_unlock(&low_power_mode_lock);
137 }
138 return 0;
139}
140
141void *thread_uevent(__attribute__((unused)) void *x)
142{
143 while (1) {
144 int nevents, ret;
145
146 nevents = poll(&pfd, 1, -1);
147
148 if (nevents == -1) {
149 if (errno == EINTR)
150 continue;
151 ALOGE("powerhal: thread_uevent: poll_wait failed\n");
152 break;
153 }
154 ret = uevent_event();
155 if (ret < 0)
156 ALOGE("Error processing the uevent event");
157 }
158 return NULL;
159}
160
161
162static void uevent_init()
163{
164 struct sockaddr_nl client;
165 pthread_t tid;
166 pfd.fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
167
168 if (pfd.fd < 0) {
169 ALOGE("%s: failed to open: %s", __func__, strerror(errno));
170 return;
171 }
172 memset(&client, 0, sizeof(struct sockaddr_nl));
173 pthread_create(&tid, NULL, thread_uevent, NULL);
174 client.nl_family = AF_NETLINK;
175 client.nl_pid = tid;
176 client.nl_groups = -1;
177 pfd.events = POLLIN;
178 bind(pfd.fd, (void *)&client, sizeof(struct sockaddr_nl));
179 return;
180}
181
182static void grouper_power_init( __attribute__((unused)) struct power_module *module)
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700183{
184 /*
185 * cpufreq interactive governor: timer 20ms, min sample 100ms,
186 * hispeed 700MHz at load 40%
187 */
188
189 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate",
Iliyan Malchev0952a3b2014-11-26 01:04:01 -0800190 "50000");
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700191 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time",
Iliyan Malchev0952a3b2014-11-26 01:04:01 -0800192 "500000");
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700193 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load",
Iliyan Malchev0952a3b2014-11-26 01:04:01 -0800194 "75");
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700195 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boost_factor",
196 "0");
197 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/input_boost",
198 "1");
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -0700199 uevent_init();
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700200}
201
Iliyan Malchev0952a3b2014-11-26 01:04:01 -0800202static void grouper_power_set_interactive(__attribute__((unused)) struct power_module *module,
203 __attribute__((unused)) int on)
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700204{
Iliyan Malchev9cb9f902014-11-27 10:16:38 -0800205 if (on) {
206 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load", "75");
207 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/core_lock_period", "3000000");
208 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/core_lock_count", "2");
209 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/input_boost", "1");
210 }
211 else {
212 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load", "85");
213 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/core_lock_period", "200000");
214 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/core_lock_count", "0");
215 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/input_boost", "0");
216 }
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700217}
218
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -0700219static void grouper_power_hint(__attribute__((unused)) struct power_module *module, power_hint_t hint,
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700220 void *data)
221{
222 char buf[80];
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -0700223 int len, cpu, ret;
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700224
225 switch (hint) {
226 case POWER_HINT_VSYNC:
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700227 break;
228
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -0700229 case POWER_HINT_LOW_POWER:
230 pthread_mutex_lock(&low_power_mode_lock);
231 if (data) {
232 low_power_mode = true;
233 for (cpu = 0; cpu < TOTAL_CPUS; cpu++) {
234 sysfs_write(cpu_path_min[cpu], LOW_POWER_MIN_FREQ);
235 ret = sysfs_write(cpu_path_max[cpu], LOW_POWER_MAX_FREQ);
236 if (!ret) {
237 freq_set[cpu] = true;
238 }
239 }
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -0700240 } else {
241 low_power_mode = false;
242 for (cpu = 0; cpu < TOTAL_CPUS; cpu++) {
243 ret = sysfs_write(cpu_path_max[cpu], NORMAL_MAX_FREQ);
244 if (!ret) {
245 freq_set[cpu] = false;
246 }
247 }
Ruchi Kandoic4d70cb2014-06-05 15:50:58 -0700248 }
249 pthread_mutex_unlock(&low_power_mode_lock);
250 break;
Jean-Baptiste Queru671476b2012-05-29 10:40:37 -0700251 default:
252 break;
253 }
254}
255
256static struct hw_module_methods_t power_module_methods = {
257 .open = NULL,
258};
259
260struct power_module HAL_MODULE_INFO_SYM = {
261 .common = {
262 .tag = HARDWARE_MODULE_TAG,
263 .module_api_version = POWER_MODULE_API_VERSION_0_2,
264 .hal_api_version = HARDWARE_HAL_API_VERSION,
265 .id = POWER_HARDWARE_MODULE_ID,
266 .name = "Grouper Power HAL",
267 .author = "The Android Open Source Project",
268 .methods = &power_module_methods,
269 },
270
271 .init = grouper_power_init,
272 .setInteractive = grouper_power_set_interactive,
273 .powerHint = grouper_power_hint,
274};