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