forked from airend/android_hardware_qcom_power
/
power-helper.c
416 lines (359 loc) · 10.9 KB
/
power-helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <cutils/properties.h>
#include <cutils/uevent.h>
#include <errno.h>
#include <sys/poll.h>
#include <pthread.h>
#include <linux/netlink.h>
#include <stdlib.h>
#include <stdbool.h>
#define LOG_TAG "PowerHAL"
#include <utils/Log.h>
#include <hardware/power.h>
#include "power-helper.h"
#define STATE_ON "state=1"
#define STATE_OFF "state=0"
#define STATE_HDR_ON "state=2"
#define STATE_HDR_OFF "state=3"
#define MAX_LENGTH 50
#define BOOST_SOCKET "/dev/socket/pb"
#define UEVENT_MSG_LEN 2048
#define TOTAL_CPUS 4
#define RETRY_TIME_CHANGING_FREQ 20
#define SLEEP_USEC_BETWN_RETRY 200
#define UEVENT_STRING "online@/devices/system/cpu/"
#if defined(PLATFORM_MSM8960)
#define LOW_POWER_MAX_FREQ "1026000"
#define LOW_POWER_MIN_FREQ "384000"
#define NORMAL_MAX_FREQ "1512000"
#elif defined(PLATFORM_MSM8974)
#define LOW_POWER_MAX_FREQ "729600"
#define LOW_POWER_MIN_FREQ "300000"
#define NORMAL_MAX_FREQ "2265600"
#else
#error "Unknown platform."
#endif
static int client_sockfd;
static struct sockaddr_un client_addr;
static int last_state = -1;
static bool skip_sync_thread;
static bool skip_touch_boost;
static struct pollfd pfd;
static char *cpu_path_min[] = {
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq",
"/sys/devices/system/cpu/cpu1/cpufreq/scaling_min_freq",
"/sys/devices/system/cpu/cpu2/cpufreq/scaling_min_freq",
"/sys/devices/system/cpu/cpu3/cpufreq/scaling_min_freq",
};
static char *cpu_path_max[] = {
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq",
"/sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq",
"/sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq",
"/sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq",
};
static bool freq_set[TOTAL_CPUS];
static bool low_power_mode = false;
static pthread_mutex_t low_power_mode_lock = PTHREAD_MUTEX_INITIALIZER;
static void socket_init()
{
if (!client_sockfd) {
client_sockfd = socket(PF_UNIX, SOCK_DGRAM, 0);
if (client_sockfd < 0) {
ALOGE("%s: failed to open: %s", __func__, strerror(errno));
return;
}
memset(&client_addr, 0, sizeof(struct sockaddr_un));
client_addr.sun_family = AF_UNIX;
snprintf(client_addr.sun_path, UNIX_PATH_MAX, BOOST_SOCKET);
}
}
static int sysfs_write(const char *path, char *s)
{
char buf[80];
int len;
int fd = open(path, O_WRONLY);
if (fd < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error opening %s: %s\n", path, buf);
return -1;
}
len = write(fd, s, strlen(s));
if (len < 0) {
strerror_r(errno, buf, sizeof(buf));
ALOGE("Error writing to %s: %s\n", path, buf);
return -1;
}
close(fd);
return 0;
}
static int uevent_event()
{
char msg[UEVENT_MSG_LEN];
char *cp;
int n, cpu, ret, retry = RETRY_TIME_CHANGING_FREQ;
n = recv(pfd.fd, msg, UEVENT_MSG_LEN, MSG_DONTWAIT);
if (n <= 0) {
return -1;
}
if (n >= UEVENT_MSG_LEN) { /* overflow -- discard */
return -1;
}
cp = msg;
if (strstr(cp, UEVENT_STRING)) {
n = strlen(cp);
errno = 0;
cpu = strtol(cp + n - 1, NULL, 10);
if (errno == EINVAL || errno == ERANGE || cpu < 0 || cpu >= TOTAL_CPUS) {
return -1;
}
pthread_mutex_lock(&low_power_mode_lock);
if (low_power_mode && !freq_set[cpu]) {
while (retry) {
sysfs_write(cpu_path_min[cpu], LOW_POWER_MIN_FREQ);
ret = sysfs_write(cpu_path_max[cpu], LOW_POWER_MAX_FREQ);
if (!ret) {
freq_set[cpu] = true;
break;
}
usleep(SLEEP_USEC_BETWN_RETRY);
retry--;
}
} else if (!low_power_mode && freq_set[cpu]) {
while (retry) {
ret = sysfs_write(cpu_path_max[cpu], NORMAL_MAX_FREQ);
if (!ret) {
freq_set[cpu] = false;
break;
}
usleep(SLEEP_USEC_BETWN_RETRY);
retry--;
}
}
pthread_mutex_unlock(&low_power_mode_lock);
}
return 0;
}
void *thread_uevent(__attribute__((unused)) void *x)
{
while (1) {
int nevents, ret;
nevents = poll(&pfd, 1, -1);
if (nevents == -1) {
if (errno == EINTR)
continue;
ALOGE("powerhal: thread_uevent: poll_wait failed\n");
break;
}
ret = uevent_event();
if (ret < 0)
ALOGE("Error processing the uevent event");
}
return NULL;
}
static void uevent_init()
{
struct sockaddr_nl client;
pthread_t tid;
pfd.fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
if (pfd.fd < 0) {
ALOGE("%s: failed to open: %s", __func__, strerror(errno));
return;
}
memset(&client, 0, sizeof(struct sockaddr_nl));
pthread_create(&tid, NULL, thread_uevent, NULL);
client.nl_family = AF_NETLINK;
client.nl_pid = tid;
client.nl_groups = -1;
pfd.events = POLLIN;
bind(pfd.fd, (void *)&client, sizeof(struct sockaddr_nl));
return;
}
void power_init(void)
{
ALOGI("%s", __func__);
skip_sync_thread = property_get_bool("persist.power.skip_st", false);
ALOGW_IF(skip_sync_thread, "%s: sync thread disabled", __func__);
skip_touch_boost = property_get_bool("persist.power.skip_tb", false);
ALOGW_IF(skip_touch_boost, "%s: touch boost disabled", __func__);
socket_init();
uevent_init();
}
static void sync_thread(int off)
{
int rc;
pid_t client;
char data[MAX_LENGTH];
if (skip_sync_thread) {
return;
}
if (client_sockfd < 0) {
ALOGE("%s: boost socket not created", __func__);
return;
}
client = getpid();
if (!off) {
snprintf(data, MAX_LENGTH, "2:%d", client);
rc = sendto(client_sockfd, data, strlen(data), 0,
(const struct sockaddr *)&client_addr, sizeof(struct sockaddr_un));
} else {
snprintf(data, MAX_LENGTH, "3:%d", client);
rc = sendto(client_sockfd, data, strlen(data), 0,
(const struct sockaddr *)&client_addr, sizeof(struct sockaddr_un));
}
if (rc < 0) {
ALOGE("%s: failed to send: %s", __func__, strerror(errno));
}
}
static void enc_boost(int off)
{
int rc;
pid_t client;
char data[MAX_LENGTH];
if (client_sockfd < 0) {
ALOGE("%s: boost socket not created", __func__);
return;
}
client = getpid();
if (!off) {
snprintf(data, MAX_LENGTH, "5:%d", client);
rc = sendto(client_sockfd, data, strlen(data), 0,
(const struct sockaddr *)&client_addr, sizeof(struct sockaddr_un));
} else {
snprintf(data, MAX_LENGTH, "6:%d", client);
rc = sendto(client_sockfd, data, strlen(data), 0,
(const struct sockaddr *)&client_addr, sizeof(struct sockaddr_un));
}
if (rc < 0) {
ALOGE("%s: failed to send: %s", __func__, strerror(errno));
}
}
static void process_video_encode_hint(void *metadata)
{
socket_init();
if (client_sockfd < 0) {
ALOGE("%s: boost socket not created", __func__);
return;
}
if (metadata) {
if (!strncmp(metadata, STATE_ON, sizeof(STATE_ON))) {
/* Video encode started */
sync_thread(1);
enc_boost(1);
} else if (!strncmp(metadata, STATE_OFF, sizeof(STATE_OFF))) {
/* Video encode stopped */
sync_thread(0);
enc_boost(0);
} else if (!strncmp(metadata, STATE_HDR_ON, sizeof(STATE_HDR_ON))) {
/* HDR usecase started */
} else if (!strncmp(metadata, STATE_HDR_OFF, sizeof(STATE_HDR_OFF))) {
/* HDR usecase stopped */
}else
return;
} else {
return;
}
}
static void touch_boost()
{
int rc;
pid_t client;
char data[MAX_LENGTH];
if (skip_touch_boost) {
return;
}
if (client_sockfd < 0) {
ALOGE("%s: boost socket not created", __func__);
return;
}
client = getpid();
snprintf(data, MAX_LENGTH, "1:%d", client);
rc = sendto(client_sockfd, data, strlen(data), 0,
(const struct sockaddr *)&client_addr, sizeof(struct sockaddr_un));
if (rc < 0) {
ALOGE("%s: failed to send: %s", __func__, strerror(errno));
}
}
void power_set_interactive(int on)
{
if (last_state == -1) {
last_state = on;
} else {
if (last_state == on)
return;
else
last_state = on;
}
ALOGV("%s %s", __func__, (on ? "ON" : "OFF"));
if (on) {
sync_thread(0);
touch_boost();
} else {
sync_thread(1);
}
}
void power_hint(power_hint_t hint, void *data)
{
int cpu, ret;
switch (hint) {
case POWER_HINT_INTERACTION:
case POWER_HINT_LAUNCH:
ALOGV("POWER_HINT_INTERACTION");
touch_boost();
break;
#if 0
case POWER_HINT_VSYNC:
ALOGV("POWER_HINT_VSYNC %s", (data ? "ON" : "OFF"));
break;
#endif
case POWER_HINT_VIDEO_ENCODE:
process_video_encode_hint(data);
break;
case POWER_HINT_LOW_POWER:
pthread_mutex_lock(&low_power_mode_lock);
ALOGI("POWER_HINT_LOW_POWER");
if (data) {
low_power_mode = true;
for (cpu = 0; cpu < TOTAL_CPUS; cpu++) {
sysfs_write(cpu_path_min[cpu], LOW_POWER_MIN_FREQ);
ret = sysfs_write(cpu_path_max[cpu], LOW_POWER_MAX_FREQ);
if (!ret) {
freq_set[cpu] = true;
}
}
} else {
low_power_mode = false;
for (cpu = 0; cpu < TOTAL_CPUS; cpu++) {
ret = sysfs_write(cpu_path_max[cpu], NORMAL_MAX_FREQ);
if (!ret) {
freq_set[cpu] = false;
}
}
}
pthread_mutex_unlock(&low_power_mode_lock);
break;
default:
break;
}
}