blob: 1edb2fd1519d2c7556b6b1bac30a0084ea28751d [file] [log] [blame]
Dilip Gudlurc798dcf2015-07-01 14:13:43 -07001/*
2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29#define LOG_NIDEBUG 0
30
31#include <errno.h>
32#include <string.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <dlfcn.h>
37#include <stdlib.h>
38
39#define LOG_TAG "QCOM PowerHAL"
40#include <utils/Log.h>
41#include <hardware/hardware.h>
42#include <hardware/power.h>
43
44#include "utils.h"
45#include "metadata-defs.h"
46#include "hint-data.h"
47#include "performance.h"
48#include "power-common.h"
49
50static int display_hint_sent;
Zhao Wei Liewe4c91272016-06-21 10:44:49 +080051static int current_power_profile = PROFILE_BALANCED;
52
53extern void interaction(int duration, int num_args, int opt_list[]);
54
55int get_number_of_profiles() {
56 return 5;
57}
58
59static int profile_high_performance[] = {
60 SCHED_BOOST_ON_V3, 0x1,
61 ALL_CPUS_PWR_CLPS_DIS_V3, 0x1,
62 CPUS_ONLINE_MIN_BIG, 0x2,
63 CPUS_ONLINE_MIN_LITTLE, 0x2,
64 MIN_FREQ_BIG_CORE_0, 0xFFF,
65 MIN_FREQ_LITTLE_CORE_0, 0xFFF,
66};
67
68static int profile_power_save[] = {
69 CPUS_ONLINE_MAX_LIMIT_BIG, 0x1,
70 MAX_FREQ_BIG_CORE_0, 0x3E8,
71 MAX_FREQ_LITTLE_CORE_0, 0x3E8,
72};
73
74static int profile_bias_power[] = {
75 MAX_FREQ_BIG_CORE_0, 0x514,
76 MAX_FREQ_LITTLE_CORE_0, 0x3E8,
77};
78
79static int profile_bias_performance[] = {
80 CPUS_ONLINE_MAX_LIMIT_BIG, 0x2,
81 CPUS_ONLINE_MAX_LIMIT_LITTLE, 0x2,
82 MIN_FREQ_BIG_CORE_0, 0x578,
83};
84
85static void set_power_profile(int profile) {
86
87 if (profile == current_power_profile)
88 return;
89
90 ALOGV("%s: Profile=%d", __func__, profile);
91
92 if (current_power_profile != PROFILE_BALANCED) {
93 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
94 ALOGV("%s: Hint undone", __func__);
95 }
96
97 if (profile == PROFILE_POWER_SAVE) {
Ricardo Cerqueira3ac88f02016-07-16 02:15:14 +010098 perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
99 ARRAY_SIZE(profile_power_save));
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800100 ALOGD("%s: Set powersave mode", __func__);
101
102 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Ricardo Cerqueira3ac88f02016-07-16 02:15:14 +0100103 perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_high_performance,
104 ARRAY_SIZE(profile_high_performance));
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800105 ALOGD("%s: Set performance mode", __func__);
106
107 } else if (profile == PROFILE_BIAS_POWER) {
Ricardo Cerqueira3ac88f02016-07-16 02:15:14 +0100108 perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power,
109 ARRAY_SIZE(profile_bias_power));
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800110 ALOGD("%s: Set bias power mode", __func__);
111
112 } else if (profile == PROFILE_BIAS_PERFORMANCE) {
Ricardo Cerqueira3ac88f02016-07-16 02:15:14 +0100113 perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_performance,
114 ARRAY_SIZE(profile_bias_performance));
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800115 ALOGD("%s: Set bias perf mode", __func__);
116
117 }
118
119 current_power_profile = profile;
120}
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700121
Dilip Gudlur032f1252015-03-02 16:30:54 -0800122static int process_cam_preview_hint(void *metadata)
123{
124 char governor[80];
125 struct cam_preview_metadata_t cam_preview_metadata;
126
127 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
128 ALOGE("Can't obtain scaling governor.");
129
130 return HINT_NONE;
131 }
132
133 /* Initialize encode metadata struct fields */
134 memset(&cam_preview_metadata, 0, sizeof(struct cam_preview_metadata_t));
135 cam_preview_metadata.state = -1;
136 cam_preview_metadata.hint_id = CAM_PREVIEW_HINT_ID;
137
138 if (metadata) {
139 if (parse_cam_preview_metadata((char *)metadata, &cam_preview_metadata) ==
140 -1) {
141 ALOGE("Error occurred while parsing metadata.");
142 return HINT_NONE;
143 }
144 } else {
145 return HINT_NONE;
146 }
147
148 if (cam_preview_metadata.state == 1) {
149 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
150 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
151 /* sched and cpufreq params
152 * above_hispeed_delay for LVT - 40ms
153 * go hispeed load for LVT - 95
Dilip Gudlur5436ef02015-11-04 17:34:37 -0800154 * hispeed freq for LVT - 556 MHz
Dilip Gudlur032f1252015-03-02 16:30:54 -0800155 * target load for LVT - 90
156 * above hispeed delay for sLVT - 40ms
157 * go hispeed load for sLVT - 95
Dilip Gudlur5436ef02015-11-04 17:34:37 -0800158 * hispeed freq for sLVT - 556 MHz
Dilip Gudlur032f1252015-03-02 16:30:54 -0800159 * target load for sLVT - 90
Dilip Gudlur5436ef02015-11-04 17:34:37 -0800160 * bus DCVS set to V2 config:
161 * low power ceil mpbs - 2500
162 * low power io percent - 50
Dilip Gudlur032f1252015-03-02 16:30:54 -0800163 */
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800164 int resource_values[] = {
165 ABOVE_HISPEED_DELAY_BIG, 0x4,
166 GO_HISPEED_LOAD_BIG, 0x5F,
167 HISPEED_FREQ_BIG, 0x22C,
168 TARGET_LOADS_BIG, 0x5A,
169 ABOVE_HISPEED_DELAY_LITTLE, 0x4,
170 GO_HISPEED_LOAD_LITTLE, 0x5F,
171 HISPEED_FREQ_LITTLE, 0x22C,
172 TARGET_LOADS_LITTLE, 0x5A,
173 LOW_POWER_CEIL_MBPS, 0x9C4,
174 LOW_POWER_IO_PERCENT, 0x32,
175 };
Dilip Gudlur032f1252015-03-02 16:30:54 -0800176
177 perform_hint_action(cam_preview_metadata.hint_id,
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800178 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlur032f1252015-03-02 16:30:54 -0800179 ALOGI("Cam Preview hint start");
180 return HINT_HANDLED;
181 }
182 } else if (cam_preview_metadata.state == 0) {
183 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
184 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
185 undo_hint_action(cam_preview_metadata.hint_id);
186 ALOGI("Cam Preview hint stop");
187 return HINT_HANDLED;
188 }
189 }
190 return HINT_NONE;
191}
192
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700193static int process_video_encode_hint(void *metadata)
194{
195 char governor[80];
196 struct video_encode_metadata_t video_encode_metadata;
197
198 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
199 ALOGE("Can't obtain scaling governor.");
200
201 return HINT_NONE;
202 }
203
204 /* Initialize encode metadata struct fields */
205 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
206 video_encode_metadata.state = -1;
207 video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID;
208
209 if (metadata) {
210 if (parse_video_encode_metadata((char *)metadata, &video_encode_metadata) ==
211 -1) {
212 ALOGE("Error occurred while parsing metadata.");
213 return HINT_NONE;
214 }
215 } else {
216 return HINT_NONE;
217 }
218
219 if (video_encode_metadata.state == 1) {
220 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
221 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
Dilip Gudlur41b58e42015-12-17 19:08:50 -0800222 /* 1. cpufreq params
223 * -above_hispeed_delay for LVT - 40ms
224 * -go hispeed load for LVT - 95
225 * -hispeed freq for LVT - 556 MHz
226 * -target load for LVT - 90
227 * -above hispeed delay for sLVT - 40ms
228 * -go hispeed load for sLVT - 95
229 * -hispeed freq for sLVT - 806 MHz
230 * -target load for sLVT - 90
231 * 2. bus DCVS set to V2 config:
232 * -low power ceil mpbs - 2500
233 * -low power io percent - 50
234 * 3. hysteresis optimization
235 * -bus dcvs hysteresis tuning
236 * -sample_ms of 10 ms
237 * -disable ignore_hispeed_notif
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700238 */
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800239 int resource_values[] = {
240 ABOVE_HISPEED_DELAY_BIG, 0x4,
241 GO_HISPEED_LOAD_BIG, 0x5F,
242 HISPEED_FREQ_BIG, 0x326,
243 TARGET_LOADS_BIG, 0x5A,
244 ABOVE_HISPEED_DELAY_LITTLE, 0x4,
245 GO_HISPEED_LOAD_LITTLE, 0x5F,
246 HISPEED_FREQ_LITTLE, 0x22C,
247 TARGET_LOADS_LITTLE, 0x5A,
248 LOW_POWER_CEIL_MBPS, 0x9C4,
249 LOW_POWER_IO_PERCENT, 0x32,
250 CPUBW_HWMON_V1, 0x0,
251 CPUBW_HWMON_SAMPLE_MS, 0xA,
252 IGNORE_HISPEED_NOTIF_LITTLE, 0x0,
253 IGNORE_HISPEED_NOTIF_BIG, 0x0,
254 };
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700255
256 perform_hint_action(video_encode_metadata.hint_id,
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800257 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlur032f1252015-03-02 16:30:54 -0800258 ALOGI("Video Encode hint start");
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700259 return HINT_HANDLED;
260 }
261 } else if (video_encode_metadata.state == 0) {
262 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
263 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
264 undo_hint_action(video_encode_metadata.hint_id);
Dilip Gudlur032f1252015-03-02 16:30:54 -0800265 ALOGI("Video Encode hint stop");
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700266 return HINT_HANDLED;
267 }
268 }
269 return HINT_NONE;
270}
271
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800272int power_hint_override(__unused struct power_module *module,
273 power_hint_t hint, void *data)
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700274{
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800275 struct timeval cur_boost_timeval = {0, 0};
276 static unsigned long long previous_boost_time = 0;
277 unsigned long long cur_boost_time;
278 double elapsed_time;
279 int duration;
280
281 int resources_launch_boost[] = {
282 SCHED_BOOST_ON_V3, 0x1,
283 MAX_FREQ_BIG_CORE_0, 0xFFF,
284 MAX_FREQ_LITTLE_CORE_0, 0xFFF,
285 MIN_FREQ_BIG_CORE_0, 0xFFF,
286 MIN_FREQ_LITTLE_CORE_0, 0xFFF,
287 CPUBW_HWMON_MIN_FREQ, 0x8C,
288 ALL_CPUS_PWR_CLPS_DIS_V3, 0x1,
289 STOR_CLK_SCALE_DIS, 0x1,
290 };
291
292 int resources_cpu_boost[] = {
293 SCHED_BOOST_ON_V3, 0x1,
294 MIN_FREQ_BIG_CORE_0, 0x3E8,
295 };
296
297 int resources_interaction_fling_boost[] = {
298 CPUBW_HWMON_MIN_FREQ, 0x33,
299 MIN_FREQ_BIG_CORE_0, 0x3E8,
300 MIN_FREQ_LITTLE_CORE_0, 0x3E8,
301 SCHED_BOOST_ON_V3, 0x1,
Steve Kondik25d81f32016-07-04 16:28:05 -0700302 // SCHED_GROUP_ON, 0x1,
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800303 };
304
305 int resources_interaction_boost[] = {
306 MIN_FREQ_BIG_CORE_0, 0x3E8,
307 };
308
309 if (hint == POWER_HINT_SET_PROFILE) {
310 set_power_profile(*(int32_t *)data);
311 return HINT_HANDLED;
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700312 }
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800313
314 /* Skip other hints in power save mode */
315 if (current_power_profile == PROFILE_POWER_SAVE)
316 return HINT_HANDLED;
317
318 if (hint == POWER_HINT_INTERACTION) {
319 duration = data ? *((int *)data) : 500;
320
321 gettimeofday(&cur_boost_timeval, NULL);
322 cur_boost_time = cur_boost_timeval.tv_sec * 1000000 + cur_boost_timeval.tv_usec;
323 elapsed_time = (double) (cur_boost_time - previous_boost_time);
324 if (elapsed_time > 750000)
325 elapsed_time = 750000;
326 /**
327 * Don't hint if it's been less than 250ms since last boost
328 * also detect if we're doing anything resembling a fling
329 * support additional boosting in case of flings
330 */
331 else if (elapsed_time < 250000 && duration <= 750)
332 return HINT_HANDLED;
333
334 previous_boost_time = cur_boost_time;
335
336 if (duration >= 1500) {
337 interaction(duration, ARRAY_SIZE(resources_interaction_fling_boost),
338 resources_interaction_fling_boost);
339 } else {
340 interaction(duration, ARRAY_SIZE(resources_interaction_boost),
341 resources_interaction_boost);
342 }
343 return HINT_HANDLED;
344 }
345
346 if (hint == POWER_HINT_LAUNCH_BOOST) {
347 launch_boost_info_t *info = (launch_boost_info_t *)data;
348 if (info == NULL) {
349 ALOGE("Invalid argument for launch boost");
350 return HINT_HANDLED;
351 }
352
353 duration = 2000;
354
355 ALOGV("LAUNCH_BOOST: %s (pid=%d)", info->packageName, info->pid);
356
357 start_prefetch(info->pid, info->packageName);
358 interaction(duration, ARRAY_SIZE(resources_launch_boost),
359 resources_launch_boost);
360 return HINT_HANDLED;
361 }
362
363 if (hint == POWER_HINT_CPU_BOOST) {
364 duration = *(int32_t *)data / 1000;
365 if (duration > 0) {
366 interaction(duration, ARRAY_SIZE(resources_cpu_boost),
367 resources_cpu_boost);
368 return HINT_HANDLED;
369 }
370 }
371
372 if (hint == POWER_HINT_CAM_PREVIEW)
373 return process_cam_preview_hint(data);
374
375 if (hint == POWER_HINT_VIDEO_ENCODE)
376 return process_video_encode_hint(data);
377
378 return HINT_NONE;
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700379}
380
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800381int set_interactive_override(__unused struct power_module *module, int on)
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700382{
383 return HINT_HANDLED; /* Don't excecute this code path, not in use */
384 char governor[80];
385
386 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
387 ALOGE("Can't obtain scaling governor.");
388
389 return HINT_NONE;
390 }
391
392 if (!on) {
393 /* Display off */
394 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
395 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
396 int resource_values[] = {}; /* dummy node */
397 if (!display_hint_sent) {
398 perform_hint_action(DISPLAY_STATE_HINT_ID,
Zhao Wei Liewe4c91272016-06-21 10:44:49 +0800399 resource_values, ARRAY_SIZE(resource_values));
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700400 display_hint_sent = 1;
Dilip Gudlur032f1252015-03-02 16:30:54 -0800401 ALOGI("Display Off hint start");
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700402 return HINT_HANDLED;
403 }
404 }
405 } else {
406 /* Display on */
407 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
408 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
409 undo_hint_action(DISPLAY_STATE_HINT_ID);
410 display_hint_sent = 0;
Dilip Gudlur032f1252015-03-02 16:30:54 -0800411 ALOGI("Display Off hint stop");
Dilip Gudlurc798dcf2015-07-01 14:13:43 -0700412 return HINT_HANDLED;
413 }
414 }
415 return HINT_NONE;
416}