blob: 77d9721325a3991956e61e313976cfb9edccac0e [file] [log] [blame]
Dilip Gudlur4eca13c2014-01-14 15:34:46 -08001/*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
Michael Bestas32ae7b02018-03-26 01:52:03 +03003 * Copyright (C) 2018 The LineageOS Project
Dilip Gudlur4eca13c2014-01-14 15:34:46 -08004 *
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 Bestas47636f62018-05-25 21:30:28 +030030
Dilip Gudlur4eca13c2014-01-14 15:34:46 -080031#define LOG_NIDEBUG 0
32
33#include <errno.h>
Craig Tatloradf8a1c2018-05-21 17:22:31 +010034#include <time.h>
Dilip Gudlur4eca13c2014-01-14 15:34:46 -080035#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 Chena42f4a82018-03-01 21:31:15 -080043#include <log/log.h>
Dilip Gudlur4eca13c2014-01-14 15:34:46 -080044#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
Dilip Gudlur4eca13c2014-01-14 15:34:46 -080053static int first_display_off_hint;
Dilip Gudlur4eca13c2014-01-14 15:34:46 -080054
Michael Bestas32ae7b02018-03-26 01:52:03 +030055static int current_power_profile = PROFILE_BALANCED;
56
Corinna Vinschen8169e392018-08-09 12:51:12 +020057/* power save mode: max 2 CPUs, max 1.2 GHz */
Michael Bestas32ae7b02018-03-26 01:52:03 +030058static int profile_power_save[] = {
59 0x0A03,
60 CPUS_ONLINE_MAX_LIMIT_2,
Stefan Assmann651aa0f2018-07-23 18:42:02 +020061 CPU0_MAX_FREQ_NONTURBO_MAX + 1,
62 CPU1_MAX_FREQ_NONTURBO_MAX + 1,
63 CPU2_MAX_FREQ_NONTURBO_MAX + 1,
64 CPU3_MAX_FREQ_NONTURBO_MAX + 1
Michael Bestas32ae7b02018-03-26 01:52:03 +030065};
66
Corinna Vinschen8169e392018-08-09 12:51:12 +020067/* efficiency mode: max 2 CPUs, max 2.4 GHz */
Michael Bestas32ae7b02018-03-26 01:52:03 +030068static int profile_bias_power[] = {
69 0x0A03,
Stefan Assmann651aa0f2018-07-23 18:42:02 +020070 CPUS_ONLINE_MAX_LIMIT_2,
71 CPU0_MAX_FREQ_NONTURBO_MAX + 14,
72 CPU1_MAX_FREQ_NONTURBO_MAX + 14,
73 CPU2_MAX_FREQ_NONTURBO_MAX + 14,
74 CPU3_MAX_FREQ_NONTURBO_MAX + 14,
Michael Bestas32ae7b02018-03-26 01:52:03 +030075};
76
Corinna Vinschen8169e392018-08-09 12:51:12 +020077/* quick mode: min 2 CPUs, min 1.1 GHz */
Michael Bestas32ae7b02018-03-26 01:52:03 +030078static int profile_bias_performance[] = {
Stefan Assmann651aa0f2018-07-23 18:42:02 +020079 CPUS_ONLINE_MIN_2,
Michael Bestas32ae7b02018-03-26 01:52:03 +030080 CPU0_MIN_FREQ_NONTURBO_MAX + 1,
81 CPU1_MIN_FREQ_NONTURBO_MAX + 1,
82 CPU2_MIN_FREQ_NONTURBO_MAX + 1,
Stefan Assmann651aa0f2018-07-23 18:42:02 +020083 CPU3_MIN_FREQ_NONTURBO_MAX + 1
84};
85
Corinna Vinschen8169e392018-08-09 12:51:12 +020086/* performance mode: min 4 CPUs, min 1.5 GHz */
Stefan Assmann651aa0f2018-07-23 18:42:02 +020087static int profile_high_performance[] = {
88 0x0901,
89 CPUS_ONLINE_MIN_4,
90 CPU0_MIN_FREQ_NONTURBO_MAX + 5,
91 CPU1_MIN_FREQ_NONTURBO_MAX + 5,
92 CPU2_MIN_FREQ_NONTURBO_MAX + 5,
93 CPU3_MIN_FREQ_NONTURBO_MAX + 5
Michael Bestas32ae7b02018-03-26 01:52:03 +030094};
95
96#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +030097int get_number_of_profiles()
98{
Michael Bestas32ae7b02018-03-26 01:52:03 +030099 return 5;
100}
101#endif
102
dianlujitao8780cb72019-02-23 20:24:57 +0800103static int set_power_profile(void *data)
Michael Bestas47636f62018-05-25 21:30:28 +0300104{
dianlujitao8780cb72019-02-23 20:24:57 +0800105 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200106 int ret = -EINVAL;
107 const char *profile_name = NULL;
108
Michael Bestas32ae7b02018-03-26 01:52:03 +0300109 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200110 return 0;
Michael Bestas32ae7b02018-03-26 01:52:03 +0300111
112 ALOGV("%s: Profile=%d", __func__, profile);
113
114 if (current_power_profile != PROFILE_BALANCED) {
115 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
116 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200117 current_power_profile = PROFILE_BALANCED;
Michael Bestas32ae7b02018-03-26 01:52:03 +0300118 }
119
120 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200121 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
Michael Bestas32ae7b02018-03-26 01:52:03 +0300122 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200123 profile_name = "powersave";
Michael Bestas32ae7b02018-03-26 01:52:03 +0300124
125 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200126 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
127 profile_high_performance, ARRAY_SIZE(profile_high_performance));
128 profile_name = "performance";
Michael Bestas32ae7b02018-03-26 01:52:03 +0300129
130 } else if (profile == PROFILE_BIAS_POWER) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200131 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power,
Michael Bestas32ae7b02018-03-26 01:52:03 +0300132 ARRAY_SIZE(profile_bias_power));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200133 profile_name = "bias power";
Michael Bestas32ae7b02018-03-26 01:52:03 +0300134
135 } else if (profile == PROFILE_BIAS_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200136 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
137 profile_bias_performance, ARRAY_SIZE(profile_bias_performance));
138 profile_name = "bias perf";
139 } else if (profile == PROFILE_BALANCED) {
140 ret = 0;
141 profile_name = "balanced";
Michael Bestas32ae7b02018-03-26 01:52:03 +0300142 }
143
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200144 if (ret == 0) {
145 current_power_profile = profile;
146 ALOGD("%s: Set %s mode", __func__, profile_name);
147 }
148 return ret;
Michael Bestas32ae7b02018-03-26 01:52:03 +0300149}
150
Corinna Vinschene7505c52019-10-25 23:17:56 +0200151/* fling boost: min 3 CPUs, min 1.5 GHz */
Michael Bestas32ae7b02018-03-26 01:52:03 +0300152static int resources_interaction_fling_boost[] = {
153 CPUS_ONLINE_MIN_3,
Corinna Vinschene7505c52019-10-25 23:17:56 +0200154 CPU0_MIN_FREQ_NONTURBO_MAX + 5,
155 CPU1_MIN_FREQ_NONTURBO_MAX + 5,
Corinna Vinschen591f5902018-10-12 21:40:24 +0200156 CPU2_MIN_FREQ_NONTURBO_MAX + 5,
157 CPU3_MIN_FREQ_NONTURBO_MAX + 5
158};
159
Corinna Vinschene7505c52019-10-25 23:17:56 +0200160/* interactive boost: min 2 CPUs, min 1.5 GHz */
161static int resources_interaction_boost[] = {
162 CPUS_ONLINE_MIN_2,
163 CPU0_MIN_FREQ_NONTURBO_MAX + 5,
164 CPU1_MIN_FREQ_NONTURBO_MAX + 5,
165 CPU2_MIN_FREQ_NONTURBO_MAX + 5,
166 CPU3_MIN_FREQ_NONTURBO_MAX + 5
167};
168
169/* lauch boost: min 3 CPUs, full power */
170static int resources_launch[] = {
171 CPUS_ONLINE_MIN_3,
172 CPU0_MIN_FREQ_TURBO_MAX,
173 CPU1_MIN_FREQ_TURBO_MAX,
174 CPU2_MIN_FREQ_TURBO_MAX,
175 CPU3_MIN_FREQ_TURBO_MAX
176};
177
Corinna Vinschenf23b3332018-08-06 10:58:20 +0200178const int DEFAULT_INTERACTIVE_DURATION = 200; /* ms */
Corinna Vinschen7dd06962018-08-06 21:14:12 +0200179const int MIN_FLING_DURATION = 1500; /* ms */
180const int MAX_INTERACTIVE_DURATION = 5000; /* ms */
tomascus8b038b12019-02-19 17:15:58 +1100181const int MAX_LAUNCH_DURATION = 5000; /* ms */
182
183static int process_activity_launch_hint(void *data)
184{
185 static int launch_handle = -1;
186 static int launch_mode = 0;
187
188 // release lock early if launch has finished
189 if (!data) {
190 if (CHECK_HANDLE(launch_handle)) {
191 release_request(launch_handle);
192 launch_handle = -1;
193 }
194 launch_mode = 0;
195 return HINT_HANDLED;
196 }
197
198 if (!launch_mode) {
199 launch_handle = interaction_with_handle(launch_handle, MAX_LAUNCH_DURATION,
200 ARRAY_SIZE(resources_launch), resources_launch);
201 if (!CHECK_HANDLE(launch_handle)) {
202 ALOGE("Failed to perform launch boost");
203 return HINT_NONE;
204 }
205 launch_mode = 1;
206 }
207 return HINT_HANDLED;
208}
Corinna Vinschen7dd06962018-08-06 21:14:12 +0200209
Michael Bestas32ae7b02018-03-26 01:52:03 +0300210int power_hint_override(power_hint_t hint, void *data)
211{
212 static struct timespec s_previous_boost_timespec;
213 struct timespec cur_boost_timespec;
214 long long elapsed_time;
215 static int s_previous_duration = 0;
216 int duration;
217
218 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800219 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200220 ALOGE("Setting power profile failed. mpdecision not started?");
Michael Bestas32ae7b02018-03-26 01:52:03 +0300221 return HINT_HANDLED;
222 }
223
224 // Skip other hints in high/low power modes
225 if (current_power_profile == PROFILE_POWER_SAVE ||
226 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
227 return HINT_HANDLED;
228 }
229
230 switch (hint) {
231 case POWER_HINT_INTERACTION:
Corinna Vinschen7dd06962018-08-06 21:14:12 +0200232 duration = DEFAULT_INTERACTIVE_DURATION;
Michael Bestas32ae7b02018-03-26 01:52:03 +0300233 if (data) {
234 int input_duration = *((int*)data);
235 if (input_duration > duration) {
Corinna Vinschen7dd06962018-08-06 21:14:12 +0200236 duration = (input_duration > MAX_INTERACTIVE_DURATION) ?
237 MAX_INTERACTIVE_DURATION : input_duration;
Michael Bestas32ae7b02018-03-26 01:52:03 +0300238 }
239 }
240
241 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
242
243 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
244 // don't hint if previous hint's duration covers this hint's duration
245 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
246 return HINT_HANDLED;
247 }
248 s_previous_boost_timespec = cur_boost_timespec;
249 s_previous_duration = duration;
250
Corinna Vinschen7dd06962018-08-06 21:14:12 +0200251 if (duration >= MIN_FLING_DURATION) {
Michael Bestas32ae7b02018-03-26 01:52:03 +0300252 interaction(duration, ARRAY_SIZE(resources_interaction_fling_boost),
253 resources_interaction_fling_boost);
254 } else {
255 interaction(duration, ARRAY_SIZE(resources_interaction_boost),
256 resources_interaction_boost);
257 }
258 return HINT_HANDLED;
Corinna Vinschen591f5902018-10-12 21:40:24 +0200259 case POWER_HINT_LAUNCH:
tomascus8b038b12019-02-19 17:15:58 +1100260 return process_activity_launch_hint(data);
Michael Bestas32ae7b02018-03-26 01:52:03 +0300261 default:
262 break;
263 }
264 return HINT_NONE;
265}
266
Rashed Abdel-Tawaba2a70712017-12-31 01:21:40 +0200267int set_interactive_override(int on)
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800268{
269 char governor[80];
270
271 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
272 ALOGE("Can't obtain scaling governor.");
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800273 return HINT_NONE;
274 }
275
276 if (!on) {
277 /* Display off. */
278 /*
279 * We need to be able to identify the first display off hint
280 * and release the current lock holder
281 */
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +0800282 if (!first_display_off_hint) {
283 undo_initial_hint_action();
284 first_display_off_hint = 1;
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800285 }
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +0800286 /* Used for all subsequent toggles to the display */
287 undo_hint_action(DISPLAY_STATE_HINT_ID_2);
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800288
Michael Bestasfc62e7c2018-03-25 23:14:21 +0300289 if (is_ondemand_governor(governor)) {
Michael Bestas47636f62018-05-25 21:30:28 +0300290 int resource_values[] = {
291 MS_500, SYNC_FREQ_600, OPTIMAL_FREQ_600, THREAD_MIGRATION_SYNC_OFF
292 };
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800293 perform_hint_action(DISPLAY_STATE_HINT_ID,
294 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800295 }
296 } else {
297 /* Display on */
Michael Bestas47636f62018-05-25 21:30:28 +0300298 int resource_values2[] = {
299 CPUS_ONLINE_MIN_2
300 };
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +0800301 perform_hint_action(DISPLAY_STATE_HINT_ID_2,
302 resource_values2, ARRAY_SIZE(resource_values2));
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800303
Michael Bestasfc62e7c2018-03-25 23:14:21 +0300304 if (is_ondemand_governor(governor)) {
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800305 undo_hint_action(DISPLAY_STATE_HINT_ID);
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800306 }
307 }
Michael Bestas47636f62018-05-25 21:30:28 +0300308 return HINT_HANDLED;
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800309}