blob: fc54805c2e08c24c858b50e71842c7a363cc5dde [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 */
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 Chena42f4a82018-03-01 21:31:15 -080041#include <log/log.h>
Anurag Singh057806b2013-04-16 16:53:45 -070042#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 Luo3b1fbdb2013-12-06 16:32:10 -080051static int first_display_off_hint;
52extern int display_boost;
Anurag Singhb15323a2013-06-08 16:55:44 -070053
Michael Bestasc1a92612018-01-22 02:30:25 +020054static int current_power_profile = PROFILE_BALANCED;
55
56static 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
65static 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
74static 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
82static 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
dianlujitao40048112018-03-02 12:40:04 +080089#ifdef INTERACTION_BOOST
Michael Bestasc1a92612018-01-22 02:30:25 +020090int get_number_of_profiles() {
91 return 5;
92}
dianlujitao40048112018-03-02 12:40:04 +080093#endif
Michael Bestasc1a92612018-01-22 02:30:25 +020094
95static 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
132void interaction(int duration, int num_args, int opt_list[]);
133
134int 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-Tawaba2a70712017-12-31 01:21:40 +0200194int set_interactive_override(int on)
Anurag Singh057806b2013-04-16 16:53:45 -0700195{
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 Luo3b1fbdb2013-12-06 16:32:10 -0800206 /*
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 Liew5a81c9a2016-06-26 21:29:34 +0800216 undo_hint_action(DISPLAY_STATE_HINT_ID_2);
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800217 }
218
Anurag Singh057806b2013-04-16 16:53:45 -0700219 if ((strncmp(governor, ONDEMAND_GOVERNOR, strlen(ONDEMAND_GOVERNOR)) == 0) &&
220 (strlen(governor) == strlen(ONDEMAND_GOVERNOR))) {
Badhri Jagan Sridharan5f118192013-07-16 12:07:56 -0700221 int resource_values[] = {MS_500, SYNC_FREQ_600, OPTIMAL_FREQ_600, THREAD_MIGRATION_SYNC_OFF};
Anurag Singh057806b2013-04-16 16:53:45 -0700222
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800223 perform_hint_action(DISPLAY_STATE_HINT_ID,
224 resource_values, ARRAY_SIZE(resource_values));
Anurag Singh057806b2013-04-16 16:53:45 -0700225
226 return HINT_HANDLED;
227 }
228 } else {
229 /* Display on */
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800230 if (display_boost) {
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800231 int resource_values2[] = {CPUS_ONLINE_MIN_2};
232 perform_hint_action(DISPLAY_STATE_HINT_ID_2,
Zhao Wei Liewd4fe61e2016-06-26 11:37:59 +0800233 resource_values2, ARRAY_SIZE(resource_values2));
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800234 }
235
Anurag Singh057806b2013-04-16 16:53:45 -0700236 if ((strncmp(governor, ONDEMAND_GOVERNOR, strlen(ONDEMAND_GOVERNOR)) == 0) &&
237 (strlen(governor) == strlen(ONDEMAND_GOVERNOR))) {
238 undo_hint_action(DISPLAY_STATE_HINT_ID);
Anurag Singh057806b2013-04-16 16:53:45 -0700239
240 return HINT_HANDLED;
241 }
242 }
243
244 return HINT_NONE;
245}