1 /* 2 * Copyright (c) 2015, The Linux Foundation. All rights reserved. 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 #define LOG_NDEBUG 1 30 31 #include <errno.h> 32 #include <string.h> 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <fcntl.h> 36 #include <dlfcn.h> 37 #include <stdlib.h> 38 39 #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL) 40 #define LOG_TAG "QCOM PowerHAL" 41 #include <utils/Log.h> 42 #include <cutils/trace.h> 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 52 static int display_hint_sent; 53 int launch_handle = -1; 54 int launch_mode; 55 56 #ifdef EXTRA_POWERHAL_HINTS 57 static int process_cam_preview_hint(void *metadata) 58 { 59 char governor[80]; 60 struct cam_preview_metadata_t cam_preview_metadata; 61 62 if (get_scaling_governor(governor, sizeof(governor)) == -1) { 63 ALOGE("Can't obtain scaling governor."); 64 65 return HINT_NONE; 66 } 67 68 /* Initialize encode metadata struct fields */ 69 memset(&cam_preview_metadata, 0, sizeof(struct cam_preview_metadata_t)); 70 cam_preview_metadata.state = -1; 71 cam_preview_metadata.hint_id = CAM_PREVIEW_HINT_ID; 72 73 if (metadata) { 74 if (parse_cam_preview_metadata((char *)metadata, &cam_preview_metadata) == 75 -1) { 76 ALOGE("Error occurred while parsing metadata."); 77 return HINT_NONE; 78 } 79 } else { 80 return HINT_NONE; 81 } 82 83 if (cam_preview_metadata.state == 1) { 84 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && 85 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { 86 /* sched and cpufreq params 87 * above_hispeed_delay for LVT - 40ms 88 * go hispeed load for LVT - 95 89 * hispeed freq for LVT - 556 MHz 90 * target load for LVT - 90 91 * above hispeed delay for sLVT - 40ms 92 * go hispeed load for sLVT - 95 93 * hispeed freq for sLVT - 556 MHz 94 * target load for sLVT - 90 95 * bus DCVS set to V2 config: 96 * low power ceil mpbs - 2500 97 * low power io percent - 50 98 */ 99 int resource_values[] = {0x41400000, 0x4, 0x41410000, 0x5F, 0x41414000, 0x22C, 100 0x41420000, 0x5A, 0x41400100, 0x4, 0x41410100, 0x5F, 0x41414100, 0x22C, 101 0x41420100, 0x5A, 0x41810000, 0x9C4, 0x41814000, 0x32}; 102 103 perform_hint_action(cam_preview_metadata.hint_id, 104 resource_values, sizeof(resource_values)/sizeof(resource_values[0])); 105 ALOGI("Cam Preview hint start"); 106 return HINT_HANDLED; 107 } else if ((strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) && 108 (strlen(governor) == strlen(SCHED_GOVERNOR))) { 109 /* 110 * lower bus BW to save power 111 * 0x41810000: low power ceil mpbs = 2500 112 * 0x41814000: low power io percent = 50 113 */ 114 int resource_values[] = {0x41810000, 0x9C4, 0x41814000, 0x32}; 115 116 perform_hint_action( 117 cam_preview_metadata.hint_id, resource_values, 118 sizeof(resource_values) / sizeof(resource_values[0])); 119 ALOGI("Cam Preview hint start"); 120 return HINT_HANDLED; 121 } 122 } else if (cam_preview_metadata.state == 0) { 123 if (((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && 124 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) || 125 ((strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) && 126 (strlen(governor) == strlen(SCHED_GOVERNOR)))) { 127 undo_hint_action(cam_preview_metadata.hint_id); 128 ALOGI("Cam Preview hint stop"); 129 return HINT_HANDLED; 130 } 131 } 132 return HINT_NONE; 133 } 134 #endif 135 136 static int process_boost(int boost_handle, int duration) 137 { 138 char governor[80]; 139 int eas_launch_resources[] = {0x40804000, 0xFFF, 0x40804100, 0xFFF, 140 0x40800000, 0xFFF, 0x40800100, 0xFFF, 141 0x41800000, 140, 0x40400000, 0x1}; 142 int hmp_launch_resources[] = {0x40C00000, 0x1, 0x40804000, 0xFFF, 143 0x40804100, 0xFFF, 0x40800000, 0xFFF, 144 0x40800100, 0xFFF, 0x41800000, 140, 145 0x40400000, 0x1}; 146 int* launch_resources; 147 size_t launch_resources_size; 148 149 if (get_scaling_governor(governor, sizeof(governor)) == -1) { 150 ALOGE("Can't obtain scaling governor."); 151 return -1; 152 } 153 if (strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) { 154 launch_resources = eas_launch_resources; 155 launch_resources_size = sizeof(eas_launch_resources) / sizeof(eas_launch_resources[0]); 156 } else if (strncmp(governor, INTERACTIVE_GOVERNOR, 157 strlen(INTERACTIVE_GOVERNOR)) == 0) { /*HMP boost*/ 158 launch_resources = hmp_launch_resources; 159 launch_resources_size = sizeof(hmp_launch_resources) / sizeof(hmp_launch_resources[0]); 160 } else { 161 ALOGE("Unsupported governor."); 162 return -1; 163 } 164 boost_handle = interaction_with_handle( 165 boost_handle, duration, launch_resources_size, launch_resources); 166 return boost_handle; 167 } 168 169 static int process_video_encode_hint(void *metadata) 170 { 171 char governor[80]; 172 static int boost_handle = -1; 173 174 if (get_scaling_governor(governor, sizeof(governor)) == -1) { 175 ALOGE("Can't obtain scaling governor."); 176 177 return HINT_NONE; 178 } 179 180 if (metadata) { 181 int duration = 2000; // boosts 2s for starting encoding 182 boost_handle = process_boost(boost_handle, duration); 183 ALOGD("LAUNCH ENCODER-ON: %d MS", duration); 184 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && 185 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { 186 /* 1. cpufreq params 187 * -above_hispeed_delay for LVT - 40ms 188 * -go hispeed load for LVT - 95 189 * -hispeed freq for LVT - 556 MHz 190 * -target load for LVT - 90 191 * -above hispeed delay for sLVT - 40ms 192 * -go hispeed load for sLVT - 95 193 * -hispeed freq for sLVT - 806 MHz 194 * -target load for sLVT - 90 195 * 2. bus DCVS set to V2 config: 196 * -low power ceil mpbs - 2500 197 * -low power io percent - 50 198 * 3. hysteresis optimization 199 * -bus dcvs hysteresis tuning 200 * -sample_ms of 10 ms 201 * -disable ignore_hispeed_notif 202 * -sLVT hispeed freq to 806MHz 203 */ 204 int resource_values[] = { 205 0x41810000, 0x9C4, 0x41814000, 0x32, 0x4180C000, 0x0, 0x41820000, 0xA, 206 0x41438100, 0x1, 0x41438000, 0x1 }; 207 208 perform_hint_action(DEFAULT_VIDEO_ENCODE_HINT_ID, 209 resource_values, sizeof(resource_values)/sizeof(resource_values[0])); 210 ALOGD("Video Encode hint start"); 211 return HINT_HANDLED; 212 } else if ((strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) && 213 (strlen(governor) == strlen(SCHED_GOVERNOR))) { 214 215 /* 1. bus DCVS set to V2 config: 216 * 0x41810000: low power ceil mpbs - 2500 217 * 0x41814000: low power io percent - 50 218 * 2. hysteresis optimization 219 * 0x4180C000: bus dcvs hysteresis tuning 220 * 0x41820000: sample_ms of 10 ms 221 */ 222 int resource_values[] = {0x41810000, 0x9C4, 0x41814000, 0x32, 223 0x4180C000, 0x0, 0x41820000, 0xA}; 224 225 perform_hint_action(DEFAULT_VIDEO_ENCODE_HINT_ID, 226 resource_values, sizeof(resource_values)/sizeof(resource_values[0])); 227 ALOGD("Video Encode hint start"); 228 return HINT_HANDLED; 229 } 230 } else { 231 // boost handle is intentionally not released, release_request(boost_handle); 232 if (((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && 233 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) || 234 ((strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) && 235 (strlen(governor) == strlen(SCHED_GOVERNOR)))) { 236 undo_hint_action(DEFAULT_VIDEO_ENCODE_HINT_ID); 237 ALOGD("Video Encode hint stop"); 238 return HINT_HANDLED; 239 } 240 } 241 return HINT_NONE; 242 } 243 244 static int process_activity_launch_hint(void *data) 245 { 246 // boost will timeout in 5s 247 int duration = 5000; 248 ATRACE_BEGIN("launch"); 249 if (sustained_performance_mode || vr_mode) { 250 ATRACE_END(); 251 return HINT_HANDLED; 252 } 253 254 ALOGD("LAUNCH HINT: %s", data ? "ON" : "OFF"); 255 if (data && launch_mode == 0) { 256 launch_handle = process_boost(launch_handle, duration); 257 if (launch_handle > 0) { 258 launch_mode = 1; 259 ALOGD("Activity launch hint handled"); 260 ATRACE_INT("launch_lock", 1); 261 ATRACE_END(); 262 return HINT_HANDLED; 263 } else { 264 ATRACE_END(); 265 return HINT_NONE; 266 } 267 } else if (data == NULL && launch_mode == 1) { 268 release_request(launch_handle); 269 ATRACE_INT("launch_lock", 0); 270 launch_mode = 0; 271 ATRACE_END(); 272 return HINT_HANDLED; 273 } 274 ATRACE_END(); 275 return HINT_NONE; 276 } 277 278 int power_hint_override(power_hint_t hint, void *data) 279 { 280 int ret_val = HINT_NONE; 281 switch(hint) { 282 #ifdef EXTRA_POWERHAL_HINTS 283 case POWER_HINT_CAM_PREVIEW: 284 ret_val = process_cam_preview_hint(data); 285 break; 286 #endif 287 case POWER_HINT_VIDEO_ENCODE: 288 ret_val = process_video_encode_hint(data); 289 break; 290 case POWER_HINT_LAUNCH: 291 ret_val = process_activity_launch_hint(data); 292 break; 293 default: 294 break; 295 } 296 return ret_val; 297 } 298 299 int set_interactive_override(int on) 300 { 301 return HINT_HANDLED; /* Don't excecute this code path, not in use */ 302 char governor[80]; 303 304 if (get_scaling_governor(governor, sizeof(governor)) == -1) { 305 ALOGE("Can't obtain scaling governor."); 306 307 return HINT_NONE; 308 } 309 310 if (!on) { 311 /* Display off */ 312 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && 313 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { 314 int resource_values[] = {}; /* dummy node */ 315 if (!display_hint_sent) { 316 perform_hint_action(DISPLAY_STATE_HINT_ID, 317 resource_values, sizeof(resource_values)/sizeof(resource_values[0])); 318 display_hint_sent = 1; 319 ALOGV("Display Off hint start"); 320 return HINT_HANDLED; 321 } 322 } 323 } else { 324 /* Display on */ 325 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) && 326 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) { 327 undo_hint_action(DISPLAY_STATE_HINT_ID); 328 display_hint_sent = 0; 329 ALOGV("Display Off hint stop"); 330 return HINT_HANDLED; 331 } 332 } 333 return HINT_NONE; 334 } 335