1 /*
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>
28 #include <pthread.h>
29 #include <semaphore.h>
30 #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
39 #define SCHEDTUNE_BOOST_PATH "/dev/stune/top-app/schedtune.boost"
40 #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
45 char schedtune_boost_norm[PROPERTY_VALUE_MAX] = "10";
46 char schedtune_boost_interactive[PROPERTY_VALUE_MAX] = SCHEDTUNE_BOOST_VAL_DEFAULT;
47 long long schedtune_boost_time_ns = 1000000000LL;
48
49 #define DEVFREQ_DDR_MIN_FREQ_PATH_PROP \
50 "ro.config.devfreq.ddr.min_freq.path"
51 #define DEVFREQ_DDR_MIN_FREQ_BOOST_PROP \
52 "ro.config.devfreq.ddr.min_freq.boost"
53
54 char devfreq_ddr_min_path[PROPERTY_VALUE_MAX];
55 char devfreq_ddr_min_orig[PROPERTY_VALUE_MAX];
56 char devfreq_ddr_min_boost[PROPERTY_VALUE_MAX];
57
58 #define DEVFREQ_GPU_MIN_FREQ_PATH_PROP \
59 "ro.config.devfreq.gpu.min_freq.path"
60 #define DEVFREQ_GPU_MIN_FREQ_BOOST_PROP \
61 "ro.config.devfreq.gpu.min_freq.boost"
62
63 char devfreq_gpu_min_path[PROPERTY_VALUE_MAX];
64 char devfreq_gpu_min_orig[PROPERTY_VALUE_MAX];
65 char devfreq_gpu_min_boost[PROPERTY_VALUE_MAX];
66
67 #define INTERACTIVE_BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse"
68 #define INTERACTIVE_IO_IS_BUSY_PATH "/sys/devices/system/cpu/cpufreq/interactive/io_is_busy"
69
70 struct hikey_power_module {
71 struct power_module base;
72 pthread_mutex_t lock;
73 /* interactive gov boost values */
74 int boostpulse_fd;
75 int boostpulse_warned;
76 /* EAS schedtune values */
77 int schedtune_boost_fd;
78 long long deboost_time;
79 sem_t signal_lock;
80 };
81
82
83 static bool low_power_mode = false;
84
85
86 #define CPUFREQ_CLUST_MAX_FREQ_PATH_PROP "ro.config.cpufreq.max_freq.cluster"
87 #define CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP "ro.config.cpufreq.low_power_max.cluster"
88 #define CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
89
90 #define NR_CLUSTERS 4
91 static int max_clusters = 1;
92 static struct hikey_cpufreq_t {
93 char path[PROPERTY_VALUE_MAX];
94 char normal_max[PROPERTY_VALUE_MAX];
95 char low_power_max[PROPERTY_VALUE_MAX];
96 } hikey_cpufreq_clusters[NR_CLUSTERS];
97
98 #define container_of(addr, struct_name, field_name) \
99 ((struct_name *)((char *)(addr) - offsetof(struct_name, field_name)))
100
101
sysfs_write(const char * path,char * s)102 static int sysfs_write(const char *path, char *s)
103 {
104 char buf[80];
105 int len;
106 int fd = open(path, O_WRONLY);
107
108 if (fd < 0) {
109 strerror_r(errno, buf, sizeof(buf));
110 ALOGE("Error opening %s: %s\n", path, buf);
111 return fd;
112 }
113
114 len = write(fd, s, strlen(s));
115 if (len < 0) {
116 strerror_r(errno, buf, sizeof(buf));
117 ALOGE("Error writing to %s: %s\n", path, buf);
118 }
119
120 close(fd);
121 return len;
122 }
123
sysfs_read(const char * path,char * s,int slen)124 static int sysfs_read(const char *path, char *s, int slen)
125 {
126 int len;
127 int fd = open(path, O_RDONLY);
128
129 if (fd < 0) {
130 ALOGE("Error opening %s\n", path);
131 return fd;
132 }
133
134 len = read(fd, s, slen);
135 if (len < 0) {
136 ALOGE("Error reading %s\n", path);
137 }
138
139 close(fd);
140 return len;
141 }
142
143 #define NSEC_PER_SEC 1000000000LL
gettime_ns(void)144 static long long gettime_ns(void)
145 {
146 struct timespec ts;
147
148 clock_gettime(CLOCK_MONOTONIC, &ts);
149 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
150 }
151
nanosleep_ns(long long ns)152 static void nanosleep_ns(long long ns)
153 {
154 struct timespec ts;
155 ts.tv_sec = ns/NSEC_PER_SEC;
156 ts.tv_nsec = ns%NSEC_PER_SEC;
157 nanosleep(&ts, NULL);
158 }
159
160 /*[interactive cpufreq gov funcs]*********************************************/
interactive_power_init(struct hikey_power_module __unused * hikey)161 static void interactive_power_init(struct hikey_power_module __unused *hikey)
162 {
163 if (sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate",
164 "20000") < 0)
165 return;
166 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_slack",
167 "20000");
168 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time",
169 "80000");
170 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq",
171 "1200000");
172 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load",
173 "99");
174 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/target_loads",
175 "65 729000:75 960000:85");
176 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay",
177 "20000");
178 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration",
179 "1000000");
180 sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/io_is_busy", "0");
181
182 }
183
interactive_boostpulse(struct hikey_power_module * hikey)184 static int interactive_boostpulse(struct hikey_power_module *hikey)
185 {
186 char buf[80];
187 int len;
188
189 if (hikey->boostpulse_fd < 0)
190 hikey->boostpulse_fd = open(INTERACTIVE_BOOSTPULSE_PATH, O_WRONLY);
191
192 if (hikey->boostpulse_fd < 0) {
193 if (!hikey->boostpulse_warned) {
194 strerror_r(errno, buf, sizeof(buf));
195 ALOGE("Error opening %s: %s\n", INTERACTIVE_BOOSTPULSE_PATH,
196 buf);
197 hikey->boostpulse_warned = 1;
198 }
199 return hikey->boostpulse_fd;
200 }
201
202 len = write(hikey->boostpulse_fd, "1", 1);
203 if (len < 0) {
204 strerror_r(errno, buf, sizeof(buf));
205 ALOGE("Error writing to %s: %s\n",
206 INTERACTIVE_BOOSTPULSE_PATH, buf);
207 return -1;
208 }
209 return 0;
210 }
211
212 static void
hikey_devfreq_set_interactive(struct hikey_power_module __unused * hikey,int on)213 hikey_devfreq_set_interactive(struct hikey_power_module __unused *hikey, int on)
214 {
215 if (!on || low_power_mode) {
216 if (devfreq_ddr_min_path[0] != '\0')
217 sysfs_write(devfreq_ddr_min_path, devfreq_ddr_min_orig);
218
219 if (devfreq_gpu_min_path[0] != '\0')
220 sysfs_write(devfreq_gpu_min_path, devfreq_gpu_min_orig);
221 } else {
222 if (devfreq_ddr_min_path[0] != '\0')
223 sysfs_write(devfreq_ddr_min_path, devfreq_ddr_min_boost);
224
225 if (devfreq_gpu_min_path[0] != '\0')
226 sysfs_write(devfreq_gpu_min_path, devfreq_gpu_min_boost);
227 }
228 }
229
hikey_devfreq_init(struct hikey_power_module __unused * hikey)230 static void hikey_devfreq_init(struct hikey_power_module __unused *hikey)
231 {
232 property_get(DEVFREQ_DDR_MIN_FREQ_PATH_PROP, devfreq_ddr_min_path, "");
233 if (devfreq_ddr_min_path[0] != '\0') {
234 sysfs_read(devfreq_ddr_min_path, devfreq_ddr_min_orig,
235 PROPERTY_VALUE_MAX);
236 property_get(DEVFREQ_DDR_MIN_FREQ_BOOST_PROP,
237 devfreq_ddr_min_boost, "");
238 }
239
240 property_get(DEVFREQ_GPU_MIN_FREQ_PATH_PROP, devfreq_gpu_min_path, "");
241 if (devfreq_gpu_min_path[0] != '\0') {
242 sysfs_read(devfreq_gpu_min_path, devfreq_gpu_min_orig,
243 PROPERTY_VALUE_MAX);
244 property_get(DEVFREQ_GPU_MIN_FREQ_BOOST_PROP,
245 devfreq_gpu_min_boost, "");
246 }
247 }
248
249 /*[schedtune functions]*******************************************************/
250
schedtune_sysfs_boost(struct hikey_power_module * hikey,char * booststr)251 int schedtune_sysfs_boost(struct hikey_power_module *hikey, char* booststr)
252 {
253 char buf[80];
254 int len;
255
256 if (hikey->schedtune_boost_fd < 0)
257 return hikey->schedtune_boost_fd;
258
259 len = write(hikey->schedtune_boost_fd, booststr, strlen(booststr));
260 if (len < 0) {
261 strerror_r(errno, buf, sizeof(buf));
262 ALOGE("Error writing to %s: %s\n", SCHEDTUNE_BOOST_PATH, buf);
263 }
264 return len;
265 }
266
schedtune_deboost_thread(void * arg)267 static void* schedtune_deboost_thread(void* arg)
268 {
269 struct hikey_power_module *hikey = (struct hikey_power_module *)arg;
270
271 while(1) {
272 sem_wait(&hikey->signal_lock);
273 while(1) {
274 long long now, sleeptime = 0;
275
276 pthread_mutex_lock(&hikey->lock);
277 now = gettime_ns();
278 if (hikey->deboost_time > now) {
279 sleeptime = hikey->deboost_time - now;
280 pthread_mutex_unlock(&hikey->lock);
281 nanosleep_ns(sleeptime);
282 continue;
283 }
284
285 schedtune_sysfs_boost(hikey, schedtune_boost_norm);
286 hikey_devfreq_set_interactive(hikey, 0);
287 hikey->deboost_time = 0;
288 pthread_mutex_unlock(&hikey->lock);
289 break;
290 }
291 }
292 return NULL;
293 }
294
schedtune_boost(struct hikey_power_module * hikey)295 static int schedtune_boost(struct hikey_power_module *hikey)
296 {
297 long long now;
298
299 if (hikey->schedtune_boost_fd < 0)
300 return hikey->schedtune_boost_fd;
301
302 now = gettime_ns();
303 if (!hikey->deboost_time) {
304 schedtune_sysfs_boost(hikey, schedtune_boost_interactive);
305 hikey_devfreq_set_interactive(hikey, 1);
306 sem_post(&hikey->signal_lock);
307 }
308 hikey->deboost_time = now + schedtune_boost_time_ns;
309
310 return 0;
311 }
312
schedtune_power_init(struct hikey_power_module * hikey)313 static void schedtune_power_init(struct hikey_power_module *hikey)
314 {
315 char buf[50];
316 pthread_t tid;
317
318 hikey->deboost_time = 0;
319 sem_init(&hikey->signal_lock, 0, 1);
320
321 hikey->schedtune_boost_fd = open(SCHEDTUNE_BOOST_PATH, O_RDWR);
322 if (hikey->schedtune_boost_fd < 0) {
323 strerror_r(errno, buf, sizeof(buf));
324 ALOGE("Error opening %s: %s\n", SCHEDTUNE_BOOST_PATH, buf);
325 return;
326 }
327
328 schedtune_boost_time_ns = property_get_int64(SCHEDTUNE_BOOST_TIME_PROP,
329 1000000000LL);
330 property_get(SCHEDTUNE_BOOST_VAL_PROP, schedtune_boost_interactive,
331 SCHEDTUNE_BOOST_VAL_DEFAULT);
332
333 if (hikey->schedtune_boost_fd >= 0) {
334 size_t len = read(hikey->schedtune_boost_fd, schedtune_boost_norm,
335 PROPERTY_VALUE_MAX);
336 if (len <= 0)
337 ALOGE("Error reading normal boost value\n");
338 else if (schedtune_boost_norm[len] == '\n')
339 schedtune_boost_norm[len] = '\0';
340
341 }
342
343 ALOGV("Starting with schedtune boost norm: %s touchboost: %s and boosttime: %lld\n",
344 schedtune_boost_norm, schedtune_boost_interactive, schedtune_boost_time_ns);
345
346 pthread_create(&tid, NULL, schedtune_deboost_thread, hikey);
347 }
348
349 /*[generic functions]*********************************************************/
350
hikey_cpufreq_set_interactive(struct power_module __unused * module,int on)351 static void hikey_cpufreq_set_interactive(struct power_module __unused *module, int on)
352 {
353 int i;
354
355 /*
356 * Lower maximum frequency when screen is off.
357 */
358 for (i=0; i < max_clusters; i++) {
359 if ((!on || low_power_mode) && hikey_cpufreq_clusters[i].low_power_max[0] != '\0')
360 sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].low_power_max);
361 else
362 sysfs_write(hikey_cpufreq_clusters[i].path, hikey_cpufreq_clusters[i].normal_max);
363 }
364 sysfs_write(INTERACTIVE_IO_IS_BUSY_PATH, on ? "1" : "0");
365 }
366
367
hikey_cpufreq_init(struct hikey_power_module __unused * hikey)368 static void hikey_cpufreq_init(struct hikey_power_module __unused *hikey)
369 {
370 char buf[128];
371 int len, i;
372
373 for (i=0; i < NR_CLUSTERS; i++) {
374 sprintf(buf,"%s%d", CPUFREQ_CLUST_MAX_FREQ_PATH_PROP, i);
375 property_get(buf, hikey_cpufreq_clusters[i].path, "");
376
377 if (hikey_cpufreq_clusters[i].path[0] == '\0') {
378 if (i == 0) {
379 /* In case no property was set, pick cpu0's cluster */
380 strncpy(hikey_cpufreq_clusters[i].path,
381 CPUFREQ_CLUST0_MAX_FREQ_PATH_DEFAULT,
382 PROPERTY_VALUE_MAX);
383 } else
384 break;
385 }
386 sprintf(buf,"%s%d", CPUFREQ_CLUST_LOW_POWER_MAX_FREQ_PROP, i);
387 property_get(buf, hikey_cpufreq_clusters[i].low_power_max, "");
388 len = sysfs_read(hikey_cpufreq_clusters[i].path,
389 hikey_cpufreq_clusters[i].normal_max,
390 PROPERTY_VALUE_MAX);
391 ALOGV("Cluster: %d path: %s low: %s norm: %s\n", i,
392 hikey_cpufreq_clusters[i].path,
393 hikey_cpufreq_clusters[i].low_power_max,
394 hikey_cpufreq_clusters[i].normal_max);
395 }
396 max_clusters = i;
397 }
398
hikey_power_init(struct power_module __unused * module)399 static void hikey_power_init(struct power_module __unused *module)
400 {
401 struct hikey_power_module *hikey = container_of(module,
402 struct hikey_power_module, base);
403 hikey_cpufreq_init(hikey);
404 hikey_devfreq_init(hikey);
405 interactive_power_init(hikey);
406 schedtune_power_init(hikey);
407 }
408
hikey_hint_interaction(struct hikey_power_module * mod)409 static void hikey_hint_interaction(struct hikey_power_module *mod)
410 {
411 /* Try interactive cpufreq boosting first */
412 if(!interactive_boostpulse(mod))
413 return;
414 /* Then try EAS schedtune boosting */
415 if(!schedtune_boost(mod))
416 return;
417 }
418
hikey_power_hint(struct power_module * module,power_hint_t hint,void * data)419 static void hikey_power_hint(struct power_module *module, power_hint_t hint,
420 void *data)
421 {
422 struct hikey_power_module *hikey = container_of(module,
423 struct hikey_power_module, base);
424
425 pthread_mutex_lock(&hikey->lock);
426 switch (hint) {
427 case POWER_HINT_INTERACTION:
428 hikey_hint_interaction(hikey);
429 break;
430
431 case POWER_HINT_VSYNC:
432 break;
433
434 case POWER_HINT_LOW_POWER:
435 low_power_mode = data;
436 hikey_cpufreq_set_interactive(module, 1);
437 break;
438
439 default:
440 break;
441 }
442 pthread_mutex_unlock(&hikey->lock);
443 }
444
set_feature(struct power_module __unused * module,feature_t feature,int state)445 static void set_feature(struct power_module __unused *module,
446 feature_t feature, int state)
447 {
448 switch (feature) {
449 default:
450 ALOGW("Error setting the feature %d and state %d, it doesn't exist\n",
451 feature, state);
452 break;
453 }
454 }
455
power_open(const hw_module_t * __unused module,const char * name,hw_device_t ** device)456 static int power_open(const hw_module_t* __unused module, const char* name,
457 hw_device_t** device)
458 {
459 int retval = 0; /* 0 is ok; -1 is error */
460 ALOGD("%s: enter; name=%s", __FUNCTION__, name);
461
462 if (strcmp(name, POWER_HARDWARE_MODULE_ID) == 0) {
463 struct hikey_power_module *dev = (struct hikey_power_module *)calloc(1,
464 sizeof(struct hikey_power_module));
465
466 if (dev) {
467 /* Common hw_device_t fields */
468 dev->base.common.tag = HARDWARE_DEVICE_TAG;
469 dev->base.common.module_api_version = POWER_MODULE_API_VERSION_0_5;
470 dev->base.common.hal_api_version = HARDWARE_HAL_API_VERSION;
471
472 dev->base.init = hikey_power_init;
473 dev->base.powerHint = hikey_power_hint;
474 dev->base.setInteractive = hikey_cpufreq_set_interactive;
475 dev->base.setFeature = set_feature;
476
477 pthread_mutex_init(&dev->lock, NULL);
478 dev->boostpulse_fd = -1;
479 dev->boostpulse_warned = 0;
480
481 *device = (hw_device_t*)&dev->base;
482 } else
483 retval = -ENOMEM;
484 } else {
485 retval = -EINVAL;
486 }
487
488 ALOGD("%s: exit %d", __FUNCTION__, retval);
489 return retval;
490 }
491
492 static struct hw_module_methods_t power_module_methods = {
493 .open = power_open,
494 };
495
496 struct hikey_power_module HAL_MODULE_INFO_SYM = {
497 .base = {
498 .common = {
499 .tag = HARDWARE_MODULE_TAG,
500 .module_api_version = POWER_MODULE_API_VERSION_0_2,
501 .hal_api_version = HARDWARE_HAL_API_VERSION,
502 .id = POWER_HARDWARE_MODULE_ID,
503 .name = "HiKey Power HAL",
504 .author = "The Android Open Source Project",
505 .methods = &power_module_methods,
506 },
507 },
508 };
509