Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 1 | /* |
Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 2 | * Copyright (c) 2012-2013,2015-2017, The Linux Foundation. All rights reserved. |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -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. |
Duy Truong | 7022245 | 2013-02-10 06:35:11 -0800 | [diff] [blame] | 13 | * * Neither the name of The Linux Foundation nor the names of its |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 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 | */ |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 29 | #define LOG_NIDEBUG 0 |
| 30 | |
| 31 | #include <dlfcn.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <errno.h> |
Ajay Dudani | 5e67680 | 2015-04-07 23:56:52 -0700 | [diff] [blame] | 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
Rashed Abdel-Tawab | 9c14c9f | 2017-12-26 23:11:03 +0200 | [diff] [blame] | 36 | #include <unistd.h> |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 37 | |
| 38 | #include "utils.h" |
| 39 | #include "list.h" |
| 40 | #include "hint-data.h" |
Anurag Singh | 057806b | 2013-04-16 16:53:45 -0700 | [diff] [blame] | 41 | #include "power-common.h" |
Rashed Abdel-Tawab | 00b2185 | 2017-11-03 12:44:16 -0700 | [diff] [blame] | 42 | #include "power-helper.h" |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 43 | |
| 44 | #define LOG_TAG "QCOM PowerHAL" |
Rashed Abdel-Tawab | 9c14c9f | 2017-12-26 23:11:03 +0200 | [diff] [blame] | 45 | #include <log/log.h> |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 46 | |
Wei Wang | 5584ebb | 2016-06-07 18:44:31 -0700 | [diff] [blame] | 47 | #define USINSEC 1000000L |
| 48 | #define NSINUS 1000L |
| 49 | |
Sravan Kumar Ambapuram | 5838747 | 2015-05-20 22:22:54 +0530 | [diff] [blame] | 50 | char scaling_gov_path[4][80] ={ |
| 51 | "sys/devices/system/cpu/cpu0/cpufreq/scaling_governor", |
| 52 | "sys/devices/system/cpu/cpu1/cpufreq/scaling_governor", |
| 53 | "sys/devices/system/cpu/cpu2/cpufreq/scaling_governor", |
| 54 | "sys/devices/system/cpu/cpu3/cpufreq/scaling_governor" |
| 55 | }; |
| 56 | |
Ananth Raghavan Subramanian | 0c226b7 | 2017-06-27 21:28:43 -0700 | [diff] [blame] | 57 | #define PERF_HAL_PATH "libqti-perfd-client.so" |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 58 | static void *qcopt_handle; |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 59 | static int (*perf_lock_acq)(unsigned long handle, int duration, |
| 60 | int list[], int numArgs); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 61 | static int (*perf_lock_rel)(unsigned long handle); |
Ananth Raghavan Subramanian | 0c226b7 | 2017-06-27 21:28:43 -0700 | [diff] [blame] | 62 | static int (*perf_hint)(int, char *, int, int); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 63 | static struct list_node active_hint_list_head; |
| 64 | |
| 65 | static void *get_qcopt_handle() |
| 66 | { |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 67 | void *handle = NULL; |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 68 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 69 | dlerror(); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 70 | |
Ananth Raghavan Subramanian | 0c226b7 | 2017-06-27 21:28:43 -0700 | [diff] [blame] | 71 | handle = dlopen(PERF_HAL_PATH, RTLD_NOW); |
| 72 | if (!handle) { |
| 73 | ALOGE("Unable to open %s: %s\n", PERF_HAL_PATH, |
| 74 | dlerror()); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 77 | return handle; |
| 78 | } |
| 79 | |
| 80 | static void __attribute__ ((constructor)) initialize(void) |
| 81 | { |
| 82 | qcopt_handle = get_qcopt_handle(); |
| 83 | |
| 84 | if (!qcopt_handle) { |
| 85 | ALOGE("Failed to get qcopt handle.\n"); |
| 86 | } else { |
| 87 | /* |
| 88 | * qc-opt handle obtained. Get the perflock acquire/release |
| 89 | * function pointers. |
| 90 | */ |
| 91 | perf_lock_acq = dlsym(qcopt_handle, "perf_lock_acq"); |
| 92 | |
| 93 | if (!perf_lock_acq) { |
| 94 | ALOGE("Unable to get perf_lock_acq function handle.\n"); |
| 95 | } |
| 96 | |
| 97 | perf_lock_rel = dlsym(qcopt_handle, "perf_lock_rel"); |
| 98 | |
| 99 | if (!perf_lock_rel) { |
| 100 | ALOGE("Unable to get perf_lock_rel function handle.\n"); |
| 101 | } |
Ananth Raghavan Subramanian | 0c226b7 | 2017-06-27 21:28:43 -0700 | [diff] [blame] | 102 | |
| 103 | perf_hint = dlsym(qcopt_handle, "perf_hint"); |
| 104 | |
| 105 | if (!perf_hint) { |
| 106 | ALOGE("Unable to get perf_hint function handle.\n"); |
| 107 | } |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | static void __attribute__ ((destructor)) cleanup(void) |
| 112 | { |
| 113 | if (qcopt_handle) { |
| 114 | if (dlclose(qcopt_handle)) |
| 115 | ALOGE("Error occurred while closing qc-opt library."); |
| 116 | } |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | int sysfs_read(char *path, char *s, int num_bytes) |
| 120 | { |
| 121 | char buf[80]; |
| 122 | int count; |
| 123 | int ret = 0; |
| 124 | int fd = open(path, O_RDONLY); |
| 125 | |
| 126 | if (fd < 0) { |
| 127 | strerror_r(errno, buf, sizeof(buf)); |
| 128 | ALOGE("Error opening %s: %s\n", path, buf); |
| 129 | |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | if ((count = read(fd, s, num_bytes - 1)) < 0) { |
| 134 | strerror_r(errno, buf, sizeof(buf)); |
| 135 | ALOGE("Error writing to %s: %s\n", path, buf); |
| 136 | |
| 137 | ret = -1; |
| 138 | } else { |
| 139 | s[count] = '\0'; |
| 140 | } |
| 141 | |
| 142 | close(fd); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | int sysfs_write(char *path, char *s) |
| 148 | { |
| 149 | char buf[80]; |
| 150 | int len; |
| 151 | int ret = 0; |
| 152 | int fd = open(path, O_WRONLY); |
| 153 | |
| 154 | if (fd < 0) { |
| 155 | strerror_r(errno, buf, sizeof(buf)); |
| 156 | ALOGE("Error opening %s: %s\n", path, buf); |
| 157 | return -1 ; |
| 158 | } |
| 159 | |
| 160 | len = write(fd, s, strlen(s)); |
| 161 | if (len < 0) { |
| 162 | strerror_r(errno, buf, sizeof(buf)); |
| 163 | ALOGE("Error writing to %s: %s\n", path, buf); |
| 164 | |
| 165 | ret = -1; |
| 166 | } |
| 167 | |
| 168 | close(fd); |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | int get_scaling_governor(char governor[], int size) |
| 174 | { |
| 175 | if (sysfs_read(SCALING_GOVERNOR_PATH, governor, |
| 176 | size) == -1) { |
| 177 | // Can't obtain the scaling governor. Return. |
| 178 | return -1; |
| 179 | } else { |
| 180 | // Strip newline at the end. |
| 181 | int len = strlen(governor); |
| 182 | |
| 183 | len--; |
| 184 | |
| 185 | while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r')) |
| 186 | governor[len--] = '\0'; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Sravan Kumar Ambapuram | 5838747 | 2015-05-20 22:22:54 +0530 | [diff] [blame] | 192 | int get_scaling_governor_check_cores(char governor[], int size,int core_num) |
| 193 | { |
| 194 | |
| 195 | if (sysfs_read(scaling_gov_path[core_num], governor, |
| 196 | size) == -1) { |
| 197 | // Can't obtain the scaling governor. Return. |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | // Strip newline at the end. |
| 202 | int len = strlen(governor); |
| 203 | len--; |
| 204 | while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r')) |
| 205 | governor[len--] = '\0'; |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 210 | int is_interactive_governor(char* governor) { |
| 211 | if (strncmp(governor, INTERACTIVE_GOVERNOR, (strlen(INTERACTIVE_GOVERNOR)+1)) == 0) |
| 212 | return 1; |
| 213 | return 0; |
| 214 | } |
| 215 | |
Paul Keith | d524aea | 2017-12-31 02:17:32 +0100 | [diff] [blame] | 216 | #ifndef INTERACTION_BOOST |
| 217 | void interaction(int UNUSED(duration), int UNUSED(num_args), int UNUSED(opt_list[])) |
| 218 | { |
| 219 | #else |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 220 | void interaction(int duration, int num_args, int opt_list[]) |
Vince Leung | 6857466 | 2013-07-16 16:47:00 -0700 | [diff] [blame] | 221 | { |
Vince Leung | 6857466 | 2013-07-16 16:47:00 -0700 | [diff] [blame] | 222 | static int lock_handle = 0; |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 223 | |
Paul Keith | d524aea | 2017-12-31 02:17:32 +0100 | [diff] [blame] | 224 | if (duration < 0 || num_args < 1 || opt_list[0] == 0) |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 225 | return; |
| 226 | |
Vince Leung | 6857466 | 2013-07-16 16:47:00 -0700 | [diff] [blame] | 227 | if (qcopt_handle) { |
| 228 | if (perf_lock_acq) { |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 229 | lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args); |
Vince Leung | 6857466 | 2013-07-16 16:47:00 -0700 | [diff] [blame] | 230 | if (lock_handle == -1) |
| 231 | ALOGE("Failed to acquire lock."); |
| 232 | } |
| 233 | } |
| 234 | #endif |
| 235 | } |
| 236 | |
Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 237 | int interaction_with_handle(int lock_handle, int duration, int num_args, int opt_list[]) |
| 238 | { |
Paul Keith | d524aea | 2017-12-31 02:17:32 +0100 | [diff] [blame] | 239 | if (duration < 0 || num_args < 1 || opt_list[0] == 0) |
Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 240 | return 0; |
| 241 | |
| 242 | if (qcopt_handle) { |
| 243 | if (perf_lock_acq) { |
| 244 | lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args); |
| 245 | if (lock_handle == -1) |
| 246 | ALOGE("Failed to acquire lock."); |
| 247 | } |
| 248 | } |
| 249 | return lock_handle; |
| 250 | } |
| 251 | |
Ananth Raghavan Subramanian | 0c226b7 | 2017-06-27 21:28:43 -0700 | [diff] [blame] | 252 | //this is interaction_with_handle using perf_hint instead of |
| 253 | //perf_lock_acq |
| 254 | int perf_hint_enable(int hint_id , int duration) |
| 255 | { |
| 256 | int lock_handle = 0; |
| 257 | |
| 258 | if (duration < 0) |
| 259 | return 0; |
| 260 | |
| 261 | if (qcopt_handle) { |
| 262 | if (perf_hint) { |
| 263 | lock_handle = perf_hint(hint_id, NULL, duration, -1); |
| 264 | if (lock_handle == -1) |
| 265 | ALOGE("Failed to acquire lock."); |
| 266 | } |
| 267 | } |
| 268 | return lock_handle; |
| 269 | } |
| 270 | |
| 271 | |
Ananth Raghavan Subramanian | e432dbf | 2017-03-24 16:30:12 -0700 | [diff] [blame] | 272 | void release_request(int lock_handle) { |
| 273 | if (qcopt_handle && perf_lock_rel) |
| 274 | perf_lock_rel(lock_handle); |
| 275 | } |
| 276 | |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 277 | void perform_hint_action(int hint_id, int resource_values[], int num_resources) |
| 278 | { |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 279 | if (qcopt_handle) { |
| 280 | if (perf_lock_acq) { |
| 281 | /* Acquire an indefinite lock for the requested resources. */ |
| 282 | int lock_handle = perf_lock_acq(0, 0, resource_values, |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 283 | num_resources); |
| 284 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 285 | if (lock_handle == -1) { |
| 286 | ALOGE("Failed to acquire lock."); |
| 287 | } else { |
| 288 | /* Add this handle to our internal hint-list. */ |
| 289 | struct hint_data *new_hint = |
| 290 | (struct hint_data *)malloc(sizeof(struct hint_data)); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 291 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 292 | if (new_hint) { |
| 293 | if (!active_hint_list_head.compare) { |
| 294 | active_hint_list_head.compare = |
| 295 | (int (*)(void *, void *))hint_compare; |
| 296 | active_hint_list_head.dump = (void (*)(void *))hint_dump; |
| 297 | } |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 298 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 299 | new_hint->hint_id = hint_id; |
| 300 | new_hint->perflock_handle = lock_handle; |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 301 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 302 | if (add_list_node(&active_hint_list_head, new_hint) == NULL) { |
| 303 | free(new_hint); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 304 | /* Can't keep track of this lock. Release it. */ |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 305 | if (perf_lock_rel) |
| 306 | perf_lock_rel(lock_handle); |
| 307 | |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 308 | ALOGE("Failed to process hint."); |
| 309 | } |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 310 | } else { |
| 311 | /* Can't keep track of this lock. Release it. */ |
| 312 | if (perf_lock_rel) |
| 313 | perf_lock_rel(lock_handle); |
| 314 | |
| 315 | ALOGE("Failed to process hint."); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 316 | } |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void undo_hint_action(int hint_id) |
| 323 | { |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 324 | if (qcopt_handle) { |
| 325 | if (perf_lock_rel) { |
| 326 | /* Get hint-data associated with this hint-id */ |
| 327 | struct list_node *found_node; |
| 328 | struct hint_data temp_hint_data = { |
| 329 | .hint_id = hint_id |
| 330 | }; |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 331 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 332 | found_node = find_node(&active_hint_list_head, |
| 333 | &temp_hint_data); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 334 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 335 | if (found_node) { |
| 336 | /* Release this lock. */ |
| 337 | struct hint_data *found_hint_data = |
| 338 | (struct hint_data *)(found_node->data); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 339 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 340 | if (found_hint_data) { |
| 341 | if (perf_lock_rel(found_hint_data->perflock_handle) == -1) |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 342 | ALOGE("Perflock release failed."); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 343 | } |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 344 | |
| 345 | if (found_node->data) { |
| 346 | /* We can free the hint-data for this node. */ |
| 347 | free(found_node->data); |
| 348 | } |
| 349 | |
| 350 | remove_list_node(&active_hint_list_head, found_node); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 351 | } else { |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 352 | ALOGE("Invalid hint ID."); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
Zhoulu Luo | 3b1fbdb | 2013-12-06 16:32:10 -0800 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * Used to release initial lock holding |
| 360 | * two cores online when the display is on |
| 361 | */ |
| 362 | void undo_initial_hint_action() |
| 363 | { |
| 364 | if (qcopt_handle) { |
| 365 | if (perf_lock_rel) { |
| 366 | perf_lock_rel(1); |
| 367 | } |
| 368 | } |
| 369 | } |
Wei Wang | 5584ebb | 2016-06-07 18:44:31 -0700 | [diff] [blame] | 370 | |
| 371 | long long calc_timespan_us(struct timespec start, struct timespec end) |
| 372 | { |
| 373 | long long diff_in_us = 0; |
| 374 | diff_in_us += (end.tv_sec - start.tv_sec) * USINSEC; |
| 375 | diff_in_us += (end.tv_nsec - start.tv_nsec) / NSINUS; |
| 376 | return diff_in_us; |
| 377 | } |