blob: 2925ebed0e2b714c07d7c0b41449129ecb18096f [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>
Craig Tatloradf8a1c2018-05-21 17:22:31 +010034#include <time.h>
Dilip Gudlure9f9c982015-01-28 15:17:45 -080035#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>
Dilip Gudlure9f9c982015-01-28 15:17:45 -080044#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
Michael Bestase5553952018-03-26 03:00:15 +030053static int current_power_profile = PROFILE_BALANCED;
54
55static int profile_high_performance[] = {
56 SCHED_BOOST_ON, CPUS_ONLINE_MAX,
57 ALL_CPUS_PWR_CLPS_DIS, 0x0901,
58 CPU0_MIN_FREQ_TURBO_MAX,
59 CPU1_MIN_FREQ_TURBO_MAX,
60 CPU2_MIN_FREQ_TURBO_MAX,
61 CPU3_MIN_FREQ_TURBO_MAX,
62 CPU4_MIN_FREQ_TURBO_MAX,
63 CPU5_MIN_FREQ_TURBO_MAX
64};
65
66static int profile_power_save[] = {
67 CPUS_ONLINE_MPD_OVERRIDE, 0x0A03,
68 CPU0_MAX_FREQ_NONTURBO_MAX - 2,
69 CPU1_MAX_FREQ_NONTURBO_MAX - 2,
70 CPU2_MAX_FREQ_NONTURBO_MAX - 2,
71 CPU3_MAX_FREQ_NONTURBO_MAX - 2,
72 CPU4_MAX_FREQ_NONTURBO_MAX - 2,
73 CPU5_MAX_FREQ_NONTURBO_MAX - 2
74};
75
76static int profile_bias_power[] = {
77 0x0A03, 0x0902,
78 CPU0_MAX_FREQ_NONTURBO_MAX - 2,
79 CPU1_MAX_FREQ_NONTURBO_MAX - 2,
Michael Bestase5553952018-03-26 03:00:15 +030080 CPU2_MAX_FREQ_NONTURBO_MAX - 2,
Han Wang34c74102018-07-24 17:12:14 +020081 CPU3_MAX_FREQ_NONTURBO_MAX - 2,
Michael Bestase5553952018-03-26 03:00:15 +030082 CPU4_MAX_FREQ_NONTURBO_MAX,
83 CPU5_MAX_FREQ_NONTURBO_MAX
84};
85
86static int profile_bias_performance[] = {
87 CPUS_ONLINE_MAX_LIMIT_MAX,
88 CPU4_MIN_FREQ_NONTURBO_MAX + 1,
89 CPU5_MIN_FREQ_NONTURBO_MAX + 1
90};
91
92#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +030093int get_number_of_profiles()
94{
Michael Bestase5553952018-03-26 03:00:15 +030095 return 5;
96}
97#endif
98
dianlujitao8780cb72019-02-23 20:24:57 +080099static int set_power_profile(void *data)
Michael Bestas47636f62018-05-25 21:30:28 +0300100{
dianlujitao8780cb72019-02-23 20:24:57 +0800101 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200102 int ret = -EINVAL;
103 const char *profile_name = NULL;
104
Michael Bestase5553952018-03-26 03:00:15 +0300105 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200106 return 0;
Michael Bestase5553952018-03-26 03:00:15 +0300107
108 ALOGV("%s: Profile=%d", __func__, profile);
109
110 if (current_power_profile != PROFILE_BALANCED) {
111 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
112 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200113 current_power_profile = PROFILE_BALANCED;
Michael Bestase5553952018-03-26 03:00:15 +0300114 }
115
116 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200117 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
Michael Bestase5553952018-03-26 03:00:15 +0300118 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200119 profile_name = "powersave";
Michael Bestase5553952018-03-26 03:00:15 +0300120
121 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200122 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
123 profile_high_performance, ARRAY_SIZE(profile_high_performance));
124 profile_name = "performance";
Michael Bestase5553952018-03-26 03:00:15 +0300125
126 } else if (profile == PROFILE_BIAS_POWER) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200127 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power,
Michael Bestase5553952018-03-26 03:00:15 +0300128 ARRAY_SIZE(profile_bias_power));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200129 profile_name = "bias power";
Michael Bestase5553952018-03-26 03:00:15 +0300130
131 } else if (profile == PROFILE_BIAS_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200132 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
133 profile_bias_performance, ARRAY_SIZE(profile_bias_performance));
134 profile_name = "bias perf";
135 } else if (profile == PROFILE_BALANCED) {
136 ret = 0;
137 profile_name = "balanced";
Michael Bestase5553952018-03-26 03:00:15 +0300138 }
139
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200140 if (ret == 0) {
141 current_power_profile = profile;
142 ALOGD("%s: Set %s mode", __func__, profile_name);
143 }
144 return ret;
Michael Bestase5553952018-03-26 03:00:15 +0300145}
146
Michael Bestas47636f62018-05-25 21:30:28 +0300147static void process_video_encode_hint(void *metadata)
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800148{
149 char governor[80];
150 struct video_encode_metadata_t video_encode_metadata;
151
152 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
153 ALOGE("Can't obtain scaling governor.");
Michael Bestas47636f62018-05-25 21:30:28 +0300154 return;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800155 }
156
Michael Bestas47636f62018-05-25 21:30:28 +0300157 if (!metadata) {
158 return;
159 }
160
161 /* Initialize encode metadata struct fields. */
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800162 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
163 video_encode_metadata.state = -1;
164 video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID;
165
Michael Bestas47636f62018-05-25 21:30:28 +0300166 if (parse_video_encode_metadata((char *)metadata,
167 &video_encode_metadata) == -1) {
168 ALOGE("Error occurred while parsing metadata.");
169 return;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800170 }
171
172 if (video_encode_metadata.state == 1) {
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800173 if (is_interactive_governor(governor)) {
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800174 /* sched and cpufreq params
175 * hispeed freq - 768 MHz
176 * target load - 90
177 * above_hispeed_delay - 40ms
178 * sched_small_tsk - 50
179 */
Michael Bestas47636f62018-05-25 21:30:28 +0300180 int resource_values[] = {
181 0x2C07, 0x2F5A, 0x2704, 0x4032
182 };
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800183 perform_hint_action(video_encode_metadata.hint_id,
Zhao Wei Liewd4fe61e2016-06-26 11:37:59 +0800184 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800185 }
186 } else if (video_encode_metadata.state == 0) {
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800187 if (is_interactive_governor(governor)) {
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800188 undo_hint_action(video_encode_metadata.hint_id);
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800189 }
190 }
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800191}
192
Michael Bestase5553952018-03-26 03:00:15 +0300193static int resources_interaction_fling_boost[] = {
194 ALL_CPUS_PWR_CLPS_DIS,
195 SCHED_BOOST_ON,
196 SCHED_PREFER_IDLE_DIS
197};
198
199static int resources_interaction_boost[] = {
200 ALL_CPUS_PWR_CLPS_DIS,
201 SCHED_PREFER_IDLE_DIS
202};
203
204static int resources_launch[] = {
205 SCHED_BOOST_ON,
206 0x20C
207};
208
tomascus8b038b12019-02-19 17:15:58 +1100209static int process_activity_launch_hint(void *data)
210{
211 static int launch_handle = -1;
212 static int launch_mode = 0;
213
214 // release lock early if launch has finished
215 if (!data) {
216 if (CHECK_HANDLE(launch_handle)) {
217 release_request(launch_handle);
218 launch_handle = -1;
219 }
220 launch_mode = 0;
221 return HINT_HANDLED;
222 }
223
224 if (!launch_mode) {
225 launch_handle = interaction_with_handle(launch_handle, 5000,
226 ARRAY_SIZE(resources_launch), resources_launch);
227 if (!CHECK_HANDLE(launch_handle)) {
228 ALOGE("Failed to perform launch boost");
229 return HINT_NONE;
230 }
231 launch_mode = 1;
232 }
233 return HINT_HANDLED;
234}
235
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800236int power_hint_override(power_hint_t hint, void *data)
237{
Michael Bestase5553952018-03-26 03:00:15 +0300238 static struct timespec s_previous_boost_timespec;
239 struct timespec cur_boost_timespec;
240 long long elapsed_time;
241 static int s_previous_duration = 0;
242 int duration;
243
244 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800245 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200246 ALOGE("Setting power profile failed. perfd not started?");
Michael Bestase5553952018-03-26 03:00:15 +0300247 return HINT_HANDLED;
248 }
249
250 // Skip other hints in high/low power modes
251 if (current_power_profile == PROFILE_POWER_SAVE ||
252 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
253 return HINT_HANDLED;
254 }
255
256 switch (hint) {
257 case POWER_HINT_INTERACTION:
Michael Bestase5553952018-03-26 03:00:15 +0300258 duration = 500; // 500ms by default
259 if (data) {
260 int input_duration = *((int*)data);
261 if (input_duration > duration) {
262 duration = (input_duration > 5000) ? 5000 : input_duration;
263 }
264 }
265
266 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
267
268 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
269 // don't hint if previous hint's duration covers this hint's duration
270 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
271 return HINT_HANDLED;
272 }
273 s_previous_boost_timespec = cur_boost_timespec;
274 s_previous_duration = duration;
275
276 if (duration >= 1500) {
277 interaction(duration, ARRAY_SIZE(resources_interaction_fling_boost),
278 resources_interaction_fling_boost);
279 } else {
280 interaction(duration, ARRAY_SIZE(resources_interaction_boost),
281 resources_interaction_boost);
282 }
283 return HINT_HANDLED;
Michael Bestase5553952018-03-26 03:00:15 +0300284 case POWER_HINT_LAUNCH:
tomascus8b038b12019-02-19 17:15:58 +1100285 return process_activity_launch_hint(data);
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800286 case POWER_HINT_VIDEO_ENCODE:
Michael Bestas47636f62018-05-25 21:30:28 +0300287 process_video_encode_hint(data);
288 return HINT_HANDLED;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800289 default:
290 break;
291 }
Michael Bestase5553952018-03-26 03:00:15 +0300292 return HINT_NONE;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800293}
294
295int set_interactive_override(int on)
296{
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800297 char governor[80];
298
299 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
300 ALOGE("Can't obtain scaling governor.");
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800301 return HINT_NONE;
302 }
303
304 if (!on) {
305 /* Display off */
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800306 if (is_interactive_governor(governor)) {
Michael Bestase5553952018-03-26 03:00:15 +0300307 // sched upmigrate = 99, sched downmigrate = 95
308 // keep the big cores around, but make them very hard to use
Michael Bestas47636f62018-05-25 21:30:28 +0300309 int resource_values[] = {
310 0x4E63, 0x4F5F
311 };
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800312 perform_hint_action(DISPLAY_STATE_HINT_ID,
313 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800314 }
315 } else {
316 /* Display on */
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800317 if (is_interactive_governor(governor)) {
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800318 undo_hint_action(DISPLAY_STATE_HINT_ID);
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800319 }
320 }
Michael Bestas47636f62018-05-25 21:30:28 +0300321 return HINT_HANDLED;
Dilip Gudlure9f9c982015-01-28 15:17:45 -0800322}