Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 1 | /* |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 2 | * Copyright (c) 2012-2013, 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 | |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 45 | static void *qcopt_handle; |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 46 | static int (*perf_lock_acq)(unsigned long handle, int duration, |
| 47 | int list[], int numArgs); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 48 | static int (*perf_lock_rel)(unsigned long handle); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 49 | static struct list_node active_hint_list_head; |
| 50 | |
| 51 | static void *get_qcopt_handle() |
| 52 | { |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 53 | char qcopt_lib_path[PATH_MAX] = {0}; |
| 54 | void *handle = NULL; |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 55 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 56 | dlerror(); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 57 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 58 | if (property_get("ro.vendor.extension_library", qcopt_lib_path, |
| 59 | NULL)) { |
| 60 | handle = dlopen(qcopt_lib_path, RTLD_NOW); |
| 61 | if (!handle) { |
| 62 | ALOGE("Unable to open %s: %s\n", qcopt_lib_path, |
| 63 | dlerror()); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 67 | return handle; |
| 68 | } |
| 69 | |
| 70 | static void __attribute__ ((constructor)) initialize(void) |
| 71 | { |
| 72 | qcopt_handle = get_qcopt_handle(); |
| 73 | |
| 74 | if (!qcopt_handle) { |
| 75 | ALOGE("Failed to get qcopt handle.\n"); |
| 76 | } else { |
| 77 | /* |
| 78 | * qc-opt handle obtained. Get the perflock acquire/release |
| 79 | * function pointers. |
| 80 | */ |
| 81 | perf_lock_acq = dlsym(qcopt_handle, "perf_lock_acq"); |
| 82 | |
| 83 | if (!perf_lock_acq) { |
| 84 | ALOGE("Unable to get perf_lock_acq function handle.\n"); |
| 85 | } |
| 86 | |
| 87 | perf_lock_rel = dlsym(qcopt_handle, "perf_lock_rel"); |
| 88 | |
| 89 | if (!perf_lock_rel) { |
| 90 | ALOGE("Unable to get perf_lock_rel function handle.\n"); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void __attribute__ ((destructor)) cleanup(void) |
| 96 | { |
| 97 | if (qcopt_handle) { |
| 98 | if (dlclose(qcopt_handle)) |
| 99 | ALOGE("Error occurred while closing qc-opt library."); |
| 100 | } |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int sysfs_read(char *path, char *s, int num_bytes) |
| 104 | { |
| 105 | char buf[80]; |
| 106 | int count; |
| 107 | int ret = 0; |
| 108 | int fd = open(path, O_RDONLY); |
| 109 | |
| 110 | if (fd < 0) { |
| 111 | strerror_r(errno, buf, sizeof(buf)); |
| 112 | ALOGE("Error opening %s: %s\n", path, buf); |
| 113 | |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | if ((count = read(fd, s, num_bytes - 1)) < 0) { |
| 118 | strerror_r(errno, buf, sizeof(buf)); |
| 119 | ALOGE("Error writing to %s: %s\n", path, buf); |
| 120 | |
| 121 | ret = -1; |
| 122 | } else { |
| 123 | s[count] = '\0'; |
| 124 | } |
| 125 | |
| 126 | close(fd); |
| 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | int sysfs_write(char *path, char *s) |
| 132 | { |
| 133 | char buf[80]; |
| 134 | int len; |
| 135 | int ret = 0; |
| 136 | int fd = open(path, O_WRONLY); |
| 137 | |
| 138 | if (fd < 0) { |
| 139 | strerror_r(errno, buf, sizeof(buf)); |
| 140 | ALOGE("Error opening %s: %s\n", path, buf); |
| 141 | return -1 ; |
| 142 | } |
| 143 | |
| 144 | len = write(fd, s, strlen(s)); |
| 145 | if (len < 0) { |
| 146 | strerror_r(errno, buf, sizeof(buf)); |
| 147 | ALOGE("Error writing to %s: %s\n", path, buf); |
| 148 | |
| 149 | ret = -1; |
| 150 | } |
| 151 | |
| 152 | close(fd); |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | int get_scaling_governor(char governor[], int size) |
| 158 | { |
| 159 | if (sysfs_read(SCALING_GOVERNOR_PATH, governor, |
| 160 | size) == -1) { |
| 161 | // Can't obtain the scaling governor. Return. |
| 162 | return -1; |
| 163 | } else { |
| 164 | // Strip newline at the end. |
| 165 | int len = strlen(governor); |
| 166 | |
| 167 | len--; |
| 168 | |
| 169 | while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r')) |
| 170 | governor[len--] = '\0'; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 176 | void interaction(int duration, int num_args, int opt_list[]) |
Vince Leung | 6857466 | 2013-07-16 16:47:00 -0700 | [diff] [blame] | 177 | { |
| 178 | #ifdef INTERACTION_BOOST |
| 179 | static int lock_handle = 0; |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 180 | |
| 181 | if (duration < 0 || num_args < 1 || opt_list[0] == NULL) |
| 182 | return; |
| 183 | |
Vince Leung | 6857466 | 2013-07-16 16:47:00 -0700 | [diff] [blame] | 184 | if (qcopt_handle) { |
| 185 | if (perf_lock_acq) { |
Vince Leung | 699c1bc | 2013-08-14 17:40:36 -0700 | [diff] [blame] | 186 | lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args); |
Vince Leung | 6857466 | 2013-07-16 16:47:00 -0700 | [diff] [blame] | 187 | if (lock_handle == -1) |
| 188 | ALOGE("Failed to acquire lock."); |
| 189 | } |
| 190 | } |
| 191 | #endif |
| 192 | } |
| 193 | |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 194 | void perform_hint_action(int hint_id, int resource_values[], int num_resources) |
| 195 | { |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 196 | if (qcopt_handle) { |
| 197 | if (perf_lock_acq) { |
| 198 | /* Acquire an indefinite lock for the requested resources. */ |
| 199 | int lock_handle = perf_lock_acq(0, 0, resource_values, |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 200 | num_resources); |
| 201 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 202 | if (lock_handle == -1) { |
| 203 | ALOGE("Failed to acquire lock."); |
| 204 | } else { |
| 205 | /* Add this handle to our internal hint-list. */ |
| 206 | struct hint_data *new_hint = |
| 207 | (struct hint_data *)malloc(sizeof(struct hint_data)); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 208 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 209 | if (new_hint) { |
| 210 | if (!active_hint_list_head.compare) { |
| 211 | active_hint_list_head.compare = |
| 212 | (int (*)(void *, void *))hint_compare; |
| 213 | active_hint_list_head.dump = (void (*)(void *))hint_dump; |
| 214 | } |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 215 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 216 | new_hint->hint_id = hint_id; |
| 217 | new_hint->perflock_handle = lock_handle; |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 218 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 219 | if (add_list_node(&active_hint_list_head, new_hint) == NULL) { |
| 220 | free(new_hint); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 221 | /* Can't keep track of this lock. Release it. */ |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 222 | if (perf_lock_rel) |
| 223 | perf_lock_rel(lock_handle); |
| 224 | |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 225 | ALOGE("Failed to process hint."); |
| 226 | } |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 227 | } else { |
| 228 | /* Can't keep track of this lock. Release it. */ |
| 229 | if (perf_lock_rel) |
| 230 | perf_lock_rel(lock_handle); |
| 231 | |
| 232 | ALOGE("Failed to process hint."); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 233 | } |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void undo_hint_action(int hint_id) |
| 240 | { |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 241 | if (qcopt_handle) { |
| 242 | if (perf_lock_rel) { |
| 243 | /* Get hint-data associated with this hint-id */ |
| 244 | struct list_node *found_node; |
| 245 | struct hint_data temp_hint_data = { |
| 246 | .hint_id = hint_id |
| 247 | }; |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 248 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 249 | found_node = find_node(&active_hint_list_head, |
| 250 | &temp_hint_data); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 251 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 252 | if (found_node) { |
| 253 | /* Release this lock. */ |
| 254 | struct hint_data *found_hint_data = |
| 255 | (struct hint_data *)(found_node->data); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 256 | |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 257 | if (found_hint_data) { |
| 258 | if (perf_lock_rel(found_hint_data->perflock_handle) == -1) |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 259 | ALOGE("Perflock release failed."); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 260 | } |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 261 | |
| 262 | if (found_node->data) { |
| 263 | /* We can free the hint-data for this node. */ |
| 264 | free(found_node->data); |
| 265 | } |
| 266 | |
| 267 | remove_list_node(&active_hint_list_head, found_node); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 268 | } else { |
Anurag Singh | fe93c1d | 2012-11-21 15:15:56 -0800 | [diff] [blame] | 269 | ALOGE("Invalid hint ID."); |
Anurag Singh | 6ec1206 | 2012-10-02 09:59:01 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
Zhoulu Luo | 3b1fbdb | 2013-12-06 16:32:10 -0800 | [diff] [blame] | 274 | |
| 275 | /* |
| 276 | * Used to release initial lock holding |
| 277 | * two cores online when the display is on |
| 278 | */ |
| 279 | void undo_initial_hint_action() |
| 280 | { |
| 281 | if (qcopt_handle) { |
| 282 | if (perf_lock_rel) { |
| 283 | perf_lock_rel(1); |
| 284 | } |
| 285 | } |
| 286 | } |