blob: e4f6a6c11fcd1aacd78854ce60a4ed3d5c217d80 [file] [log] [blame]
Vince Leung699c1bc2013-08-14 17:40:36 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
Michael Bestasce25d842018-03-26 02:13:51 +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 */
Craig Tatloradf8a1c2018-05-21 17:22:31 +010030
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 Bestasce25d842018-03-26 02:13:51 +030053static int current_power_profile = PROFILE_BALANCED;
54
55static int profile_high_performance[] = {
56 CPUS_ONLINE_MIN_2,
57 CPU0_MIN_FREQ_TURBO_MAX,
58 CPU1_MIN_FREQ_TURBO_MAX
59};
60
61static int profile_power_save[] = {
62 CPUS_ONLINE_MAX_LIMIT_2,
63 CPU0_MAX_FREQ_NONTURBO_MAX,
64 CPU1_MAX_FREQ_NONTURBO_MAX
65};
66
67#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +030068int get_number_of_profiles()
69{
Michael Bestasce25d842018-03-26 02:13:51 +030070 return 3;
71}
72#endif
73
dianlujitao8780cb72019-02-23 20:24:57 +080074static int set_power_profile(void *data)
Michael Bestas47636f62018-05-25 21:30:28 +030075{
dianlujitao8780cb72019-02-23 20:24:57 +080076 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020077 int ret = -EINVAL;
78 const char *profile_name = NULL;
79
Michael Bestasce25d842018-03-26 02:13:51 +030080 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020081 return 0;
Michael Bestasce25d842018-03-26 02:13:51 +030082
83 ALOGV("%s: Profile=%d", __func__, profile);
84
85 if (current_power_profile != PROFILE_BALANCED) {
86 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
87 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020088 current_power_profile = PROFILE_BALANCED;
Michael Bestasce25d842018-03-26 02:13:51 +030089 }
90
91 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020092 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save,
Michael Bestasce25d842018-03-26 02:13:51 +030093 ARRAY_SIZE(profile_power_save));
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020094 profile_name = "powersave";
Michael Bestasce25d842018-03-26 02:13:51 +030095
96 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +020097 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
98 profile_high_performance, ARRAY_SIZE(profile_high_performance));
99 profile_name = "performance";
100
101 } else if (profile == PROFILE_BALANCED) {
102 ret = 0;
103 profile_name = "balanced";
Michael Bestasce25d842018-03-26 02:13:51 +0300104 }
105
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200106 if (ret == 0) {
107 current_power_profile = profile;
108 ALOGD("%s: Set %s mode", __func__, profile_name);
109 }
110 return ret;
Michael Bestasce25d842018-03-26 02:13:51 +0300111}
112
113static int resources_interaction_boost[] = {
114 CPUS_ONLINE_MIN_2,
115 0x20B,
116 0x30B
117};
118
Rashed Abdel-Tawaba2a70712017-12-31 01:21:40 +0200119int power_hint_override(power_hint_t hint, void *data)
Vince Leung699c1bc2013-08-14 17:40:36 -0700120{
Michael Bestasce25d842018-03-26 02:13:51 +0300121 static struct timespec s_previous_boost_timespec;
122 struct timespec cur_boost_timespec;
123 long long elapsed_time;
124 static int s_previous_duration = 0;
125 int duration;
126
127 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800128 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200129 ALOGE("Setting power profile failed. mpdecision not started?");
Michael Bestasce25d842018-03-26 02:13:51 +0300130 return HINT_HANDLED;
131 }
132
133 // Skip other hints in high/low power modes
134 if (current_power_profile == PROFILE_POWER_SAVE ||
135 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
136 return HINT_HANDLED;
137 }
138
139 switch (hint) {
Vince Leung699c1bc2013-08-14 17:40:36 -0700140 case POWER_HINT_INTERACTION:
Michael Bestasce25d842018-03-26 02:13:51 +0300141 duration = 500; // 500ms by default
142 if (data) {
143 int input_duration = *((int*)data);
144 if (input_duration > duration) {
145 duration = (input_duration > 5000) ? 5000 : input_duration;
146 }
147 }
Vince Leung699c1bc2013-08-14 17:40:36 -0700148
Michael Bestasce25d842018-03-26 02:13:51 +0300149 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
150
151 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
152 // don't hint if previous hint's duration covers this hint's duration
153 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
154 return HINT_HANDLED;
155 }
156 s_previous_boost_timespec = cur_boost_timespec;
157 s_previous_duration = duration;
158
159 interaction(duration, ARRAY_SIZE(resources_interaction_boost),
160 resources_interaction_boost);
Vince Leung699c1bc2013-08-14 17:40:36 -0700161 return HINT_HANDLED;
Michael Bestasce25d842018-03-26 02:13:51 +0300162 default:
163 break;
Vince Leung699c1bc2013-08-14 17:40:36 -0700164 }
165 return HINT_NONE;
166}