Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 3 | * Copyright (C) 2018 The LineageOS Project |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 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 | */ |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 30 | |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 31 | #define LOG_NIDEBUG 0 |
| 32 | |
| 33 | #include <errno.h> |
Craig Tatlor | adf8a1c | 2018-05-21 17:22:31 +0100 | [diff] [blame^] | 34 | #include <time.h> |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [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" |
Ethan Chen | a42f4a8 | 2018-03-01 21:31:15 -0800 | [diff] [blame] | 43 | #include <log/log.h> |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 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 | |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 53 | static int current_power_profile = PROFILE_BALANCED; |
| 54 | |
| 55 | static int profile_high_performance[] = { |
| 56 | SCHED_BOOST_ON, CPUS_ONLINE_MAX, |
| 57 | ALL_CPUS_PWR_CLPS_DIS, 0x0901, |
| 58 | CPU0_MIN_FREQ_TURBO_MAX, |
| 59 | CPU1_MIN_FREQ_TURBO_MAX, |
| 60 | CPU2_MIN_FREQ_TURBO_MAX, |
| 61 | CPU3_MIN_FREQ_TURBO_MAX, |
| 62 | CPU4_MIN_FREQ_TURBO_MAX, |
| 63 | CPU5_MIN_FREQ_TURBO_MAX |
| 64 | }; |
| 65 | |
| 66 | static int profile_power_save[] = { |
| 67 | CPUS_ONLINE_MPD_OVERRIDE, 0x0A03, |
| 68 | CPU0_MAX_FREQ_NONTURBO_MAX - 2, |
| 69 | CPU1_MAX_FREQ_NONTURBO_MAX - 2, |
| 70 | CPU2_MAX_FREQ_NONTURBO_MAX - 2, |
| 71 | CPU3_MAX_FREQ_NONTURBO_MAX - 2, |
| 72 | CPU4_MAX_FREQ_NONTURBO_MAX - 2, |
| 73 | CPU5_MAX_FREQ_NONTURBO_MAX - 2 |
| 74 | }; |
| 75 | |
| 76 | static int profile_bias_power[] = { |
| 77 | 0x0A03, 0x0902, |
| 78 | CPU0_MAX_FREQ_NONTURBO_MAX - 2, |
| 79 | CPU1_MAX_FREQ_NONTURBO_MAX - 2, |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 80 | CPU2_MAX_FREQ_NONTURBO_MAX - 2, |
Han Wang | 34c7410 | 2018-07-24 17:12:14 +0200 | [diff] [blame] | 81 | CPU3_MAX_FREQ_NONTURBO_MAX - 2, |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 82 | CPU4_MAX_FREQ_NONTURBO_MAX, |
| 83 | CPU5_MAX_FREQ_NONTURBO_MAX |
| 84 | }; |
| 85 | |
| 86 | static int profile_bias_performance[] = { |
| 87 | CPUS_ONLINE_MAX_LIMIT_MAX, |
| 88 | CPU4_MIN_FREQ_NONTURBO_MAX + 1, |
| 89 | CPU5_MIN_FREQ_NONTURBO_MAX + 1 |
| 90 | }; |
| 91 | |
| 92 | #ifdef INTERACTION_BOOST |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 93 | int get_number_of_profiles() |
| 94 | { |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 95 | return 5; |
| 96 | } |
| 97 | #endif |
| 98 | |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 99 | static int set_power_profile(int profile) |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 100 | { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 101 | int ret = -EINVAL; |
| 102 | const char *profile_name = NULL; |
| 103 | |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 104 | if (profile == current_power_profile) |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 105 | return 0; |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 106 | |
| 107 | ALOGV("%s: Profile=%d", __func__, profile); |
| 108 | |
| 109 | if (current_power_profile != PROFILE_BALANCED) { |
| 110 | undo_hint_action(DEFAULT_PROFILE_HINT_ID); |
| 111 | ALOGV("%s: Hint undone", __func__); |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 112 | current_power_profile = PROFILE_BALANCED; |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if (profile == PROFILE_POWER_SAVE) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 116 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save, |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 117 | ARRAY_SIZE(profile_power_save)); |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 118 | profile_name = "powersave"; |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 119 | |
| 120 | } else if (profile == PROFILE_HIGH_PERFORMANCE) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 121 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
| 122 | profile_high_performance, ARRAY_SIZE(profile_high_performance)); |
| 123 | profile_name = "performance"; |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 124 | |
| 125 | } else if (profile == PROFILE_BIAS_POWER) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 126 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power, |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 127 | ARRAY_SIZE(profile_bias_power)); |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 128 | profile_name = "bias power"; |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 129 | |
| 130 | } else if (profile == PROFILE_BIAS_PERFORMANCE) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 131 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
| 132 | profile_bias_performance, ARRAY_SIZE(profile_bias_performance)); |
| 133 | profile_name = "bias perf"; |
| 134 | } else if (profile == PROFILE_BALANCED) { |
| 135 | ret = 0; |
| 136 | profile_name = "balanced"; |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 137 | } |
| 138 | |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 139 | if (ret == 0) { |
| 140 | current_power_profile = profile; |
| 141 | ALOGD("%s: Set %s mode", __func__, profile_name); |
| 142 | } |
| 143 | return ret; |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 144 | } |
| 145 | |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 146 | static void process_video_encode_hint(void *metadata) |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 147 | { |
| 148 | char governor[80]; |
| 149 | struct video_encode_metadata_t video_encode_metadata; |
| 150 | |
| 151 | if (get_scaling_governor(governor, sizeof(governor)) == -1) { |
| 152 | ALOGE("Can't obtain scaling governor."); |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 153 | return; |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 156 | if (!metadata) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | /* Initialize encode metadata struct fields. */ |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 161 | memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t)); |
| 162 | video_encode_metadata.state = -1; |
| 163 | video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID; |
| 164 | |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 165 | if (parse_video_encode_metadata((char *)metadata, |
| 166 | &video_encode_metadata) == -1) { |
| 167 | ALOGE("Error occurred while parsing metadata."); |
| 168 | return; |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (video_encode_metadata.state == 1) { |
Ethan Chen | 4b4acdf | 2018-03-01 21:41:04 -0800 | [diff] [blame] | 172 | if (is_interactive_governor(governor)) { |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 173 | /* sched and cpufreq params |
| 174 | * hispeed freq - 768 MHz |
| 175 | * target load - 90 |
| 176 | * above_hispeed_delay - 40ms |
| 177 | * sched_small_tsk - 50 |
| 178 | */ |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 179 | int resource_values[] = { |
| 180 | 0x2C07, 0x2F5A, 0x2704, 0x4032 |
| 181 | }; |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 182 | perform_hint_action(video_encode_metadata.hint_id, |
Zhao Wei Liew | d4fe61e | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 183 | resource_values, ARRAY_SIZE(resource_values)); |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 184 | } |
| 185 | } else if (video_encode_metadata.state == 0) { |
Ethan Chen | 4b4acdf | 2018-03-01 21:41:04 -0800 | [diff] [blame] | 186 | if (is_interactive_governor(governor)) { |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 187 | undo_hint_action(video_encode_metadata.hint_id); |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 192 | static int resources_interaction_fling_boost[] = { |
| 193 | ALL_CPUS_PWR_CLPS_DIS, |
| 194 | SCHED_BOOST_ON, |
| 195 | SCHED_PREFER_IDLE_DIS |
| 196 | }; |
| 197 | |
| 198 | static int resources_interaction_boost[] = { |
| 199 | ALL_CPUS_PWR_CLPS_DIS, |
| 200 | SCHED_PREFER_IDLE_DIS |
| 201 | }; |
| 202 | |
| 203 | static int resources_launch[] = { |
| 204 | SCHED_BOOST_ON, |
| 205 | 0x20C |
| 206 | }; |
| 207 | |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 208 | int power_hint_override(power_hint_t hint, void *data) |
| 209 | { |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 210 | static struct timespec s_previous_boost_timespec; |
| 211 | struct timespec cur_boost_timespec; |
| 212 | long long elapsed_time; |
| 213 | static int s_previous_duration = 0; |
| 214 | int duration; |
| 215 | |
| 216 | if (hint == POWER_HINT_SET_PROFILE) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 217 | if (set_power_profile(*(int32_t *)data) < 0) |
| 218 | ALOGE("Setting power profile failed. perfd not started?"); |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 219 | return HINT_HANDLED; |
| 220 | } |
| 221 | |
| 222 | // Skip other hints in high/low power modes |
| 223 | if (current_power_profile == PROFILE_POWER_SAVE || |
| 224 | current_power_profile == PROFILE_HIGH_PERFORMANCE) { |
| 225 | return HINT_HANDLED; |
| 226 | } |
| 227 | |
| 228 | switch (hint) { |
| 229 | case POWER_HINT_INTERACTION: |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 230 | duration = 500; // 500ms by default |
| 231 | if (data) { |
| 232 | int input_duration = *((int*)data); |
| 233 | if (input_duration > duration) { |
| 234 | duration = (input_duration > 5000) ? 5000 : input_duration; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec); |
| 239 | |
| 240 | elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec); |
| 241 | // don't hint if previous hint's duration covers this hint's duration |
| 242 | if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) { |
| 243 | return HINT_HANDLED; |
| 244 | } |
| 245 | s_previous_boost_timespec = cur_boost_timespec; |
| 246 | s_previous_duration = duration; |
| 247 | |
| 248 | if (duration >= 1500) { |
| 249 | interaction(duration, ARRAY_SIZE(resources_interaction_fling_boost), |
| 250 | resources_interaction_fling_boost); |
| 251 | } else { |
| 252 | interaction(duration, ARRAY_SIZE(resources_interaction_boost), |
| 253 | resources_interaction_boost); |
| 254 | } |
| 255 | return HINT_HANDLED; |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 256 | case POWER_HINT_LAUNCH: |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 257 | duration = 2000; |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 258 | interaction(duration, ARRAY_SIZE(resources_launch), |
| 259 | resources_launch); |
| 260 | return HINT_HANDLED; |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 261 | case POWER_HINT_VIDEO_ENCODE: |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 262 | process_video_encode_hint(data); |
| 263 | return HINT_HANDLED; |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 264 | default: |
| 265 | break; |
| 266 | } |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 267 | return HINT_NONE; |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | int set_interactive_override(int on) |
| 271 | { |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 272 | char governor[80]; |
| 273 | |
| 274 | if (get_scaling_governor(governor, sizeof(governor)) == -1) { |
| 275 | ALOGE("Can't obtain scaling governor."); |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 276 | return HINT_NONE; |
| 277 | } |
| 278 | |
| 279 | if (!on) { |
| 280 | /* Display off */ |
Ethan Chen | 4b4acdf | 2018-03-01 21:41:04 -0800 | [diff] [blame] | 281 | if (is_interactive_governor(governor)) { |
Michael Bestas | e555395 | 2018-03-26 03:00:15 +0300 | [diff] [blame] | 282 | // sched upmigrate = 99, sched downmigrate = 95 |
| 283 | // keep the big cores around, but make them very hard to use |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 284 | int resource_values[] = { |
| 285 | 0x4E63, 0x4F5F |
| 286 | }; |
Zhao Wei Liew | 5a81c9a | 2016-06-26 21:29:34 +0800 | [diff] [blame] | 287 | perform_hint_action(DISPLAY_STATE_HINT_ID, |
| 288 | resource_values, ARRAY_SIZE(resource_values)); |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 289 | } |
| 290 | } else { |
| 291 | /* Display on */ |
Ethan Chen | 4b4acdf | 2018-03-01 21:41:04 -0800 | [diff] [blame] | 292 | if (is_interactive_governor(governor)) { |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 293 | undo_hint_action(DISPLAY_STATE_HINT_ID); |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 296 | return HINT_HANDLED; |
Dilip Gudlur | e9f9c98 | 2015-01-28 15:17:45 -0800 | [diff] [blame] | 297 | } |