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