blob: fe003e30aeb9be2a30f56e5ecf46ecc9575fbb86 [file] [log] [blame]
vaibhav bhallaeef51ad2015-02-23 20:11:59 +05301/*
2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
dianlujitao46ac4af2018-03-25 18:08:45 +08003 * Copyright (C) 2018 The LineageOS Project
vaibhav bhallaeef51ad2015-02-23 20:11:59 +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>
Craig Tatloradf8a1c2018-05-21 17:22:31 +010034#include <time.h>
vaibhav bhallaeef51ad2015-02-23 20:11:59 +053035#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
Michael Bestas1add9ac2018-03-25 22:56:44 +030042#define LOG_TAG "QCOM PowerHAL"
Ethan Chena42f4a82018-03-01 21:31:15 -080043#include <log/log.h>
vaibhav bhallaeef51ad2015-02-23 20:11:59 +053044#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
53#define MIN_FREQ_CPU0_DISP_OFF 400000
54#define MIN_FREQ_CPU0_DISP_ON 960000
55
Corinna Vinschened3426d2018-04-10 11:57:37 +020056const char *scaling_min_freq[4] = {
57 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq",
58 "/sys/devices/system/cpu/cpu1/cpufreq/scaling_min_freq",
59 "/sys/devices/system/cpu/cpu2/cpufreq/scaling_min_freq",
60 "/sys/devices/system/cpu/cpu3/cpufreq/scaling_min_freq"
Nikhil Kumar Kansal1bd02892015-07-22 18:47:46 +053061};
62
Zhao Wei Liew6fe52522016-07-19 19:57:06 +080063/**
Zhao Wei Liew143f8072018-11-02 00:33:14 +000064 * Returns true if the target is MSM8916.
Zhao Wei Liew6fe52522016-07-19 19:57:06 +080065 */
66static bool is_target_8916(void)
vaibhav bhallaeef51ad2015-02-23 20:11:59 +053067{
Zhao Wei Liew143f8072018-11-02 00:33:14 +000068 static int is_8916 = -1;
Zhao Wei Liew6fe52522016-07-19 19:57:06 +080069 int soc_id;
vaibhav bhallaeef51ad2015-02-23 20:11:59 +053070
Zhao Wei Liew143f8072018-11-02 00:33:14 +000071 if (is_8916 >= 0)
72 return is_8916;
73
Zhao Wei Liew6fe52522016-07-19 19:57:06 +080074 soc_id = get_soc_id();
Zhao Wei Liew143f8072018-11-02 00:33:14 +000075 is_8916 = soc_id == 206 || (soc_id >= 247 && soc_id <= 250);
Zhao Wei Liew6fe52522016-07-19 19:57:06 +080076
77 return is_8916;
vaibhav bhallaeef51ad2015-02-23 20:11:59 +053078}
79
dianlujitao46ac4af2018-03-25 18:08:45 +080080static int current_power_profile = PROFILE_BALANCED;
81
82static int profile_high_performance_8916[3] = {
83 0x1C00, 0x0901, CPU0_MIN_FREQ_TURBO_MAX,
84};
85
86static int profile_high_performance_8939[11] = {
87 SCHED_BOOST_ON, 0x1C00, 0x0901,
88 CPU0_MIN_FREQ_TURBO_MAX, CPU1_MIN_FREQ_TURBO_MAX,
89 CPU2_MIN_FREQ_TURBO_MAX, CPU3_MIN_FREQ_TURBO_MAX,
90 CPU4_MIN_FREQ_TURBO_MAX, CPU5_MIN_FREQ_TURBO_MAX,
91 CPU6_MIN_FREQ_TURBO_MAX, CPU7_MIN_FREQ_TURBO_MAX,
92};
93
94static int profile_power_save_8916[1] = {
95 CPU0_MAX_FREQ_NONTURBO_MAX,
96};
97
98static int profile_power_save_8939[5] = {
99 CPUS_ONLINE_MAX_LIMIT_2,
100 CPU0_MAX_FREQ_NONTURBO_MAX, CPU1_MAX_FREQ_NONTURBO_MAX,
101 CPU2_MAX_FREQ_NONTURBO_MAX, CPU3_MAX_FREQ_NONTURBO_MAX,
102};
103
104#ifdef INTERACTION_BOOST
Michael Bestas47636f62018-05-25 21:30:28 +0300105int get_number_of_profiles()
106{
dianlujitao46ac4af2018-03-25 18:08:45 +0800107 return 3;
108}
109#endif
110
dianlujitao8780cb72019-02-23 20:24:57 +0800111static int set_power_profile(void *data)
Michael Bestas47636f62018-05-25 21:30:28 +0300112{
dianlujitao8780cb72019-02-23 20:24:57 +0800113 int profile = data ? *((int*)data) : 0;
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200114 int ret = -EINVAL;
115 const char *profile_name = NULL;
116
dianlujitao46ac4af2018-03-25 18:08:45 +0800117 if (profile == current_power_profile)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200118 return 0;
dianlujitao46ac4af2018-03-25 18:08:45 +0800119
120 ALOGV("%s: Profile=%d", __func__, profile);
121
122 if (current_power_profile != PROFILE_BALANCED) {
123 undo_hint_action(DEFAULT_PROFILE_HINT_ID);
124 ALOGV("%s: Hint undone", __func__);
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200125 current_power_profile = PROFILE_BALANCED;
dianlujitao46ac4af2018-03-25 18:08:45 +0800126 }
127
128 if (profile == PROFILE_POWER_SAVE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200129 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
130 is_target_8916() ? profile_power_save_8916 :
131 profile_power_save_8939,
132 is_target_8916() ? ARRAY_SIZE(profile_power_save_8916) :
133 ARRAY_SIZE(profile_power_save_8939));
134 profile_name = "powersave";
Michael Bestas47636f62018-05-25 21:30:28 +0300135
dianlujitao46ac4af2018-03-25 18:08:45 +0800136 } else if (profile == PROFILE_HIGH_PERFORMANCE) {
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200137 ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID,
138 is_target_8916() ? profile_high_performance_8916 :
139 profile_high_performance_8939,
140 is_target_8916() ? ARRAY_SIZE(profile_high_performance_8916) :
141 ARRAY_SIZE(profile_high_performance_8939));
142 profile_name = "performance";
143
144 } else if (profile == PROFILE_BALANCED) {
145 ret = 0;
146 profile_name = "balanced";
dianlujitao46ac4af2018-03-25 18:08:45 +0800147 }
148
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200149 if (ret == 0) {
150 current_power_profile = profile;
151 ALOGD("%s: Set %s mode", __func__, profile_name);
152 }
153 return ret;
dianlujitao46ac4af2018-03-25 18:08:45 +0800154}
155
156static int resources_interaction_fling_boost[] = {
157 ALL_CPUS_PWR_CLPS_DIS,
158 SCHED_BOOST_ON,
159 SCHED_PREFER_IDLE_DIS,
160 0x20D
161};
162
163static int resources_interaction_boost[] = {
164 ALL_CPUS_PWR_CLPS_DIS,
165 SCHED_PREFER_IDLE_DIS,
166 0x20D
167};
168
169static int resources_launch[] = {
170 ALL_CPUS_PWR_CLPS_DIS,
171 SCHED_BOOST_ON,
172 SCHED_PREFER_IDLE_DIS,
173 0x20F,
174 0x1C00,
175 0x4001,
176 0x4101,
177 0x4201
178};
179
tomascus8b038b12019-02-19 17:15:58 +1100180static int process_activity_launch_hint(void *data)
181{
182 static int launch_handle = -1;
183 static int launch_mode = 0;
184
185 // release lock early if launch has finished
186 if (!data) {
187 if (CHECK_HANDLE(launch_handle)) {
188 release_request(launch_handle);
189 launch_handle = -1;
190 }
191 launch_mode = 0;
192 return HINT_HANDLED;
193 }
194
195 if (!launch_mode) {
196 launch_handle = interaction_with_handle(launch_handle, 5000,
197 ARRAY_SIZE(resources_launch), resources_launch);
198 if (!CHECK_HANDLE(launch_handle)) {
199 ALOGE("Failed to perform launch boost");
200 return HINT_NONE;
201 }
202 launch_mode = 1;
203 }
204 return HINT_HANDLED;
205}
206
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800207int power_hint_override(power_hint_t hint, void *data)
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530208{
dianlujitao46ac4af2018-03-25 18:08:45 +0800209 static struct timespec s_previous_boost_timespec;
210 struct timespec cur_boost_timespec;
211 long long elapsed_time;
212 static int s_previous_duration = 0;
213 int duration;
214
215 if (hint == POWER_HINT_SET_PROFILE) {
dianlujitao8780cb72019-02-23 20:24:57 +0800216 if (set_power_profile(data) < 0)
Corinna Vinschen8599e3c2018-08-26 22:11:49 +0200217 ALOGE("Setting power profile failed. perfd not started?");
dianlujitao46ac4af2018-03-25 18:08:45 +0800218 return HINT_HANDLED;
219 }
220
221 // Skip other hints in high/low power modes
222 if (current_power_profile == PROFILE_POWER_SAVE ||
223 current_power_profile == PROFILE_HIGH_PERFORMANCE) {
224 return HINT_HANDLED;
225 }
226
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800227 switch (hint) {
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530228 case POWER_HINT_INTERACTION:
dianlujitao46ac4af2018-03-25 18:08:45 +0800229 duration = 500; // 500ms by default
230 if (data) {
231 int input_duration = *((int*)data);
232 if (input_duration > duration) {
233 duration = (input_duration > 5000) ? 5000 : input_duration;
234 }
235 }
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530236
dianlujitao46ac4af2018-03-25 18:08:45 +0800237 clock_gettime(CLOCK_MONOTONIC, &cur_boost_timespec);
238
239 elapsed_time = calc_timespan_us(s_previous_boost_timespec, cur_boost_timespec);
240 // don't hint if previous hint's duration covers this hint's duration
241 if ((s_previous_duration * 1000) > (elapsed_time + duration * 1000)) {
242 return HINT_HANDLED;
243 }
244 s_previous_boost_timespec = cur_boost_timespec;
245 s_previous_duration = duration;
246
247 if (duration >= 1500) {
248 interaction(duration, ARRAY_SIZE(resources_interaction_fling_boost),
249 resources_interaction_fling_boost);
250 } else {
251 interaction(duration, ARRAY_SIZE(resources_interaction_boost),
252 resources_interaction_boost);
253 }
254 return HINT_HANDLED;
dianlujitao46ac4af2018-03-25 18:08:45 +0800255 case POWER_HINT_LAUNCH:
tomascus8b038b12019-02-19 17:15:58 +1100256 return process_activity_launch_hint(data);
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800257 case POWER_HINT_VIDEO_ENCODE: /* Do nothing for encode case */
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530258 return HINT_HANDLED;
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800259 case POWER_HINT_VIDEO_DECODE: /* Do nothing for decode case */
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530260 return HINT_HANDLED;
Nikhil Kumar Kansal1bd02892015-07-22 18:47:46 +0530261 default:
dianlujitao46ac4af2018-03-25 18:08:45 +0800262 break;
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530263 }
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800264 return HINT_NONE;
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530265}
266
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800267int set_interactive_override(int on)
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530268{
269 char governor[80];
270 char tmp_str[NODE_MAX];
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530271
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800272 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) {
273 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) {
274 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) {
275 if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) {
Nikhil Kumar Kansal1bd02892015-07-22 18:47:46 +0530276 ALOGE("Can't obtain scaling governor.");
Michael Bestas47636f62018-05-25 21:30:28 +0300277 return HINT_NONE;
Nikhil Kumar Kansal1bd02892015-07-22 18:47:46 +0530278 }
279 }
280 }
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530281 }
282
283 if (!on) {
284 /* Display off. */
Zhao Wei Liew6fe52522016-07-19 19:57:06 +0800285 if (is_target_8916()) {
Ethan Chen4b4acdf2018-03-01 21:41:04 -0800286 if (is_interactive_governor(governor)) {
Michael Bestas47636f62018-05-25 21:30:28 +0300287 int resource_values[] = {
288 TR_MS_50, THREAD_MIGRATION_SYNC_OFF
289 };
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800290 perform_hint_action(DISPLAY_STATE_HINT_ID,
291 resource_values, ARRAY_SIZE(resource_values));
Michael Bestas47636f62018-05-25 21:30:28 +0300292 }
Zhao Wei Liew6fe52522016-07-19 19:57:06 +0800293 } else {
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800294 if (is_interactive_governor(governor)) {
Michael Bestas47636f62018-05-25 21:30:28 +0300295 int resource_values[] = {
296 TR_MS_CPU0_50, TR_MS_CPU4_50, THREAD_MIGRATION_SYNC_OFF
297 };
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530298
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800299 /* Set CPU0 MIN FREQ to 400Mhz avoid extra peak power
300 impact in volume key press */
301 snprintf(tmp_str, NODE_MAX, "%d", MIN_FREQ_CPU0_DISP_OFF);
302 if (sysfs_write(scaling_min_freq[0], tmp_str) != 0) {
303 if (sysfs_write(scaling_min_freq[1], tmp_str) != 0) {
304 if (sysfs_write(scaling_min_freq[2], tmp_str) != 0) {
305 if (sysfs_write(scaling_min_freq[3], tmp_str) != 0) {
306 ALOGE("Failed to write to %s", SCALING_MIN_FREQ);
307 }
308 }
309 }
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530310 }
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800311 perform_hint_action(DISPLAY_STATE_HINT_ID,
312 resource_values, ARRAY_SIZE(resource_values));
Michael Bestas47636f62018-05-25 21:30:28 +0300313 }
314 }
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530315 } else {
316 /* Display on. */
Zhao Wei Liew6fe52522016-07-19 19:57:06 +0800317 if (is_target_8916()) {
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800318 if (is_interactive_governor(governor)) {
319 undo_hint_action(DISPLAY_STATE_HINT_ID);
320 }
Zhao Wei Liew6fe52522016-07-19 19:57:06 +0800321 } else {
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800322 if (is_interactive_governor(governor)) {
dianlujitaoce3ca5c2018-03-25 17:37:31 +0800323 /* Recovering MIN_FREQ in display ON case */
324 snprintf(tmp_str, NODE_MAX, "%d", MIN_FREQ_CPU0_DISP_ON);
325 if (sysfs_write(scaling_min_freq[0], tmp_str) != 0) {
326 if (sysfs_write(scaling_min_freq[1], tmp_str) != 0) {
327 if (sysfs_write(scaling_min_freq[2], tmp_str) != 0) {
328 if (sysfs_write(scaling_min_freq[3], tmp_str) != 0) {
329 ALOGE("Failed to write to %s", SCALING_MIN_FREQ);
330 }
331 }
332 }
Nikhil Kumar Kansal1bd02892015-07-22 18:47:46 +0530333 }
Zhao Wei Liew5a81c9a2016-06-26 21:29:34 +0800334 undo_hint_action(DISPLAY_STATE_HINT_ID);
335 }
Michael Bestas47636f62018-05-25 21:30:28 +0300336 }
Zhao Wei Liew6fe52522016-07-19 19:57:06 +0800337 }
vaibhav bhallaeef51ad2015-02-23 20:11:59 +0530338 return HINT_HANDLED;
339}