power_hikey: Add schedtune boosting and fallback am: 4c1a82b68a am: cda4f41e09
am: 99bba671bd

Change-Id: I38826f2ac4178ac3a1f608c0574f60f5e8458a1e
diff --git a/power/power_hikey.c b/power/power_hikey.c
index 9b28e73..2dfe76d 100644
--- a/power/power_hikey.c
+++ b/power/power_hikey.c
@@ -25,6 +25,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include <pthread.h>
+#include <semaphore.h>
 #include <cutils/properties.h>
 //#define LOG_NDEBUG 0
 
@@ -34,6 +36,10 @@
 #include <hardware/hardware.h>
 #include <hardware/power.h>
 
+#define SCHEDTUNE_BOOST_PATH "/sys/fs/cgroup/stune/foreground/schedtune.boost"
+#define SCHEDTUNE_BOOST_NORM "10"
+#define SCHEDTUNE_BOOST_INTERACTIVE "40"
+#define SCHEDTUNE_BOOST_TIME_NS 1000000000LL
 #define INTERACTIVE_BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse"
 #define INTERACTIVE_IO_IS_BUSY_PATH "/sys/devices/system/cpu/cpufreq/interactive/io_is_busy"
 #define CPU_MAX_FREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
@@ -46,8 +52,13 @@
 struct hikey_power_module {
     struct power_module base;
     pthread_mutex_t lock;
+    /* interactive gov boost values */
     int boostpulse_fd;
     int boostpulse_warned;
+    /* EAS schedtune values */
+    int schedtune_boost_fd;
+    long long deboost_time;
+    sem_t signal_lock;
 };
 
 static bool low_power_mode = false;
@@ -81,6 +92,23 @@
     close(fd);
 }
 
+#define NSEC_PER_SEC 1000000000LL
+static long long gettime_ns(void)
+{
+    struct timespec ts;
+
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
+}
+
+static void nanosleep_ns(long long ns)
+{
+    struct timespec ts;
+    ts.tv_sec = ns/NSEC_PER_SEC;
+    ts.tv_nsec = ns%NSEC_PER_SEC;
+    nanosleep(&ts, NULL);
+}
+
 /*[interactive cpufreq gov funcs]*********************************************/
 static void interactive_power_init(struct hikey_power_module __unused *hikey)
 {
@@ -157,13 +185,103 @@
     return 0;
 }
 
-/*[generic functions]*********************************************************/
+/*[schedtune functions]*******************************************************/
 
+int schedtune_sysfs_boost(struct hikey_power_module *hikey, char* booststr)
+{
+    char buf[80];
+    int len;
+
+    if (hikey->schedtune_boost_fd < 0)
+        return hikey->schedtune_boost_fd;
+
+    len = write(hikey->schedtune_boost_fd, booststr, 2);
+    if (len < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error writing to %s: %s\n", SCHEDTUNE_BOOST_PATH, buf);
+    }
+    return len;
+}
+
+static void* schedtune_deboost_thread(void* arg)
+{
+    struct hikey_power_module *hikey = (struct hikey_power_module *)arg;
+
+    while(1) {
+        sem_wait(&hikey->signal_lock);
+        while(1) {
+            long long now, sleeptime = 0;
+
+            pthread_mutex_lock(&hikey->lock);
+            now = gettime_ns();
+            if (hikey->deboost_time > now) {
+                sleeptime = hikey->deboost_time - now;
+                pthread_mutex_unlock(&hikey->lock);
+                nanosleep_ns(sleeptime);
+                continue;
+            }
+
+            schedtune_sysfs_boost(hikey, SCHEDTUNE_BOOST_NORM);
+            hikey->deboost_time = 0;
+            pthread_mutex_unlock(&hikey->lock);
+            break;
+        }
+    }
+    return NULL;
+}
+
+static int schedtune_boost(struct hikey_power_module *hikey)
+{
+    long long now;
+
+    if (hikey->schedtune_boost_fd < 0)
+        return hikey->schedtune_boost_fd;
+
+    now = gettime_ns();
+    if (!hikey->deboost_time) {
+        schedtune_sysfs_boost(hikey, SCHEDTUNE_BOOST_INTERACTIVE);
+        sem_post(&hikey->signal_lock);
+    }
+    hikey->deboost_time = now + SCHEDTUNE_BOOST_TIME_NS;
+
+    return 0;
+}
+
+static void schedtune_power_init(struct hikey_power_module *hikey)
+{
+    char buf[50];
+    pthread_t tid;
+
+
+    hikey->deboost_time = 0;
+    sem_init(&hikey->signal_lock, 0, 1);
+
+    hikey->schedtune_boost_fd = open(SCHEDTUNE_BOOST_PATH, O_WRONLY);
+    if (hikey->schedtune_boost_fd < 0) {
+        strerror_r(errno, buf, sizeof(buf));
+        ALOGE("Error opening %s: %s\n", SCHEDTUNE_BOOST_PATH, buf);
+    }
+
+    pthread_create(&tid, NULL, schedtune_deboost_thread, hikey);
+}
+
+/*[generic functions]*********************************************************/
 static void hikey_power_init(struct power_module __unused *module)
 {
     struct hikey_power_module *hikey = container_of(module,
                                               struct hikey_power_module, base);
     interactive_power_init(hikey);
+    schedtune_power_init(hikey);
+}
+
+static void hikey_hint_interaction(struct hikey_power_module *mod)
+{
+    /* Try interactive cpufreq boosting first */
+    if(!interactive_boostpulse(mod))
+        return;
+    /* Then try EAS schedtune boosting */
+    if(!schedtune_boost(mod))
+        return;
 }
 
 static void hikey_power_hint(struct power_module *module, power_hint_t hint,
@@ -175,7 +293,7 @@
     pthread_mutex_lock(&hikey->lock);
     switch (hint) {
      case POWER_HINT_INTERACTION:
-        interactive_boostpulse(hikey);
+        hikey_hint_interaction(hikey);
         break;
 
    case POWER_HINT_VSYNC: