blob: 860ae9deea4d71e6d8c7979e683c580ddf55bffc [file] [log] [blame]
Anurag Singh6ec12062012-10-02 09:59:01 -07001/*
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -07002 * Copyright (c) 2012-2013,2015-2017, 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
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700201int is_interactive_governor(char* governor) {
202 if (strncmp(governor, INTERACTIVE_GOVERNOR, (strlen(INTERACTIVE_GOVERNOR)+1)) == 0)
203 return 1;
204 return 0;
205}
206
Vince Leung699c1bc2013-08-14 17:40:36 -0700207void interaction(int duration, int num_args, int opt_list[])
Vince Leung68574662013-07-16 16:47:00 -0700208{
209#ifdef INTERACTION_BOOST
210 static int lock_handle = 0;
Vince Leung699c1bc2013-08-14 17:40:36 -0700211
212 if (duration < 0 || num_args < 1 || opt_list[0] == NULL)
213 return;
214
Vince Leung68574662013-07-16 16:47:00 -0700215 if (qcopt_handle) {
216 if (perf_lock_acq) {
Vince Leung699c1bc2013-08-14 17:40:36 -0700217 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
Vince Leung68574662013-07-16 16:47:00 -0700218 if (lock_handle == -1)
219 ALOGE("Failed to acquire lock.");
220 }
221 }
222#endif
223}
224
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700225int interaction_with_handle(int lock_handle, int duration, int num_args, int opt_list[])
226{
227 if (duration < 0 || num_args < 1 || opt_list[0] == NULL)
228 return 0;
229
230 if (qcopt_handle) {
231 if (perf_lock_acq) {
232 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
233 if (lock_handle == -1)
234 ALOGE("Failed to acquire lock.");
235 }
236 }
237 return lock_handle;
238}
239
240void release_request(int lock_handle) {
241 if (qcopt_handle && perf_lock_rel)
242 perf_lock_rel(lock_handle);
243}
244
Anurag Singh6ec12062012-10-02 09:59:01 -0700245void perform_hint_action(int hint_id, int resource_values[], int num_resources)
246{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800247 if (qcopt_handle) {
248 if (perf_lock_acq) {
249 /* Acquire an indefinite lock for the requested resources. */
250 int lock_handle = perf_lock_acq(0, 0, resource_values,
Anurag Singh6ec12062012-10-02 09:59:01 -0700251 num_resources);
252
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800253 if (lock_handle == -1) {
254 ALOGE("Failed to acquire lock.");
255 } else {
256 /* Add this handle to our internal hint-list. */
257 struct hint_data *new_hint =
258 (struct hint_data *)malloc(sizeof(struct hint_data));
Anurag Singh6ec12062012-10-02 09:59:01 -0700259
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800260 if (new_hint) {
261 if (!active_hint_list_head.compare) {
262 active_hint_list_head.compare =
263 (int (*)(void *, void *))hint_compare;
264 active_hint_list_head.dump = (void (*)(void *))hint_dump;
265 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700266
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800267 new_hint->hint_id = hint_id;
268 new_hint->perflock_handle = lock_handle;
Anurag Singh6ec12062012-10-02 09:59:01 -0700269
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800270 if (add_list_node(&active_hint_list_head, new_hint) == NULL) {
271 free(new_hint);
Anurag Singh6ec12062012-10-02 09:59:01 -0700272 /* Can't keep track of this lock. Release it. */
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800273 if (perf_lock_rel)
274 perf_lock_rel(lock_handle);
275
Anurag Singh6ec12062012-10-02 09:59:01 -0700276 ALOGE("Failed to process hint.");
277 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800278 } else {
279 /* Can't keep track of this lock. Release it. */
280 if (perf_lock_rel)
281 perf_lock_rel(lock_handle);
282
283 ALOGE("Failed to process hint.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700284 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700285 }
286 }
287 }
288}
289
290void undo_hint_action(int hint_id)
291{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800292 if (qcopt_handle) {
293 if (perf_lock_rel) {
294 /* Get hint-data associated with this hint-id */
295 struct list_node *found_node;
296 struct hint_data temp_hint_data = {
297 .hint_id = hint_id
298 };
Anurag Singh6ec12062012-10-02 09:59:01 -0700299
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800300 found_node = find_node(&active_hint_list_head,
301 &temp_hint_data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700302
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800303 if (found_node) {
304 /* Release this lock. */
305 struct hint_data *found_hint_data =
306 (struct hint_data *)(found_node->data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700307
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800308 if (found_hint_data) {
309 if (perf_lock_rel(found_hint_data->perflock_handle) == -1)
Anurag Singh6ec12062012-10-02 09:59:01 -0700310 ALOGE("Perflock release failed.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700311 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800312
313 if (found_node->data) {
314 /* We can free the hint-data for this node. */
315 free(found_node->data);
316 }
317
318 remove_list_node(&active_hint_list_head, found_node);
Anurag Singh6ec12062012-10-02 09:59:01 -0700319 } else {
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800320 ALOGE("Invalid hint ID.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700321 }
322 }
323 }
324}
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800325
326/*
327 * Used to release initial lock holding
328 * two cores online when the display is on
329 */
330void undo_initial_hint_action()
331{
332 if (qcopt_handle) {
333 if (perf_lock_rel) {
334 perf_lock_rel(1);
335 }
336 }
337}