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