David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 1 | /* |
Deevana Murthy Bandaru | 48bb283 | 2018-08-20 14:48:35 +0530 | [diff] [blame] | 2 | * Copyright (c) 2016, 2018, The Linux Foundation. All rights reserved. |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 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 | |
| 30 | #define LOG_NIDEBUG 0 |
| 31 | |
| 32 | #include <errno.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <dlfcn.h> |
| 38 | #include <stdlib.h> |
Naveen Kumar | a0a3719 | 2018-07-03 08:34:46 +0530 | [diff] [blame] | 39 | #include <pthread.h> |
| 40 | #include <unistd.h> |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 41 | |
| 42 | #define LOG_TAG "QTI PowerHAL" |
| 43 | #include <utils/Log.h> |
| 44 | #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 | |
Deevana Murthy Bandaru | 48bb283 | 2018-08-20 14:48:35 +0530 | [diff] [blame] | 53 | #define MIN_VAL(X,Y) ((X>Y)?(Y):(X)) |
| 54 | |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 55 | static int saved_interactive_mode = -1; |
| 56 | static int display_hint_sent; |
| 57 | static int video_encode_hint_sent; |
| 58 | static int cam_preview_hint_sent; |
| 59 | |
| 60 | pthread_mutex_t camera_hint_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 61 | static int camera_hint_ref_count; |
| 62 | static void process_video_encode_hint(void *metadata); |
| 63 | //static void process_cam_preview_hint(void *metadata); |
| 64 | |
vhunac | ba74c44 | 2018-02-16 12:24:32 +0530 | [diff] [blame] | 65 | static int display_fd; |
| 66 | #define SYS_DISPLAY_PWR "/sys/kernel/hbtp/display_pwr" |
| 67 | |
Deevana Murthy Bandaru | 48bb283 | 2018-08-20 14:48:35 +0530 | [diff] [blame] | 68 | static bool is_target_SDM632() /* Returns value=632 if target is SDM632 else value 0 */ |
| 69 | { |
| 70 | int fd; |
| 71 | bool is_target_SDM632 = false; |
| 72 | char buf[10] = {0}; |
| 73 | fd = open("/sys/devices/soc0/soc_id", O_RDONLY); |
| 74 | if (fd >= 0) { |
| 75 | if (read(fd, buf, sizeof(buf) - 1) == -1) { |
| 76 | ALOGW("Unable to read soc_id"); |
| 77 | is_target_SDM632 = false; |
| 78 | } else { |
| 79 | int soc_id = atoi(buf); |
| 80 | if (soc_id == 349 || soc_id == 350) { |
| 81 | is_target_SDM632 = true; /* Above SOCID for SDM632 */ |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | close(fd); |
| 86 | return is_target_SDM632; |
| 87 | } |
| 88 | |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 89 | int power_hint_override(struct power_module *module, power_hint_t hint, |
| 90 | void *data) |
| 91 | { |
| 92 | |
| 93 | switch(hint) { |
| 94 | case POWER_HINT_VSYNC: |
| 95 | break; |
| 96 | case POWER_HINT_VIDEO_ENCODE: |
| 97 | { |
| 98 | process_video_encode_hint(data); |
| 99 | return HINT_HANDLED; |
| 100 | } |
| 101 | } |
| 102 | return HINT_NONE; |
| 103 | } |
| 104 | |
| 105 | int set_interactive_override(struct power_module *module, int on) |
| 106 | { |
| 107 | char governor[80]; |
| 108 | char tmp_str[NODE_MAX]; |
| 109 | struct video_encode_metadata_t video_encode_metadata; |
vhunac | ba74c44 | 2018-02-16 12:24:32 +0530 | [diff] [blame] | 110 | int rc = 0; |
| 111 | |
| 112 | static const char *display_on = "1"; |
| 113 | static const char *display_off = "0"; |
| 114 | char err_buf[80]; |
| 115 | static int init_interactive_hint = 0; |
| 116 | static int set_i_count = 0; |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 117 | |
| 118 | ALOGI("Got set_interactive hint"); |
| 119 | |
| 120 | if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU0) == -1) { |
| 121 | if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU1) == -1) { |
| 122 | if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU2) == -1) { |
| 123 | if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU3) == -1) { |
| 124 | ALOGE("Can't obtain scaling governor."); |
| 125 | return HINT_HANDLED; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (!on) { |
| 132 | /* Display off. */ |
| 133 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 134 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 135 | /* timer rate - 40mS*/ |
| 136 | int resource_values[] = {0x41424000, 0x28, |
| 137 | }; |
| 138 | if (!display_hint_sent) { |
| 139 | perform_hint_action(DISPLAY_STATE_HINT_ID, |
| 140 | resource_values, sizeof(resource_values)/sizeof(resource_values[0])); |
| 141 | display_hint_sent = 1; |
| 142 | } |
| 143 | } /* Perf time rate set for CORE0,CORE4 8952 target*/ |
| 144 | |
| 145 | } else { |
| 146 | /* Display on. */ |
| 147 | if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 148 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 149 | |
| 150 | undo_hint_action(DISPLAY_STATE_HINT_ID); |
| 151 | display_hint_sent = 0; |
| 152 | } |
| 153 | } |
| 154 | saved_interactive_mode = !!on; |
vhunac | ba74c44 | 2018-02-16 12:24:32 +0530 | [diff] [blame] | 155 | |
| 156 | set_i_count ++; |
| 157 | ALOGI("Got set_interactive hint on= %d, count= %d\n", on, set_i_count); |
| 158 | |
| 159 | if (init_interactive_hint == 0) |
| 160 | { |
| 161 | //First time the display is turned off |
| 162 | display_fd = TEMP_FAILURE_RETRY(open(SYS_DISPLAY_PWR, O_RDWR)); |
| 163 | if (display_fd < 0) { |
| 164 | strerror_r(errno,err_buf,sizeof(err_buf)); |
| 165 | ALOGE("Error opening %s: %s\n", SYS_DISPLAY_PWR, err_buf); |
| 166 | return HINT_HANDLED; |
| 167 | } |
| 168 | else |
| 169 | init_interactive_hint = 1; |
| 170 | } |
| 171 | else |
| 172 | if (!on ) { |
| 173 | /* Display off. */ |
| 174 | rc = TEMP_FAILURE_RETRY(write(display_fd, display_off, strlen(display_off))); |
| 175 | if (rc < 0) { |
| 176 | strerror_r(errno,err_buf,sizeof(err_buf)); |
| 177 | ALOGE("Error writing %s to %s: %s\n", display_off, SYS_DISPLAY_PWR, err_buf); |
| 178 | } |
| 179 | } |
| 180 | else { |
| 181 | /* Display on */ |
| 182 | rc = TEMP_FAILURE_RETRY(write(display_fd, display_on, strlen(display_on))); |
| 183 | if (rc < 0) { |
| 184 | strerror_r(errno,err_buf,sizeof(err_buf)); |
| 185 | ALOGE("Error writing %s to %s: %s\n", display_on, SYS_DISPLAY_PWR, err_buf); |
| 186 | } |
| 187 | } |
| 188 | |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 189 | return HINT_HANDLED; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /* Video Encode Hint */ |
| 194 | static void process_video_encode_hint(void *metadata) |
| 195 | { |
Deevana Murthy Bandaru | 48bb283 | 2018-08-20 14:48:35 +0530 | [diff] [blame] | 196 | char governor[80] = {0}; |
| 197 | int resource_values[20] = {0}; |
| 198 | int num_resources = 0; |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 199 | struct video_encode_metadata_t video_encode_metadata; |
| 200 | |
| 201 | ALOGI("Got process_video_encode_hint"); |
| 202 | |
| 203 | if (get_scaling_governor_check_cores(governor, |
| 204 | sizeof(governor),CPU0) == -1) { |
| 205 | if (get_scaling_governor_check_cores(governor, |
| 206 | sizeof(governor),CPU1) == -1) { |
| 207 | if (get_scaling_governor_check_cores(governor, |
| 208 | sizeof(governor),CPU2) == -1) { |
| 209 | if (get_scaling_governor_check_cores(governor, |
| 210 | sizeof(governor),CPU3) == -1) { |
| 211 | ALOGE("Can't obtain scaling governor."); |
| 212 | // return HINT_HANDLED; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* Initialize encode metadata struct fields. */ |
| 219 | memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t)); |
| 220 | video_encode_metadata.state = -1; |
| 221 | video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID; |
| 222 | |
| 223 | if (metadata) { |
| 224 | if (parse_video_encode_metadata((char *)metadata, |
| 225 | &video_encode_metadata) == -1) { |
| 226 | ALOGE("Error occurred while parsing metadata."); |
| 227 | return; |
| 228 | } |
| 229 | } else { |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | if (video_encode_metadata.state == 1) { |
Deevana Murthy Bandaru | 48bb283 | 2018-08-20 14:48:35 +0530 | [diff] [blame] | 234 | if((strncmp(governor, SCHEDUTIL_GOVERNOR, |
| 235 | strlen(SCHEDUTIL_GOVERNOR)) == 0) && |
| 236 | (strlen(governor) == strlen(SCHEDUTIL_GOVERNOR))) { |
| 237 | if(is_target_SDM632()) { |
| 238 | /* sample_ms = 10mS |
| 239 | * SLB for Core0 = -6 |
| 240 | * SLB for Core1 = -6 |
| 241 | * SLB for Core2 = -6 |
| 242 | * SLB for Core3 = -6 |
| 243 | * hispeed load = 95 |
| 244 | * hispeed freq = 1036 */ |
| 245 | int res[] = {0x41820000, 0xa, |
| 246 | 0x40c68100, 0xfffffffa, |
| 247 | 0x40c68110, 0xfffffffa, |
| 248 | 0x40c68120, 0xfffffffa, |
| 249 | 0x40c68130, 0xfffffffa, |
| 250 | 0x41440100, 0x5f, |
| 251 | 0x4143c100, 0x40c, |
| 252 | }; |
| 253 | memcpy(resource_values, res, MIN_VAL(sizeof(resource_values), sizeof(res))); |
| 254 | num_resources = sizeof(res)/sizeof(res[0]); |
| 255 | pthread_mutex_lock(&camera_hint_mutex); |
| 256 | camera_hint_ref_count++; |
| 257 | if (camera_hint_ref_count == 1) { |
| 258 | if (!video_encode_hint_sent) { |
| 259 | perform_hint_action(video_encode_metadata.hint_id, |
| 260 | resource_values, num_resources); |
| 261 | video_encode_hint_sent = 1; |
| 262 | } |
| 263 | } |
| 264 | pthread_mutex_unlock(&camera_hint_mutex); |
| 265 | } |
| 266 | else { |
| 267 | /* sample_ms = 10mS */ |
| 268 | int res[] = {0x41820000, 0xa, |
| 269 | }; |
| 270 | memcpy(resource_values, res, MIN_VAL(sizeof(resource_values), sizeof(res))); |
| 271 | num_resources = sizeof(res)/sizeof(res[0]); |
| 272 | pthread_mutex_lock(&camera_hint_mutex); |
| 273 | camera_hint_ref_count++; |
| 274 | if (camera_hint_ref_count == 1) { |
| 275 | if (!video_encode_hint_sent) { |
| 276 | perform_hint_action(video_encode_metadata.hint_id, |
| 277 | resource_values, num_resources); |
| 278 | video_encode_hint_sent = 1; |
| 279 | } |
| 280 | } |
| 281 | pthread_mutex_unlock(&camera_hint_mutex); |
| 282 | } |
| 283 | } |
| 284 | else if ((strncmp(governor, INTERACTIVE_GOVERNOR, |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 285 | strlen(INTERACTIVE_GOVERNOR)) == 0) && |
| 286 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { |
| 287 | /* Sched_load and migration_notification disable |
Deevana Murthy Bandaru | 48bb283 | 2018-08-20 14:48:35 +0530 | [diff] [blame] | 288 | * timer rate - 40mS*/ |
| 289 | int res[] = {0x41430000, 0x1, |
| 290 | 0x41434000, 0x1, |
| 291 | 0x41424000, 0x28, |
| 292 | }; |
| 293 | memcpy(resource_values, res, MIN_VAL(sizeof(resource_values), sizeof(res))); |
| 294 | num_resources = sizeof(res)/sizeof(res[0]); |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 295 | pthread_mutex_lock(&camera_hint_mutex); |
| 296 | camera_hint_ref_count++; |
| 297 | if (camera_hint_ref_count == 1) { |
| 298 | if (!video_encode_hint_sent) { |
| 299 | perform_hint_action(video_encode_metadata.hint_id, |
Deevana Murthy Bandaru | 48bb283 | 2018-08-20 14:48:35 +0530 | [diff] [blame] | 300 | resource_values, num_resources); |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 301 | video_encode_hint_sent = 1; |
| 302 | } |
| 303 | } |
| 304 | pthread_mutex_unlock(&camera_hint_mutex); |
| 305 | } |
| 306 | } else if (video_encode_metadata.state == 0) { |
Deevana Murthy Bandaru | 48bb283 | 2018-08-20 14:48:35 +0530 | [diff] [blame] | 307 | if (((strncmp(governor, INTERACTIVE_GOVERNOR, |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 308 | strlen(INTERACTIVE_GOVERNOR)) == 0) && |
Deevana Murthy Bandaru | 48bb283 | 2018-08-20 14:48:35 +0530 | [diff] [blame] | 309 | (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) || |
| 310 | ((strncmp(governor, SCHEDUTIL_GOVERNOR, |
| 311 | strlen(SCHEDUTIL_GOVERNOR)) == 0) && |
| 312 | (strlen(governor) == strlen(SCHEDUTIL_GOVERNOR)))) { |
David Ng | ee7c4c5 | 2018-03-22 23:49:12 -0700 | [diff] [blame] | 313 | pthread_mutex_lock(&camera_hint_mutex); |
| 314 | camera_hint_ref_count--; |
| 315 | if (!camera_hint_ref_count) { |
| 316 | undo_hint_action(video_encode_metadata.hint_id); |
| 317 | video_encode_hint_sent = 0; |
| 318 | } |
| 319 | pthread_mutex_unlock(&camera_hint_mutex); |
| 320 | return ; |
| 321 | } |
| 322 | } |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | |