Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
Michael Bestas | abe7e65 | 2018-03-26 04:10:16 +0300 | [diff] [blame] | 3 | * Copyright (C) 2018 The LineageOS Project |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 4 | * |
| 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> |
| 34 | #include <string.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <dlfcn.h> |
| 39 | #include <stdlib.h> |
| 40 | |
Michael Bestas | 1add9ac | 2018-03-25 22:56:44 +0300 | [diff] [blame] | 41 | #define LOG_TAG "QCOM PowerHAL" |
Ethan Chen | daff0dd | 2018-03-01 21:31:15 -0800 | [diff] [blame] | 42 | #include <log/log.h> |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 43 | #include <hardware/hardware.h> |
| 44 | #include <hardware/power.h> |
| 45 | |
| 46 | #include "utils.h" |
| 47 | #include "metadata-defs.h" |
| 48 | #include "hint-data.h" |
| 49 | #include "performance.h" |
| 50 | #include "power-common.h" |
| 51 | |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 52 | static int video_encode_hint_sent; |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 53 | |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 54 | static int current_power_profile = PROFILE_BALANCED; |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 55 | |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 56 | static int profile_high_performance[] = { |
| 57 | CPUS_ONLINE_MIN_BIG, 0x4, |
| 58 | CPUS_ONLINE_MIN_LITTLE, 0x4, |
| 59 | CPU0_MIN_FREQ_TURBO_MAX, |
| 60 | CPU4_MIN_FREQ_TURBO_MAX, |
| 61 | }; |
| 62 | |
| 63 | static int profile_power_save[] = { |
dianlujitao | 5eb472d | 2018-09-06 21:43:14 +0800 | [diff] [blame] | 64 | CPUS_ONLINE_MAX_BIG, 0x0, |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 65 | CPU0_MAX_FREQ_NONTURBO_MAX, |
| 66 | CPU4_MAX_FREQ_NONTURBO_MAX, |
| 67 | }; |
| 68 | |
| 69 | static int profile_bias_power[] = { |
| 70 | CPU0_MAX_FREQ_NONTURBO_MAX, |
| 71 | CPU4_MAX_FREQ_NONTURBO_MAX, |
| 72 | }; |
| 73 | |
| 74 | static int profile_bias_performance[] = { |
| 75 | CPU0_MIN_FREQ_NONTURBO_MAX + 1, |
| 76 | CPU4_MIN_FREQ_NONTURBO_MAX + 1, |
| 77 | }; |
| 78 | |
dianlujitao | 4004811 | 2018-03-02 12:40:04 +0800 | [diff] [blame] | 79 | #ifdef INTERACTION_BOOST |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 80 | int get_number_of_profiles() |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 81 | { |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 82 | return 5; |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 83 | } |
dianlujitao | 4004811 | 2018-03-02 12:40:04 +0800 | [diff] [blame] | 84 | #endif |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 85 | |
dianlujitao | 8780cb7 | 2019-02-23 20:24:57 +0800 | [diff] [blame] | 86 | static int set_power_profile(void *data) |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 87 | { |
dianlujitao | 8780cb7 | 2019-02-23 20:24:57 +0800 | [diff] [blame] | 88 | int profile = data ? *((int*)data) : 0; |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 89 | int ret = -EINVAL; |
| 90 | const char *profile_name = NULL; |
| 91 | |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 92 | if (profile == current_power_profile) |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 93 | return 0; |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 94 | |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 95 | ALOGV("%s: Profile=%d", __func__, profile); |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 96 | |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 97 | if (current_power_profile != PROFILE_BALANCED) { |
| 98 | undo_hint_action(DEFAULT_PROFILE_HINT_ID); |
| 99 | ALOGV("%s: Hint undone", __func__); |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 100 | current_power_profile = PROFILE_BALANCED; |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 103 | if (profile == PROFILE_POWER_SAVE) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 104 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_power_save, |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 105 | ARRAY_SIZE(profile_power_save)); |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 106 | profile_name = "powersave"; |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 107 | |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 108 | } else if (profile == PROFILE_HIGH_PERFORMANCE) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 109 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
| 110 | profile_high_performance, ARRAY_SIZE(profile_high_performance)); |
| 111 | profile_name = "performance"; |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 112 | |
| 113 | } else if (profile == PROFILE_BIAS_POWER) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 114 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, profile_bias_power, |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 115 | ARRAY_SIZE(profile_bias_power)); |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 116 | profile_name = "bias power"; |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 117 | |
| 118 | } else if (profile == PROFILE_BIAS_PERFORMANCE) { |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 119 | ret = perform_hint_action(DEFAULT_PROFILE_HINT_ID, |
| 120 | profile_bias_performance, ARRAY_SIZE(profile_bias_performance)); |
| 121 | profile_name = "bias perf"; |
| 122 | } else if (profile == PROFILE_BALANCED) { |
| 123 | ret = 0; |
| 124 | profile_name = "balanced"; |
Ethan Chen | 22dd9f7 | 2018-03-01 21:40:04 -0800 | [diff] [blame] | 125 | } |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 126 | |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 127 | if (ret == 0) { |
| 128 | current_power_profile = profile; |
| 129 | ALOGD("%s: Set %s mode", __func__, profile_name); |
| 130 | } |
| 131 | return ret; |
Nikhil Kumar Kansal | b40c136 | 2015-05-05 16:10:43 +0530 | [diff] [blame] | 132 | } |
vanajau | 3353dc5 | 2016-09-19 14:32:32 +0530 | [diff] [blame] | 133 | |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 134 | static void process_video_encode_hint(void *metadata) |
| 135 | { |
| 136 | char governor[80]; |
| 137 | struct video_encode_metadata_t video_encode_metadata; |
| 138 | |
Michael Bestas | abe7e65 | 2018-03-26 04:10:16 +0300 | [diff] [blame] | 139 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) { |
| 140 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) { |
| 141 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) { |
| 142 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) { |
Ethan Chen | 22dd9f7 | 2018-03-01 21:40:04 -0800 | [diff] [blame] | 143 | ALOGE("Can't obtain scaling governor."); |
| 144 | return; |
| 145 | } |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 146 | } |
Ethan Chen | 22dd9f7 | 2018-03-01 21:40:04 -0800 | [diff] [blame] | 147 | } |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 148 | } |
| 149 | |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 150 | if (!metadata) { |
| 151 | return; |
| 152 | } |
| 153 | |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 154 | /* Initialize encode metadata struct fields. */ |
| 155 | memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t)); |
| 156 | video_encode_metadata.state = -1; |
| 157 | video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID; |
| 158 | |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 159 | if (parse_video_encode_metadata((char *)metadata, |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 160 | &video_encode_metadata) == -1) { |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 161 | ALOGE("Error occurred while parsing metadata."); |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
| 165 | if (video_encode_metadata.state == 1) { |
Ethan Chen | 70b1f44 | 2018-03-01 21:41:04 -0800 | [diff] [blame] | 166 | if (is_interactive_governor(governor)) { |
Ethan Chen | 22dd9f7 | 2018-03-01 21:40:04 -0800 | [diff] [blame] | 167 | int resource_values[] = { |
| 168 | INT_OP_CLUSTER0_USE_SCHED_LOAD, 0x1, |
| 169 | INT_OP_CLUSTER1_USE_SCHED_LOAD, 0x1, |
| 170 | INT_OP_CLUSTER0_USE_MIGRATION_NOTIF, 0x1, |
| 171 | INT_OP_CLUSTER1_USE_MIGRATION_NOTIF, 0x1, |
| 172 | INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_40, |
| 173 | INT_OP_CLUSTER1_TIMER_RATE, BIG_LITTLE_TR_MS_40 |
| 174 | }; |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 175 | if (!video_encode_hint_sent) { |
| 176 | perform_hint_action(video_encode_metadata.hint_id, |
Ethan Chen | 22dd9f7 | 2018-03-01 21:40:04 -0800 | [diff] [blame] | 177 | resource_values, ARRAY_SIZE(resource_values)); |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 178 | video_encode_hint_sent = 1; |
| 179 | } |
| 180 | } |
| 181 | } else if (video_encode_metadata.state == 0) { |
Ethan Chen | 70b1f44 | 2018-03-01 21:41:04 -0800 | [diff] [blame] | 182 | if (is_interactive_governor(governor)) { |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 183 | undo_hint_action(video_encode_metadata.hint_id); |
| 184 | video_encode_hint_sent = 0; |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 185 | } |
| 186 | } |
Nikhil Kumar Kansal | 813ac90 | 2015-06-24 16:55:16 +0530 | [diff] [blame] | 187 | } |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 188 | |
| 189 | int power_hint_override(power_hint_t hint, void *data) |
| 190 | { |
| 191 | if (hint == POWER_HINT_SET_PROFILE) { |
dianlujitao | 8780cb7 | 2019-02-23 20:24:57 +0800 | [diff] [blame] | 192 | if (set_power_profile(data) < 0) |
Corinna Vinschen | 8599e3c | 2018-08-26 22:11:49 +0200 | [diff] [blame] | 193 | ALOGE("Setting power profile failed. perfd not started?"); |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 194 | return HINT_HANDLED; |
| 195 | } |
| 196 | |
| 197 | // Skip other hints in high/low power modes |
| 198 | if (current_power_profile == PROFILE_POWER_SAVE || |
| 199 | current_power_profile == PROFILE_HIGH_PERFORMANCE) { |
| 200 | return HINT_HANDLED; |
| 201 | } |
| 202 | |
| 203 | switch (hint) { |
| 204 | case POWER_HINT_VIDEO_ENCODE: |
| 205 | process_video_encode_hint(data); |
| 206 | return HINT_HANDLED; |
| 207 | default: |
| 208 | break; |
| 209 | } |
| 210 | return HINT_NONE; |
| 211 | } |
| 212 | |
| 213 | int set_interactive_override(int on) |
| 214 | { |
| 215 | char governor[80]; |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 216 | |
Michael Bestas | abe7e65 | 2018-03-26 04:10:16 +0300 | [diff] [blame] | 217 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU0) == -1) { |
| 218 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU1) == -1) { |
| 219 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU2) == -1) { |
| 220 | if (get_scaling_governor_check_cores(governor, sizeof(governor), CPU3) == -1) { |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 221 | ALOGE("Can't obtain scaling governor."); |
| 222 | return HINT_NONE; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (!on) { |
| 229 | /* Display off. */ |
| 230 | if (is_interactive_governor(governor)) { |
| 231 | int resource_values[] = { |
| 232 | INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_50, |
| 233 | INT_OP_CLUSTER1_TIMER_RATE, BIG_LITTLE_TR_MS_50, |
| 234 | INT_OP_NOTIFY_ON_MIGRATE, 0x00 |
| 235 | }; |
Michael Bestas | 47636f6 | 2018-05-25 21:30:28 +0300 | [diff] [blame] | 236 | perform_hint_action(DISPLAY_STATE_HINT_ID, |
| 237 | resource_values, ARRAY_SIZE(resource_values)); |
| 238 | } |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 239 | } else { |
| 240 | /* Display on. */ |
| 241 | if (is_interactive_governor(governor)) { |
| 242 | undo_hint_action(DISPLAY_STATE_HINT_ID); |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
Ethan Chen | a36e212 | 2018-02-25 20:46:37 -0800 | [diff] [blame] | 245 | return HINT_HANDLED; |
| 246 | } |