blob: 1d7731cd923f60e4ae164f75ffcbdc193be62d3e [file] [log] [blame]
Steve Kondik40ea4d62014-03-27 22:00:00 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
Steve Kondikc8b48222014-05-19 17:26:34 -07003 * Copyright (c) 2014, The CyanogenMod Project
Steve Kondik40ea4d62014-03-27 22:00:00 -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"
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
51static int display_hint_sent;
52static int display_hint2_sent;
53static int first_display_off_hint;
54extern int display_boost;
55
Steve Kondikc8b48222014-05-19 17:26:34 -070056static int current_power_profile = PROFILE_BALANCED;
57
Steve Kondikd9668ba2015-11-03 03:27:42 -080058int get_number_of_profiles() {
59 return 5;
60}
61
Steve Kondikc8b48222014-05-19 17:26:34 -070062static 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 Kondik79646022015-10-29 23:37:54 -070075 int resource_values[] = { CPUS_ONLINE_MIN_4, 0x0901,
Steve Kondik2e65c972014-10-30 01:03:41 -070076 CPU0_MIN_FREQ_TURBO_MAX, CPU1_MIN_FREQ_TURBO_MAX,
77 CPU2_MIN_FREQ_TURBO_MAX, CPU3_MIN_FREQ_TURBO_MAX };
Steve Kondikc8b48222014-05-19 17:26:34 -070078 perform_hint_action(DEFAULT_PROFILE_HINT_ID,
Zhao Wei Liew50a1ce22016-06-26 11:37:59 +080079 resource_values, ARRAY_SIZE(resource_values));
Steve Kondikc8b48222014-05-19 17:26:34 -070080 ALOGD("%s: set performance mode", __func__);
Steve Kondika58ddde2015-11-01 04:31:46 -080081 } 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 Liew50a1ce22016-06-26 11:37:59 +080086 resource_values, ARRAY_SIZE(resource_values));
Steve Kondika58ddde2015-11-01 04:31:46 -080087 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 Liew50a1ce22016-06-26 11:37:59 +080093 resource_values, ARRAY_SIZE(resource_values));
Steve Kondika58ddde2015-11-01 04:31:46 -080094 ALOGD("%s: set bias power mode", __func__);
Steve Kondikc8b48222014-05-19 17:26:34 -070095 } else if (profile == PROFILE_POWER_SAVE) {
Steve Kondika58ddde2015-11-01 04:31:46 -080096 int resource_values[] = { 0x0A03, CPUS_ONLINE_MAX_LIMIT_2,
Steve Kondik2e65c972014-10-30 01:03:41 -070097 CPU0_MAX_FREQ_NONTURBO_MAX, CPU1_MAX_FREQ_NONTURBO_MAX,
98 CPU2_MAX_FREQ_NONTURBO_MAX, CPU3_MAX_FREQ_NONTURBO_MAX };
Steve Kondikc8b48222014-05-19 17:26:34 -070099 perform_hint_action(DEFAULT_PROFILE_HINT_ID,
Zhao Wei Liew50a1ce22016-06-26 11:37:59 +0800100 resource_values, ARRAY_SIZE(resource_values));
Steve Kondikc8b48222014-05-19 17:26:34 -0700101 ALOGD("%s: set powersave", __func__);
102 }
103
104 current_power_profile = profile;
105}
106
107extern void interaction(int duration, int num_args, int opt_list[]);
108
Steve Kondik1921d302014-12-08 01:37:30 -0800109int power_hint_override(__attribute__((unused)) struct power_module *module,
110 power_hint_t hint, void *data)
Steve Kondikc8b48222014-05-19 17:26:34 -0700111{
Steve Kondikfc552c72015-09-04 04:21:44 -0700112 if (hint == POWER_HINT_SET_PROFILE) {
Steve Kondikd9668ba2015-11-03 03:27:42 -0800113 set_power_profile(*(int32_t *)data);
Steve Kondikfc552c72015-09-04 04:21:44 -0700114 return HINT_HANDLED;
115 }
Steve Kondikc8b48222014-05-19 17:26:34 -0700116
Steve Kondika58ddde2015-11-01 04:31:46 -0800117 // Skip other hints in high/low power modes
118 if (current_power_profile == PROFILE_POWER_SAVE ||
119 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
Steve Kondikfc552c72015-09-04 04:21:44 -0700120 return HINT_HANDLED;
121 }
Steve Kondikc8b48222014-05-19 17:26:34 -0700122
Steve Kondikfc552c72015-09-04 04:21:44 -0700123 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 Liew50a1ce22016-06-26 11:37:59 +0800129 interaction(duration, ARRAY_SIZE(resources), resources);
Steve Kondikfc552c72015-09-04 04:21:44 -0700130
131 return HINT_HANDLED;
132 }
133
134 if (hint == POWER_HINT_CPU_BOOST) {
Steve Kondikd9668ba2015-11-03 03:27:42 -0800135 int duration = *(int32_t *)data / 1000;
136 int resources[] = { CPUS_ONLINE_MIN_2,
137 0x20F, 0x30F, 0x40F, 0x50F };
Steve Kondikc8b48222014-05-19 17:26:34 -0700138
Steve Kondikfc552c72015-09-04 04:21:44 -0700139 if (duration)
Zhao Wei Liew50a1ce22016-06-26 11:37:59 +0800140 interaction(duration, ARRAY_SIZE(resources), resources);
Steve Kondikc8b48222014-05-19 17:26:34 -0700141
142 return HINT_HANDLED;
Steve Kondikfc552c72015-09-04 04:21:44 -0700143 }
Steve Kondikc8b48222014-05-19 17:26:34 -0700144
Steve Kondik0125db42016-03-10 07:58:05 -0800145 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 Liew50a1ce22016-06-26 11:37:59 +0800173 interaction(duration, ARRAY_SIZE(resources), resources);
Steve Kondik0125db42016-03-10 07:58:05 -0800174
175 return HINT_HANDLED;
176 }
177
178
Steve Kondikfc552c72015-09-04 04:21:44 -0700179 return HINT_NONE;
Steve Kondikc8b48222014-05-19 17:26:34 -0700180}
181
Steve Kondika3cfade2015-01-02 23:38:44 -0800182int set_interactive_override(struct power_module *module __unused, int on)
Steve Kondik40ea4d62014-03-27 22:00:00 -0700183{
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 Liew50a1ce22016-06-26 11:37:59 +0800216 resource_values, ARRAY_SIZE(resource_values));
Steve Kondik40ea4d62014-03-27 22:00:00 -0700217 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 Liew50a1ce22016-06-26 11:37:59 +0800227 resource_values2, ARRAY_SIZE(resource_values2));
Steve Kondik40ea4d62014-03-27 22:00:00 -0700228 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}