blob: 0aad8e1636a935fe41b3e8fe655c3e16e5d1edf6 [file] [log] [blame]
Anurag Singh6ec12062012-10-02 09:59:01 -07001/*
Anurag Singhfe93c1d2012-11-21 15:15:56 -08002 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Anurag Singh6ec12062012-10-02 09:59:01 -07003 *
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 Truong70222452013-02-10 06:35:11 -080013 * * Neither the name of The Linux Foundation nor the names of its
Anurag Singh6ec12062012-10-02 09:59:01 -070014 * 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 Singh6ec12062012-10-02 09:59:01 -070029#define LOG_NIDEBUG 0
30
31#include <dlfcn.h>
32#include <fcntl.h>
33#include <errno.h>
Ajay Dudani5e676802015-04-07 23:56:52 -070034#include <stdlib.h>
35#include <string.h>
Anurag Singh6ec12062012-10-02 09:59:01 -070036
37#include "utils.h"
38#include "list.h"
39#include "hint-data.h"
Anurag Singh057806b2013-04-16 16:53:45 -070040#include "power-common.h"
Anurag Singh6ec12062012-10-02 09:59:01 -070041
42#define LOG_TAG "QCOM PowerHAL"
43#include <utils/Log.h>
44
Sravan Kumar Ambapuram58387472015-05-20 22:22:54 +053045char 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
Anurag Singh6ec12062012-10-02 09:59:01 -070052static void *qcopt_handle;
Anurag Singh6ec12062012-10-02 09:59:01 -070053static int (*perf_lock_acq)(unsigned long handle, int duration,
54 int list[], int numArgs);
Anurag Singh6ec12062012-10-02 09:59:01 -070055static int (*perf_lock_rel)(unsigned long handle);
Anurag Singh6ec12062012-10-02 09:59:01 -070056static struct list_node active_hint_list_head;
57
58static void *get_qcopt_handle()
59{
Anurag Singhfe93c1d2012-11-21 15:15:56 -080060 char qcopt_lib_path[PATH_MAX] = {0};
61 void *handle = NULL;
Anurag Singh6ec12062012-10-02 09:59:01 -070062
Anurag Singhfe93c1d2012-11-21 15:15:56 -080063 dlerror();
Anurag Singh6ec12062012-10-02 09:59:01 -070064
Anurag Singhfe93c1d2012-11-21 15:15:56 -080065 if (property_get("ro.vendor.extension_library", qcopt_lib_path,
66 NULL)) {
67 handle = dlopen(qcopt_lib_path, RTLD_NOW);
68 if (!handle) {
69 ALOGE("Unable to open %s: %s\n", qcopt_lib_path,
70 dlerror());
Anurag Singh6ec12062012-10-02 09:59:01 -070071 }
72 }
73
Anurag Singhfe93c1d2012-11-21 15:15:56 -080074 return handle;
75}
76
77static 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 }
99 }
100}
101
102static void __attribute__ ((destructor)) cleanup(void)
103{
104 if (qcopt_handle) {
105 if (dlclose(qcopt_handle))
106 ALOGE("Error occurred while closing qc-opt library.");
107 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700108}
109
110int sysfs_read(char *path, char *s, int num_bytes)
111{
112 char buf[80];
113 int count;
114 int ret = 0;
115 int fd = open(path, O_RDONLY);
116
117 if (fd < 0) {
118 strerror_r(errno, buf, sizeof(buf));
119 ALOGE("Error opening %s: %s\n", path, buf);
120
121 return -1;
122 }
123
124 if ((count = read(fd, s, num_bytes - 1)) < 0) {
125 strerror_r(errno, buf, sizeof(buf));
126 ALOGE("Error writing to %s: %s\n", path, buf);
127
128 ret = -1;
129 } else {
130 s[count] = '\0';
131 }
132
133 close(fd);
134
135 return ret;
136}
137
138int sysfs_write(char *path, char *s)
139{
140 char buf[80];
141 int len;
142 int ret = 0;
143 int fd = open(path, O_WRONLY);
144
145 if (fd < 0) {
146 strerror_r(errno, buf, sizeof(buf));
147 ALOGE("Error opening %s: %s\n", path, buf);
148 return -1 ;
149 }
150
151 len = write(fd, s, strlen(s));
152 if (len < 0) {
153 strerror_r(errno, buf, sizeof(buf));
154 ALOGE("Error writing to %s: %s\n", path, buf);
155
156 ret = -1;
157 }
158
159 close(fd);
160
161 return ret;
162}
163
164int get_scaling_governor(char governor[], int size)
165{
166 if (sysfs_read(SCALING_GOVERNOR_PATH, governor,
167 size) == -1) {
168 // Can't obtain the scaling governor. Return.
169 return -1;
170 } else {
171 // Strip newline at the end.
172 int len = strlen(governor);
173
174 len--;
175
176 while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r'))
177 governor[len--] = '\0';
178 }
179
180 return 0;
181}
182
Sravan Kumar Ambapuram58387472015-05-20 22:22:54 +0530183int get_scaling_governor_check_cores(char governor[], int size,int core_num)
184{
185
186 if (sysfs_read(scaling_gov_path[core_num], governor,
187 size) == -1) {
188 // Can't obtain the scaling governor. Return.
189 return -1;
190 }
191
192 // Strip newline at the end.
193 int len = strlen(governor);
194 len--;
195 while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r'))
196 governor[len--] = '\0';
197
198 return 0;
199}
200
Vince Leung699c1bc2013-08-14 17:40:36 -0700201void interaction(int duration, int num_args, int opt_list[])
Vince Leung68574662013-07-16 16:47:00 -0700202{
203#ifdef INTERACTION_BOOST
204 static int lock_handle = 0;
Vince Leung699c1bc2013-08-14 17:40:36 -0700205
206 if (duration < 0 || num_args < 1 || opt_list[0] == NULL)
207 return;
208
Vince Leung68574662013-07-16 16:47:00 -0700209 if (qcopt_handle) {
210 if (perf_lock_acq) {
Vince Leung699c1bc2013-08-14 17:40:36 -0700211 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
Vince Leung68574662013-07-16 16:47:00 -0700212 if (lock_handle == -1)
213 ALOGE("Failed to acquire lock.");
214 }
215 }
216#endif
217}
218
Anurag Singh6ec12062012-10-02 09:59:01 -0700219void perform_hint_action(int hint_id, int resource_values[], int num_resources)
220{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800221 if (qcopt_handle) {
222 if (perf_lock_acq) {
223 /* Acquire an indefinite lock for the requested resources. */
224 int lock_handle = perf_lock_acq(0, 0, resource_values,
Anurag Singh6ec12062012-10-02 09:59:01 -0700225 num_resources);
226
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800227 if (lock_handle == -1) {
228 ALOGE("Failed to acquire lock.");
229 } else {
230 /* Add this handle to our internal hint-list. */
231 struct hint_data *new_hint =
232 (struct hint_data *)malloc(sizeof(struct hint_data));
Anurag Singh6ec12062012-10-02 09:59:01 -0700233
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800234 if (new_hint) {
235 if (!active_hint_list_head.compare) {
236 active_hint_list_head.compare =
237 (int (*)(void *, void *))hint_compare;
238 active_hint_list_head.dump = (void (*)(void *))hint_dump;
239 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700240
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800241 new_hint->hint_id = hint_id;
242 new_hint->perflock_handle = lock_handle;
Anurag Singh6ec12062012-10-02 09:59:01 -0700243
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800244 if (add_list_node(&active_hint_list_head, new_hint) == NULL) {
245 free(new_hint);
Anurag Singh6ec12062012-10-02 09:59:01 -0700246 /* Can't keep track of this lock. Release it. */
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800247 if (perf_lock_rel)
248 perf_lock_rel(lock_handle);
249
Anurag Singh6ec12062012-10-02 09:59:01 -0700250 ALOGE("Failed to process hint.");
251 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800252 } else {
253 /* Can't keep track of this lock. Release it. */
254 if (perf_lock_rel)
255 perf_lock_rel(lock_handle);
256
257 ALOGE("Failed to process hint.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700258 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700259 }
260 }
261 }
262}
263
264void undo_hint_action(int hint_id)
265{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800266 if (qcopt_handle) {
267 if (perf_lock_rel) {
268 /* Get hint-data associated with this hint-id */
269 struct list_node *found_node;
270 struct hint_data temp_hint_data = {
271 .hint_id = hint_id
272 };
Anurag Singh6ec12062012-10-02 09:59:01 -0700273
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800274 found_node = find_node(&active_hint_list_head,
275 &temp_hint_data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700276
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800277 if (found_node) {
278 /* Release this lock. */
279 struct hint_data *found_hint_data =
280 (struct hint_data *)(found_node->data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700281
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800282 if (found_hint_data) {
283 if (perf_lock_rel(found_hint_data->perflock_handle) == -1)
Anurag Singh6ec12062012-10-02 09:59:01 -0700284 ALOGE("Perflock release failed.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700285 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800286
287 if (found_node->data) {
288 /* We can free the hint-data for this node. */
289 free(found_node->data);
290 }
291
292 remove_list_node(&active_hint_list_head, found_node);
Anurag Singh6ec12062012-10-02 09:59:01 -0700293 } else {
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800294 ALOGE("Invalid hint ID.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700295 }
296 }
297 }
298}
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800299
300/*
301 * Used to release initial lock holding
302 * two cores online when the display is on
303 */
304void undo_initial_hint_action()
305{
306 if (qcopt_handle) {
307 if (perf_lock_rel) {
308 perf_lock_rel(1);
309 }
310 }
311}