blob: 5ca7cf317160046dfc05c626c9d9a0a4fe4a2747 [file] [log] [blame]
Anurag Singh057806b2013-04-16 16:53:45 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
Michael Bestasc1a92612018-01-22 02:30:25 +02003 * Copyright (C) 2018 The LineageOS Project
Anurag Singh057806b2013-04-16 16:53:45 -07004 *
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
Anurag Singh057806b2013-04-16 16:53:45 -070031#define LOG_NIDEBUG 0
32
33#include <errno.h>
Craig Tatlord43c2c32018-05-21 17:22:31 +010034#include <time.h>
Anurag Singh057806b2013-04-16 16:53:45 -070035#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>
Anurag Singh057806b2013-04-16 16:53:45 -070044#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
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -080053static int first_display_off_hint;
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +080054
55/**
Zhao Wei Liew143f8072018-11-02 00:33:14 +000056 * Returns true if the target is MSM8974AB or MSM8974AC.
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +080057 */
58static bool is_target_8974pro(void)
59{
Zhao Wei Liew143f8072018-11-02 00:33:14 +000060 static int is_8974pro = -1;
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +080061 int soc_id;
62
Zhao Wei Liew143f8072018-11-02 00:33:14 +000063 if (is_8974pro >= 0)
64 return is_8974pro;
65
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +080066 soc_id = get_soc_id();
Zhao Wei Liew143f8072018-11-02 00:33:14 +000067 is_8974pro = soc_id == 194 || (soc_id >= 208 && soc_id <= 218);
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +080068
69 return is_8974pro;
70}
Anurag Singhb15323a2013-06-08 16:55:44 -070071
Michael Bestasc1a92612018-01-22 02:30:25 +020072static int current_power_profile = PROFILE_BALANCED;
73
74static int profile_high_performance[] = {
75 CPUS_ONLINE_MIN_4,
76 0x0901,
77 CPU0_MIN_FREQ_TURBO_MAX,
78 CPU1_MIN_FREQ_TURBO_MAX,
79 CPU2_MIN_FREQ_TURBO_MAX,
80 CPU3_MIN_FREQ_TURBO_MAX
81};
82
83static int profile_power_save[] = {
84 0x0A03,
85 CPUS_ONLINE_MAX_LIMIT_2,
86 CPU0_MAX_FREQ_NONTURBO_MAX,
87 CPU1_MAX_FREQ_NONTURBO_MAX,
88 CPU2_MAX_FREQ_NONTURBO_MAX,
89 CPU3_MAX_FREQ_NONTURBO_MAX
90};
91
92static int profile_bias_power[] = {
93 0x0A03,
94 CPU0_MAX_FREQ_NONTURBO_MAX,
95 CPU1_MAX_FREQ_NONTURBO_MAX,
Han Wang34c74102018-07-24 17:12:14 +020096 CPU2_MAX_FREQ_NONTURBO_MAX,
97 CPU3_MAX_FREQ_NONTURBO_MAX
Michael Bestasc1a92612018-01-22 02:30:25 +020098};
99
100static int profile_bias_performance[] = {
101 CPU0_MIN_FREQ_NONTURBO_MAX + 1,
102 CPU1_MIN_FREQ_NONTURBO_MAX + 1,
103 CPU2_MIN_FREQ_NONTURBO_MAX + 1,
Han Wang34c74102018-07-24 17:12:14 +0200104 CPU3_MIN_FREQ_NONTURBO_MAX + 1
Michael Bestasc1a92612018-01-22 02:30:25 +0200105};
106
dianlujitao40048112018-03-02 12:40:04 +0800107#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +0300108int get_number_of_profiles()
109{
Michael Bestasc1a92612018-01-22 02:30:25 +0200110 return 5;
111}
dianlujitao40048112018-03-02 12:40:04 +0800112#endif
Michael Bestasc1a92612018-01-22 02:30:25 +0200113
dianlujitao8780cb72019-02-23 20:24:57 +0800114static int set_power_profile(void *data)
Michael Bestas47636f62018-05-25 21:30:28 +0300115{
dianlujitao8780cb72019-02-23 20:24:57 +0800116 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200117 int ret = -EINVAL;
118 const char *profile_name = NULL;
119
Michael Bestasc1a92612018-01-22 02:30:25 +0200120 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200121 return 0;
Michael Bestasc1a92612018-01-22 02:30:25 +0200122
123 ALOGV("%s: Profile=%d", __func__, profile);
124
125 if (current_power_profile != PROFILE_BALANCED) {
126 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
127 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200128 current_power_profile = PROFILE_BALANCED;
Michael Bestasc1a92612018-01-22 02:30:25 +0200129 }
130
131 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200132 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
Michael Bestasc1a92612018-01-22 02:30:25 +0200133 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200134 profile_name = "powersave";
Michael Bestasc1a92612018-01-22 02:30:25 +0200135
136 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200137 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
138 profile_high_performance, ARRAY_SIZE(profile_high_performance));
139 profile_name = "performance";
Michael Bestasc1a92612018-01-22 02:30:25 +0200140
141 } else if (profile == PROFILE_BIAS_POWER) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200142 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power,
Michael Bestasc1a92612018-01-22 02:30:25 +0200143 ARRAY_SIZE(profile_bias_power));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200144 profile_name = "bias power";
Michael Bestasc1a92612018-01-22 02:30:25 +0200145
146 } else if (profile == PROFILE_BIAS_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200147 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
148 profile_bias_performance, ARRAY_SIZE(profile_bias_performance));
149 profile_name = "bias perf";
150 } else if (profile == PROFILE_BALANCED) {
151 ret = 0;
152 profile_name = "balanced";
Michael Bestasc1a92612018-01-22 02:30:25 +0200153 }
154
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200155 if (ret == 0) {
156 current_power_profile = profile;
157 ALOGD("%s: Set %s mode", __func__, profile_name);
158 }
159 return ret;
Michael Bestasc1a92612018-01-22 02:30:25 +0200160}
161
Michael Bestas8c55d3c2018-03-26 03:24:26 +0300162static int resources_interaction_fling_boost[] = {
163 CPUS_ONLINE_MIN_3,
164 0x20F,
165 0x30F,
166 0x40F,
167 0x50F
168};
169
170static int resources_interaction_boost[] = {
171 CPUS_ONLINE_MIN_2,
172 0x20F,
173 0x30F,
174 0x40F,
175 0x50F
176};
177
178static int resources_launch[] = {
179 CPUS_ONLINE_MIN_3,
180 CPU0_MIN_FREQ_TURBO_MAX,
181 CPU1_MIN_FREQ_TURBO_MAX,
182 CPU2_MIN_FREQ_TURBO_MAX,
183 CPU3_MIN_FREQ_TURBO_MAX
184};
185
tomascus8b038b12019-02-19 17:15:58 +1100186static int process_activity_launch_hint(void *data)
187{
188 static int launch_handle = -1;
189 static int launch_mode = 0;
190
191 // release lock early if launch has finished
192 if (!data) {
193 if (CHECK_HANDLE(launch_handle)) {
194 release_request(launch_handle);
195 launch_handle = -1;
196 }
197 launch_mode = 0;
198 return HINT_HANDLED;
199 }
200
201 if (!launch_mode) {
202 launch_handle = interaction_with_handle(launch_handle, 5000,
203 ARRAY_SIZE(resources_launch), resources_launch);
204 if (!CHECK_HANDLE(launch_handle)) {
205 ALOGE("Failed to perform launch boost");
206 return HINT_NONE;
207 }
208 launch_mode = 1;
209 }
210 return HINT_HANDLED;
211}
212
Michael Bestasc1a92612018-01-22 02:30:25 +0200213int power_hint_override(power_hint_t hint, void *data)
214{
Michael Bestas8c55d3c2018-03-26 03:24:26 +0300215 static struct timespec s_previous_boost_timespec;
216 struct timespec cur_boost_timespec;
217 long long elapsed_time;
218 static int s_previous_duration = 0;
219 int duration;
220
Michael Bestasc1a92612018-01-22 02:30:25 +0200221 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800222 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200223 ALOGE("Setting power profile failed. mpdecision not started?");
Michael Bestasc1a92612018-01-22 02:30:25 +0200224 return HINT_HANDLED;
225 }
226
227 // Skip other hints in high/low power modes
228 if (current_power_profile == PROFILE_POWER_SAVE ||
229 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
230 return HINT_HANDLED;
231 }
232
Michael Bestas8c55d3c2018-03-26 03:24:26 +0300233 switch (hint) {
234 case POWER_HINT_INTERACTION:
Michael Bestas8c55d3c2018-03-26 03:24:26 +0300235 duration = 500; // 500ms by default
236 if (data) {
237 int input_duration = *((int*)data);
238 if (input_duration > duration) {
239 duration = (input_duration > 5000) ? 5000 : input_duration;
240 }
241 }
Michael Bestasc1a92612018-01-22 02:30:25 +0200242
Michael Bestas8c55d3c2018-03-26 03:24:26 +0300243 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
Michael Bestasc1a92612018-01-22 02:30:25 +0200244
Michael Bestas8c55d3c2018-03-26 03:24:26 +0300245 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
246 // don't hint if previous hint's duration covers this hint's duration
247 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
248 return HINT_HANDLED;
249 }
250 s_previous_boost_timespec = cur_boost_timespec;
251 s_previous_duration = duration;
Michael Bestasc1a92612018-01-22 02:30:25 +0200252
Michael Bestas8c55d3c2018-03-26 03:24:26 +0300253 if (duration >= 1500) {
254 interaction(duration, ARRAY_SIZE(resources_interaction_fling_boost),
255 resources_interaction_fling_boost);
256 } else {
257 interaction(duration, ARRAY_SIZE(resources_interaction_boost),
258 resources_interaction_boost);
259 }
Michael Bestasc1a92612018-01-22 02:30:25 +0200260 return HINT_HANDLED;
Michael Bestas8c55d3c2018-03-26 03:24:26 +0300261 case POWER_HINT_LAUNCH:
tomascus8b038b12019-02-19 17:15:58 +1100262 return process_activity_launch_hint(data);
Michael Bestas8c55d3c2018-03-26 03:24:26 +0300263 default:
264 break;
Michael Bestasc1a92612018-01-22 02:30:25 +0200265 }
Michael Bestasc1a92612018-01-22 02:30:25 +0200266 return HINT_NONE;
267}
268
Rashed Abdel-Tawaba2a70712017-12-31 01:21:40 +0200269int set_interactive_override(int on)
Anurag Singh057806b2013-04-16 16:53:45 -0700270{
271 char governor[80];
272
273 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
274 ALOGE("Can't obtain scaling governor.");
Anurag Singh057806b2013-04-16 16:53:45 -0700275 return HINT_NONE;
276 }
277
278 if (!on) {
279 /* Display off. */
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800280 /*
281 * We need to be able to identify the first display off hint
282 * and release the current lock holder
283 */
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +0800284 if (is_target_8974pro()) {
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800285 if (!first_display_off_hint) {
286 undo_initial_hint_action();
287 first_display_off_hint = 1;
288 }
289 /* used for all subsequent toggles to the display */
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800290 undo_hint_action(DISPLAY_STATE_HINT_ID_2);
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800291 }
292
Michael Bestasfc62e7c2018-03-25 23:14:21 +0300293 if (is_ondemand_governor(governor)) {
Michael Bestas47636f62018-05-25 21:30:28 +0300294 int resource_values[] = {
295 MS_500, SYNC_FREQ_600, OPTIMAL_FREQ_600, THREAD_MIGRATION_SYNC_OFF
296 };
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800297 perform_hint_action(DISPLAY_STATE_HINT_ID,
298 resource_values, ARRAY_SIZE(resource_values));
Anurag Singh057806b2013-04-16 16:53:45 -0700299 }
300 } else {
301 /* Display on */
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +0800302 if (is_target_8974pro()) {
Michael Bestas47636f62018-05-25 21:30:28 +0300303 int resource_values2[] = {
304 CPUS_ONLINE_MIN_2
305 };
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800306 perform_hint_action(DISPLAY_STATE_HINT_ID_2,
Zhao Wei Liewd4fe61e2016-06-26 11:37:59 +0800307 resource_values2, ARRAY_SIZE(resource_values2));
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800308 }
309
Michael Bestasfc62e7c2018-03-25 23:14:21 +0300310 if (is_ondemand_governor(governor)) {
Anurag Singh057806b2013-04-16 16:53:45 -0700311 undo_hint_action(DISPLAY_STATE_HINT_ID);
Anurag Singh057806b2013-04-16 16:53:45 -0700312 }
313 }
Michael Bestas47636f62018-05-25 21:30:28 +0300314 return HINT_HANDLED;
Anurag Singh057806b2013-04-16 16:53:45 -0700315}