blob: 46617025e16988297998675ae9f8a5ad098145c7 [file] [log] [blame]
John Stultz7266c5f2016-06-09 14:18:57 -07001/*
2 * Copyright (C) 2013 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 * Based on the FlounderPowerHAL
17 */
18
19#include <dirent.h>
20#include <errno.h>
21#include <string.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <stdbool.h>
John Stultz4c1a82b2016-08-18 14:49:01 -070028#include <pthread.h>
29#include <semaphore.h>
John Stultz7266c5f2016-06-09 14:18:57 -070030#include <cutils/properties.h>
31//#define LOG_NDEBUG 0
32
33#define LOG_TAG "HiKeyPowerHAL"
34#include <utils/Log.h>
35
36#include <hardware/hardware.h>
37#include <hardware/power.h>
38
John Stultz17e11242017-03-14 18:14:24 -070039#define SCHEDTUNE_BOOST_PATH "/dev/stune/top-app/schedtune.boost"
John Stultz0e785a22017-05-23 13:01:56 -070040#define SCHEDTUNE_BOOST_VAL_PROP "ro.config.schetune.touchboost.value"
41#define SCHEDTUNE_BOOST_TIME_PROP "ro.config.schetune.touchboost.time_ns"
42
43#define SCHEDTUNE_BOOST_VAL_DEFAULT "40"
44
45char schedtune_boost_norm[PROPERTY_VALUE_MAX] = "10";
46char schedtune_boost_interactive[PROPERTY_VALUE_MAX] = SCHEDTUNE_BOOST_VAL_DEFAULT;
47long long schedtune_boost_time_ns = 1000000000LL;
48
John Stultz127e1cc2016-08-18 12:20:40 -070049#define INTERACTIVE_BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse"
50#define INTERACTIVE_IO_IS_BUSY_PATH "/sys/devices/system/cpu/cpufreq/interactive/io_is_busy"
John Stultz7266c5f2016-06-09 14:18:57 -070051
52struct hikey_power_module {
53 struct power_module base;
54 pthread_mutex_t lock;
John Stultz4c1a82b2016-08-18 14:49:01 -070055 /* interactive gov boost values */
John Stultz7266c5f2016-06-09 14:18:57 -070056 int boostpulse_fd;
57 int boostpulse_warned;
John Stultz4c1a82b2016-08-18 14:49:01 -070058 /* EAS schedtune values */
59 int schedtune_boost_fd;
60 long long deboost_time;
61 sem_t signal_lock;
John Stultz7266c5f2016-06-09 14:18:57 -070062};
63
John Stultzed771732017-05-23 17:39:53 -070064
John Stultz7266c5f2016-06-09 14:18:57 -070065static bool low_power_mode = false;
66
John Stultzed771732017-05-23 17:39:53 -070067
68#define CPUFREQ_CLUST_MAX_FREQ_PATH_PROP "ro.config.cpufreq.max_freq.cluster"
69#define CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP "ro.config.cpufreq.low_power_max.cluster"
70#define CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
71
72#define NR_CLUSTERS 4
73static int max_clusters = 1;
74static struct hikey_cpufreq_t {
75 char path[PROPERTY_VALUE_MAX];
76 char normal_max[PROPERTY_VALUE_MAX];
77 char low_power_max[PROPERTY_VALUE_MAX];
78} hikey_cpufreq_clusters[NR_CLUSTERS];
John Stultz7266c5f2016-06-09 14:18:57 -070079
John Stultze150ab32016-08-18 14:04:34 -070080
81#define container_of(addr, struct_name, field_name) \
82 ((struct_name *)((char *)(addr) - offsetof(struct_name, field_name)))
83
84
Dmitry Shmidtcc147582016-11-07 13:13:34 -080085static int sysfs_write(const char *path, char *s)
John Stultz7266c5f2016-06-09 14:18:57 -070086{
87 char buf[80];
88 int len;
89 int fd = open(path, O_WRONLY);
90
91 if (fd < 0) {
92 strerror_r(errno, buf, sizeof(buf));
93 ALOGE("Error opening %s: %s\n", path, buf);
Dmitry Shmidtcc147582016-11-07 13:13:34 -080094 return fd;
John Stultz7266c5f2016-06-09 14:18:57 -070095 }
96
97 len = write(fd, s, strlen(s));
98 if (len < 0) {
99 strerror_r(errno, buf, sizeof(buf));
100 ALOGE("Error writing to %s: %s\n", path, buf);
101 }
102
103 close(fd);
Dmitry Shmidtcc147582016-11-07 13:13:34 -0800104 return len;
John Stultz7266c5f2016-06-09 14:18:57 -0700105}
106
John Stultzed771732017-05-23 17:39:53 -0700107static int sysfs_read(const char *path, char *s, int slen)
108{
109 int len;
110 int fd = open(path, O_RDONLY);
111
112 if (fd < 0) {
113 ALOGE("Error opening %s\n", path);
114 return fd;
115 }
116
117 len = read(fd, s, slen);
118 if (len < 0) {
119 ALOGE("Error reading %s\n", path);
120 }
121
122 close(fd);
123 return len;
124}
125
John Stultz4c1a82b2016-08-18 14:49:01 -0700126#define NSEC_PER_SEC 1000000000LL
127static long long gettime_ns(void)
128{
129 struct timespec ts;
130
131 clock_gettime(CLOCK_MONOTONIC, &ts);
132 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
133}
134
135static void nanosleep_ns(long long ns)
136{
137 struct timespec ts;
138 ts.tv_sec = ns/NSEC_PER_SEC;
139 ts.tv_nsec = ns%NSEC_PER_SEC;
140 nanosleep(&ts, NULL);
141}
142
John Stultzc5793712016-08-18 14:00:49 -0700143/*[interactive cpufreq gov funcs]*********************************************/
John Stultze150ab32016-08-18 14:04:34 -0700144static void interactive_power_init(struct hikey_power_module __unused *hikey)
John Stultz7266c5f2016-06-09 14:18:57 -0700145{
Dmitry Shmidtcc147582016-11-07 13:13:34 -0800146 if (sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate",
147 "20000") < 0)
148 return;
John Stultz7266c5f2016-06-09 14:18:57 -0700149 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_slack",
150 "20000");
151 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time",
152 "80000");
153 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq",
154 "1200000");
155 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load",
156 "99");
157 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/target_loads",
158 "65 729000:75 960000:85");
159 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay",
160 "20000");
161 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration",
162 "1000000");
163 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/io_is_busy", "0");
164
John Stultz7266c5f2016-06-09 14:18:57 -0700165}
166
John Stultz5e1546b2016-08-18 11:56:31 -0700167static int interactive_boostpulse(struct hikey_power_module *hikey)
168{
169 char buf[80];
170 int len;
171
John Stultz64b54ca2016-08-18 15:01:33 -0700172 if (hikey->boostpulse_fd < 0)
John Stultz127e1cc2016-08-18 12:20:40 -0700173 hikey->boostpulse_fd = open(INTERACTIVE_BOOSTPULSE_PATH, O_WRONLY);
John Stultz5e1546b2016-08-18 11:56:31 -0700174
John Stultz64b54ca2016-08-18 15:01:33 -0700175 if (hikey->boostpulse_fd < 0) {
176 if (!hikey->boostpulse_warned) {
John Stultz5e1546b2016-08-18 11:56:31 -0700177 strerror_r(errno, buf, sizeof(buf));
John Stultz127e1cc2016-08-18 12:20:40 -0700178 ALOGE("Error opening %s: %s\n", INTERACTIVE_BOOSTPULSE_PATH,
John Stultz64b54ca2016-08-18 15:01:33 -0700179 buf);
180 hikey->boostpulse_warned = 1;
John Stultz5e1546b2016-08-18 11:56:31 -0700181 }
John Stultz64b54ca2016-08-18 15:01:33 -0700182 return hikey->boostpulse_fd;
183 }
184
185 len = write(hikey->boostpulse_fd, "1", 1);
186 if (len < 0) {
187 strerror_r(errno, buf, sizeof(buf));
188 ALOGE("Error writing to %s: %s\n",
John Stultz127e1cc2016-08-18 12:20:40 -0700189 INTERACTIVE_BOOSTPULSE_PATH, buf);
John Stultz64b54ca2016-08-18 15:01:33 -0700190 return -1;
John Stultz5e1546b2016-08-18 11:56:31 -0700191 }
192 return 0;
193}
194
John Stultz4c1a82b2016-08-18 14:49:01 -0700195/*[schedtune functions]*******************************************************/
John Stultzc5793712016-08-18 14:00:49 -0700196
John Stultz4c1a82b2016-08-18 14:49:01 -0700197int schedtune_sysfs_boost(struct hikey_power_module *hikey, char* booststr)
198{
199 char buf[80];
200 int len;
201
202 if (hikey->schedtune_boost_fd < 0)
203 return hikey->schedtune_boost_fd;
204
John Stultz0e785a22017-05-23 13:01:56 -0700205 len = write(hikey->schedtune_boost_fd, booststr, strlen(booststr));
John Stultz4c1a82b2016-08-18 14:49:01 -0700206 if (len < 0) {
207 strerror_r(errno, buf, sizeof(buf));
208 ALOGE("Error writing to %s: %s\n", SCHEDTUNE_BOOST_PATH, buf);
209 }
210 return len;
211}
212
213static void* schedtune_deboost_thread(void* arg)
214{
215 struct hikey_power_module *hikey = (struct hikey_power_module *)arg;
216
217 while(1) {
218 sem_wait(&hikey->signal_lock);
219 while(1) {
220 long long now, sleeptime = 0;
221
222 pthread_mutex_lock(&hikey->lock);
223 now = gettime_ns();
224 if (hikey->deboost_time > now) {
225 sleeptime = hikey->deboost_time - now;
226 pthread_mutex_unlock(&hikey->lock);
227 nanosleep_ns(sleeptime);
228 continue;
229 }
230
John Stultz0e785a22017-05-23 13:01:56 -0700231 schedtune_sysfs_boost(hikey, schedtune_boost_norm);
John Stultz4c1a82b2016-08-18 14:49:01 -0700232 hikey->deboost_time = 0;
233 pthread_mutex_unlock(&hikey->lock);
234 break;
235 }
236 }
237 return NULL;
238}
239
240static int schedtune_boost(struct hikey_power_module *hikey)
241{
242 long long now;
243
244 if (hikey->schedtune_boost_fd < 0)
245 return hikey->schedtune_boost_fd;
246
247 now = gettime_ns();
248 if (!hikey->deboost_time) {
John Stultz0e785a22017-05-23 13:01:56 -0700249 schedtune_sysfs_boost(hikey, schedtune_boost_interactive);
John Stultz4c1a82b2016-08-18 14:49:01 -0700250 sem_post(&hikey->signal_lock);
251 }
John Stultz0e785a22017-05-23 13:01:56 -0700252 hikey->deboost_time = now + schedtune_boost_time_ns;
John Stultz4c1a82b2016-08-18 14:49:01 -0700253
254 return 0;
255}
256
257static void schedtune_power_init(struct hikey_power_module *hikey)
258{
259 char buf[50];
260 pthread_t tid;
261
John Stultz4c1a82b2016-08-18 14:49:01 -0700262 hikey->deboost_time = 0;
263 sem_init(&hikey->signal_lock, 0, 1);
264
John Stultz0e785a22017-05-23 13:01:56 -0700265 hikey->schedtune_boost_fd = open(SCHEDTUNE_BOOST_PATH, O_RDWR);
John Stultz4c1a82b2016-08-18 14:49:01 -0700266 if (hikey->schedtune_boost_fd < 0) {
267 strerror_r(errno, buf, sizeof(buf));
268 ALOGE("Error opening %s: %s\n", SCHEDTUNE_BOOST_PATH, buf);
John Stultz0e785a22017-05-23 13:01:56 -0700269 return;
John Stultz4c1a82b2016-08-18 14:49:01 -0700270 }
271
John Stultz0e785a22017-05-23 13:01:56 -0700272 schedtune_boost_time_ns = property_get_int64(SCHEDTUNE_BOOST_TIME_PROP,
273 1000000000LL);
274 property_get(SCHEDTUNE_BOOST_VAL_PROP, schedtune_boost_interactive,
275 SCHEDTUNE_BOOST_VAL_DEFAULT);
276
277 if (hikey->schedtune_boost_fd >= 0) {
278 size_t len = read(hikey->schedtune_boost_fd, schedtune_boost_norm,
279 PROPERTY_VALUE_MAX);
280 if (len <= 0)
281 ALOGE("Error reading normal boost value\n");
282 else if (schedtune_boost_norm[len] == '\n')
283 schedtune_boost_norm[len] = '\0';
284
285 }
286
287 ALOGV("Starting with schedtune boost norm: %s touchboost: %s and boosttime: %lld\n",
288 schedtune_boost_norm, schedtune_boost_interactive, schedtune_boost_time_ns);
289
John Stultz4c1a82b2016-08-18 14:49:01 -0700290 pthread_create(&tid, NULL, schedtune_deboost_thread, hikey);
291}
292
293/*[generic functions]*********************************************************/
John Stultzed771732017-05-23 17:39:53 -0700294
295static void hikey_cpufreq_set_interactive(struct power_module __unused *module, int on)
296{
297 int i;
298
299 /*
300 * Lower maximum frequency when screen is off.
301 */
302 for (i=0; i < max_clusters; i++) {
303 if ((!on || low_power_mode) && hikey_cpufreq_clusters[i].low_power_max[0] != '\0')
304 sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].low_power_max);
305 else
306 sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].normal_max);
307 }
308 sysfs_write(INTERACTIVE_IO_IS_BUSY_PATH, on ? "1" : "0");
309}
310
311
Dmitry Shmidtb4db15e2017-10-24 16:02:26 -0700312static void hikey_cpufreq_init(struct hikey_power_module __unused *hikey)
John Stultzed771732017-05-23 17:39:53 -0700313{
314 char buf[128];
315 int len, i;
316
317 for (i=0; i < NR_CLUSTERS; i++) {
318 sprintf(buf,"%s%d", CPUFREQ_CLUST_MAX_FREQ_PATH_PROP, i);
319 property_get(buf, hikey_cpufreq_clusters[i].path, "");
320
321 if (hikey_cpufreq_clusters[i].path[0] == '\0') {
322 if (i == 0) {
323 /* In case no property was set, pick cpu0's cluster */
324 strncpy(hikey_cpufreq_clusters[i].path,
325 CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT,
326 PROPERTY_VALUE_MAX);
327 } else
328 break;
329 }
330 sprintf(buf,"%s%d", CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP, i);
331 property_get(buf, hikey_cpufreq_clusters[i].low_power_max, "");
332 len = sysfs_read(hikey_cpufreq_clusters[i].path,
333 hikey_cpufreq_clusters[i].normal_max,
334 PROPERTY_VALUE_MAX);
335 ALOGV("Cluster: %d path: %s low: %s norm: %s\n", i,
336 hikey_cpufreq_clusters[i].path,
337 hikey_cpufreq_clusters[i].low_power_max,
338 hikey_cpufreq_clusters[i].normal_max);
339 }
340 max_clusters = i;
341}
342
343
John Stultzc5793712016-08-18 14:00:49 -0700344static void hikey_power_init(struct power_module __unused *module)
345{
John Stultze150ab32016-08-18 14:04:34 -0700346 struct hikey_power_module *hikey = container_of(module,
347 struct hikey_power_module, base);
John Stultzed771732017-05-23 17:39:53 -0700348 hikey_cpufreq_init(hikey);
John Stultze150ab32016-08-18 14:04:34 -0700349 interactive_power_init(hikey);
John Stultz4c1a82b2016-08-18 14:49:01 -0700350 schedtune_power_init(hikey);
351}
352
353static void hikey_hint_interaction(struct hikey_power_module *mod)
354{
355 /* Try interactive cpufreq boosting first */
356 if(!interactive_boostpulse(mod))
357 return;
358 /* Then try EAS schedtune boosting */
359 if(!schedtune_boost(mod))
360 return;
John Stultzc5793712016-08-18 14:00:49 -0700361}
362
John Stultz7266c5f2016-06-09 14:18:57 -0700363static void hikey_power_hint(struct power_module *module, power_hint_t hint,
364 void *data)
365{
John Stultze150ab32016-08-18 14:04:34 -0700366 struct hikey_power_module *hikey = container_of(module,
367 struct hikey_power_module, base);
John Stultz7266c5f2016-06-09 14:18:57 -0700368
369 pthread_mutex_lock(&hikey->lock);
370 switch (hint) {
371 case POWER_HINT_INTERACTION:
John Stultz4c1a82b2016-08-18 14:49:01 -0700372 hikey_hint_interaction(hikey);
John Stultz7266c5f2016-06-09 14:18:57 -0700373 break;
374
375 case POWER_HINT_VSYNC:
376 break;
377
378 case POWER_HINT_LOW_POWER:
John Stultz7266c5f2016-06-09 14:18:57 -0700379 low_power_mode = data;
John Stultzed771732017-05-23 17:39:53 -0700380 hikey_cpufreq_set_interactive(module, 1);
John Stultz7266c5f2016-06-09 14:18:57 -0700381 break;
382
383 default:
384 break;
385 }
386 pthread_mutex_unlock(&hikey->lock);
387}
388
John Stultzab1f6a72016-08-18 12:28:55 -0700389static void set_feature(struct power_module *module, feature_t feature, int state)
390{
John Stultze150ab32016-08-18 14:04:34 -0700391 struct hikey_power_module *hikey = container_of(module,
392 struct hikey_power_module, base);
John Stultzab1f6a72016-08-18 12:28:55 -0700393 switch (feature) {
394 default:
Dmitry Shmidt0ea8f4d2016-11-07 12:55:16 -0800395 ALOGW("Error setting the feature %d and state %d, it doesn't exist\n",
396 feature, state);
John Stultzab1f6a72016-08-18 12:28:55 -0700397 break;
398 }
399}
400
Dmitry Shmidt0ea8f4d2016-11-07 12:55:16 -0800401static int power_open(const hw_module_t* __unused module, const char* name,
Ruchi Kandoi96c621c2016-11-03 12:46:10 -0700402 hw_device_t** device)
403{
404 int retval = 0; /* 0 is ok; -1 is error */
405 ALOGD("%s: enter; name=%s", __FUNCTION__, name);
406
407 if (strcmp(name, POWER_HARDWARE_MODULE_ID) == 0) {
John Stultz5af9bdc2017-11-02 21:23:22 -0700408 struct hikey_power_module *dev = (struct hikey_power_module *)calloc(1,
409 sizeof(struct hikey_power_module));
Ruchi Kandoi96c621c2016-11-03 12:46:10 -0700410
411 if (dev) {
412 /* Common hw_device_t fields */
John Stultz5af9bdc2017-11-02 21:23:22 -0700413 dev->base.common.tag = HARDWARE_DEVICE_TAG;
414 dev->base.common.module_api_version = POWER_MODULE_API_VERSION_0_5;
415 dev->base.common.hal_api_version = HARDWARE_HAL_API_VERSION;
Ruchi Kandoi96c621c2016-11-03 12:46:10 -0700416
John Stultz5af9bdc2017-11-02 21:23:22 -0700417 dev->base.init = hikey_power_init;
418 dev->base.powerHint = hikey_power_hint;
419 dev->base.setInteractive = hikey_cpufreq_set_interactive;
420 dev->base.setFeature = set_feature;
Ruchi Kandoi96c621c2016-11-03 12:46:10 -0700421
John Stultz5af9bdc2017-11-02 21:23:22 -0700422 pthread_mutex_init(&dev->lock, NULL);
423 dev->boostpulse_fd = -1;
424 dev->boostpulse_warned = 0;
425
426 *device = (hw_device_t*)&dev->base;
Ruchi Kandoi96c621c2016-11-03 12:46:10 -0700427 } else
428 retval = -ENOMEM;
429 } else {
430 retval = -EINVAL;
431 }
432
433 ALOGD("%s: exit %d", __FUNCTION__, retval);
434 return retval;
435}
436
John Stultz7266c5f2016-06-09 14:18:57 -0700437static struct hw_module_methods_t power_module_methods = {
Ruchi Kandoi96c621c2016-11-03 12:46:10 -0700438 .open = power_open,
John Stultz7266c5f2016-06-09 14:18:57 -0700439};
440
441struct hikey_power_module HAL_MODULE_INFO_SYM = {
Dmitry Shmidt0ea8f4d2016-11-07 12:55:16 -0800442 .base = {
443 .common = {
444 .tag = HARDWARE_MODULE_TAG,
445 .module_api_version = POWER_MODULE_API_VERSION_0_2,
446 .hal_api_version = HARDWARE_HAL_API_VERSION,
447 .id = POWER_HARDWARE_MODULE_ID,
448 .name = "HiKey Power HAL",
449 .author = "The Android Open Source Project",
450 .methods = &power_module_methods,
John Stultz7266c5f2016-06-09 14:18:57 -0700451 },
John Stultz7266c5f2016-06-09 14:18:57 -0700452 },
John Stultz7266c5f2016-06-09 14:18:57 -0700453};