blob: 9bbb15fa7286b12f96a2348f0aaa47a3269b6e9f [file] [log] [blame]
Dilip Gudlure9f9c982015-01-28 15:17:45 -08001/*
2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
Michael Bestase5553952018-03-26 03:00:15 +03003 * Copyright (C) 2018 The LineageOS Project
Dilip Gudlure9f9c982015-01-28 15:17:45 -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 Gudlure9f9c982015-01-28 15:17:45 -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 Gudlure9f9c982015-01-28 15:17:45 -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
Michael Bestase5553952018-03-26 03:00:15 +030052static int current_power_profile = PROFILE_BALANCED;
53
54static int profile_high_performance[] = {
55 SCHED_BOOST_ON, CPUS_ONLINE_MAX,
56 ALL_CPUS_PWR_CLPS_DIS, 0x0901,
57 CPU0_MIN_FREQ_TURBO_MAX,
58 CPU1_MIN_FREQ_TURBO_MAX,
59 CPU2_MIN_FREQ_TURBO_MAX,
60 CPU3_MIN_FREQ_TURBO_MAX,
61 CPU4_MIN_FREQ_TURBO_MAX,
62 CPU5_MIN_FREQ_TURBO_MAX
63};
64
65static int profile_power_save[] = {
66 CPUS_ONLINE_MPD_OVERRIDE, 0x0A03,
67 CPU0_MAX_FREQ_NONTURBO_MAX - 2,
68 CPU1_MAX_FREQ_NONTURBO_MAX - 2,
69 CPU2_MAX_FREQ_NONTURBO_MAX - 2,
70 CPU3_MAX_FREQ_NONTURBO_MAX - 2,
71 CPU4_MAX_FREQ_NONTURBO_MAX - 2,
72 CPU5_MAX_FREQ_NONTURBO_MAX - 2
73};
74
75static int profile_bias_power[] = {
76 0x0A03, 0x0902,
77 CPU0_MAX_FREQ_NONTURBO_MAX - 2,
78 CPU1_MAX_FREQ_NONTURBO_MAX - 2,
Michael Bestase5553952018-03-26 03:00:15 +030079 CPU2_MAX_FREQ_NONTURBO_MAX - 2,
Han Wang34c74102018-07-24 17:12:14 +020080 CPU3_MAX_FREQ_NONTURBO_MAX - 2,
Michael Bestase5553952018-03-26 03:00:15 +030081 CPU4_MAX_FREQ_NONTURBO_MAX,
82 CPU5_MAX_FREQ_NONTURBO_MAX
83};
84
85static int profile_bias_performance[] = {
86 CPUS_ONLINE_MAX_LIMIT_MAX,
87 CPU4_MIN_FREQ_NONTURBO_MAX + 1,
88 CPU5_MIN_FREQ_NONTURBO_MAX + 1
89};
90
91#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +030092int get_number_of_profiles()
93{
Michael Bestase5553952018-03-26 03:00:15 +030094 return 5;
95}
96#endif
97
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020098static int set_power_profile(int profile)
Michael Bestas47636f62018-05-25 21:30:28 +030099{
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200100 int ret = -EINVAL;
101 const char *profile_name = NULL;
102
Michael Bestase5553952018-03-26 03:00:15 +0300103 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200104 return 0;
Michael Bestase5553952018-03-26 03:00:15 +0300105
106 ALOGV("%s: Profile=%d", __func__, profile);
107
108 if (current_power_profile != PROFILE_BALANCED) {
109 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
110 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200111 current_power_profile = PROFILE_BALANCED;
Michael Bestase5553952018-03-26 03:00:15 +0300112 }
113
114 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200115 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
Michael Bestase5553952018-03-26 03:00:15 +0300116 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200117 profile_name = "powersave";
Michael Bestase5553952018-03-26 03:00:15 +0300118
119 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200120 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
121 profile_high_performance, ARRAY_SIZE(profile_high_performance));
122 profile_name = "performance";
Michael Bestase5553952018-03-26 03:00:15 +0300123
124 } else if (profile == PROFILE_BIAS_POWER) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200125 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power,
Michael Bestase5553952018-03-26 03:00:15 +0300126 ARRAY_SIZE(profile_bias_power));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200127 profile_name = "bias power";
Michael Bestase5553952018-03-26 03:00:15 +0300128
129 } else if (profile == PROFILE_BIAS_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200130 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
131 profile_bias_performance, ARRAY_SIZE(profile_bias_performance));
132 profile_name = "bias perf";
133 } else if (profile == PROFILE_BALANCED) {
134 ret = 0;
135 profile_name = "balanced";
Michael Bestase5553952018-03-26 03:00:15 +0300136 }
137
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200138 if (ret == 0) {
139 current_power_profile = profile;
140 ALOGD("%s: Set %s mode", __func__, profile_name);
141 }
142 return ret;
Michael Bestase5553952018-03-26 03:00:15 +0300143}
144
Michael Bestas47636f62018-05-25 21:30:28 +0300145static void process_video_encode_hint(void *metadata)
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800146{
147 char governor[80];
148 struct video_encode_metadata_t video_encode_metadata;
149
150 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
151 ALOGE("Can't obtain scaling governor.");
Michael Bestas47636f62018-05-25 21:30:28 +0300152 return;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800153 }
154
Michael Bestas47636f62018-05-25 21:30:28 +0300155 if (!metadata) {
156 return;
157 }
158
159 /* Initialize encode metadata struct fields. */
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800160 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
161 video_encode_metadata.state = -1;
162 video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID;
163
Michael Bestas47636f62018-05-25 21:30:28 +0300164 if (parse_video_encode_metadata((char *)metadata,
165 &video_encode_metadata) == -1) {
166 ALOGE("Error occurred while parsing metadata.");
167 return;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800168 }
169
170 if (video_encode_metadata.state == 1) {
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800171 if (is_interactive_governor(governor)) {
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800172 /* sched and cpufreq params
173 * hispeed freq - 768 MHz
174 * target load - 90
175 * above_hispeed_delay - 40ms
176 * sched_small_tsk - 50
177 */
Michael Bestas47636f62018-05-25 21:30:28 +0300178 int resource_values[] = {
179 0x2C07, 0x2F5A, 0x2704, 0x4032
180 };
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800181 perform_hint_action(video_encode_metadata.hint_id,
Zhao Wei Liewd4fe61e2016-06-26 11:37:59 +0800182 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800183 }
184 } else if (video_encode_metadata.state == 0) {
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800185 if (is_interactive_governor(governor)) {
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800186 undo_hint_action(video_encode_metadata.hint_id);
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800187 }
188 }
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800189}
190
Michael Bestase5553952018-03-26 03:00:15 +0300191static int resources_interaction_fling_boost[] = {
192 ALL_CPUS_PWR_CLPS_DIS,
193 SCHED_BOOST_ON,
194 SCHED_PREFER_IDLE_DIS
195};
196
197static int resources_interaction_boost[] = {
198 ALL_CPUS_PWR_CLPS_DIS,
199 SCHED_PREFER_IDLE_DIS
200};
201
202static int resources_launch[] = {
203 SCHED_BOOST_ON,
204 0x20C
205};
206
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800207int power_hint_override(power_hint_t hint, void *data)
208{
Michael Bestase5553952018-03-26 03:00:15 +0300209 static struct timespec s_previous_boost_timespec;
210 struct timespec cur_boost_timespec;
211 long long elapsed_time;
212 static int s_previous_duration = 0;
213 int duration;
214
215 if (hint == POWER_HINT_SET_PROFILE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200216 if (set_power_profile(*(int32_t *)data) < 0)
217 ALOGE("Setting power profile failed. perfd not started?");
Michael Bestase5553952018-03-26 03:00:15 +0300218 return HINT_HANDLED;
219 }
220
221 // Skip other hints in high/low power modes
222 if (current_power_profile == PROFILE_POWER_SAVE ||
223 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
224 return HINT_HANDLED;
225 }
226
227 switch (hint) {
228 case POWER_HINT_INTERACTION:
Michael Bestase5553952018-03-26 03:00:15 +0300229 duration = 500; // 500ms by default
230 if (data) {
231 int input_duration = *((int*)data);
232 if (input_duration > duration) {
233 duration = (input_duration > 5000) ? 5000 : input_duration;
234 }
235 }
236
237 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
238
239 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
240 // don't hint if previous hint's duration covers this hint's duration
241 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
242 return HINT_HANDLED;
243 }
244 s_previous_boost_timespec = cur_boost_timespec;
245 s_previous_duration = duration;
246
247 if (duration >= 1500) {
248 interaction(duration, ARRAY_SIZE(resources_interaction_fling_boost),
249 resources_interaction_fling_boost);
250 } else {
251 interaction(duration, ARRAY_SIZE(resources_interaction_boost),
252 resources_interaction_boost);
253 }
254 return HINT_HANDLED;
Michael Bestase5553952018-03-26 03:00:15 +0300255 case POWER_HINT_LAUNCH:
Michael Bestase5553952018-03-26 03:00:15 +0300256 duration = 2000;
Michael Bestase5553952018-03-26 03:00:15 +0300257 interaction(duration, ARRAY_SIZE(resources_launch),
258 resources_launch);
259 return HINT_HANDLED;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800260 case POWER_HINT_VIDEO_ENCODE:
Michael Bestas47636f62018-05-25 21:30:28 +0300261 process_video_encode_hint(data);
262 return HINT_HANDLED;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800263 default:
264 break;
265 }
Michael Bestase5553952018-03-26 03:00:15 +0300266 return HINT_NONE;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800267}
268
269int set_interactive_override(int on)
270{
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800271 char governor[80];
272
273 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
274 ALOGE("Can't obtain scaling governor.");
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800275 return HINT_NONE;
276 }
277
278 if (!on) {
279 /* Display off */
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800280 if (is_interactive_governor(governor)) {
Michael Bestase5553952018-03-26 03:00:15 +0300281 // sched upmigrate = 99, sched downmigrate = 95
282 // keep the big cores around, but make them very hard to use
Michael Bestas47636f62018-05-25 21:30:28 +0300283 int resource_values[] = {
284 0x4E63, 0x4F5F
285 };
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800286 perform_hint_action(DISPLAY_STATE_HINT_ID,
287 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800288 }
289 } else {
290 /* Display on */
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800291 if (is_interactive_governor(governor)) {
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800292 undo_hint_action(DISPLAY_STATE_HINT_ID);
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800293 }
294 }
Michael Bestas47636f62018-05-25 21:30:28 +0300295 return HINT_HANDLED;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800296}