Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -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 | |
| 30 | #define LOG_NIDEBUG 0 |
| 31 | |
| 32 | #include <errno.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <dlfcn.h> |
| 38 | #include <stdlib.h> |
| 39 | |
| 40 | #define LOG_TAG "QTI PowerHAL" |
| 41 | #include <utils/Log.h> |
| 42 | #include <hardware/hardware.h> |
| 43 | #include <hardware/power.h> |
| 44 | |
| 45 | #include "utils.h" |
| 46 | #include "metadata-defs.h" |
| 47 | #include "hint-data.h" |
| 48 | #include "performance.h" |
| 49 | #include "power-common.h" |
| 50 | |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 51 | static int display_hint_sent; |
| 52 | static int video_encode_hint_sent; |
| 53 | static int current_power_profile = PROFILE_BALANCED; |
| 54 | |
| 55 | static void process_video_encode_hint(void *metadata); |
| 56 | |
| 57 | extern void interaction(int duration, int num_args, int opt_list[]); |
| 58 | |
| 59 | static int profile_high_performance_8952[11] = { |
| 60 | SCHED_BOOST_ON, |
| 61 | 0x704, 0x4d04, /* Enable all CPUs */ |
| 62 | CPU0_MIN_FREQ_TURBO_MAX, CPU1_MIN_FREQ_TURBO_MAX, |
| 63 | CPU2_MIN_FREQ_TURBO_MAX, CPU3_MIN_FREQ_TURBO_MAX, |
| 64 | CPU4_MIN_FREQ_TURBO_MAX, CPU5_MIN_FREQ_TURBO_MAX, |
| 65 | CPU6_MIN_FREQ_TURBO_MAX, CPU7_MIN_FREQ_TURBO_MAX, |
| 66 | }; |
| 67 | |
Pat Erley | ffbc05e | 2016-06-03 15:51:17 -0700 | [diff] [blame] | 68 | static int profile_power_save_8952[] = { |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 69 | 0x8fe, 0x3dfd, /* 1 big core, 2 little cores*/ |
| 70 | CPUS_ONLINE_MAX_LIMIT_2, |
| 71 | CPU0_MAX_FREQ_NONTURBO_MAX, CPU1_MAX_FREQ_NONTURBO_MAX, |
| 72 | CPU2_MAX_FREQ_NONTURBO_MAX, CPU3_MAX_FREQ_NONTURBO_MAX, |
| 73 | }; |
| 74 | |
| 75 | int get_number_of_profiles() { |
| 76 | return 3; |
| 77 | } |
| 78 | |
| 79 | static void set_power_profile(int profile) { |
| 80 | |
| 81 | if (profile == current_power_profile) |
| 82 | return; |
| 83 | |
| 84 | ALOGV("%s: profile=%d", __func__, profile); |
| 85 | |
| 86 | if (current_power_profile != PROFILE_BALANCED) { |
| 87 | undo_hint_action(DEFAULT_PROFILE_HINT_ID); |
| 88 | ALOGV("%s: hint undone", __func__); |
| 89 | } |
| 90 | |
| 91 | if (profile == PROFILE_HIGH_PERFORMANCE) { |
| 92 | int *resource_values = profile_high_performance_8952; |
| 93 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, resource_values, |
| 94 | ARRAY_SIZE(resource_values)); |
| 95 | ALOGD("%s: set performance mode", __func__); |
| 96 | |
| 97 | } else if (profile == PROFILE_POWER_SAVE) { |
| 98 | int *resource_values = profile_power_save_8952; |
| 99 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, resource_values, |
| 100 | ARRAY_SIZE(resource_values)); |
| 101 | ALOGD("%s: set powersave", __func__); |
| 102 | } |
| 103 | |
| 104 | current_power_profile = profile; |
| 105 | } |
| 106 | |
| 107 | int power_hint_override(struct power_module *module, power_hint_t hint, |
| 108 | void *data) |
| 109 | { |
Steve Kondik | 30d3220 | 2016-03-12 22:58:33 -0800 | [diff] [blame] | 110 | int duration, duration_hint; |
Zhao Wei Liew | 1f7d8af | 2016-06-21 17:08:54 +0800 | [diff] [blame] | 111 | static unsigned long long previous_boost_time = 0; |
| 112 | unsigned long long cur_boost_time; |
Steve Kondik | 30d3220 | 2016-03-12 22:58:33 -0800 | [diff] [blame] | 113 | struct timeval cur_boost_timeval = {0, 0}; |
| 114 | double elapsed_time; |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 115 | int resources_launch_boost[] = { |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 116 | ALL_CPUS_PWR_CLPS_DIS, |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 117 | SCHED_BOOST_ON, |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 118 | SCHED_PREFER_IDLE_DIS, |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 119 | 0x20f, |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 120 | 0x4001, |
| 121 | 0x4101, |
| 122 | 0x4201, |
| 123 | }; |
| 124 | int resources_cpu_boost[] = { |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 125 | ALL_CPUS_PWR_CLPS_DIS, |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 126 | SCHED_BOOST_ON, |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 127 | SCHED_PREFER_IDLE_DIS, |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 128 | 0x20d, |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 129 | }; |
Steve Kondik | 30d3220 | 2016-03-12 22:58:33 -0800 | [diff] [blame] | 130 | int resources_interaction_boost[] = { |
Zhao Wei Liew | 458a99b | 2016-05-29 00:41:44 +0800 | [diff] [blame] | 131 | SCHED_PREFER_IDLE_DIS, |
Steve Kondik | 30d3220 | 2016-03-12 22:58:33 -0800 | [diff] [blame] | 132 | 0x20d, |
| 133 | 0x3d01, |
Steve Kondik | 30d3220 | 2016-03-12 22:58:33 -0800 | [diff] [blame] | 134 | }; |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 135 | |
| 136 | if (hint == POWER_HINT_SET_PROFILE) { |
| 137 | set_power_profile(*(int32_t *)data); |
| 138 | return HINT_HANDLED; |
| 139 | } |
| 140 | |
| 141 | // Skip other hints in custom power modes |
| 142 | if (current_power_profile != PROFILE_BALANCED) { |
| 143 | return HINT_HANDLED; |
| 144 | } |
| 145 | |
| 146 | switch (hint) { |
Steve Kondik | 30d3220 | 2016-03-12 22:58:33 -0800 | [diff] [blame] | 147 | case POWER_HINT_INTERACTION: |
| 148 | duration = 500; |
| 149 | duration_hint = 0; |
| 150 | |
| 151 | if (data) { |
| 152 | duration_hint = *((int *)data); |
| 153 | } |
| 154 | |
| 155 | duration = duration_hint > 0 ? duration_hint : 500; |
| 156 | |
| 157 | gettimeofday(&cur_boost_timeval, NULL); |
| 158 | cur_boost_time = cur_boost_timeval.tv_sec * 1000000 + cur_boost_timeval.tv_usec; |
| 159 | elapsed_time = (double)(cur_boost_time - previous_boost_time); |
| 160 | if (elapsed_time > 750000) |
| 161 | elapsed_time = 750000; |
| 162 | // don't hint if it's been less than 250ms since last boost |
| 163 | // also detect if we're doing anything resembling a fling |
| 164 | // support additional boosting in case of flings |
| 165 | else if (elapsed_time < 250000 && duration <= 750) |
| 166 | return HINT_HANDLED; |
| 167 | |
| 168 | previous_boost_time = cur_boost_time; |
| 169 | |
| 170 | if (duration >= 1500) { |
| 171 | interaction(duration, ARRAY_SIZE(resources_cpu_boost), |
| 172 | resources_cpu_boost); |
| 173 | } else { |
| 174 | interaction(duration, ARRAY_SIZE(resources_interaction_boost), |
| 175 | resources_interaction_boost); |
| 176 | } |
| 177 | return HINT_HANDLED; |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 178 | case POWER_HINT_LAUNCH_BOOST: |
| 179 | duration = 2000; |
| 180 | interaction(duration, ARRAY_SIZE(resources_launch_boost), |
| 181 | resources_launch_boost); |
| 182 | return HINT_HANDLED; |
| 183 | case POWER_HINT_CPU_BOOST: |
| 184 | duration = *(int32_t *)data / 1000; |
| 185 | if (duration > 0) { |
| 186 | interaction(duration, ARRAY_SIZE(resources_cpu_boost), |
| 187 | resources_cpu_boost); |
| 188 | } |
| 189 | return HINT_HANDLED; |
| 190 | case POWER_HINT_VIDEO_ENCODE: |
| 191 | process_video_encode_hint(data); |
| 192 | return HINT_HANDLED; |
| 193 | } |
| 194 | return HINT_NONE; |
| 195 | } |
| 196 | |
| 197 | int set_interactive_override(struct power_module *module, int on) |
| 198 | { |
| 199 | char governor[80]; |
| 200 | |
| 201 | ALOGI("Got set_interactive hint"); |
| 202 | |
| 203 | if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU0) == -1) { |
| 204 | if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU1) == -1) { |
| 205 | if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU2) == -1) { |
| 206 | if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU3) == -1) { |
| 207 | ALOGE("Can't obtain scaling governor."); |
| 208 | return HINT_HANDLED; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (!on) { |
| 215 | /* Display off. */ |
| 216 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 217 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 218 | int resource_values[] = {TR_MS_CPU0_50, TR_MS_CPU4_50}; |
| 219 | |
| 220 | if (!display_hint_sent) { |
| 221 | perform_hint_action(DISPLAY_STATE_HINT_ID, |
| 222 | resource_values, ARRAY_SIZE(resource_values)); |
| 223 | display_hint_sent = 1; |
| 224 | } |
| 225 | } /* Perf time rate set for CORE0,CORE4 8952 target*/ |
| 226 | |
| 227 | } else { |
| 228 | /* Display on. */ |
| 229 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 230 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 231 | |
| 232 | undo_hint_action(DISPLAY_STATE_HINT_ID); |
| 233 | display_hint_sent = 0; |
| 234 | } |
| 235 | } |
| 236 | return HINT_HANDLED; |
| 237 | } |
| 238 | |
| 239 | /* Video Encode Hint */ |
| 240 | static void process_video_encode_hint(void *metadata) |
| 241 | { |
| 242 | char governor[80]; |
| 243 | struct video_encode_metadata_t video_encode_metadata; |
| 244 | |
| 245 | ALOGI("Got process_video_encode_hint"); |
| 246 | |
| 247 | if (get_scaling_governor_check_cores(governor, |
| 248 | sizeof(governor),CPU0) == -1) { |
| 249 | if (get_scaling_governor_check_cores(governor, |
| 250 | sizeof(governor),CPU1) == -1) { |
| 251 | if (get_scaling_governor_check_cores(governor, |
| 252 | sizeof(governor),CPU2) == -1) { |
| 253 | if (get_scaling_governor_check_cores(governor, |
| 254 | sizeof(governor),CPU3) == -1) { |
| 255 | ALOGE("Can't obtain scaling governor."); |
Ethan Chen | f64ea3a | 2016-06-10 18:44:30 -0700 | [diff] [blame] | 256 | return; |
Ethan Chen | 542ac97 | 2016-01-03 14:52:58 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* Initialize encode metadata struct fields. */ |
| 263 | memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t)); |
| 264 | video_encode_metadata.state = -1; |
| 265 | video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID; |
| 266 | |
| 267 | if (metadata) { |
| 268 | if (parse_video_encode_metadata((char *)metadata, |
| 269 | &video_encode_metadata) == -1) { |
| 270 | ALOGE("Error occurred while parsing metadata."); |
| 271 | return; |
| 272 | } |
| 273 | } else { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | if (video_encode_metadata.state == 1) { |
| 278 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, |
| 279 | strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 280 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 281 | int resource_values[] = {TR_MS_CPU0_30, TR_MS_CPU4_30}; |
| 282 | if (!video_encode_hint_sent) { |
| 283 | perform_hint_action(video_encode_metadata.hint_id, |
| 284 | resource_values, |
| 285 | ARRAY_SIZE(resource_values)); |
| 286 | video_encode_hint_sent = 1; |
| 287 | } |
| 288 | } |
| 289 | } else if (video_encode_metadata.state == 0) { |
| 290 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, |
| 291 | strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 292 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 293 | undo_hint_action(video_encode_metadata.hint_id); |
| 294 | video_encode_hint_sent = 0; |
| 295 | return ; |
| 296 | } |
| 297 | } |
| 298 | return; |
| 299 | } |