BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 3 | * Copyright (C) 2018 The LineageOS Project |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials provided |
| 13 | * with the distribution. |
| 14 | * * Neither the name of The Linux Foundation nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 22 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 28 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #define LOG_NIDEBUG 0 |
| 32 | |
| 33 | #include <errno.h> |
BeYkeRYkt | bd52e81 | 2018-12-13 06:42:35 +0900 | [diff] [blame] | 34 | #include <time.h> |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 35 | #include <string.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <dlfcn.h> |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | #define LOG_TAG "QCOM PowerHAL" |
| 43 | #include <log/log.h> |
| 44 | #include <hardware/hardware.h> |
| 45 | #include <hardware/power.h> |
| 46 | |
| 47 | #include "utils.h" |
| 48 | #include "metadata-defs.h" |
| 49 | #include "hint-data.h" |
| 50 | #include "performance.h" |
| 51 | #include "power-common.h" |
| 52 | |
tomascus | 8b038b1 | 2019-02-19 17:15:58 +1100 | [diff] [blame] | 53 | const int kMaxLaunchDuration = 5000; /* ms */ |
BeYkeRYkt | bd52e81 | 2018-12-13 06:42:35 +0900 | [diff] [blame] | 54 | const int kMaxInteractiveDuration = 5000; /* ms */ |
| 55 | const int kMinInteractiveDuration = 500; /* ms */ |
BeYkeRYkt | bd52e81 | 2018-12-13 06:42:35 +0900 | [diff] [blame] | 56 | |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 57 | static int video_encode_hint_sent; |
| 58 | |
| 59 | static int current_power_profile = PROFILE_BALANCED; |
| 60 | |
| 61 | static int profile_high_performance[] = { |
BeYkeRYkt | 48b4e27 | 2018-12-23 23:21:50 +0900 | [diff] [blame] | 62 | SCHED_BOOST_ON_V3, 0x1, |
| 63 | ALL_CPUS_PWR_CLPS_DIS_V3, 0x1, |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 64 | CPUS_ONLINE_MIN_BIG, 0x4, |
BeYkeRYkt | 48b4e27 | 2018-12-23 23:21:50 +0900 | [diff] [blame] | 65 | MIN_FREQ_BIG_CORE_0, 0xFFF, |
| 66 | MIN_FREQ_LITTLE_CORE_0, 0xFFF, |
| 67 | GPU_MIN_POWER_LEVEL, 0x1, |
| 68 | SCHED_PREFER_IDLE_DIS_V3, 0x1, |
| 69 | SCHED_SMALL_TASK, 0x1, |
| 70 | SCHED_MOSTLY_IDLE_NR_RUN, 0x1, |
| 71 | SCHED_MOSTLY_IDLE_LOAD, 0x1, |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static int profile_power_save[] = { |
BeYkeRYkt | 48b4e27 | 2018-12-23 23:21:50 +0900 | [diff] [blame] | 75 | CPUS_ONLINE_MAX_BIG, 0x1, |
| 76 | MAX_FREQ_BIG_CORE_0, 0x3bf, |
| 77 | MAX_FREQ_LITTLE_CORE_0, 0x300, |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static int profile_bias_power[] = { |
BeYkeRYkt | 48b4e27 | 2018-12-23 23:21:50 +0900 | [diff] [blame] | 81 | MAX_FREQ_BIG_CORE_0, 0x4B0, |
| 82 | MAX_FREQ_LITTLE_CORE_0, 0x300, |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static int profile_bias_performance[] = { |
BeYkeRYkt | 48b4e27 | 2018-12-23 23:21:50 +0900 | [diff] [blame] | 86 | CPUS_ONLINE_MAX_BIG, 0x4, |
| 87 | MIN_FREQ_BIG_CORE_0, 0x540, |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | #ifdef INTERACTION_BOOST |
| 91 | int get_number_of_profiles() |
| 92 | { |
| 93 | return 5; |
| 94 | } |
| 95 | #endif |
| 96 | |
dianlujitao | 8780cb7 | 2019-02-23 20:24:57 +0800 | [diff] [blame] | 97 | static int set_power_profile(void *data) |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 98 | { |
dianlujitao | 8780cb7 | 2019-02-23 20:24:57 +0800 | [diff] [blame] | 99 | int profile = data ? *((int*)data) : 0; |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 100 | int ret = -EINVAL; |
| 101 | const char *profile_name = NULL; |
| 102 | |
| 103 | if (profile == current_power_profile) |
| 104 | return 0; |
| 105 | |
| 106 | ALOGV("%s: Profile=%d", __func__, profile); |
| 107 | |
| 108 | if (current_power_profile != PROFILE_BALANCED) { |
| 109 | undo_hint_action(DEFAULT_PROFILE_HINT_ID); |
| 110 | ALOGV("%s: Hint undone", __func__); |
| 111 | current_power_profile = PROFILE_BALANCED; |
| 112 | } |
| 113 | |
| 114 | if (profile == PROFILE_POWER_SAVE) { |
| 115 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save, |
| 116 | ARRAY_SIZE(profile_power_save)); |
| 117 | profile_name = "powersave"; |
| 118 | |
| 119 | } else if (profile == PROFILE_HIGH_PERFORMANCE) { |
| 120 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
| 121 | profile_high_performance, ARRAY_SIZE(profile_high_performance)); |
| 122 | profile_name = "performance"; |
| 123 | |
| 124 | } else if (profile == PROFILE_BIAS_POWER) { |
| 125 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power, |
| 126 | ARRAY_SIZE(profile_bias_power)); |
| 127 | profile_name = "bias power"; |
| 128 | |
| 129 | } else if (profile == PROFILE_BIAS_PERFORMANCE) { |
| 130 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
| 131 | profile_bias_performance, ARRAY_SIZE(profile_bias_performance)); |
| 132 | profile_name = "bias perf"; |
| 133 | } else if (profile == PROFILE_BALANCED) { |
| 134 | ret = 0; |
| 135 | profile_name = "balanced"; |
| 136 | } |
| 137 | |
| 138 | if (ret == 0) { |
| 139 | current_power_profile = profile; |
| 140 | ALOGD("%s: Set %s mode", __func__, profile_name); |
| 141 | } |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static void process_video_encode_hint(void *metadata) |
| 146 | { |
| 147 | char governor[80]; |
| 148 | struct video_encode_metadata_t video_encode_metadata; |
| 149 | |
| 150 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) { |
| 151 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) { |
| 152 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) { |
| 153 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) { |
| 154 | ALOGE("Can't obtain scaling governor."); |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (!metadata) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | /* Initialize encode metadata struct fields. */ |
| 166 | memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t)); |
| 167 | video_encode_metadata.state = -1; |
| 168 | video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID; |
| 169 | |
| 170 | if (parse_video_encode_metadata((char *)metadata, |
| 171 | &video_encode_metadata) == -1) { |
| 172 | ALOGE("Error occurred while parsing metadata."); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (video_encode_metadata.state == 1) { |
| 177 | if (is_interactive_governor(governor)) { |
| 178 | int resource_values[] = { |
| 179 | INT_OP_CLUSTER0_USE_SCHED_LOAD, 0x1, |
| 180 | INT_OP_CLUSTER1_USE_SCHED_LOAD, 0x1, |
| 181 | INT_OP_CLUSTER0_USE_MIGRATION_NOTIF, 0x1, |
| 182 | INT_OP_CLUSTER1_USE_MIGRATION_NOTIF, 0x1, |
| 183 | INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_40, |
| 184 | INT_OP_CLUSTER1_TIMER_RATE, BIG_LITTLE_TR_MS_40 |
| 185 | }; |
| 186 | if (!video_encode_hint_sent) { |
| 187 | perform_hint_action(video_encode_metadata.hint_id, |
| 188 | resource_values, ARRAY_SIZE(resource_values)); |
| 189 | video_encode_hint_sent = 1; |
| 190 | } |
| 191 | } |
| 192 | } else if (video_encode_metadata.state == 0) { |
| 193 | if (is_interactive_governor(governor)) { |
| 194 | undo_hint_action(video_encode_metadata.hint_id); |
| 195 | video_encode_hint_sent = 0; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
tomascus | 8b038b1 | 2019-02-19 17:15:58 +1100 | [diff] [blame] | 200 | static int process_activity_launch_hint(void *data) |
BeYkeRYkt | bd52e81 | 2018-12-13 06:42:35 +0900 | [diff] [blame] | 201 | { |
tomascus | 8b038b1 | 2019-02-19 17:15:58 +1100 | [diff] [blame] | 202 | static int launch_handle = -1; |
| 203 | static int launch_mode = 0; |
| 204 | |
| 205 | // release lock early if launch has finished |
| 206 | if (!data) { |
| 207 | if (CHECK_HANDLE(launch_handle)) { |
| 208 | release_request(launch_handle); |
| 209 | launch_handle = -1; |
| 210 | } |
| 211 | launch_mode = 0; |
| 212 | return HINT_HANDLED; |
| 213 | } |
| 214 | |
| 215 | if (!launch_mode) { |
| 216 | launch_handle = perf_hint_enable_with_type(VENDOR_HINT_FIRST_LAUNCH_BOOST, |
| 217 | kMaxLaunchDuration, LAUNCH_BOOST_V1); |
| 218 | if (!CHECK_HANDLE(launch_handle)) { |
| 219 | ALOGE("Failed to perform launch boost"); |
| 220 | return HINT_NONE; |
| 221 | } |
| 222 | launch_mode = 1; |
| 223 | } |
| 224 | return HINT_HANDLED; |
BeYkeRYkt | bd52e81 | 2018-12-13 06:42:35 +0900 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static void process_interaction_hint(void *data) |
| 228 | { |
| 229 | static struct timespec s_previous_boost_timespec; |
| 230 | static int s_previous_duration = 0; |
| 231 | |
| 232 | struct timespec cur_boost_timespec; |
| 233 | long long elapsed_time; |
| 234 | int duration = kMinInteractiveDuration; |
| 235 | |
| 236 | if (data) { |
| 237 | int input_duration = *((int*)data); |
| 238 | if (input_duration > duration) { |
| 239 | duration = (input_duration > kMaxInteractiveDuration) ? |
| 240 | kMaxInteractiveDuration : input_duration; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec); |
| 245 | |
| 246 | elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec); |
| 247 | // don't hint if previous hint's duration covers this hint's duration |
| 248 | if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) { |
| 249 | return; |
| 250 | } |
| 251 | s_previous_boost_timespec = cur_boost_timespec; |
| 252 | s_previous_duration = duration; |
| 253 | |
tomascus | 1c38bac | 2019-02-13 14:42:27 +1100 | [diff] [blame] | 254 | perf_hint_enable_with_type(VENDOR_HINT_SCROLL_BOOST, duration, SCROLL_VERTICAL); |
BeYkeRYkt | bd52e81 | 2018-12-13 06:42:35 +0900 | [diff] [blame] | 255 | } |
| 256 | |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 257 | int power_hint_override(power_hint_t hint, void *data) |
| 258 | { |
| 259 | if (hint == POWER_HINT_SET_PROFILE) { |
dianlujitao | 8780cb7 | 2019-02-23 20:24:57 +0800 | [diff] [blame] | 260 | if (set_power_profile(data) < 0) |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 261 | ALOGE("Setting power profile failed. perfd not started?"); |
| 262 | return HINT_HANDLED; |
| 263 | } |
| 264 | |
| 265 | // Skip other hints in high/low power modes |
| 266 | if (current_power_profile == PROFILE_POWER_SAVE || |
| 267 | current_power_profile == PROFILE_HIGH_PERFORMANCE) { |
| 268 | return HINT_HANDLED; |
| 269 | } |
| 270 | |
| 271 | switch (hint) { |
| 272 | case POWER_HINT_VIDEO_ENCODE: |
| 273 | process_video_encode_hint(data); |
| 274 | return HINT_HANDLED; |
BeYkeRYkt | bd52e81 | 2018-12-13 06:42:35 +0900 | [diff] [blame] | 275 | case POWER_HINT_INTERACTION: |
| 276 | process_interaction_hint(data); |
| 277 | return HINT_HANDLED; |
| 278 | case POWER_HINT_LAUNCH: |
tomascus | 8b038b1 | 2019-02-19 17:15:58 +1100 | [diff] [blame] | 279 | return process_activity_launch_hint(data); |
BeYkeRYkt | 535cc21 | 2018-12-23 22:55:35 +0900 | [diff] [blame] | 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | return HINT_NONE; |
| 284 | } |
| 285 | |
| 286 | int set_interactive_override(int on) |
| 287 | { |
| 288 | char governor[80]; |
| 289 | |
| 290 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) { |
| 291 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) { |
| 292 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) { |
| 293 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) { |
| 294 | ALOGE("Can't obtain scaling governor."); |
| 295 | return HINT_NONE; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (!on) { |
| 302 | /* Display off. */ |
| 303 | if (is_interactive_governor(governor)) { |
| 304 | int resource_values[] = { |
| 305 | INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_50, |
| 306 | INT_OP_CLUSTER1_TIMER_RATE, BIG_LITTLE_TR_MS_50, |
| 307 | INT_OP_NOTIFY_ON_MIGRATE, 0x00 |
| 308 | }; |
| 309 | perform_hint_action(DISPLAY_STATE_HINT_ID, |
| 310 | resource_values, ARRAY_SIZE(resource_values)); |
| 311 | } |
| 312 | } else { |
| 313 | /* Display on. */ |
| 314 | if (is_interactive_governor(governor)) { |
| 315 | undo_hint_action(DISPLAY_STATE_HINT_ID); |
| 316 | } |
| 317 | } |
| 318 | return HINT_HANDLED; |
| 319 | } |