blob: 8a0e0e7bb19759f152c0eb6c310e40fbca880a83 [file] [log] [blame]
Vince Leung699c1bc2013-08-14 17:40:36 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
Michael Bestasdbc67812018-03-26 02:07:03 +03003 * Copyright (C) 2018 The LineageOS Project
Vince Leung699c1bc2013-08-14 17:40:36 -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
Vince Leung699c1bc2013-08-14 17:40:36 -070031#define LOG_NIDEBUG 0
32
33#include <errno.h>
Craig Tatloradf8a1c2018-05-21 17:22:31 +010034#include <time.h>
Vince Leung699c1bc2013-08-14 17:40:36 -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"
Ethan Chena42f4a82018-03-01 21:31:15 -080043#include <log/log.h>
Vince Leung699c1bc2013-08-14 17:40:36 -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 Bestasdbc67812018-03-26 02:07:03 +030053static int current_power_profile = PROFILE_BALANCED;
54
55static int profile_high_performance[] = {
56 CPUS_ONLINE_MIN_4,
57 CPU0_MIN_FREQ_TURBO_MAX,
58 CPU1_MIN_FREQ_TURBO_MAX,
59 CPU2_MIN_FREQ_TURBO_MAX,
60 CPU3_MIN_FREQ_TURBO_MAX
61};
62
63static int profile_power_save[] = {
64 CPUS_ONLINE_MAX_LIMIT_2,
65 CPU0_MAX_FREQ_NONTURBO_MAX,
66 CPU1_MAX_FREQ_NONTURBO_MAX,
67 CPU2_MAX_FREQ_NONTURBO_MAX,
68 CPU3_MAX_FREQ_NONTURBO_MAX
69};
70
71#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +030072int get_number_of_profiles()
73{
Michael Bestasdbc67812018-03-26 02:07:03 +030074 return 3;
75}
76#endif
77
dianlujitao8780cb72019-02-23 20:24:57 +080078static int set_power_profile(void *data)
Michael Bestas47636f62018-05-25 21:30:28 +030079{
dianlujitao8780cb72019-02-23 20:24:57 +080080 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020081 int ret = -EINVAL;
82 const char *profile_name = NULL;
83
Michael Bestasdbc67812018-03-26 02:07:03 +030084 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020085 return 0;
Michael Bestasdbc67812018-03-26 02:07:03 +030086
87 ALOGV("%s: Profile=%d", __func__, profile);
88
89 if (current_power_profile != PROFILE_BALANCED) {
90 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
91 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020092 current_power_profile = PROFILE_BALANCED;
Michael Bestasdbc67812018-03-26 02:07:03 +030093 }
94
95 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020096 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
Michael Bestasdbc67812018-03-26 02:07:03 +030097 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020098 profile_name = "powersave";
Michael Bestasdbc67812018-03-26 02:07:03 +030099
100 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200101 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
102 profile_high_performance, ARRAY_SIZE(profile_high_performance));
103 profile_name = "performance";
104
105 } else if (profile == PROFILE_BALANCED) {
106 ret = 0;
107 profile_name = "balanced";
Michael Bestasdbc67812018-03-26 02:07:03 +0300108 }
109
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200110 if (ret == 0) {
111 current_power_profile = profile;
112 ALOGD("%s: Set %s mode", __func__, profile_name);
113 }
114 return ret;
Michael Bestasdbc67812018-03-26 02:07:03 +0300115}
116
117static int resources_interaction_boost[] = {
118 CPUS_ONLINE_MIN_2,
119 0x20B,
120 0x30B
121};
122
Rashed Abdel-Tawaba2a70712017-12-31 01:21:40 +0200123int power_hint_override(power_hint_t hint, void *data)
Vince Leung699c1bc2013-08-14 17:40:36 -0700124{
Michael Bestasdbc67812018-03-26 02:07:03 +0300125 static struct timespec s_previous_boost_timespec;
126 struct timespec cur_boost_timespec;
127 long long elapsed_time;
128 static int s_previous_duration = 0;
129 int duration;
130
131 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800132 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200133 ALOGE("Setting power profile failed. mpdecision not started?");
Michael Bestasdbc67812018-03-26 02:07:03 +0300134 return HINT_HANDLED;
135 }
136
137 // Skip other hints in high/low power modes
138 if (current_power_profile == PROFILE_POWER_SAVE ||
139 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
140 return HINT_HANDLED;
141 }
142
143 switch (hint) {
Vince Leung699c1bc2013-08-14 17:40:36 -0700144 case POWER_HINT_INTERACTION:
Michael Bestasdbc67812018-03-26 02:07:03 +0300145 duration = 500; // 500ms by default
146 if (data) {
147 int input_duration = *((int*)data);
148 if (input_duration > duration) {
149 duration = (input_duration > 5000) ? 5000 : input_duration;
150 }
151 }
Vince Leung699c1bc2013-08-14 17:40:36 -0700152
Michael Bestasdbc67812018-03-26 02:07:03 +0300153 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
154
155 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
156 // don't hint if previous hint's duration covers this hint's duration
157 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
158 return HINT_HANDLED;
159 }
160 s_previous_boost_timespec = cur_boost_timespec;
161 s_previous_duration = duration;
162
163 interaction(duration, ARRAY_SIZE(resources_interaction_boost),
164 resources_interaction_boost);
Vince Leung699c1bc2013-08-14 17:40:36 -0700165 return HINT_HANDLED;
Michael Bestasdbc67812018-03-26 02:07:03 +0300166 default:
167 break;
Vince Leung699c1bc2013-08-14 17:40:36 -0700168 }
169 return HINT_NONE;
170}