blob: 37296cc2c783442238052e87490e65f7c707a7d6 [file] [log] [blame]
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +05301/*
2 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
Michael Bestas47636f62018-05-25 21:30:28 +03003 * Copyright (C) 2018 The LineageOS Project
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +05304 *
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>
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +053035#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
Michael Bestas1add9ac2018-03-25 22:56:44 +030042#define LOG_TAG "QCOM PowerHAL"
Ethan Chena42f4a82018-03-01 21:31:15 -080043#include <log/log.h>
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +053044#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
dianlujitao5288c4a2018-01-20 19:00:05 +080053#define NUM_PERF_MODES 3
54
tomascus8b038b12019-02-19 17:15:58 +110055const int kMaxLaunchDuration = 5000; /* ms */
BeYkeRYktbd52e812018-12-13 06:42:35 +090056const int kMaxInteractiveDuration = 5000; /* ms */
57const int kMinInteractiveDuration = 500; /* ms */
BeYkeRYktbd52e812018-12-13 06:42:35 +090058
Michael Bestasf6ea7ab2018-05-25 22:57:04 +030059static int current_power_profile = PROFILE_BALANCED;
60
61static int profile_high_performance[] = {
62 SCHED_BOOST_ON_V3, 0x1,
63 ALL_CPUS_PWR_CLPS_DIS_V3, 0x1,
64 CPUS_ONLINE_MIN_BIG, 0x4,
65 MIN_FREQ_BIG_CORE_0, 0xFFF,
66 MIN_FREQ_LITTLE_CORE_0, 0xFFF,
dianlujitao5eb472d2018-09-06 21:43:14 +080067 GPU_MIN_POWER_LEVEL, 0x1,
Michael Bestasf6ea7ab2018-05-25 22:57:04 +030068 SCHED_PREFER_IDLE_DIS_V3, 0x1,
dianlujitao5eb472d2018-09-06 21:43:14 +080069 SCHED_SMALL_TASK, 0x1,
70 SCHED_MOSTLY_IDLE_NR_RUN, 0x1,
71 SCHED_MOSTLY_IDLE_LOAD, 0x1,
Michael Bestasf6ea7ab2018-05-25 22:57:04 +030072};
73
74static int profile_power_save[] = {
dianlujitao5eb472d2018-09-06 21:43:14 +080075 CPUS_ONLINE_MAX_BIG, 0x1,
Michael Bestasf6ea7ab2018-05-25 22:57:04 +030076 MAX_FREQ_BIG_CORE_0, 0x3bf,
77 MAX_FREQ_LITTLE_CORE_0, 0x300,
78};
79
80static int profile_bias_power[] = {
81 MAX_FREQ_BIG_CORE_0, 0x4B0,
82 MAX_FREQ_LITTLE_CORE_0, 0x300,
83};
84
85static int profile_bias_performance[] = {
dianlujitao5eb472d2018-09-06 21:43:14 +080086 CPUS_ONLINE_MAX_BIG, 0x4,
Michael Bestasf6ea7ab2018-05-25 22:57:04 +030087 MIN_FREQ_BIG_CORE_0, 0x540,
88};
89
90#ifdef INTERACTION_BOOST
91int get_number_of_profiles()
92{
93 return 5;
94}
95#endif
96
dianlujitao8780cb72019-02-23 20:24:57 +080097static int set_power_profile(void *data)
Michael Bestasf6ea7ab2018-05-25 22:57:04 +030098{
dianlujitao8780cb72019-02-23 20:24:57 +080099 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200100 int ret = -EINVAL;
101 const char *profile_name = NULL;
102
Michael Bestasf6ea7ab2018-05-25 22:57:04 +0300103 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200104 return 0;
Michael Bestasf6ea7ab2018-05-25 22:57:04 +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 Bestasf6ea7ab2018-05-25 22:57:04 +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 Bestasf6ea7ab2018-05-25 22:57:04 +0300116 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200117 profile_name = "powersave";
Michael Bestasf6ea7ab2018-05-25 22:57:04 +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 Bestasf6ea7ab2018-05-25 22:57:04 +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 Bestasf6ea7ab2018-05-25 22:57:04 +0300126 ARRAY_SIZE(profile_bias_power));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200127 profile_name = "bias power";
Michael Bestasf6ea7ab2018-05-25 22:57:04 +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 Bestasf6ea7ab2018-05-25 22:57:04 +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 Bestasf6ea7ab2018-05-25 22:57:04 +0300143}
144
dianlujitao5288c4a2018-01-20 19:00:05 +0800145typedef enum {
146 NORMAL_MODE = 0,
147 SUSTAINED_MODE = 1,
148 VR_MODE = 2,
149 VR_SUSTAINED_MODE = (SUSTAINED_MODE|VR_MODE),
150 INVALID_MODE = 0xFF
151} perf_mode_type_t;
152
153typedef struct perf_mode {
154 perf_mode_type_t type;
155 int perf_hint_id;
156} perf_mode_t;
157
158perf_mode_t perf_modes[NUM_PERF_MODES] = {
159 { SUSTAINED_MODE, SUSTAINED_PERF_HINT },
160 { VR_MODE, VR_MODE_HINT },
161 { VR_SUSTAINED_MODE, VR_MODE_SUSTAINED_PERF_HINT }
162};
163
164static int current_mode = NORMAL_MODE;
165
166static inline int get_perfd_hint_id(perf_mode_type_t type) {
167 int i;
168 for (i = 0; i < NUM_PERF_MODES; i++) {
169 if (perf_modes[i].type == type) {
170 ALOGD("Hint id is 0x%x for mode 0x%x", perf_modes[i].perf_hint_id, type);
171 return perf_modes[i].perf_hint_id;
172 }
173 }
174 ALOGD("Couldn't find the hint for mode 0x%x", type);
175 return 0;
176}
177
178static int switch_mode(perf_mode_type_t mode) {
179 int hint_id = 0;
180 static int perfd_mode_handle = -1;
181
182 // release existing mode if any
183 if (CHECK_HANDLE(perfd_mode_handle)) {
184 ALOGD("Releasing handle 0x%x", perfd_mode_handle);
185 release_request(perfd_mode_handle);
186 perfd_mode_handle = -1;
187 }
188 // switch to a perf mode
189 hint_id = get_perfd_hint_id(mode);
190 if (hint_id != 0) {
191 perfd_mode_handle = perf_hint_enable(hint_id, 0);
192 if (!CHECK_HANDLE(perfd_mode_handle)) {
193 ALOGE("Failed perf_hint_interaction for mode: 0x%x", mode);
194 return -1;
195 }
196 ALOGD("Acquired handle 0x%x", perfd_mode_handle);
197 }
198 return 0;
199}
200
201static int process_perf_hint(void *data, perf_mode_type_t mode) {
202 // enable
dianlujitao8780cb72019-02-23 20:24:57 +0800203 if (data) {
dianlujitao5288c4a2018-01-20 19:00:05 +0800204 ALOGI("Enable request for mode: 0x%x", mode);
205 // check if mode is current mode
206 if (current_mode & mode) {
207 ALOGD("Mode 0x%x already enabled", mode);
208 return HINT_HANDLED;
209 }
210 // enable requested mode
211 if (0 != switch_mode(current_mode | mode)) {
212 ALOGE("Couldn't enable mode 0x%x", mode);
213 return HINT_NONE;
214 }
215 current_mode |= mode;
216 ALOGI("Current mode is 0x%x", current_mode);
217 // disable
218 } else {
219 ALOGI("Disable request for mode: 0x%x", mode);
220 // check if mode is enabled
221 if (!(current_mode & mode)) {
222 ALOGD("Mode 0x%x already disabled", mode);
223 return HINT_HANDLED;
224 }
225 // disable requested mode
226 if (0 != switch_mode(current_mode & ~mode)) {
227 ALOGE("Couldn't disable mode 0x%x", mode);
228 return HINT_NONE;
229 }
230 current_mode &= ~mode;
231 ALOGI("Current mode is 0x%x", current_mode);
232 }
233
234 return HINT_HANDLED;
235}
236
dianlujitao2f570312018-01-20 18:54:04 +0800237static int process_video_encode_hint(void *metadata)
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +0530238{
239 char governor[80];
240 struct video_encode_metadata_t video_encode_metadata;
dianlujitao2f570312018-01-20 18:54:04 +0800241 static int video_encode_handle = 0;
242
243 if (!metadata) {
244 return HINT_NONE;
245 }
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +0530246
Michael Bestas47636f62018-05-25 21:30:28 +0300247 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) {
248 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) {
249 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) {
250 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) {
251 ALOGE("Can't obtain scaling governor.");
dianlujitao2f570312018-01-20 18:54:04 +0800252 return HINT_NONE;
Michael Bestas47636f62018-05-25 21:30:28 +0300253 }
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +0530254 }
Michael Bestas47636f62018-05-25 21:30:28 +0300255 }
256 }
257
dianlujitao2f570312018-01-20 18:54:04 +0800258 /* Initialize encode metadata struct fields */
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +0530259 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
260 video_encode_metadata.state = -1;
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +0530261
dianlujitao2f570312018-01-20 18:54:04 +0800262 if (parse_video_encode_metadata((char *)metadata, &video_encode_metadata) == -1) {
Michael Bestas47636f62018-05-25 21:30:28 +0300263 ALOGE("Error occurred while parsing metadata.");
dianlujitao2f570312018-01-20 18:54:04 +0800264 return HINT_NONE;
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +0530265 }
266
267 if (video_encode_metadata.state == 1) {
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800268 if (is_interactive_governor(governor)) {
dianlujitao2f570312018-01-20 18:54:04 +0800269 video_encode_handle = perf_hint_enable(
270 VIDEO_ENCODE_HINT, 0);
271 return HINT_HANDLED;
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +0530272 }
273 } else if (video_encode_metadata.state == 0) {
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800274 if (is_interactive_governor(governor)) {
dianlujitao2f570312018-01-20 18:54:04 +0800275 release_request(video_encode_handle);
276 return HINT_HANDLED;
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +0530277 }
278 }
dianlujitao2f570312018-01-20 18:54:04 +0800279 return HINT_NONE;
Michael Bestas47636f62018-05-25 21:30:28 +0300280}
281
tomascus8b038b12019-02-19 17:15:58 +1100282static int process_activity_launch_hint(void *data)
BeYkeRYktbd52e812018-12-13 06:42:35 +0900283{
tomascus8b038b12019-02-19 17:15:58 +1100284 static int launch_handle = -1;
285 static int launch_mode = 0;
286
287 // release lock early if launch has finished
288 if (!data) {
289 if (CHECK_HANDLE(launch_handle)) {
290 release_request(launch_handle);
291 launch_handle = -1;
292 }
293 launch_mode = 0;
294 return HINT_HANDLED;
295 }
296
BeYkeRYktbd52e812018-12-13 06:42:35 +0900297 if (current_mode != NORMAL_MODE) {
298 ALOGV("%s: ignoring due to other active perf hints", __func__);
tomascus8b038b12019-02-19 17:15:58 +1100299 } else if (!launch_mode) {
300 launch_handle = perf_hint_enable_with_type(VENDOR_HINT_FIRST_LAUNCH_BOOST,
301 kMaxLaunchDuration, LAUNCH_BOOST_V1);
302 if (!CHECK_HANDLE(launch_handle)) {
303 ALOGE("Failed to perform launch boost");
304 return HINT_NONE;
305 }
306 launch_mode = 1;
BeYkeRYktbd52e812018-12-13 06:42:35 +0900307 }
308 return HINT_HANDLED;
309}
310
311static int process_interaction_hint(void *data)
312{
313 static struct timespec s_previous_boost_timespec;
314 static int s_previous_duration = 0;
315
316 struct timespec cur_boost_timespec;
317 long long elapsed_time;
318 int duration = kMinInteractiveDuration;
319
320 if (current_mode != NORMAL_MODE) {
321 ALOGV("%s: ignoring due to other active perf hints", __func__);
322 return HINT_HANDLED;
323 }
324
325 if (data) {
326 int input_duration = *((int*)data);
327 if (input_duration > duration) {
328 duration = (input_duration > kMaxInteractiveDuration) ?
329 kMaxInteractiveDuration : input_duration;
330 }
331 }
332
333 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
334
335 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
336 // don't hint if previous hint's duration covers this hint's duration
337 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
338 return HINT_HANDLED;
339 }
340 s_previous_boost_timespec = cur_boost_timespec;
341 s_previous_duration = duration;
342
tomascus1c38bac2019-02-13 14:42:27 +1100343 perf_hint_enable_with_type(VENDOR_HINT_SCROLL_BOOST, duration, SCROLL_VERTICAL);
BeYkeRYktbd52e812018-12-13 06:42:35 +0900344
345 return HINT_HANDLED;
346}
347
Michael Bestas47636f62018-05-25 21:30:28 +0300348int power_hint_override(power_hint_t hint, void *data)
349{
dianlujitao2f570312018-01-20 18:54:04 +0800350 int ret_val = HINT_NONE;
351
Michael Bestasf6ea7ab2018-05-25 22:57:04 +0300352 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800353 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200354 ALOGE("Setting power profile failed. perf HAL not started?");
Michael Bestasf6ea7ab2018-05-25 22:57:04 +0300355 return HINT_HANDLED;
356 }
357
358 // Skip other hints in high/low power modes
359 if (current_power_profile == PROFILE_POWER_SAVE ||
360 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
361 return HINT_HANDLED;
362 }
363
Michael Bestas47636f62018-05-25 21:30:28 +0300364 switch (hint) {
365 case POWER_HINT_VSYNC:
366 break;
367 case POWER_HINT_VIDEO_ENCODE:
dianlujitao2f570312018-01-20 18:54:04 +0800368 ret_val = process_video_encode_hint(data);
369 break;
dianlujitao5288c4a2018-01-20 19:00:05 +0800370 case POWER_HINT_SUSTAINED_PERFORMANCE:
371 ret_val = process_perf_hint(data, SUSTAINED_MODE);
372 break;
373 case POWER_HINT_VR_MODE:
374 ret_val = process_perf_hint(data, VR_MODE);
375 break;
BeYkeRYktbd52e812018-12-13 06:42:35 +0900376 case POWER_HINT_INTERACTION:
377 ret_val = process_interaction_hint(data);
378 break;
379 case POWER_HINT_LAUNCH:
380 ret_val = process_activity_launch_hint(data);
381 break;
Michael Bestas47636f62018-05-25 21:30:28 +0300382 default:
383 break;
384 }
dianlujitao2f570312018-01-20 18:54:04 +0800385 return ret_val;
Michael Bestas47636f62018-05-25 21:30:28 +0300386}
387
388int set_interactive_override(int on)
389{
390 char governor[80];
391
392 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) {
393 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) {
394 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) {
395 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) {
396 ALOGE("Can't obtain scaling governor.");
397 return HINT_NONE;
398 }
399 }
400 }
401 }
402
403 if (!on) {
404 /* Display off. */
405 if (is_interactive_governor(governor)) {
406 int resource_values[] = {
407 INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_40
408 };
409 perform_hint_action(DISPLAY_STATE_HINT_ID,
410 resource_values, ARRAY_SIZE(resource_values));
411 }
412 } else {
413 /* Display on. */
414 if (is_interactive_governor(governor)) {
415 undo_hint_action(DISPLAY_STATE_HINT_ID);
416 }
417 }
418 return HINT_HANDLED;
Sravan Kumar Ambapurameea8e562016-02-28 15:09:37 +0530419}