John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 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> |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 28 | #include <pthread.h> |
| 29 | #include <semaphore.h> |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 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 | |
John Stultz | 884c713 | 2016-08-24 20:19:35 -0700 | [diff] [blame] | 39 | #define SCHEDTUNE_BOOST_PATH "/dev/stune/foreground/schedtune.boost" |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 40 | #define SCHEDTUNE_BOOST_NORM "10" |
| 41 | #define SCHEDTUNE_BOOST_INTERACTIVE "40" |
| 42 | #define SCHEDTUNE_BOOST_TIME_NS 1000000000LL |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 43 | #define INTERACTIVE_BOOSTPULSE_PATH "/sys/devices/system/cpu/cpufreq/interactive/boostpulse" |
| 44 | #define INTERACTIVE_IO_IS_BUSY_PATH "/sys/devices/system/cpu/cpufreq/interactive/io_is_busy" |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 45 | #define CPU_MAX_FREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 46 | #define LOW_POWER_MAX_FREQ "729000" |
| 47 | #define NORMAL_MAX_FREQ "1200000" |
| 48 | #define SVELTE_PROP "ro.boot.svelte" |
| 49 | #define SVELTE_MAX_FREQ_PROP "ro.config.svelte.max_cpu_freq" |
| 50 | #define SVELTE_LOW_POWER_MAX_FREQ_PROP "ro.config.svelte.low_power_max_cpu_freq" |
| 51 | |
| 52 | struct hikey_power_module { |
| 53 | struct power_module base; |
| 54 | pthread_mutex_t lock; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 55 | /* interactive gov boost values */ |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 56 | int boostpulse_fd; |
| 57 | int boostpulse_warned; |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 58 | /* EAS schedtune values */ |
| 59 | int schedtune_boost_fd; |
| 60 | long long deboost_time; |
| 61 | sem_t signal_lock; |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static bool low_power_mode = false; |
| 65 | |
| 66 | static char *max_cpu_freq = NORMAL_MAX_FREQ; |
| 67 | static char *low_power_max_cpu_freq = LOW_POWER_MAX_FREQ; |
| 68 | |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 69 | |
| 70 | #define container_of(addr, struct_name, field_name) \ |
| 71 | ((struct_name *)((char *)(addr) - offsetof(struct_name, field_name))) |
| 72 | |
| 73 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 74 | static void sysfs_write(const char *path, char *s) |
| 75 | { |
| 76 | char buf[80]; |
| 77 | int len; |
| 78 | int fd = open(path, O_WRONLY); |
| 79 | |
| 80 | if (fd < 0) { |
| 81 | strerror_r(errno, buf, sizeof(buf)); |
| 82 | ALOGE("Error opening %s: %s\n", path, buf); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | len = write(fd, s, strlen(s)); |
| 87 | if (len < 0) { |
| 88 | strerror_r(errno, buf, sizeof(buf)); |
| 89 | ALOGE("Error writing to %s: %s\n", path, buf); |
| 90 | } |
| 91 | |
| 92 | close(fd); |
| 93 | } |
| 94 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 95 | #define NSEC_PER_SEC 1000000000LL |
| 96 | static long long gettime_ns(void) |
| 97 | { |
| 98 | struct timespec ts; |
| 99 | |
| 100 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 101 | return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec; |
| 102 | } |
| 103 | |
| 104 | static void nanosleep_ns(long long ns) |
| 105 | { |
| 106 | struct timespec ts; |
| 107 | ts.tv_sec = ns/NSEC_PER_SEC; |
| 108 | ts.tv_nsec = ns%NSEC_PER_SEC; |
| 109 | nanosleep(&ts, NULL); |
| 110 | } |
| 111 | |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 112 | /*[interactive cpufreq gov funcs]*********************************************/ |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 113 | static void interactive_power_init(struct hikey_power_module __unused *hikey) |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 114 | { |
John Stultz | 65de27c | 2016-08-18 11:50:42 -0700 | [diff] [blame] | 115 | int32_t is_svelte = property_get_int32(SVELTE_PROP, 0); |
| 116 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 117 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_rate", |
| 118 | "20000"); |
| 119 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/timer_slack", |
| 120 | "20000"); |
| 121 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/min_sample_time", |
| 122 | "80000"); |
| 123 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/hispeed_freq", |
| 124 | "1200000"); |
| 125 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load", |
| 126 | "99"); |
| 127 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/target_loads", |
| 128 | "65 729000:75 960000:85"); |
| 129 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay", |
| 130 | "20000"); |
| 131 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/boostpulse_duration", |
| 132 | "1000000"); |
| 133 | sysfs_write("/sys/devices/system/cpu/cpufreq/interactive/io_is_busy", "0"); |
| 134 | |
John Stultz | 65de27c | 2016-08-18 11:50:42 -0700 | [diff] [blame] | 135 | if (is_svelte) { |
| 136 | char prop_buffer[PROPERTY_VALUE_MAX]; |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 137 | int len = property_get(SVELTE_MAX_FREQ_PROP, prop_buffer, |
| 138 | LOW_POWER_MAX_FREQ); |
John Stultz | 65de27c | 2016-08-18 11:50:42 -0700 | [diff] [blame] | 139 | |
| 140 | max_cpu_freq = strndup(prop_buffer, len); |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 141 | len = property_get(SVELTE_LOW_POWER_MAX_FREQ_PROP, prop_buffer, |
| 142 | LOW_POWER_MAX_FREQ); |
John Stultz | 65de27c | 2016-08-18 11:50:42 -0700 | [diff] [blame] | 143 | low_power_max_cpu_freq = strndup(prop_buffer, len); |
| 144 | } |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void power_set_interactive(struct power_module __unused *module, int on) |
| 148 | { |
| 149 | ALOGV("power_set_interactive: %d\n", on); |
| 150 | |
| 151 | /* |
| 152 | * Lower maximum frequency when screen is off. |
| 153 | */ |
| 154 | sysfs_write(CPU_MAX_FREQ_PATH, |
| 155 | (!on || low_power_mode) ? low_power_max_cpu_freq : max_cpu_freq); |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 156 | sysfs_write(INTERACTIVE_IO_IS_BUSY_PATH, on ? "1" : "0"); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 157 | ALOGV("power_set_interactive: %d done\n", on); |
| 158 | } |
| 159 | |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 160 | static int interactive_boostpulse(struct hikey_power_module *hikey) |
| 161 | { |
| 162 | char buf[80]; |
| 163 | int len; |
| 164 | |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 165 | if (hikey->boostpulse_fd < 0) |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 166 | hikey->boostpulse_fd = open(INTERACTIVE_BOOSTPULSE_PATH, O_WRONLY); |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 167 | |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 168 | if (hikey->boostpulse_fd < 0) { |
| 169 | if (!hikey->boostpulse_warned) { |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 170 | strerror_r(errno, buf, sizeof(buf)); |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 171 | ALOGE("Error opening %s: %s\n", INTERACTIVE_BOOSTPULSE_PATH, |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 172 | buf); |
| 173 | hikey->boostpulse_warned = 1; |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 174 | } |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 175 | return hikey->boostpulse_fd; |
| 176 | } |
| 177 | |
| 178 | len = write(hikey->boostpulse_fd, "1", 1); |
| 179 | if (len < 0) { |
| 180 | strerror_r(errno, buf, sizeof(buf)); |
| 181 | ALOGE("Error writing to %s: %s\n", |
John Stultz | 127e1cc | 2016-08-18 12:20:40 -0700 | [diff] [blame] | 182 | INTERACTIVE_BOOSTPULSE_PATH, buf); |
John Stultz | 64b54ca | 2016-08-18 15:01:33 -0700 | [diff] [blame] | 183 | return -1; |
John Stultz | 5e1546b | 2016-08-18 11:56:31 -0700 | [diff] [blame] | 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 188 | /*[schedtune functions]*******************************************************/ |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 189 | |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 190 | int schedtune_sysfs_boost(struct hikey_power_module *hikey, char* booststr) |
| 191 | { |
| 192 | char buf[80]; |
| 193 | int len; |
| 194 | |
| 195 | if (hikey->schedtune_boost_fd < 0) |
| 196 | return hikey->schedtune_boost_fd; |
| 197 | |
| 198 | len = write(hikey->schedtune_boost_fd, booststr, 2); |
| 199 | if (len < 0) { |
| 200 | strerror_r(errno, buf, sizeof(buf)); |
| 201 | ALOGE("Error writing to %s: %s\n", SCHEDTUNE_BOOST_PATH, buf); |
| 202 | } |
| 203 | return len; |
| 204 | } |
| 205 | |
| 206 | static void* schedtune_deboost_thread(void* arg) |
| 207 | { |
| 208 | struct hikey_power_module *hikey = (struct hikey_power_module *)arg; |
| 209 | |
| 210 | while(1) { |
| 211 | sem_wait(&hikey->signal_lock); |
| 212 | while(1) { |
| 213 | long long now, sleeptime = 0; |
| 214 | |
| 215 | pthread_mutex_lock(&hikey->lock); |
| 216 | now = gettime_ns(); |
| 217 | if (hikey->deboost_time > now) { |
| 218 | sleeptime = hikey->deboost_time - now; |
| 219 | pthread_mutex_unlock(&hikey->lock); |
| 220 | nanosleep_ns(sleeptime); |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | schedtune_sysfs_boost(hikey, SCHEDTUNE_BOOST_NORM); |
| 225 | hikey->deboost_time = 0; |
| 226 | pthread_mutex_unlock(&hikey->lock); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | static int schedtune_boost(struct hikey_power_module *hikey) |
| 234 | { |
| 235 | long long now; |
| 236 | |
| 237 | if (hikey->schedtune_boost_fd < 0) |
| 238 | return hikey->schedtune_boost_fd; |
| 239 | |
| 240 | now = gettime_ns(); |
| 241 | if (!hikey->deboost_time) { |
| 242 | schedtune_sysfs_boost(hikey, SCHEDTUNE_BOOST_INTERACTIVE); |
| 243 | sem_post(&hikey->signal_lock); |
| 244 | } |
| 245 | hikey->deboost_time = now + SCHEDTUNE_BOOST_TIME_NS; |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static void schedtune_power_init(struct hikey_power_module *hikey) |
| 251 | { |
| 252 | char buf[50]; |
| 253 | pthread_t tid; |
| 254 | |
| 255 | |
| 256 | hikey->deboost_time = 0; |
| 257 | sem_init(&hikey->signal_lock, 0, 1); |
| 258 | |
| 259 | hikey->schedtune_boost_fd = open(SCHEDTUNE_BOOST_PATH, O_WRONLY); |
| 260 | if (hikey->schedtune_boost_fd < 0) { |
| 261 | strerror_r(errno, buf, sizeof(buf)); |
| 262 | ALOGE("Error opening %s: %s\n", SCHEDTUNE_BOOST_PATH, buf); |
| 263 | } |
| 264 | |
| 265 | pthread_create(&tid, NULL, schedtune_deboost_thread, hikey); |
| 266 | } |
| 267 | |
| 268 | /*[generic functions]*********************************************************/ |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 269 | static void hikey_power_init(struct power_module __unused *module) |
| 270 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 271 | struct hikey_power_module *hikey = container_of(module, |
| 272 | struct hikey_power_module, base); |
| 273 | interactive_power_init(hikey); |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 274 | schedtune_power_init(hikey); |
| 275 | } |
| 276 | |
| 277 | static void hikey_hint_interaction(struct hikey_power_module *mod) |
| 278 | { |
| 279 | /* Try interactive cpufreq boosting first */ |
| 280 | if(!interactive_boostpulse(mod)) |
| 281 | return; |
| 282 | /* Then try EAS schedtune boosting */ |
| 283 | if(!schedtune_boost(mod)) |
| 284 | return; |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 285 | } |
| 286 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 287 | static void hikey_power_hint(struct power_module *module, power_hint_t hint, |
| 288 | void *data) |
| 289 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 290 | struct hikey_power_module *hikey = container_of(module, |
| 291 | struct hikey_power_module, base); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 292 | |
| 293 | pthread_mutex_lock(&hikey->lock); |
| 294 | switch (hint) { |
| 295 | case POWER_HINT_INTERACTION: |
John Stultz | 4c1a82b | 2016-08-18 14:49:01 -0700 | [diff] [blame] | 296 | hikey_hint_interaction(hikey); |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 297 | break; |
| 298 | |
| 299 | case POWER_HINT_VSYNC: |
| 300 | break; |
| 301 | |
| 302 | case POWER_HINT_LOW_POWER: |
| 303 | if (data) { |
| 304 | sysfs_write(CPU_MAX_FREQ_PATH, low_power_max_cpu_freq); |
| 305 | } else { |
| 306 | sysfs_write(CPU_MAX_FREQ_PATH, max_cpu_freq); |
| 307 | } |
| 308 | low_power_mode = data; |
| 309 | break; |
| 310 | |
| 311 | default: |
| 312 | break; |
| 313 | } |
| 314 | pthread_mutex_unlock(&hikey->lock); |
| 315 | } |
| 316 | |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 317 | static void set_feature(struct power_module *module, feature_t feature, int state) |
| 318 | { |
John Stultz | e150ab3 | 2016-08-18 14:04:34 -0700 | [diff] [blame] | 319 | struct hikey_power_module *hikey = container_of(module, |
| 320 | struct hikey_power_module, base); |
John Stultz | ab1f6a7 | 2016-08-18 12:28:55 -0700 | [diff] [blame] | 321 | switch (feature) { |
| 322 | default: |
| 323 | ALOGW("Error setting the feature, it doesn't exist %d\n", feature); |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 328 | static struct hw_module_methods_t power_module_methods = { |
| 329 | .open = NULL, |
| 330 | }; |
| 331 | |
| 332 | struct hikey_power_module HAL_MODULE_INFO_SYM = { |
| 333 | base: { |
| 334 | common: { |
| 335 | tag: HARDWARE_MODULE_TAG, |
| 336 | module_api_version: POWER_MODULE_API_VERSION_0_2, |
| 337 | hal_api_version: HARDWARE_HAL_API_VERSION, |
| 338 | id: POWER_HARDWARE_MODULE_ID, |
| 339 | name: "HiKey Power HAL", |
| 340 | author: "The Android Open Source Project", |
| 341 | methods: &power_module_methods, |
| 342 | }, |
| 343 | |
John Stultz | c579371 | 2016-08-18 14:00:49 -0700 | [diff] [blame] | 344 | init: hikey_power_init, |
John Stultz | 7266c5f | 2016-06-09 14:18:57 -0700 | [diff] [blame] | 345 | setInteractive: power_set_interactive, |
| 346 | powerHint: hikey_power_hint, |
| 347 | setFeature: set_feature, |
| 348 | }, |
| 349 | |
| 350 | lock: PTHREAD_MUTEX_INITIALIZER, |
| 351 | boostpulse_fd: -1, |
| 352 | boostpulse_warned: 0, |
| 353 | }; |
| 354 | |