blob: dae575bc4f5e533f8e9ea59f374e2fe9719f087d [file] [log] [blame]
Dilip Gudlur4edd2c32015-07-01 14:13:43 -07001/*
Dilip Gudlur84060b62016-07-18 10:36:08 -07002 * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
dianlujitaoc9f97d72018-01-18 23:34:38 +08003 * Copyright (C) 2018 The LineageOS Project
Dilip Gudlur4edd2c32015-07-01 14:13:43 -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 */
Michael Bestas47636f62018-05-25 21:30:28 +030030
Dilip Gudlur4edd2c32015-07-01 14:13:43 -070031#define LOG_NIDEBUG 0
32
33#include <errno.h>
BeYkeRYktbd52e812018-12-13 06:42:35 +090034#include <time.h>
Dilip Gudlur4edd2c32015-07-01 14:13:43 -070035#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"
Rashed Abdel-Tawab9c14c9f2017-12-26 23:11:03 +020043#include <log/log.h>
Dilip Gudlur4edd2c32015-07-01 14:13:43 -070044#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 Bestas47636f62018-05-25 21:30:28 +030053#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
dianlujitaoc9f97d72018-01-18 23:34:38 +080059static 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, 0x2,
65 CPUS_ONLINE_MIN_LITTLE, 0x2,
66 MIN_FREQ_BIG_CORE_0, 0xFFF,
67 MIN_FREQ_LITTLE_CORE_0, 0xFFF,
68};
69
70static int profile_power_save[] = {
dianlujitao5eb472d2018-09-06 21:43:14 +080071 CPUS_ONLINE_MAX_BIG, 0x1,
dianlujitaoc9f97d72018-01-18 23:34:38 +080072 MAX_FREQ_BIG_CORE_0, 0x3E8,
73 MAX_FREQ_LITTLE_CORE_0, 0x3E8,
74};
75
76static int profile_bias_power[] = {
77 MAX_FREQ_BIG_CORE_0, 0x514,
78 MAX_FREQ_LITTLE_CORE_0, 0x3E8,
79};
80
81static int profile_bias_performance[] = {
dianlujitao5eb472d2018-09-06 21:43:14 +080082 CPUS_ONLINE_MAX_BIG, 0x2,
83 CPUS_ONLINE_MAX_LITTLE, 0x2,
dianlujitaoc9f97d72018-01-18 23:34:38 +080084 MIN_FREQ_BIG_CORE_0, 0x578,
85};
86
dianlujitao40048112018-03-02 12:40:04 +080087#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +030088int get_number_of_profiles()
89{
dianlujitaoc9f97d72018-01-18 23:34:38 +080090 return 5;
91}
dianlujitao40048112018-03-02 12:40:04 +080092#endif
dianlujitaoc9f97d72018-01-18 23:34:38 +080093
dianlujitao8780cb72019-02-23 20:24:57 +080094static int set_power_profile(void *data)
Michael Bestas47636f62018-05-25 21:30:28 +030095{
dianlujitao8780cb72019-02-23 20:24:57 +080096 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020097 int ret = -EINVAL;
98 const char *profile_name = NULL;
99
dianlujitaoc9f97d72018-01-18 23:34:38 +0800100 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200101 return 0;
dianlujitaoc9f97d72018-01-18 23:34:38 +0800102
103 ALOGV("%s: Profile=%d", __func__, profile);
104
105 if (current_power_profile != PROFILE_BALANCED) {
106 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
107 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200108 current_power_profile = PROFILE_BALANCED;
dianlujitaoc9f97d72018-01-18 23:34:38 +0800109 }
110
111 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200112 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
dianlujitaoc9f97d72018-01-18 23:34:38 +0800113 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200114 profile_name = "powersave";
dianlujitaoc9f97d72018-01-18 23:34:38 +0800115
116 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200117 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
118 profile_high_performance, ARRAY_SIZE(profile_high_performance));
119 profile_name = "performance";
dianlujitaoc9f97d72018-01-18 23:34:38 +0800120
121 } else if (profile == PROFILE_BIAS_POWER) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200122 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power,
dianlujitaoc9f97d72018-01-18 23:34:38 +0800123 ARRAY_SIZE(profile_bias_power));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200124 profile_name = "bias power";
dianlujitaoc9f97d72018-01-18 23:34:38 +0800125
126 } else if (profile == PROFILE_BIAS_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200127 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
128 profile_bias_performance, ARRAY_SIZE(profile_bias_performance));
129 profile_name = "bias perf";
130 } else if (profile == PROFILE_BALANCED) {
131 ret = 0;
132 profile_name = "balanced";
dianlujitaoc9f97d72018-01-18 23:34:38 +0800133 }
134
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200135 if (ret == 0) {
136 current_power_profile = profile;
137 ALOGD("%s: Set %s mode", __func__, profile_name);
138 }
139 return ret;
dianlujitaoc9f97d72018-01-18 23:34:38 +0800140}
141
dianlujitaofed6fde2018-01-20 19:00:05 +0800142typedef enum {
143 NORMAL_MODE = 0,
144 SUSTAINED_MODE = 1,
145 VR_MODE = 2,
146 VR_SUSTAINED_MODE = (SUSTAINED_MODE|VR_MODE),
147 INVALID_MODE = 0xFF
148} perf_mode_type_t;
149
150typedef struct perf_mode {
151 perf_mode_type_t type;
152 int perf_hint_id;
153} perf_mode_t;
154
155perf_mode_t perf_modes[NUM_PERF_MODES] = {
156 { SUSTAINED_MODE, SUSTAINED_PERF_HINT },
157 { VR_MODE, VR_MODE_HINT },
158 { VR_SUSTAINED_MODE, VR_MODE_SUSTAINED_PERF_HINT }
159};
160
161static int current_mode = NORMAL_MODE;
162
Michael Bestas47636f62018-05-25 21:30:28 +0300163static inline int get_perfd_hint_id(perf_mode_type_t type) {
dianlujitaofed6fde2018-01-20 19:00:05 +0800164 int i;
165 for (i = 0; i < NUM_PERF_MODES; i++) {
166 if (perf_modes[i].type == type) {
167 ALOGD("Hint id is 0x%x for mode 0x%x", perf_modes[i].perf_hint_id, type);
168 return perf_modes[i].perf_hint_id;
169 }
170 }
171 ALOGD("Couldn't find the hint for mode 0x%x", type);
172 return 0;
173}
174
175static int switch_mode(perf_mode_type_t mode) {
dianlujitaofed6fde2018-01-20 19:00:05 +0800176 int hint_id = 0;
177 static int perfd_mode_handle = -1;
178
179 // release existing mode if any
180 if (CHECK_HANDLE(perfd_mode_handle)) {
181 ALOGD("Releasing handle 0x%x", perfd_mode_handle);
182 release_request(perfd_mode_handle);
183 perfd_mode_handle = -1;
184 }
185 // switch to a perf mode
186 hint_id = get_perfd_hint_id(mode);
187 if (hint_id != 0) {
188 perfd_mode_handle = perf_hint_enable(hint_id, 0);
189 if (!CHECK_HANDLE(perfd_mode_handle)) {
190 ALOGE("Failed perf_hint_interaction for mode: 0x%x", mode);
191 return -1;
192 }
193 ALOGD("Acquired handle 0x%x", perfd_mode_handle);
194 }
195 return 0;
196}
197
198static int process_perf_hint(void *data, perf_mode_type_t mode) {
dianlujitaofed6fde2018-01-20 19:00:05 +0800199 // enable
dianlujitao8780cb72019-02-23 20:24:57 +0800200 if (data) {
dianlujitaofed6fde2018-01-20 19:00:05 +0800201 ALOGI("Enable request for mode: 0x%x", mode);
202 // check if mode is current mode
Michael Bestas47636f62018-05-25 21:30:28 +0300203 if (current_mode & mode) {
dianlujitaofed6fde2018-01-20 19:00:05 +0800204 ALOGD("Mode 0x%x already enabled", mode);
205 return HINT_HANDLED;
206 }
207 // enable requested mode
Michael Bestas47636f62018-05-25 21:30:28 +0300208 if (0 != switch_mode(current_mode | mode)) {
dianlujitaofed6fde2018-01-20 19:00:05 +0800209 ALOGE("Couldn't enable mode 0x%x", mode);
210 return HINT_NONE;
211 }
212 current_mode |= mode;
213 ALOGI("Current mode is 0x%x", current_mode);
214 // disable
215 } else {
216 ALOGI("Disable request for mode: 0x%x", mode);
217 // check if mode is enabled
Michael Bestas47636f62018-05-25 21:30:28 +0300218 if (!(current_mode & mode)) {
dianlujitaofed6fde2018-01-20 19:00:05 +0800219 ALOGD("Mode 0x%x already disabled", mode);
220 return HINT_HANDLED;
221 }
Michael Bestas47636f62018-05-25 21:30:28 +0300222 // disable requested mode
223 if (0 != switch_mode(current_mode & ~mode)) {
dianlujitaofed6fde2018-01-20 19:00:05 +0800224 ALOGE("Couldn't disable mode 0x%x", mode);
225 return HINT_NONE;
226 }
227 current_mode &= ~mode;
228 ALOGI("Current mode is 0x%x", current_mode);
229 }
230
231 return HINT_HANDLED;
232}
233
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700234static int process_video_encode_hint(void *metadata)
235{
236 char governor[80];
237 struct video_encode_metadata_t video_encode_metadata;
dianlujitao281c1362018-01-20 18:54:04 +0800238 static int video_encode_handle = 0;
239
240 if (!metadata) {
241 return HINT_NONE;
242 }
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700243
244 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
245 ALOGE("Can't obtain scaling governor.");
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700246 return HINT_NONE;
247 }
248
249 /* Initialize encode metadata struct fields */
250 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
251 video_encode_metadata.state = -1;
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700252
dianlujitao281c1362018-01-20 18:54:04 +0800253 if (parse_video_encode_metadata((char *)metadata, &video_encode_metadata) == -1) {
254 ALOGE("Error occurred while parsing metadata.");
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700255 return HINT_NONE;
256 }
257
258 if (video_encode_metadata.state == 1) {
dianlujitao281c1362018-01-20 18:54:04 +0800259 if (is_interactive_governor(governor)) {
260 video_encode_handle = perf_hint_enable(
261 VIDEO_ENCODE_HINT, 0);
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700262 return HINT_HANDLED;
263 }
264 } else if (video_encode_metadata.state == 0) {
dianlujitao281c1362018-01-20 18:54:04 +0800265 if (is_interactive_governor(governor)) {
266 release_request(video_encode_handle);
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700267 return HINT_HANDLED;
268 }
269 }
270 return HINT_NONE;
271}
272
tomascus8b038b12019-02-19 17:15:58 +1100273static int process_activity_launch_hint(void *data)
BeYkeRYktbd52e812018-12-13 06:42:35 +0900274{
tomascus8b038b12019-02-19 17:15:58 +1100275 static int launch_handle = -1;
276 static int launch_mode = 0;
277
278 // release lock early if launch has finished
279 if (!data) {
280 if (CHECK_HANDLE(launch_handle)) {
281 release_request(launch_handle);
282 launch_handle = -1;
283 }
284 launch_mode = 0;
285 return HINT_HANDLED;
286 }
287
BeYkeRYktbd52e812018-12-13 06:42:35 +0900288 if (current_mode != NORMAL_MODE) {
289 ALOGV("%s: ignoring due to other active perf hints", __func__);
tomascus8b038b12019-02-19 17:15:58 +1100290 } else if (!launch_mode) {
291 launch_handle = perf_hint_enable_with_type(VENDOR_HINT_FIRST_LAUNCH_BOOST,
292 kMaxLaunchDuration, LAUNCH_BOOST_V1);
293 if (!CHECK_HANDLE(launch_handle)) {
294 ALOGE("Failed to perform launch boost");
295 return HINT_NONE;
296 }
297 launch_mode = 1;
BeYkeRYktbd52e812018-12-13 06:42:35 +0900298 }
299 return HINT_HANDLED;
300}
301
302static int process_interaction_hint(void *data)
303{
304 static struct timespec s_previous_boost_timespec;
305 static int s_previous_duration = 0;
306
307 struct timespec cur_boost_timespec;
308 long long elapsed_time;
309 int duration = kMinInteractiveDuration;
310
311 if (current_mode != NORMAL_MODE) {
312 ALOGV("%s: ignoring due to other active perf hints", __func__);
313 return HINT_HANDLED;
314 }
315
316 if (data) {
317 int input_duration = *((int*)data);
318 if (input_duration > duration) {
319 duration = (input_duration > kMaxInteractiveDuration) ?
320 kMaxInteractiveDuration : input_duration;
321 }
322 }
323
324 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
325
326 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
327 // don't hint if previous hint's duration covers this hint's duration
328 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
329 return HINT_HANDLED;
330 }
331 s_previous_boost_timespec = cur_boost_timespec;
332 s_previous_duration = duration;
333
tomascus1c38bac2019-02-13 14:42:27 +1100334 perf_hint_enable_with_type(VENDOR_HINT_SCROLL_BOOST, duration, SCROLL_VERTICAL);
BeYkeRYktbd52e812018-12-13 06:42:35 +0900335
336 return HINT_HANDLED;
337}
338
Rashed Abdel-Tawab00b21852017-11-03 12:44:16 -0700339int power_hint_override(power_hint_t hint, void *data)
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700340{
341 int ret_val = HINT_NONE;
dianlujitaoc9f97d72018-01-18 23:34:38 +0800342
343 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800344 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200345 ALOGE("Setting power profile failed. perf HAL not started?");
dianlujitaoc9f97d72018-01-18 23:34:38 +0800346 return HINT_HANDLED;
347 }
348
Michael Bestas8ff509e2018-03-26 05:21:23 +0300349 // Skip other hints in high/low power modes
350 if (current_power_profile == PROFILE_POWER_SAVE ||
351 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
dianlujitaoc9f97d72018-01-18 23:34:38 +0800352 return HINT_HANDLED;
Michael Bestas8ff509e2018-03-26 05:21:23 +0300353 }
dianlujitaoc9f97d72018-01-18 23:34:38 +0800354
Michael Bestas8ff509e2018-03-26 05:21:23 +0300355 switch (hint) {
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700356 case POWER_HINT_VIDEO_ENCODE:
357 ret_val = process_video_encode_hint(data);
358 break;
dianlujitaofed6fde2018-01-20 19:00:05 +0800359 case POWER_HINT_SUSTAINED_PERFORMANCE:
360 ret_val = process_perf_hint(data, SUSTAINED_MODE);
361 break;
362 case POWER_HINT_VR_MODE:
363 ret_val = process_perf_hint(data, VR_MODE);
364 break;
BeYkeRYktbd52e812018-12-13 06:42:35 +0900365 case POWER_HINT_INTERACTION:
366 ret_val = process_interaction_hint(data);
367 break;
368 case POWER_HINT_LAUNCH:
369 ret_val = process_activity_launch_hint(data);
370 break;
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700371 default:
372 break;
373 }
374 return ret_val;
375}
376
Michael Bestas6204c672018-03-26 03:26:31 +0300377int set_interactive_override(int UNUSED(on))
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700378{
379 return HINT_HANDLED; /* Don't excecute this code path, not in use */
Dilip Gudlur4edd2c32015-07-01 14:13:43 -0700380}