Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, The Linux Foundation. All rights reserved. |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 3 | * Copyright (C) 2018 The LineageOS Project |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -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 | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 31 | #define LOG_NIDEBUG 0 |
| 32 | |
| 33 | #include <errno.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <dlfcn.h> |
| 39 | #include <stdlib.h> |
| 40 | |
| 41 | #define LOG_TAG "QCOM PowerHAL" |
Ethan Chen | a42f4a8 | 2018-03-01 21:31:15 -0800 | [diff] [blame] | 42 | #include <log/log.h> |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 43 | #include <hardware/hardware.h> |
| 44 | #include <hardware/power.h> |
| 45 | |
| 46 | #include "utils.h" |
| 47 | #include "metadata-defs.h" |
| 48 | #include "hint-data.h" |
| 49 | #include "performance.h" |
| 50 | #include "power-common.h" |
| 51 | |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 52 | static int first_display_off_hint; |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 53 | |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 54 | static int current_power_profile = PROFILE_BALANCED; |
| 55 | |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 56 | static int profile_power_save[] = { |
| 57 | 0x0A03, |
| 58 | CPUS_ONLINE_MAX_LIMIT_2, |
Stefan Assmann | 651aa0f | 2018-07-23 18:42:02 +0200 | [diff] [blame^] | 59 | CPU0_MAX_FREQ_NONTURBO_MAX + 1, |
| 60 | CPU1_MAX_FREQ_NONTURBO_MAX + 1, |
| 61 | CPU2_MAX_FREQ_NONTURBO_MAX + 1, |
| 62 | CPU3_MAX_FREQ_NONTURBO_MAX + 1 |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static int profile_bias_power[] = { |
| 66 | 0x0A03, |
Stefan Assmann | 651aa0f | 2018-07-23 18:42:02 +0200 | [diff] [blame^] | 67 | CPUS_ONLINE_MAX_LIMIT_2, |
| 68 | CPU0_MAX_FREQ_NONTURBO_MAX + 14, |
| 69 | CPU1_MAX_FREQ_NONTURBO_MAX + 14, |
| 70 | CPU2_MAX_FREQ_NONTURBO_MAX + 14, |
| 71 | CPU3_MAX_FREQ_NONTURBO_MAX + 14, |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static int profile_bias_performance[] = { |
Stefan Assmann | 651aa0f | 2018-07-23 18:42:02 +0200 | [diff] [blame^] | 75 | CPUS_ONLINE_MIN_2, |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 76 | CPU0_MIN_FREQ_NONTURBO_MAX + 1, |
| 77 | CPU1_MIN_FREQ_NONTURBO_MAX + 1, |
| 78 | CPU2_MIN_FREQ_NONTURBO_MAX + 1, |
Stefan Assmann | 651aa0f | 2018-07-23 18:42:02 +0200 | [diff] [blame^] | 79 | CPU3_MIN_FREQ_NONTURBO_MAX + 1 |
| 80 | }; |
| 81 | |
| 82 | static int profile_high_performance[] = { |
| 83 | 0x0901, |
| 84 | CPUS_ONLINE_MIN_4, |
| 85 | CPU0_MIN_FREQ_NONTURBO_MAX + 5, |
| 86 | CPU1_MIN_FREQ_NONTURBO_MAX + 5, |
| 87 | CPU2_MIN_FREQ_NONTURBO_MAX + 5, |
| 88 | CPU3_MIN_FREQ_NONTURBO_MAX + 5 |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | #ifdef INTERACTION_BOOST |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 92 | int get_number_of_profiles() |
| 93 | { |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 94 | return 5; |
| 95 | } |
| 96 | #endif |
| 97 | |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 98 | static void set_power_profile(int profile) |
| 99 | { |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 100 | if (profile == current_power_profile) |
| 101 | return; |
| 102 | |
| 103 | ALOGV("%s: Profile=%d", __func__, profile); |
| 104 | |
| 105 | if (current_power_profile != PROFILE_BALANCED) { |
| 106 | undo_hint_action(DEFAULT_PROFILE_HINT_ID); |
| 107 | ALOGV("%s: Hint undone", __func__); |
| 108 | } |
| 109 | |
| 110 | if (profile == PROFILE_POWER_SAVE) { |
| 111 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save, |
| 112 | ARRAY_SIZE(profile_power_save)); |
| 113 | ALOGD("%s: Set powersave mode", __func__); |
| 114 | |
| 115 | } else if (profile == PROFILE_HIGH_PERFORMANCE) { |
| 116 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_high_performance, |
| 117 | ARRAY_SIZE(profile_high_performance)); |
| 118 | ALOGD("%s: Set performance mode", __func__); |
| 119 | |
| 120 | } else if (profile == PROFILE_BIAS_POWER) { |
| 121 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power, |
| 122 | ARRAY_SIZE(profile_bias_power)); |
| 123 | ALOGD("%s: Set bias power mode", __func__); |
| 124 | |
| 125 | } else if (profile == PROFILE_BIAS_PERFORMANCE) { |
| 126 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_performance, |
| 127 | ARRAY_SIZE(profile_bias_performance)); |
| 128 | ALOGD("%s: Set bias perf mode", __func__); |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | current_power_profile = profile; |
| 132 | } |
| 133 | |
| 134 | static int resources_interaction_fling_boost[] = { |
| 135 | CPUS_ONLINE_MIN_3, |
| 136 | 0x20F, |
| 137 | 0x30F, |
| 138 | 0x40F, |
| 139 | 0x50F |
| 140 | }; |
| 141 | |
| 142 | static int resources_interaction_boost[] = { |
| 143 | CPUS_ONLINE_MIN_2, |
| 144 | 0x20F, |
| 145 | 0x30F, |
| 146 | 0x40F, |
| 147 | 0x50F |
| 148 | }; |
| 149 | |
| 150 | static int resources_launch[] = { |
| 151 | CPUS_ONLINE_MIN_3, |
| 152 | CPU0_MIN_FREQ_TURBO_MAX, |
| 153 | CPU1_MIN_FREQ_TURBO_MAX, |
| 154 | CPU2_MIN_FREQ_TURBO_MAX, |
| 155 | CPU3_MIN_FREQ_TURBO_MAX |
| 156 | }; |
| 157 | |
| 158 | int power_hint_override(power_hint_t hint, void *data) |
| 159 | { |
| 160 | static struct timespec s_previous_boost_timespec; |
| 161 | struct timespec cur_boost_timespec; |
| 162 | long long elapsed_time; |
| 163 | static int s_previous_duration = 0; |
| 164 | int duration; |
| 165 | |
| 166 | if (hint == POWER_HINT_SET_PROFILE) { |
| 167 | set_power_profile(*(int32_t *)data); |
| 168 | return HINT_HANDLED; |
| 169 | } |
| 170 | |
| 171 | // Skip other hints in high/low power modes |
| 172 | if (current_power_profile == PROFILE_POWER_SAVE || |
| 173 | current_power_profile == PROFILE_HIGH_PERFORMANCE) { |
| 174 | return HINT_HANDLED; |
| 175 | } |
| 176 | |
| 177 | switch (hint) { |
| 178 | case POWER_HINT_INTERACTION: |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 179 | duration = 500; // 500ms by default |
| 180 | if (data) { |
| 181 | int input_duration = *((int*)data); |
| 182 | if (input_duration > duration) { |
| 183 | duration = (input_duration > 5000) ? 5000 : input_duration; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec); |
| 188 | |
| 189 | elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec); |
| 190 | // don't hint if previous hint's duration covers this hint's duration |
| 191 | if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) { |
| 192 | return HINT_HANDLED; |
| 193 | } |
| 194 | s_previous_boost_timespec = cur_boost_timespec; |
| 195 | s_previous_duration = duration; |
| 196 | |
| 197 | if (duration >= 1500) { |
| 198 | interaction(duration, ARRAY_SIZE(resources_interaction_fling_boost), |
| 199 | resources_interaction_fling_boost); |
| 200 | } else { |
| 201 | interaction(duration, ARRAY_SIZE(resources_interaction_boost), |
| 202 | resources_interaction_boost); |
| 203 | } |
| 204 | return HINT_HANDLED; |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 205 | case POWER_HINT_LAUNCH: |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 206 | duration = 2000; |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 207 | interaction(duration, ARRAY_SIZE(resources_launch), |
| 208 | resources_launch); |
| 209 | return HINT_HANDLED; |
Michael Bestas | 32ae7b0 | 2018-03-26 01:52:03 +0300 | [diff] [blame] | 210 | default: |
| 211 | break; |
| 212 | } |
| 213 | return HINT_NONE; |
| 214 | } |
| 215 | |
Rashed Abdel-Tawab | a2a7071 | 2017-12-31 01:21:40 +0200 | [diff] [blame] | 216 | int set_interactive_override(int on) |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 217 | { |
| 218 | char governor[80]; |
| 219 | |
| 220 | if (get_scaling_governor(governor, sizeof(governor)) == -1) { |
| 221 | ALOGE("Can't obtain scaling governor."); |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 222 | return HINT_NONE; |
| 223 | } |
| 224 | |
| 225 | if (!on) { |
| 226 | /* Display off. */ |
| 227 | /* |
| 228 | * We need to be able to identify the first display off hint |
| 229 | * and release the current lock holder |
| 230 | */ |
Zhao Wei Liew | be1a5d5 | 2016-07-19 20:01:01 +0800 | [diff] [blame] | 231 | if (!first_display_off_hint) { |
| 232 | undo_initial_hint_action(); |
| 233 | first_display_off_hint = 1; |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 234 | } |
Zhao Wei Liew | be1a5d5 | 2016-07-19 20:01:01 +0800 | [diff] [blame] | 235 | /* Used for all subsequent toggles to the display */ |
| 236 | undo_hint_action(DISPLAY_STATE_HINT_ID_2); |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 237 | |
Michael Bestas | fc62e7c | 2018-03-25 23:14:21 +0300 | [diff] [blame] | 238 | if (is_ondemand_governor(governor)) { |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 239 | int resource_values[] = { |
| 240 | MS_500, SYNC_FREQ_600, OPTIMAL_FREQ_600, THREAD_MIGRATION_SYNC_OFF |
| 241 | }; |
Zhao Wei Liew | 5a81c9a | 2016-06-26 21:29:34 +0800 | [diff] [blame] | 242 | perform_hint_action(DISPLAY_STATE_HINT_ID, |
| 243 | resource_values, ARRAY_SIZE(resource_values)); |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 244 | } |
| 245 | } else { |
| 246 | /* Display on */ |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 247 | int resource_values2[] = { |
| 248 | CPUS_ONLINE_MIN_2 |
| 249 | }; |
Zhao Wei Liew | be1a5d5 | 2016-07-19 20:01:01 +0800 | [diff] [blame] | 250 | perform_hint_action(DISPLAY_STATE_HINT_ID_2, |
| 251 | resource_values2, ARRAY_SIZE(resource_values2)); |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 252 | |
Michael Bestas | fc62e7c | 2018-03-25 23:14:21 +0300 | [diff] [blame] | 253 | if (is_ondemand_governor(governor)) { |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 254 | undo_hint_action(DISPLAY_STATE_HINT_ID); |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 257 | return HINT_HANDLED; |
Dilip Gudlur | 4eca13c | 2014-01-14 15:34:46 -0800 | [diff] [blame] | 258 | } |