blob: 6669cdaadbe46af9354a558fb890cb8eb4bdadb7 [file] [log] [blame]
Dilip Gudlurcf3644b2014-11-02 17:25:52 -08001/*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
Michael Bestas11016fa2018-03-26 03:00:30 +03003 * Copyright (C) 2018 The LineageOS Project
Dilip Gudlurcf3644b2014-11-02 17:25:52 -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 Gudlurcf3644b2014-11-02 17:25:52 -080031#define LOG_NIDEBUG 0
32
33#include <errno.h>
Craig Tatloradf8a1c2018-05-21 17:22:31 +010034#include <time.h>
Dilip Gudlurcf3644b2014-11-02 17:25:52 -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 Gudlurcf3644b2014-11-02 17:25:52 -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 Bestas11016fa2018-03-26 03:00:30 +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 CPU6_MIN_FREQ_TURBO_MAX,
65 CPU7_MIN_FREQ_TURBO_MAX
66};
67
68static int profile_power_save[] = {
69 CPUS_ONLINE_MPD_OVERRIDE, 0x0A03,
70 CPU0_MAX_FREQ_NONTURBO_MAX - 2,
71 CPU1_MAX_FREQ_NONTURBO_MAX - 2,
72 CPU2_MAX_FREQ_NONTURBO_MAX - 2,
73 CPU3_MAX_FREQ_NONTURBO_MAX - 2,
74 CPU4_MAX_FREQ_NONTURBO_MAX - 2,
75 CPU5_MAX_FREQ_NONTURBO_MAX - 2,
76 CPU6_MAX_FREQ_NONTURBO_MAX - 2,
77 CPU7_MAX_FREQ_NONTURBO_MAX - 2
78};
79
80static int profile_bias_power[] = {
81 0x0A03, 0x0902,
82 CPU0_MAX_FREQ_NONTURBO_MAX - 2,
83 CPU1_MAX_FREQ_NONTURBO_MAX - 2,
Michael Bestas11016fa2018-03-26 03:00:30 +030084 CPU2_MAX_FREQ_NONTURBO_MAX - 2,
Han Wang34c74102018-07-24 17:12:14 +020085 CPU3_MAX_FREQ_NONTURBO_MAX - 2,
Michael Bestas11016fa2018-03-26 03:00:30 +030086 CPU4_MAX_FREQ_NONTURBO_MAX,
87 CPU5_MAX_FREQ_NONTURBO_MAX,
88 CPU6_MAX_FREQ_NONTURBO_MAX,
89 CPU7_MAX_FREQ_NONTURBO_MAX
90};
91
92static int profile_bias_performance[] = {
93 CPUS_ONLINE_MAX_LIMIT_MAX,
94 CPU4_MIN_FREQ_NONTURBO_MAX + 1,
95 CPU5_MIN_FREQ_NONTURBO_MAX + 1,
96 CPU6_MIN_FREQ_NONTURBO_MAX + 1,
97 CPU7_MIN_FREQ_NONTURBO_MAX + 1
98};
99
100#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +0300101int get_number_of_profiles()
102{
Michael Bestas11016fa2018-03-26 03:00:30 +0300103 return 5;
104}
105#endif
106
dianlujitao8780cb72019-02-23 20:24:57 +0800107static int set_power_profile(void *data)
Michael Bestas47636f62018-05-25 21:30:28 +0300108{
dianlujitao8780cb72019-02-23 20:24:57 +0800109 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200110 int ret = -EINVAL;
111 const char *profile_name = NULL;
112
Michael Bestas11016fa2018-03-26 03:00:30 +0300113 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200114 return 0;
Michael Bestas11016fa2018-03-26 03:00:30 +0300115
116 ALOGV("%s: Profile=%d", __func__, profile);
117
118 if (current_power_profile != PROFILE_BALANCED) {
119 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
120 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200121 current_power_profile = PROFILE_BALANCED;
Michael Bestas11016fa2018-03-26 03:00:30 +0300122 }
123
124 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200125 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
Michael Bestas11016fa2018-03-26 03:00:30 +0300126 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200127 profile_name = "powersave";
Michael Bestas11016fa2018-03-26 03:00:30 +0300128
129 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200130 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
131 profile_high_performance, ARRAY_SIZE(profile_high_performance));
132 profile_name = "performance";
Michael Bestas11016fa2018-03-26 03:00:30 +0300133
134 } else if (profile == PROFILE_BIAS_POWER) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200135 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power,
Michael Bestas11016fa2018-03-26 03:00:30 +0300136 ARRAY_SIZE(profile_bias_power));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200137 profile_name = "bias power";
Michael Bestas11016fa2018-03-26 03:00:30 +0300138
139 } else if (profile == PROFILE_BIAS_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200140 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
141 profile_bias_performance, ARRAY_SIZE(profile_bias_performance));
142 profile_name = "bias perf";
143 } else if (profile == PROFILE_BALANCED) {
144 ret = 0;
145 profile_name = "balanced";
Michael Bestas11016fa2018-03-26 03:00:30 +0300146 }
147
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200148 if (ret == 0) {
149 current_power_profile = profile;
150 ALOGD("%s: Set %s mode", __func__, profile_name);
151 }
152 return ret;
Michael Bestas11016fa2018-03-26 03:00:30 +0300153}
154
Michael Bestas47636f62018-05-25 21:30:28 +0300155static void process_video_encode_hint(void *metadata)
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800156{
157 char governor[80];
158 struct video_encode_metadata_t video_encode_metadata;
159
160 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
161 ALOGE("Can't obtain scaling governor.");
Michael Bestas47636f62018-05-25 21:30:28 +0300162 return;
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800163 }
164
Michael Bestas47636f62018-05-25 21:30:28 +0300165 if (!metadata) {
166 return;
167 }
168
169 /* Initialize encode metadata struct fields. */
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800170 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
171 video_encode_metadata.state = -1;
172 video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID;
173
Michael Bestas47636f62018-05-25 21:30:28 +0300174 if (parse_video_encode_metadata((char *)metadata,
175 &video_encode_metadata) == -1) {
176 ALOGE("Error occurred while parsing metadata.");
177 return;
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800178 }
179
180 if (video_encode_metadata.state == 1) {
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800181 if (is_interactive_governor(governor)) {
Dilip Gudlur6ecbcfb2014-12-20 15:30:18 -0800182 /* sched and cpufreq params
183 * hispeed freq - 768 MHz
184 * target load - 90
185 * above_hispeed_delay - 40ms
186 * sched_small_tsk - 50
187 */
Michael Bestas47636f62018-05-25 21:30:28 +0300188 int resource_values[] = {
189 0x2C07, 0x2F5A, 0x2704, 0x4032
190 };
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800191 perform_hint_action(video_encode_metadata.hint_id,
Zhao Wei Liewd4fe61e2016-06-26 11:37:59 +0800192 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800193 }
194 } else if (video_encode_metadata.state == 0) {
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800195 if (is_interactive_governor(governor)) {
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800196 undo_hint_action(video_encode_metadata.hint_id);
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800197 }
198 }
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800199}
200
Michael Bestas11016fa2018-03-26 03:00:30 +0300201static int resources_interaction_fling_boost[] = {
202 ALL_CPUS_PWR_CLPS_DIS,
203 SCHED_BOOST_ON,
204 SCHED_PREFER_IDLE_DIS
205};
206
207static int resources_interaction_boost[] = {
208 ALL_CPUS_PWR_CLPS_DIS,
209 SCHED_PREFER_IDLE_DIS
210};
211
212static int resources_launch[] = {
213 SCHED_BOOST_ON,
214 0x20C
215};
216
tomascus8b038b12019-02-19 17:15:58 +1100217static int process_activity_launch_hint(void *data)
218{
219 static int launch_handle = -1;
220 static int launch_mode = 0;
221
222 // release lock early if launch has finished
223 if (!data) {
224 if (CHECK_HANDLE(launch_handle)) {
225 release_request(launch_handle);
226 launch_handle = -1;
227 }
228 launch_mode = 0;
229 return HINT_HANDLED;
230 }
231
232 if (!launch_mode) {
233 launch_handle = interaction_with_handle(launch_handle, 5000,
234 ARRAY_SIZE(resources_launch), resources_launch);
235 if (!CHECK_HANDLE(launch_handle)) {
236 ALOGE("Failed to perform launch boost");
237 return HINT_NONE;
238 }
239 launch_mode = 1;
240 }
241 return HINT_HANDLED;
242}
243
Rashed Abdel-Tawaba2a70712017-12-31 01:21:40 +0200244int power_hint_override(power_hint_t hint, void *data)
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800245{
Michael Bestas11016fa2018-03-26 03:00:30 +0300246 static struct timespec s_previous_boost_timespec;
247 struct timespec cur_boost_timespec;
248 long long elapsed_time;
249 static int s_previous_duration = 0;
250 int duration;
251
252 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800253 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200254 ALOGE("Setting power profile failed. perfd not started?");
Michael Bestas11016fa2018-03-26 03:00:30 +0300255 return HINT_HANDLED;
256 }
257
258 // Skip other hints in high/low power modes
259 if (current_power_profile == PROFILE_POWER_SAVE ||
260 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
261 return HINT_HANDLED;
262 }
263
264 switch (hint) {
265 case POWER_HINT_INTERACTION:
Michael Bestas11016fa2018-03-26 03:00:30 +0300266 duration = 500; // 500ms by default
267 if (data) {
268 int input_duration = *((int*)data);
269 if (input_duration > duration) {
270 duration = (input_duration > 5000) ? 5000 : input_duration;
271 }
272 }
273
274 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
275
276 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
277 // don't hint if previous hint's duration covers this hint's duration
278 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
279 return HINT_HANDLED;
280 }
281 s_previous_boost_timespec = cur_boost_timespec;
282 s_previous_duration = duration;
283
284 if (duration >= 1500) {
285 interaction(duration, ARRAY_SIZE(resources_interaction_fling_boost),
286 resources_interaction_fling_boost);
287 } else {
288 interaction(duration, ARRAY_SIZE(resources_interaction_boost),
289 resources_interaction_boost);
290 }
291 return HINT_HANDLED;
Michael Bestas11016fa2018-03-26 03:00:30 +0300292 case POWER_HINT_LAUNCH:
tomascus8b038b12019-02-19 17:15:58 +1100293 return process_activity_launch_hint(data);
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800294 case POWER_HINT_VIDEO_ENCODE:
Michael Bestas47636f62018-05-25 21:30:28 +0300295 process_video_encode_hint(data);
296 return HINT_HANDLED;
Dilip Gudlurcfdc4662014-11-05 16:08:26 -0800297 default:
298 break;
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800299 }
Michael Bestas11016fa2018-03-26 03:00:30 +0300300 return HINT_NONE;
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800301}
302
Rashed Abdel-Tawaba2a70712017-12-31 01:21:40 +0200303int set_interactive_override(int on)
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800304{
305 char governor[80];
306
307 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
308 ALOGE("Can't obtain scaling governor.");
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800309 return HINT_NONE;
310 }
311
312 if (!on) {
Dilip Gudlurcfdc4662014-11-05 16:08:26 -0800313 /* Display off */
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800314 if (is_interactive_governor(governor)) {
Michael Bestas11016fa2018-03-26 03:00:30 +0300315 // sched upmigrate = 99, sched downmigrate = 95
316 // keep the big cores around, but make them very hard to use
Michael Bestas47636f62018-05-25 21:30:28 +0300317 int resource_values[] = {
318 0x4E63, 0x4F5F
319 };
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800320 perform_hint_action(DISPLAY_STATE_HINT_ID,
321 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlurcfdc4662014-11-05 16:08:26 -0800322 }
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800323 } else {
Dilip Gudlurcfdc4662014-11-05 16:08:26 -0800324 /* Display on */
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800325 if (is_interactive_governor(governor)) {
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800326 undo_hint_action(DISPLAY_STATE_HINT_ID);
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800327 }
328 }
Michael Bestas47636f62018-05-25 21:30:28 +0300329 return HINT_HANDLED;
Dilip Gudlurcf3644b2014-11-02 17:25:52 -0800330}