Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
Michael Bestas | c1a9261 | 2018-01-22 02:30:25 +0200 | [diff] [blame] | 3 | * Copyright (C) 2018 The LineageOS Project |
Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [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 | */ |
| 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 "QCOM PowerHAL" |
Ethan Chen | a42f4a8 | 2018-03-01 21:31:15 -0800 | [diff] [blame] | 41 | #include <log/log.h> |
Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [diff] [blame] | 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 | |
Zhoulu Luo | 3b1fbdb | 2013-12-06 16:32:10 -0800 | [diff] [blame] | 51 | static int first_display_off_hint; |
| 52 | extern int display_boost; |
Anurag Singh | b15323a | 2013-06-08 16:55:44 -0700 | [diff] [blame] | 53 | |
Michael Bestas | c1a9261 | 2018-01-22 02:30:25 +0200 | [diff] [blame] | 54 | static int current_power_profile = PROFILE_BALANCED; |
| 55 | |
| 56 | static int profile_high_performance[] = { |
| 57 | CPUS_ONLINE_MIN_4, |
| 58 | 0x0901, |
| 59 | CPU0_MIN_FREQ_TURBO_MAX, |
| 60 | CPU1_MIN_FREQ_TURBO_MAX, |
| 61 | CPU2_MIN_FREQ_TURBO_MAX, |
| 62 | CPU3_MIN_FREQ_TURBO_MAX |
| 63 | }; |
| 64 | |
| 65 | static int profile_power_save[] = { |
| 66 | 0x0A03, |
| 67 | CPUS_ONLINE_MAX_LIMIT_2, |
| 68 | CPU0_MAX_FREQ_NONTURBO_MAX, |
| 69 | CPU1_MAX_FREQ_NONTURBO_MAX, |
| 70 | CPU2_MAX_FREQ_NONTURBO_MAX, |
| 71 | CPU3_MAX_FREQ_NONTURBO_MAX |
| 72 | }; |
| 73 | |
| 74 | static int profile_bias_power[] = { |
| 75 | 0x0A03, |
| 76 | CPU0_MAX_FREQ_NONTURBO_MAX, |
| 77 | CPU1_MAX_FREQ_NONTURBO_MAX, |
| 78 | CPU1_MAX_FREQ_NONTURBO_MAX, |
| 79 | CPU2_MAX_FREQ_NONTURBO_MAX |
| 80 | }; |
| 81 | |
| 82 | static int profile_bias_performance[] = { |
| 83 | CPU0_MIN_FREQ_NONTURBO_MAX + 1, |
| 84 | CPU1_MIN_FREQ_NONTURBO_MAX + 1, |
| 85 | CPU2_MIN_FREQ_NONTURBO_MAX + 1, |
| 86 | CPU2_MIN_FREQ_NONTURBO_MAX + 1 |
| 87 | }; |
| 88 | |
dianlujitao | 4004811 | 2018-03-02 12:40:04 +0800 | [diff] [blame] | 89 | #ifdef INTERACTION_BOOST |
Michael Bestas | c1a9261 | 2018-01-22 02:30:25 +0200 | [diff] [blame] | 90 | int get_number_of_profiles() { |
| 91 | return 5; |
| 92 | } |
dianlujitao | 4004811 | 2018-03-02 12:40:04 +0800 | [diff] [blame] | 93 | #endif |
Michael Bestas | c1a9261 | 2018-01-22 02:30:25 +0200 | [diff] [blame] | 94 | |
| 95 | static void set_power_profile(int profile) { |
| 96 | |
| 97 | if (profile == current_power_profile) |
| 98 | return; |
| 99 | |
| 100 | ALOGV("%s: Profile=%d", __func__, profile); |
| 101 | |
| 102 | if (current_power_profile != PROFILE_BALANCED) { |
| 103 | undo_hint_action(DEFAULT_PROFILE_HINT_ID); |
| 104 | ALOGV("%s: Hint undone", __func__); |
| 105 | } |
| 106 | |
| 107 | if (profile == PROFILE_POWER_SAVE) { |
| 108 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save, |
| 109 | ARRAY_SIZE(profile_power_save)); |
| 110 | ALOGD("%s: Set powersave mode", __func__); |
| 111 | |
| 112 | } else if (profile == PROFILE_HIGH_PERFORMANCE) { |
| 113 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_high_performance, |
| 114 | ARRAY_SIZE(profile_high_performance)); |
| 115 | ALOGD("%s: Set performance mode", __func__); |
| 116 | |
| 117 | } else if (profile == PROFILE_BIAS_POWER) { |
| 118 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power, |
| 119 | ARRAY_SIZE(profile_bias_power)); |
| 120 | ALOGD("%s: Set bias power mode", __func__); |
| 121 | |
| 122 | } else if (profile == PROFILE_BIAS_PERFORMANCE) { |
| 123 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_performance, |
| 124 | ARRAY_SIZE(profile_bias_performance)); |
| 125 | ALOGD("%s: Set bias perf mode", __func__); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | current_power_profile = profile; |
| 130 | } |
| 131 | |
| 132 | void interaction(int duration, int num_args, int opt_list[]); |
| 133 | |
| 134 | int power_hint_override(power_hint_t hint, void *data) |
| 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 high/low power modes |
| 142 | if (current_power_profile == PROFILE_POWER_SAVE || |
| 143 | current_power_profile == PROFILE_HIGH_PERFORMANCE) { |
| 144 | return HINT_HANDLED; |
| 145 | } |
| 146 | |
| 147 | if (hint == POWER_HINT_LAUNCH) { |
| 148 | int duration = 2000; |
| 149 | int resources[] = { CPUS_ONLINE_MIN_3, |
| 150 | CPU0_MIN_FREQ_TURBO_MAX, CPU1_MIN_FREQ_TURBO_MAX, |
| 151 | CPU2_MIN_FREQ_TURBO_MAX, CPU3_MIN_FREQ_TURBO_MAX }; |
| 152 | |
| 153 | interaction(duration, ARRAY_SIZE(resources), resources); |
| 154 | |
| 155 | return HINT_HANDLED; |
| 156 | } |
| 157 | |
| 158 | if (hint == POWER_HINT_INTERACTION) { |
| 159 | int duration = 500, duration_hint = 0; |
| 160 | static struct timespec s_previous_boost_timespec; |
| 161 | struct timespec cur_boost_timespec; |
| 162 | long long elapsed_time; |
| 163 | |
| 164 | if (data) { |
| 165 | duration_hint = *((int *)data); |
| 166 | } |
| 167 | |
| 168 | duration = duration_hint > 0 ? duration_hint : 500; |
| 169 | |
| 170 | clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec); |
| 171 | elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec); |
| 172 | if (elapsed_time > 750000) |
| 173 | elapsed_time = 750000; |
| 174 | // don't hint if it's been less than 250ms since last boost |
| 175 | // also detect if we're doing anything resembling a fling |
| 176 | // support additional boosting in case of flings |
| 177 | else if (elapsed_time < 250000 && duration <= 750) |
| 178 | return HINT_HANDLED; |
| 179 | |
| 180 | s_previous_boost_timespec = cur_boost_timespec; |
| 181 | |
| 182 | int resources[] = { (duration >= 2000 ? CPUS_ONLINE_MIN_3 : CPUS_ONLINE_MIN_2), |
| 183 | 0x20F, 0x30F, 0x40F, 0x50F }; |
| 184 | |
| 185 | if (duration) |
| 186 | interaction(duration, ARRAY_SIZE(resources), resources); |
| 187 | |
| 188 | return HINT_HANDLED; |
| 189 | } |
| 190 | |
| 191 | return HINT_NONE; |
| 192 | } |
| 193 | |
Rashed Abdel-Tawab | a2a7071 | 2017-12-31 01:21:40 +0200 | [diff] [blame] | 194 | int set_interactive_override(int on) |
Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [diff] [blame] | 195 | { |
| 196 | char governor[80]; |
| 197 | |
| 198 | if (get_scaling_governor(governor, sizeof(governor)) == -1) { |
| 199 | ALOGE("Can't obtain scaling governor."); |
| 200 | |
| 201 | return HINT_NONE; |
| 202 | } |
| 203 | |
| 204 | if (!on) { |
| 205 | /* Display off. */ |
Zhoulu Luo | 3b1fbdb | 2013-12-06 16:32:10 -0800 | [diff] [blame] | 206 | /* |
| 207 | * We need to be able to identify the first display off hint |
| 208 | * and release the current lock holder |
| 209 | */ |
| 210 | if (display_boost) { |
| 211 | if (!first_display_off_hint) { |
| 212 | undo_initial_hint_action(); |
| 213 | first_display_off_hint = 1; |
| 214 | } |
| 215 | /* used for all subsequent toggles to the display */ |
Zhao Wei Liew | 5a81c9a | 2016-06-26 21:29:34 +0800 | [diff] [blame^] | 216 | undo_hint_action(DISPLAY_STATE_HINT_ID_2); |
Zhoulu Luo | 3b1fbdb | 2013-12-06 16:32:10 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [diff] [blame] | 219 | if ((strncmp(governor, ONDEMAND_GOVERNOR, strlen(ONDEMAND_GOVERNOR)) == 0) && |
| 220 | (strlen(governor) == strlen(ONDEMAND_GOVERNOR))) { |
Badhri Jagan Sridharan | 5f11819 | 2013-07-16 12:07:56 -0700 | [diff] [blame] | 221 | int resource_values[] = {MS_500, SYNC_FREQ_600, OPTIMAL_FREQ_600, THREAD_MIGRATION_SYNC_OFF}; |
Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [diff] [blame] | 222 | |
Zhao Wei Liew | 5a81c9a | 2016-06-26 21:29:34 +0800 | [diff] [blame^] | 223 | perform_hint_action(DISPLAY_STATE_HINT_ID, |
| 224 | resource_values, ARRAY_SIZE(resource_values)); |
Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [diff] [blame] | 225 | |
| 226 | return HINT_HANDLED; |
| 227 | } |
| 228 | } else { |
| 229 | /* Display on */ |
Zhao Wei Liew | 5a81c9a | 2016-06-26 21:29:34 +0800 | [diff] [blame^] | 230 | if (display_boost) { |
Zhoulu Luo | 3b1fbdb | 2013-12-06 16:32:10 -0800 | [diff] [blame] | 231 | int resource_values2[] = {CPUS_ONLINE_MIN_2}; |
| 232 | perform_hint_action(DISPLAY_STATE_HINT_ID_2, |
Zhao Wei Liew | d4fe61e | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 233 | resource_values2, ARRAY_SIZE(resource_values2)); |
Zhoulu Luo | 3b1fbdb | 2013-12-06 16:32:10 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [diff] [blame] | 236 | if ((strncmp(governor, ONDEMAND_GOVERNOR, strlen(ONDEMAND_GOVERNOR)) == 0) && |
| 237 | (strlen(governor) == strlen(ONDEMAND_GOVERNOR))) { |
| 238 | undo_hint_action(DISPLAY_STATE_HINT_ID); |
Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [diff] [blame] | 239 | |
| 240 | return HINT_HANDLED; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return HINT_NONE; |
| 245 | } |