blob: a49c361052d42ca3d76d23ad001a6026dec9b26d [file] [log] [blame]
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +05301/*
2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
Michael Bestasabe7e652018-03-26 04:10:16 +03003 * Copyright (C) 2018 The LineageOS Project
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +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>
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
Michael Bestas1add9ac2018-03-25 22:56:44 +030041#define LOG_TAG "QCOM PowerHAL"
Ethan Chendaff0dd2018-03-01 21:31:15 -080042#include <log/log.h>
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +053043#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
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +053052static int video_encode_hint_sent;
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +053053
Ethan Chena36e2122018-02-25 20:46:37 -080054static int current_power_profile = PROFILE_BALANCED;
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +053055
Ethan Chena36e2122018-02-25 20:46:37 -080056static int profile_high_performance[] = {
57 CPUS_ONLINE_MIN_BIG, 0x4,
58 CPUS_ONLINE_MIN_LITTLE, 0x4,
59 CPU0_MIN_FREQ_TURBO_MAX,
60 CPU4_MIN_FREQ_TURBO_MAX,
61};
62
63static int profile_power_save[] = {
dianlujitao5eb472d2018-09-06 21:43:14 +080064 CPUS_ONLINE_MAX_BIG, 0x0,
Ethan Chena36e2122018-02-25 20:46:37 -080065 CPU0_MAX_FREQ_NONTURBO_MAX,
66 CPU4_MAX_FREQ_NONTURBO_MAX,
67};
68
69static int profile_bias_power[] = {
70 CPU0_MAX_FREQ_NONTURBO_MAX,
71 CPU4_MAX_FREQ_NONTURBO_MAX,
72};
73
74static int profile_bias_performance[] = {
75 CPU0_MIN_FREQ_NONTURBO_MAX + 1,
76 CPU4_MIN_FREQ_NONTURBO_MAX + 1,
77};
78
dianlujitao40048112018-03-02 12:40:04 +080079#ifdef INTERACTION_BOOST
Ethan Chena36e2122018-02-25 20:46:37 -080080int get_number_of_profiles()
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +053081{
Ethan Chena36e2122018-02-25 20:46:37 -080082 return 5;
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +053083}
dianlujitao40048112018-03-02 12:40:04 +080084#endif
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +053085
dianlujitao8780cb72019-02-23 20:24:57 +080086static int set_power_profile(void *data)
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +053087{
dianlujitao8780cb72019-02-23 20:24:57 +080088 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020089 int ret = -EINVAL;
90 const char *profile_name = NULL;
91
Ethan Chena36e2122018-02-25 20:46:37 -080092 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020093 return 0;
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +053094
Ethan Chena36e2122018-02-25 20:46:37 -080095 ALOGV("%s: Profile=%d", __func__, profile);
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +053096
Ethan Chena36e2122018-02-25 20:46:37 -080097 if (current_power_profile != PROFILE_BALANCED) {
98 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
99 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200100 current_power_profile = PROFILE_BALANCED;
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +0530101 }
102
Ethan Chena36e2122018-02-25 20:46:37 -0800103 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200104 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
Ethan Chena36e2122018-02-25 20:46:37 -0800105 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200106 profile_name = "powersave";
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +0530107
Ethan Chena36e2122018-02-25 20:46:37 -0800108 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200109 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
110 profile_high_performance, ARRAY_SIZE(profile_high_performance));
111 profile_name = "performance";
Ethan Chena36e2122018-02-25 20:46:37 -0800112
113 } else if (profile == PROFILE_BIAS_POWER) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200114 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power,
Ethan Chena36e2122018-02-25 20:46:37 -0800115 ARRAY_SIZE(profile_bias_power));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200116 profile_name = "bias power";
Ethan Chena36e2122018-02-25 20:46:37 -0800117
118 } else if (profile == PROFILE_BIAS_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200119 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
120 profile_bias_performance, ARRAY_SIZE(profile_bias_performance));
121 profile_name = "bias perf";
122 } else if (profile == PROFILE_BALANCED) {
123 ret = 0;
124 profile_name = "balanced";
Ethan Chen22dd9f72018-03-01 21:40:04 -0800125 }
Ethan Chena36e2122018-02-25 20:46:37 -0800126
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200127 if (ret == 0) {
128 current_power_profile = profile;
129 ALOGD("%s: Set %s mode", __func__, profile_name);
130 }
131 return ret;
Nikhil Kumar Kansalb40c1362015-05-05 16:10:43 +0530132}
vanajau3353dc52016-09-19 14:32:32 +0530133
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530134static void process_video_encode_hint(void *metadata)
135{
136 char governor[80];
137 struct video_encode_metadata_t video_encode_metadata;
138
Michael Bestasabe7e652018-03-26 04:10:16 +0300139 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) {
140 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) {
141 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) {
142 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) {
Ethan Chen22dd9f72018-03-01 21:40:04 -0800143 ALOGE("Can't obtain scaling governor.");
144 return;
145 }
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530146 }
Ethan Chen22dd9f72018-03-01 21:40:04 -0800147 }
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530148 }
149
Ethan Chena36e2122018-02-25 20:46:37 -0800150 if (!metadata) {
151 return;
152 }
153
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530154 /* Initialize encode metadata struct fields. */
155 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
156 video_encode_metadata.state = -1;
157 video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID;
158
Ethan Chena36e2122018-02-25 20:46:37 -0800159 if (parse_video_encode_metadata((char *)metadata,
Michael Bestas47636f62018-05-25 21:30:28 +0300160 &video_encode_metadata) == -1) {
Ethan Chena36e2122018-02-25 20:46:37 -0800161 ALOGE("Error occurred while parsing metadata.");
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530162 return;
163 }
164
165 if (video_encode_metadata.state == 1) {
Ethan Chen70b1f442018-03-01 21:41:04 -0800166 if (is_interactive_governor(governor)) {
Ethan Chen22dd9f72018-03-01 21:40:04 -0800167 int resource_values[] = {
168 INT_OP_CLUSTER0_USE_SCHED_LOAD, 0x1,
169 INT_OP_CLUSTER1_USE_SCHED_LOAD, 0x1,
170 INT_OP_CLUSTER0_USE_MIGRATION_NOTIF, 0x1,
171 INT_OP_CLUSTER1_USE_MIGRATION_NOTIF, 0x1,
172 INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_40,
173 INT_OP_CLUSTER1_TIMER_RATE, BIG_LITTLE_TR_MS_40
174 };
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530175 if (!video_encode_hint_sent) {
176 perform_hint_action(video_encode_metadata.hint_id,
Ethan Chen22dd9f72018-03-01 21:40:04 -0800177 resource_values, ARRAY_SIZE(resource_values));
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530178 video_encode_hint_sent = 1;
179 }
180 }
181 } else if (video_encode_metadata.state == 0) {
Ethan Chen70b1f442018-03-01 21:41:04 -0800182 if (is_interactive_governor(governor)) {
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530183 undo_hint_action(video_encode_metadata.hint_id);
184 video_encode_hint_sent = 0;
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530185 }
186 }
Nikhil Kumar Kansal813ac902015-06-24 16:55:16 +0530187}
Ethan Chena36e2122018-02-25 20:46:37 -0800188
189int power_hint_override(power_hint_t hint, void *data)
190{
191 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800192 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200193 ALOGE("Setting power profile failed. perfd not started?");
Ethan Chena36e2122018-02-25 20:46:37 -0800194 return HINT_HANDLED;
195 }
196
197 // Skip other hints in high/low power modes
198 if (current_power_profile == PROFILE_POWER_SAVE ||
199 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
200 return HINT_HANDLED;
201 }
202
203 switch (hint) {
204 case POWER_HINT_VIDEO_ENCODE:
205 process_video_encode_hint(data);
206 return HINT_HANDLED;
207 default:
208 break;
209 }
210 return HINT_NONE;
211}
212
213int set_interactive_override(int on)
214{
215 char governor[80];
Ethan Chena36e2122018-02-25 20:46:37 -0800216
Michael Bestasabe7e652018-03-26 04:10:16 +0300217 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) {
218 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) {
219 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) {
220 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) {
Ethan Chena36e2122018-02-25 20:46:37 -0800221 ALOGE("Can't obtain scaling governor.");
222 return HINT_NONE;
223 }
224 }
225 }
226 }
227
228 if (!on) {
229 /* Display off. */
230 if (is_interactive_governor(governor)) {
231 int resource_values[] = {
232 INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_50,
233 INT_OP_CLUSTER1_TIMER_RATE, BIG_LITTLE_TR_MS_50,
234 INT_OP_NOTIFY_ON_MIGRATE, 0x00
235 };
Michael Bestas47636f62018-05-25 21:30:28 +0300236 perform_hint_action(DISPLAY_STATE_HINT_ID,
237 resource_values, ARRAY_SIZE(resource_values));
238 }
Ethan Chena36e2122018-02-25 20:46:37 -0800239 } else {
240 /* Display on. */
241 if (is_interactive_governor(governor)) {
242 undo_hint_action(DISPLAY_STATE_HINT_ID);
Ethan Chena36e2122018-02-25 20:46:37 -0800243 }
244 }
Ethan Chena36e2122018-02-25 20:46:37 -0800245 return HINT_HANDLED;
246}