Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | #define LOG_NIDEBUG 0 |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <string.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <dlfcn.h> |
| 37 | #include <stdlib.h> |
| 38 | |
| 39 | #define LOG_TAG "QCOM PowerHAL" |
| 40 | #include <utils/Log.h> |
| 41 | #include <hardware/hardware.h> |
| 42 | #include <hardware/power.h> |
| 43 | |
| 44 | #include "utils.h" |
| 45 | #include "metadata-defs.h" |
| 46 | #include "hint-data.h" |
| 47 | #include "performance.h" |
| 48 | #include "power-common.h" |
| 49 | |
| 50 | static int display_hint_sent; |
| 51 | |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 52 | int get_number_of_profiles() { |
| 53 | return 5; |
| 54 | } |
| 55 | |
| 56 | static int current_power_profile = PROFILE_BALANCED; |
| 57 | |
| 58 | static void set_power_profile(int profile) { |
| 59 | |
| 60 | if (profile == current_power_profile) |
| 61 | return; |
| 62 | |
| 63 | ALOGV("%s: profile=%d", __func__, profile); |
| 64 | |
| 65 | if (current_power_profile != PROFILE_BALANCED) { |
| 66 | undo_hint_action(DEFAULT_PROFILE_HINT_ID); |
| 67 | ALOGV("%s: hint undone", __func__); |
| 68 | } |
| 69 | |
| 70 | if (profile == PROFILE_POWER_SAVE) { |
| 71 | int resource_values[] = { CPUS_ONLINE_MPD_OVERRIDE, 0x0A03, |
| 72 | CPU0_MAX_FREQ_NONTURBO_MAX - 2, CPU1_MAX_FREQ_NONTURBO_MAX - 2, |
| 73 | CPU2_MAX_FREQ_NONTURBO_MAX - 2, CPU3_MAX_FREQ_NONTURBO_MAX - 2, |
| 74 | CPU4_MAX_FREQ_NONTURBO_MAX - 2, CPU5_MAX_FREQ_NONTURBO_MAX - 2 }; |
| 75 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 76 | resource_values, ARRAY_SIZE(resource_values)); |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 77 | ALOGD("%s: set powersave", __func__); |
| 78 | } else if (profile == PROFILE_HIGH_PERFORMANCE) { |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 79 | int resource_values[] = { SCHED_BOOST_ON, CPUS_ONLINE_MAX, |
| 80 | ALL_CPUS_PWR_CLPS_DIS, 0x0901, |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 81 | CPU0_MIN_FREQ_TURBO_MAX, CPU1_MIN_FREQ_TURBO_MAX, |
| 82 | CPU2_MIN_FREQ_TURBO_MAX, CPU3_MIN_FREQ_TURBO_MAX, |
| 83 | CPU4_MIN_FREQ_TURBO_MAX, CPU5_MIN_FREQ_TURBO_MAX }; |
| 84 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 85 | resource_values, ARRAY_SIZE(resource_values)); |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 86 | ALOGD("%s: set performance mode", __func__); |
| 87 | } else if (profile == PROFILE_BIAS_POWER) { |
| 88 | int resource_values[] = { 0x0A03, 0x0902, |
| 89 | CPU0_MAX_FREQ_NONTURBO_MAX - 2, CPU1_MAX_FREQ_NONTURBO_MAX - 2, |
| 90 | CPU1_MAX_FREQ_NONTURBO_MAX - 2, CPU2_MAX_FREQ_NONTURBO_MAX - 2, |
| 91 | CPU4_MAX_FREQ_NONTURBO_MAX, CPU5_MAX_FREQ_NONTURBO_MAX }; |
| 92 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 93 | resource_values, ARRAY_SIZE(resource_values)); |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 94 | ALOGD("%s: set bias power mode", __func__); |
| 95 | } else if (profile == PROFILE_BIAS_PERFORMANCE) { |
| 96 | int resource_values[] = { CPUS_ONLINE_MAX_LIMIT_MAX, |
| 97 | CPU4_MIN_FREQ_NONTURBO_MAX + 1, CPU5_MIN_FREQ_NONTURBO_MAX + 1 }; |
| 98 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 99 | resource_values, ARRAY_SIZE(resource_values)); |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 100 | ALOGD("%s: set bias perf mode", __func__); |
| 101 | } |
| 102 | |
| 103 | current_power_profile = profile; |
| 104 | } |
| 105 | |
Zhao Wei Liew | 83fd4f1 | 2016-02-21 07:48:02 +0800 | [diff] [blame] | 106 | extern void interaction(int duration, int num_args, int opt_list[]); |
| 107 | |
Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 108 | static int process_video_encode_hint(void *metadata) |
| 109 | { |
| 110 | char governor[80]; |
| 111 | struct video_encode_metadata_t video_encode_metadata; |
| 112 | |
| 113 | if (get_scaling_governor(governor, sizeof(governor)) == -1) { |
| 114 | ALOGE("Can't obtain scaling governor."); |
| 115 | |
| 116 | return HINT_NONE; |
| 117 | } |
| 118 | |
| 119 | /* Initialize encode metadata struct fields */ |
| 120 | memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t)); |
| 121 | video_encode_metadata.state = -1; |
| 122 | video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID; |
| 123 | |
| 124 | if (metadata) { |
| 125 | if (parse_video_encode_metadata((char *)metadata, &video_encode_metadata) == |
| 126 | -1) { |
| 127 | ALOGE("Error occurred while parsing metadata."); |
| 128 | return HINT_NONE; |
| 129 | } |
| 130 | } else { |
| 131 | return HINT_NONE; |
| 132 | } |
| 133 | |
| 134 | if (video_encode_metadata.state == 1) { |
| 135 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 136 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 137 | /* sched and cpufreq params |
| 138 | * hispeed freq - 768 MHz |
| 139 | * target load - 90 |
| 140 | * above_hispeed_delay - 40ms |
| 141 | * sched_small_tsk - 50 |
| 142 | */ |
| 143 | int resource_values[] = {0x2C07, 0x2F5A, 0x2704, 0x4032}; |
| 144 | |
| 145 | perform_hint_action(video_encode_metadata.hint_id, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 146 | resource_values, ARRAY_SIZE(resource_values)); |
Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 147 | return HINT_HANDLED; |
| 148 | } |
| 149 | } else if (video_encode_metadata.state == 0) { |
| 150 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 151 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 152 | undo_hint_action(video_encode_metadata.hint_id); |
| 153 | return HINT_HANDLED; |
| 154 | } |
| 155 | } |
| 156 | return HINT_NONE; |
| 157 | } |
| 158 | |
Zhao Wei Liew | 83fd4f1 | 2016-02-21 07:48:02 +0800 | [diff] [blame] | 159 | int power_hint_override(__attribute__((unused)) struct power_module *module, |
Steve Kondik | b1da841 | 2016-05-06 04:33:11 -0700 | [diff] [blame] | 160 | power_hint_t hint, void *data) |
Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 161 | { |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 162 | if (hint == POWER_HINT_SET_PROFILE) { |
| 163 | set_power_profile(*(int32_t *)data); |
| 164 | return HINT_HANDLED; |
Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 165 | } |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 166 | |
| 167 | // Skip other hints in custom power modes |
Steve Kondik | b1da841 | 2016-05-06 04:33:11 -0700 | [diff] [blame] | 168 | if (current_power_profile == PROFILE_POWER_SAVE) { |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 169 | return HINT_HANDLED; |
| 170 | } |
| 171 | |
| 172 | if (hint == POWER_HINT_INTERACTION) { |
| 173 | int duration = 500, duration_hint = 0; |
| 174 | static unsigned long long previous_boost_time = 0; |
| 175 | |
| 176 | if (data) { |
| 177 | duration_hint = *((int *)data); |
| 178 | } |
| 179 | |
| 180 | duration = duration_hint > 0 ? duration_hint : 500; |
| 181 | |
| 182 | struct timeval cur_boost_timeval = {0, 0}; |
| 183 | gettimeofday(&cur_boost_timeval, NULL); |
| 184 | unsigned long long cur_boost_time = cur_boost_timeval.tv_sec * 1000000 + cur_boost_timeval.tv_usec; |
| 185 | double elapsed_time = (double)(cur_boost_time - previous_boost_time); |
| 186 | if (elapsed_time > 750000) |
| 187 | elapsed_time = 750000; |
| 188 | // don't hint if it's been less than 250ms since last boost |
| 189 | // also detect if we're doing anything resembling a fling |
| 190 | // support additional boosting in case of flings |
| 191 | else if (elapsed_time < 250000 && duration <= 750) |
| 192 | return HINT_HANDLED; |
| 193 | |
| 194 | previous_boost_time = cur_boost_time; |
| 195 | |
| 196 | if (duration >= 1500) { |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 197 | int resources[] = { |
| 198 | ALL_CPUS_PWR_CLPS_DIS, |
| 199 | SCHED_BOOST_ON, |
Steve Kondik | 2f802f5 | 2016-06-09 03:01:07 -0700 | [diff] [blame] | 200 | SCHED_PREFER_IDLE_DIS |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 201 | }; |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 202 | interaction(duration, ARRAY_SIZE(resources), resources); |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 203 | } else { |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 204 | int resources[] = { |
| 205 | ALL_CPUS_PWR_CLPS_DIS, |
Steve Kondik | 2f802f5 | 2016-06-09 03:01:07 -0700 | [diff] [blame] | 206 | SCHED_PREFER_IDLE_DIS |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 207 | }; |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 208 | interaction(duration, ARRAY_SIZE(resources), resources); |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 209 | } |
| 210 | return HINT_HANDLED; |
| 211 | } |
| 212 | |
| 213 | if (hint == POWER_HINT_LAUNCH_BOOST) { |
Steve Kondik | b1da841 | 2016-05-06 04:33:11 -0700 | [diff] [blame] | 214 | launch_boost_info_t *info = (launch_boost_info_t *)data; |
| 215 | if (info == NULL) { |
| 216 | ALOGE("Invalid argument for launch boost"); |
| 217 | return HINT_HANDLED; |
| 218 | } |
| 219 | |
| 220 | ALOGV("LAUNCH_BOOST: %s (pid=%d)", info->packageName, info->pid); |
| 221 | |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 222 | int duration = 2000; |
Steve Kondik | 2f802f5 | 2016-06-09 03:01:07 -0700 | [diff] [blame] | 223 | int resources[] = { SCHED_BOOST_ON, 0x20C }; |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 224 | |
Steve Kondik | b1da841 | 2016-05-06 04:33:11 -0700 | [diff] [blame] | 225 | start_prefetch(info->pid, info->packageName); |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 226 | interaction(duration, ARRAY_SIZE(resources), resources); |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 227 | |
| 228 | return HINT_HANDLED; |
| 229 | } |
| 230 | |
| 231 | if (hint == POWER_HINT_CPU_BOOST) { |
| 232 | int duration = *(int32_t *)data / 1000; |
| 233 | int resources[] = { SCHED_BOOST_ON }; |
| 234 | |
| 235 | if (duration > 0) |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 236 | interaction(duration, ARRAY_SIZE(resources), resources); |
Steve Kondik | 9e93e92 | 2016-04-29 14:17:02 -0700 | [diff] [blame] | 237 | |
| 238 | return HINT_HANDLED; |
| 239 | } |
| 240 | |
| 241 | if (hint == POWER_HINT_VIDEO_ENCODE) { |
| 242 | return process_video_encode_hint(data); |
| 243 | } |
| 244 | |
| 245 | return HINT_NONE; |
Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Zhao Wei Liew | 83fd4f1 | 2016-02-21 07:48:02 +0800 | [diff] [blame] | 248 | int set_interactive_override(__attribute__((unused)) struct power_module *module, int on) |
Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 249 | { |
Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 250 | char governor[80]; |
| 251 | |
| 252 | if (get_scaling_governor(governor, sizeof(governor)) == -1) { |
| 253 | ALOGE("Can't obtain scaling governor."); |
| 254 | |
| 255 | return HINT_NONE; |
| 256 | } |
| 257 | |
| 258 | if (!on) { |
| 259 | /* Display off */ |
| 260 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 261 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
Steve Kondik | 116ff68 | 2016-05-05 15:24:08 -0700 | [diff] [blame] | 262 | // sched upmigrate = 99, sched downmigrate = 95 |
| 263 | // keep the big cores around, but make them very hard to use |
| 264 | int resource_values[] = { 0x4E63, 0x4F5F }; |
Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 265 | if (!display_hint_sent) { |
| 266 | perform_hint_action(DISPLAY_STATE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 267 | resource_values, ARRAY_SIZE(resource_values)); |
Dilip Gudlur | b0eb8ec | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 268 | display_hint_sent = 1; |
| 269 | return HINT_HANDLED; |
| 270 | } |
| 271 | } |
| 272 | } else { |
| 273 | /* Display on */ |
| 274 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 275 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 276 | undo_hint_action(DISPLAY_STATE_HINT_ID); |
| 277 | display_hint_sent = 0; |
| 278 | return HINT_HANDLED; |
| 279 | } |
| 280 | } |
| 281 | return HINT_NONE; |
| 282 | } |