blob: 2dcff8c44c93ef621a138a351fc3693a8fe0823b [file] [log] [blame]
BeYkeRYkt535cc212018-12-23 22:55:35 +09001/*
2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2018 The LineageOS Project
4 *
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
31#define LOG_NIDEBUG 0
32
33#include <errno.h>
BeYkeRYktbd52e812018-12-13 06:42:35 +090034#include <time.h>
BeYkeRYkt535cc212018-12-23 22:55:35 +090035#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"
43#include <log/log.h>
44#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
tomascus8b038b12019-02-19 17:15:58 +110053const int kMaxLaunchDuration = 5000; /* ms */
BeYkeRYktbd52e812018-12-13 06:42:35 +090054const int kMaxInteractiveDuration = 5000; /* ms */
55const int kMinInteractiveDuration = 500; /* ms */
56const int kMinFlingDuration = 1500; /* ms */
57
BeYkeRYkt535cc212018-12-23 22:55:35 +090058static int video_encode_hint_sent;
59
60static int current_power_profile = PROFILE_BALANCED;
61
62static int profile_high_performance[] = {
BeYkeRYkt48b4e272018-12-23 23:21:50 +090063 SCHED_BOOST_ON_V3, 0x1,
64 ALL_CPUS_PWR_CLPS_DIS_V3, 0x1,
BeYkeRYkt535cc212018-12-23 22:55:35 +090065 CPUS_ONLINE_MIN_BIG, 0x4,
BeYkeRYkt48b4e272018-12-23 23:21:50 +090066 MIN_FREQ_BIG_CORE_0, 0xFFF,
67 MIN_FREQ_LITTLE_CORE_0, 0xFFF,
68 GPU_MIN_POWER_LEVEL, 0x1,
69 SCHED_PREFER_IDLE_DIS_V3, 0x1,
70 SCHED_SMALL_TASK, 0x1,
71 SCHED_MOSTLY_IDLE_NR_RUN, 0x1,
72 SCHED_MOSTLY_IDLE_LOAD, 0x1,
BeYkeRYkt535cc212018-12-23 22:55:35 +090073};
74
75static int profile_power_save[] = {
BeYkeRYkt48b4e272018-12-23 23:21:50 +090076 CPUS_ONLINE_MAX_BIG, 0x1,
77 MAX_FREQ_BIG_CORE_0, 0x3bf,
78 MAX_FREQ_LITTLE_CORE_0, 0x300,
BeYkeRYkt535cc212018-12-23 22:55:35 +090079};
80
81static int profile_bias_power[] = {
BeYkeRYkt48b4e272018-12-23 23:21:50 +090082 MAX_FREQ_BIG_CORE_0, 0x4B0,
83 MAX_FREQ_LITTLE_CORE_0, 0x300,
BeYkeRYkt535cc212018-12-23 22:55:35 +090084};
85
86static int profile_bias_performance[] = {
BeYkeRYkt48b4e272018-12-23 23:21:50 +090087 CPUS_ONLINE_MAX_BIG, 0x4,
88 MIN_FREQ_BIG_CORE_0, 0x540,
BeYkeRYkt535cc212018-12-23 22:55:35 +090089};
90
91#ifdef INTERACTION_BOOST
92int get_number_of_profiles()
93{
94 return 5;
95}
96#endif
97
dianlujitao8780cb72019-02-23 20:24:57 +080098static int set_power_profile(void *data)
BeYkeRYkt535cc212018-12-23 22:55:35 +090099{
dianlujitao8780cb72019-02-23 20:24:57 +0800100 int profile = data ? *((int*)data) : 0;
BeYkeRYkt535cc212018-12-23 22:55:35 +0900101 int ret = -EINVAL;
102 const char *profile_name = NULL;
103
104 if (profile == current_power_profile)
105 return 0;
106
107 ALOGV("%s: Profile=%d", __func__, profile);
108
109 if (current_power_profile != PROFILE_BALANCED) {
110 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
111 ALOGV("%s: Hint undone", __func__);
112 current_power_profile = PROFILE_BALANCED;
113 }
114
115 if (profile == PROFILE_POWER_SAVE) {
116 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
117 ARRAY_SIZE(profile_power_save));
118 profile_name = "powersave";
119
120 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
121 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
122 profile_high_performance, ARRAY_SIZE(profile_high_performance));
123 profile_name = "performance";
124
125 } else if (profile == PROFILE_BIAS_POWER) {
126 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power,
127 ARRAY_SIZE(profile_bias_power));
128 profile_name = "bias power";
129
130 } else if (profile == PROFILE_BIAS_PERFORMANCE) {
131 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
132 profile_bias_performance, ARRAY_SIZE(profile_bias_performance));
133 profile_name = "bias perf";
134 } else if (profile == PROFILE_BALANCED) {
135 ret = 0;
136 profile_name = "balanced";
137 }
138
139 if (ret == 0) {
140 current_power_profile = profile;
141 ALOGD("%s: Set %s mode", __func__, profile_name);
142 }
143 return ret;
144}
145
146static void process_video_encode_hint(void *metadata)
147{
148 char governor[80];
149 struct video_encode_metadata_t video_encode_metadata;
150
151 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) {
152 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) {
153 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) {
154 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) {
155 ALOGE("Can't obtain scaling governor.");
156 return;
157 }
158 }
159 }
160 }
161
162 if (!metadata) {
163 return;
164 }
165
166 /* Initialize encode metadata struct fields. */
167 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
168 video_encode_metadata.state = -1;
169 video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID;
170
171 if (parse_video_encode_metadata((char *)metadata,
172 &video_encode_metadata) == -1) {
173 ALOGE("Error occurred while parsing metadata.");
174 return;
175 }
176
177 if (video_encode_metadata.state == 1) {
178 if (is_interactive_governor(governor)) {
179 int resource_values[] = {
180 INT_OP_CLUSTER0_USE_SCHED_LOAD, 0x1,
181 INT_OP_CLUSTER1_USE_SCHED_LOAD, 0x1,
182 INT_OP_CLUSTER0_USE_MIGRATION_NOTIF, 0x1,
183 INT_OP_CLUSTER1_USE_MIGRATION_NOTIF, 0x1,
184 INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_40,
185 INT_OP_CLUSTER1_TIMER_RATE, BIG_LITTLE_TR_MS_40
186 };
187 if (!video_encode_hint_sent) {
188 perform_hint_action(video_encode_metadata.hint_id,
189 resource_values, ARRAY_SIZE(resource_values));
190 video_encode_hint_sent = 1;
191 }
192 }
193 } else if (video_encode_metadata.state == 0) {
194 if (is_interactive_governor(governor)) {
195 undo_hint_action(video_encode_metadata.hint_id);
196 video_encode_hint_sent = 0;
197 }
198 }
199}
200
tomascus8b038b12019-02-19 17:15:58 +1100201static int process_activity_launch_hint(void *data)
BeYkeRYktbd52e812018-12-13 06:42:35 +0900202{
tomascus8b038b12019-02-19 17:15:58 +1100203 static int launch_handle = -1;
204 static int launch_mode = 0;
205
206 // release lock early if launch has finished
207 if (!data) {
208 if (CHECK_HANDLE(launch_handle)) {
209 release_request(launch_handle);
210 launch_handle = -1;
211 }
212 launch_mode = 0;
213 return HINT_HANDLED;
214 }
215
216 if (!launch_mode) {
217 launch_handle = perf_hint_enable_with_type(VENDOR_HINT_FIRST_LAUNCH_BOOST,
218 kMaxLaunchDuration, LAUNCH_BOOST_V1);
219 if (!CHECK_HANDLE(launch_handle)) {
220 ALOGE("Failed to perform launch boost");
221 return HINT_NONE;
222 }
223 launch_mode = 1;
224 }
225 return HINT_HANDLED;
BeYkeRYktbd52e812018-12-13 06:42:35 +0900226}
227
228static void process_interaction_hint(void *data)
229{
230 static struct timespec s_previous_boost_timespec;
231 static int s_previous_duration = 0;
232
233 struct timespec cur_boost_timespec;
234 long long elapsed_time;
235 int duration = kMinInteractiveDuration;
236
237 if (data) {
238 int input_duration = *((int*)data);
239 if (input_duration > duration) {
240 duration = (input_duration > kMaxInteractiveDuration) ?
241 kMaxInteractiveDuration : input_duration;
242 }
243 }
244
245 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
246
247 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
248 // don't hint if previous hint's duration covers this hint's duration
249 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
250 return;
251 }
252 s_previous_boost_timespec = cur_boost_timespec;
253 s_previous_duration = duration;
254
255 if (duration >= kMinFlingDuration) {
256 // Use launch boost resources for fling boost
257 perf_hint_enable_with_type(VENDOR_HINT_FIRST_LAUNCH_BOOST, -1, LAUNCH_BOOST_V1);
258 } else {
259 perf_hint_enable_with_type(VENDOR_HINT_SCROLL_BOOST, duration, SCROLL_VERTICAL);
260 }
261}
262
BeYkeRYkt535cc212018-12-23 22:55:35 +0900263int power_hint_override(power_hint_t hint, void *data)
264{
265 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800266 if (set_power_profile(data) < 0)
BeYkeRYkt535cc212018-12-23 22:55:35 +0900267 ALOGE("Setting power profile failed. perfd not started?");
268 return HINT_HANDLED;
269 }
270
271 // Skip other hints in high/low power modes
272 if (current_power_profile == PROFILE_POWER_SAVE ||
273 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
274 return HINT_HANDLED;
275 }
276
277 switch (hint) {
278 case POWER_HINT_VIDEO_ENCODE:
279 process_video_encode_hint(data);
280 return HINT_HANDLED;
BeYkeRYktbd52e812018-12-13 06:42:35 +0900281 case POWER_HINT_INTERACTION:
282 process_interaction_hint(data);
283 return HINT_HANDLED;
284 case POWER_HINT_LAUNCH:
tomascus8b038b12019-02-19 17:15:58 +1100285 return process_activity_launch_hint(data);
BeYkeRYkt535cc212018-12-23 22:55:35 +0900286 default:
287 break;
288 }
289 return HINT_NONE;
290}
291
292int set_interactive_override(int on)
293{
294 char governor[80];
295
296 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) {
297 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) {
298 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) {
299 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) {
300 ALOGE("Can't obtain scaling governor.");
301 return HINT_NONE;
302 }
303 }
304 }
305 }
306
307 if (!on) {
308 /* Display off. */
309 if (is_interactive_governor(governor)) {
310 int resource_values[] = {
311 INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_50,
312 INT_OP_CLUSTER1_TIMER_RATE, BIG_LITTLE_TR_MS_50,
313 INT_OP_NOTIFY_ON_MIGRATE, 0x00
314 };
315 perform_hint_action(DISPLAY_STATE_HINT_ID,
316 resource_values, ARRAY_SIZE(resource_values));
317 }
318 } else {
319 /* Display on. */
320 if (is_interactive_governor(governor)) {
321 undo_hint_action(DISPLAY_STATE_HINT_ID);
322 }
323 }
324 return HINT_HANDLED;
325}