blob: ecdb1d6df8239425355a8654e4b7f40ecb8ea420 [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>
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 Chena42f4a82018-03-01 21:31:15 -080042#include <log/log.h>
Dilip Gudlur4eca13c2014-01-14 15:34:46 -080043#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 Gudlur4eca13c2014-01-14 15:34:46 -080052static int first_display_off_hint;
Dilip Gudlur4eca13c2014-01-14 15:34:46 -080053
Michael Bestas32ae7b02018-03-26 01:52:03 +030054static int current_power_profile = PROFILE_BALANCED;
55
Michael Bestas32ae7b02018-03-26 01:52:03 +030056static int profile_power_save[] = {
57 0x0A03,
58 CPUS_ONLINE_MAX_LIMIT_2,
Stefan Assmann651aa0f2018-07-23 18:42:02 +020059 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 Bestas32ae7b02018-03-26 01:52:03 +030063};
64
65static int profile_bias_power[] = {
66 0x0A03,
Stefan Assmann651aa0f2018-07-23 18:42:02 +020067 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 Bestas32ae7b02018-03-26 01:52:03 +030072};
73
74static int profile_bias_performance[] = {
Stefan Assmann651aa0f2018-07-23 18:42:02 +020075 CPUS_ONLINE_MIN_2,
Michael Bestas32ae7b02018-03-26 01:52:03 +030076 CPU0_MIN_FREQ_NONTURBO_MAX + 1,
77 CPU1_MIN_FREQ_NONTURBO_MAX + 1,
78 CPU2_MIN_FREQ_NONTURBO_MAX + 1,
Stefan Assmann651aa0f2018-07-23 18:42:02 +020079 CPU3_MIN_FREQ_NONTURBO_MAX + 1
80};
81
82static 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 Bestas32ae7b02018-03-26 01:52:03 +030089};
90
91#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +030092int get_number_of_profiles()
93{
Michael Bestas32ae7b02018-03-26 01:52:03 +030094 return 5;
95}
96#endif
97
Michael Bestas47636f62018-05-25 21:30:28 +030098static void set_power_profile(int profile)
99{
Michael Bestas32ae7b02018-03-26 01:52:03 +0300100 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 Bestas32ae7b02018-03-26 01:52:03 +0300129 }
130
131 current_power_profile = profile;
132}
133
134static int resources_interaction_fling_boost[] = {
135 CPUS_ONLINE_MIN_3,
136 0x20F,
137 0x30F,
138 0x40F,
139 0x50F
140};
141
142static int resources_interaction_boost[] = {
143 CPUS_ONLINE_MIN_2,
144 0x20F,
145 0x30F,
146 0x40F,
147 0x50F
148};
149
150static 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
158int 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 Bestas32ae7b02018-03-26 01:52:03 +0300179 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 Bestas32ae7b02018-03-26 01:52:03 +0300205 case POWER_HINT_LAUNCH:
Michael Bestas32ae7b02018-03-26 01:52:03 +0300206 duration = 2000;
Michael Bestas32ae7b02018-03-26 01:52:03 +0300207 interaction(duration, ARRAY_SIZE(resources_launch),
208 resources_launch);
209 return HINT_HANDLED;
Michael Bestas32ae7b02018-03-26 01:52:03 +0300210 default:
211 break;
212 }
213 return HINT_NONE;
214}
215
Rashed Abdel-Tawaba2a70712017-12-31 01:21:40 +0200216int set_interactive_override(int on)
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800217{
218 char governor[80];
219
220 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
221 ALOGE("Can't obtain scaling governor.");
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800222 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 Liewbe1a5d52016-07-19 20:01:01 +0800231 if (!first_display_off_hint) {
232 undo_initial_hint_action();
233 first_display_off_hint = 1;
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800234 }
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +0800235 /* Used for all subsequent toggles to the display */
236 undo_hint_action(DISPLAY_STATE_HINT_ID_2);
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800237
Michael Bestasfc62e7c2018-03-25 23:14:21 +0300238 if (is_ondemand_governor(governor)) {
Michael Bestas47636f62018-05-25 21:30:28 +0300239 int resource_values[] = {
240 MS_500, SYNC_FREQ_600, OPTIMAL_FREQ_600, THREAD_MIGRATION_SYNC_OFF
241 };
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800242 perform_hint_action(DISPLAY_STATE_HINT_ID,
243 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800244 }
245 } else {
246 /* Display on */
Michael Bestas47636f62018-05-25 21:30:28 +0300247 int resource_values2[] = {
248 CPUS_ONLINE_MIN_2
249 };
Zhao Wei Liewbe1a5d52016-07-19 20:01:01 +0800250 perform_hint_action(DISPLAY_STATE_HINT_ID_2,
251 resource_values2, ARRAY_SIZE(resource_values2));
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800252
Michael Bestasfc62e7c2018-03-25 23:14:21 +0300253 if (is_ondemand_governor(governor)) {
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800254 undo_hint_action(DISPLAY_STATE_HINT_ID);
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800255 }
256 }
Michael Bestas47636f62018-05-25 21:30:28 +0300257 return HINT_HANDLED;
Dilip Gudlur4eca13c2014-01-14 15:34:46 -0800258}