Steve Kondik | 40ea4d6 | 2014-03-27 22:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, The CyanogenMod Project |
Steve Kondik | 40ea4d6 | 2014-03-27 22:00:00 -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" |
| 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 | |
| 51 | static int display_hint_sent; |
| 52 | static int display_hint2_sent; |
| 53 | static int first_display_off_hint; |
| 54 | extern int display_boost; |
| 55 | |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 56 | static int current_power_profile = PROFILE_BALANCED; |
| 57 | |
Steve Kondik | d9668ba | 2015-11-03 03:27:42 -0800 | [diff] [blame] | 58 | int get_number_of_profiles() { |
| 59 | return 5; |
| 60 | } |
| 61 | |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 62 | static void set_power_profile(int profile) { |
| 63 | |
| 64 | if (profile == current_power_profile) |
| 65 | return; |
| 66 | |
| 67 | ALOGV("%s: profile=%d", __func__, profile); |
| 68 | |
| 69 | if (current_power_profile != PROFILE_BALANCED) { |
| 70 | undo_hint_action(DEFAULT_PROFILE_HINT_ID); |
| 71 | ALOGV("%s: hint undone", __func__); |
| 72 | } |
| 73 | |
| 74 | if (profile == PROFILE_HIGH_PERFORMANCE) { |
Steve Kondik | 7964602 | 2015-10-29 23:37:54 -0700 | [diff] [blame] | 75 | int resource_values[] = { CPUS_ONLINE_MIN_4, 0x0901, |
Steve Kondik | 2e65c97 | 2014-10-30 01:03:41 -0700 | [diff] [blame] | 76 | CPU0_MIN_FREQ_TURBO_MAX, CPU1_MIN_FREQ_TURBO_MAX, |
| 77 | CPU2_MIN_FREQ_TURBO_MAX, CPU3_MIN_FREQ_TURBO_MAX }; |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 78 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 79 | resource_values, ARRAY_SIZE(resource_values)); |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 80 | ALOGD("%s: set performance mode", __func__); |
Steve Kondik | a58ddde | 2015-11-01 04:31:46 -0800 | [diff] [blame] | 81 | } else if (profile == PROFILE_BIAS_PERFORMANCE) { |
| 82 | int resource_values[] = { |
| 83 | CPU0_MIN_FREQ_NONTURBO_MAX + 1, CPU1_MIN_FREQ_NONTURBO_MAX + 1, |
| 84 | CPU2_MIN_FREQ_NONTURBO_MAX + 1, CPU2_MIN_FREQ_NONTURBO_MAX + 1 }; |
| 85 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 86 | resource_values, ARRAY_SIZE(resource_values)); |
Steve Kondik | a58ddde | 2015-11-01 04:31:46 -0800 | [diff] [blame] | 87 | ALOGD("%s: set bias perf mode", __func__); |
| 88 | } else if (profile == PROFILE_BIAS_POWER) { |
| 89 | int resource_values[] = { 0x0A03, |
| 90 | CPU0_MAX_FREQ_NONTURBO_MAX, CPU1_MAX_FREQ_NONTURBO_MAX, |
| 91 | CPU1_MAX_FREQ_NONTURBO_MAX, CPU2_MAX_FREQ_NONTURBO_MAX }; |
| 92 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 93 | resource_values, ARRAY_SIZE(resource_values)); |
Steve Kondik | a58ddde | 2015-11-01 04:31:46 -0800 | [diff] [blame] | 94 | ALOGD("%s: set bias power mode", __func__); |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 95 | } else if (profile == PROFILE_POWER_SAVE) { |
Steve Kondik | a58ddde | 2015-11-01 04:31:46 -0800 | [diff] [blame] | 96 | int resource_values[] = { 0x0A03, CPUS_ONLINE_MAX_LIMIT_2, |
Steve Kondik | 2e65c97 | 2014-10-30 01:03:41 -0700 | [diff] [blame] | 97 | CPU0_MAX_FREQ_NONTURBO_MAX, CPU1_MAX_FREQ_NONTURBO_MAX, |
| 98 | CPU2_MAX_FREQ_NONTURBO_MAX, CPU3_MAX_FREQ_NONTURBO_MAX }; |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 99 | perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 100 | resource_values, ARRAY_SIZE(resource_values)); |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 101 | ALOGD("%s: set powersave", __func__); |
| 102 | } |
| 103 | |
| 104 | current_power_profile = profile; |
| 105 | } |
| 106 | |
| 107 | extern void interaction(int duration, int num_args, int opt_list[]); |
| 108 | |
Steve Kondik | 1921d30 | 2014-12-08 01:37:30 -0800 | [diff] [blame] | 109 | int power_hint_override(__attribute__((unused)) struct power_module *module, |
| 110 | power_hint_t hint, void *data) |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 111 | { |
Steve Kondik | fc552c7 | 2015-09-04 04:21:44 -0700 | [diff] [blame] | 112 | if (hint == POWER_HINT_SET_PROFILE) { |
Steve Kondik | d9668ba | 2015-11-03 03:27:42 -0800 | [diff] [blame] | 113 | set_power_profile(*(int32_t *)data); |
Steve Kondik | fc552c7 | 2015-09-04 04:21:44 -0700 | [diff] [blame] | 114 | return HINT_HANDLED; |
| 115 | } |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 116 | |
Steve Kondik | a58ddde | 2015-11-01 04:31:46 -0800 | [diff] [blame] | 117 | // Skip other hints in high/low power modes |
| 118 | if (current_power_profile == PROFILE_POWER_SAVE || |
| 119 | current_power_profile == PROFILE_HIGH_PERFORMANCE) { |
Steve Kondik | fc552c7 | 2015-09-04 04:21:44 -0700 | [diff] [blame] | 120 | return HINT_HANDLED; |
| 121 | } |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 122 | |
Steve Kondik | fc552c7 | 2015-09-04 04:21:44 -0700 | [diff] [blame] | 123 | if (hint == POWER_HINT_LAUNCH_BOOST) { |
| 124 | int duration = 2000; |
| 125 | int resources[] = { CPUS_ONLINE_MIN_3, |
| 126 | CPU0_MIN_FREQ_TURBO_MAX, CPU1_MIN_FREQ_TURBO_MAX, |
| 127 | CPU2_MIN_FREQ_TURBO_MAX, CPU3_MIN_FREQ_TURBO_MAX }; |
| 128 | |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 129 | interaction(duration, ARRAY_SIZE(resources), resources); |
Steve Kondik | fc552c7 | 2015-09-04 04:21:44 -0700 | [diff] [blame] | 130 | |
| 131 | return HINT_HANDLED; |
| 132 | } |
| 133 | |
| 134 | if (hint == POWER_HINT_CPU_BOOST) { |
Steve Kondik | d9668ba | 2015-11-03 03:27:42 -0800 | [diff] [blame] | 135 | int duration = *(int32_t *)data / 1000; |
| 136 | int resources[] = { CPUS_ONLINE_MIN_2, |
| 137 | 0x20F, 0x30F, 0x40F, 0x50F }; |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 138 | |
Steve Kondik | fc552c7 | 2015-09-04 04:21:44 -0700 | [diff] [blame] | 139 | if (duration) |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 140 | interaction(duration, ARRAY_SIZE(resources), resources); |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 141 | |
| 142 | return HINT_HANDLED; |
Steve Kondik | fc552c7 | 2015-09-04 04:21:44 -0700 | [diff] [blame] | 143 | } |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 144 | |
Steve Kondik | 0125db4 | 2016-03-10 07:58:05 -0800 | [diff] [blame] | 145 | if (hint == POWER_HINT_INTERACTION) { |
| 146 | int duration = 500, duration_hint = 0; |
| 147 | static unsigned long long previous_boost_time = 0; |
| 148 | |
| 149 | if (data) { |
| 150 | duration_hint = *((int *)data); |
| 151 | } |
| 152 | |
| 153 | duration = duration_hint > 0 ? duration_hint : 500; |
| 154 | |
| 155 | struct timeval cur_boost_timeval = {0, 0}; |
| 156 | gettimeofday(&cur_boost_timeval, NULL); |
| 157 | unsigned long long cur_boost_time = cur_boost_timeval.tv_sec * 1000000 + cur_boost_timeval.tv_usec; |
| 158 | double elapsed_time = (double)(cur_boost_time - previous_boost_time); |
| 159 | if (elapsed_time > 750000) |
| 160 | elapsed_time = 750000; |
| 161 | // don't hint if it's been less than 250ms since last boost |
| 162 | // also detect if we're doing anything resembling a fling |
| 163 | // support additional boosting in case of flings |
| 164 | else if (elapsed_time < 250000 && duration <= 750) |
| 165 | return HINT_HANDLED; |
| 166 | |
| 167 | previous_boost_time = cur_boost_time; |
| 168 | |
| 169 | int resources[] = { (duration >= 2000 ? CPUS_ONLINE_MIN_3 : CPUS_ONLINE_MIN_2), |
| 170 | 0x20F, 0x30F, 0x40F, 0x50F }; |
| 171 | |
| 172 | if (duration) |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 173 | interaction(duration, ARRAY_SIZE(resources), resources); |
Steve Kondik | 0125db4 | 2016-03-10 07:58:05 -0800 | [diff] [blame] | 174 | |
| 175 | return HINT_HANDLED; |
| 176 | } |
| 177 | |
| 178 | |
Steve Kondik | fc552c7 | 2015-09-04 04:21:44 -0700 | [diff] [blame] | 179 | return HINT_NONE; |
Steve Kondik | c8b4822 | 2014-05-19 17:26:34 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Steve Kondik | a3cfade | 2015-01-02 23:38:44 -0800 | [diff] [blame] | 182 | int set_interactive_override(struct power_module *module __unused, int on) |
Steve Kondik | 40ea4d6 | 2014-03-27 22:00:00 -0700 | [diff] [blame] | 183 | { |
| 184 | char governor[80]; |
| 185 | |
| 186 | if (get_scaling_governor(governor, sizeof(governor)) == -1) { |
| 187 | ALOGE("Can't obtain scaling governor."); |
| 188 | |
| 189 | return HINT_NONE; |
| 190 | } |
| 191 | |
| 192 | if (!on) { |
| 193 | /* Display off. */ |
| 194 | /* |
| 195 | * We need to be able to identify the first display off hint |
| 196 | * and release the current lock holder |
| 197 | */ |
| 198 | if (display_boost) { |
| 199 | if (!first_display_off_hint) { |
| 200 | undo_initial_hint_action(); |
| 201 | first_display_off_hint = 1; |
| 202 | } |
| 203 | /* used for all subsequent toggles to the display */ |
| 204 | if (!display_hint2_sent) { |
| 205 | undo_hint_action(DISPLAY_STATE_HINT_ID_2); |
| 206 | display_hint2_sent = 1; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if ((strncmp(governor, ONDEMAND_GOVERNOR, strlen(ONDEMAND_GOVERNOR)) == 0) && |
| 211 | (strlen(governor) == strlen(ONDEMAND_GOVERNOR))) { |
| 212 | int resource_values[] = {MS_500, SYNC_FREQ_600, OPTIMAL_FREQ_600, THREAD_MIGRATION_SYNC_OFF}; |
| 213 | |
| 214 | if (!display_hint_sent) { |
| 215 | perform_hint_action(DISPLAY_STATE_HINT_ID, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 216 | resource_values, ARRAY_SIZE(resource_values)); |
Steve Kondik | 40ea4d6 | 2014-03-27 22:00:00 -0700 | [diff] [blame] | 217 | display_hint_sent = 1; |
| 218 | } |
| 219 | |
| 220 | return HINT_HANDLED; |
| 221 | } |
| 222 | } else { |
| 223 | /* Display on */ |
| 224 | if (display_boost && display_hint2_sent) { |
| 225 | int resource_values2[] = {CPUS_ONLINE_MIN_2}; |
| 226 | perform_hint_action(DISPLAY_STATE_HINT_ID_2, |
Zhao Wei Liew | 50a1ce2 | 2016-06-26 11:37:59 +0800 | [diff] [blame] | 227 | resource_values2, ARRAY_SIZE(resource_values2)); |
Steve Kondik | 40ea4d6 | 2014-03-27 22:00:00 -0700 | [diff] [blame] | 228 | display_hint2_sent = 0; |
| 229 | } |
| 230 | |
| 231 | if ((strncmp(governor, ONDEMAND_GOVERNOR, strlen(ONDEMAND_GOVERNOR)) == 0) && |
| 232 | (strlen(governor) == strlen(ONDEMAND_GOVERNOR))) { |
| 233 | undo_hint_action(DISPLAY_STATE_HINT_ID); |
| 234 | display_hint_sent = 0; |
| 235 | |
| 236 | return HINT_HANDLED; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return HINT_NONE; |
| 241 | } |