blob: 0456377830024bbefdb8581abb585602f2295be2 [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>
Rashed Abdel-Tawab9c14c9f2017-12-26 23:11:03 +020036#include <unistd.h>
Anurag Singh6ec12062012-10-02 09:59:01 -070037
38#include "utils.h"
39#include "list.h"
40#include "hint-data.h"
Anurag Singh057806b2013-04-16 16:53:45 -070041#include "power-common.h"
Anurag Singh6ec12062012-10-02 09:59:01 -070042
43#define LOG_TAG "QCOM PowerHAL"
Rashed Abdel-Tawab9c14c9f2017-12-26 23:11:03 +020044#include <log/log.h>
Anurag Singh6ec12062012-10-02 09:59:01 -070045
Sravan Kumar Ambapuram58387472015-05-20 22:22:54 +053046char scaling_gov_path[4][80] ={
47 "sys/devices/system/cpu/cpu0/cpufreq/scaling_governor",
48 "sys/devices/system/cpu/cpu1/cpufreq/scaling_governor",
49 "sys/devices/system/cpu/cpu2/cpufreq/scaling_governor",
50 "sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
51};
52
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -070053#define PERF_HAL_PATH "libqti-perfd-client.so"
Anurag Singh6ec12062012-10-02 09:59:01 -070054static void *qcopt_handle;
Anurag Singh6ec12062012-10-02 09:59:01 -070055static int (*perf_lock_acq)(unsigned long handle, int duration,
56 int list[], int numArgs);
Anurag Singh6ec12062012-10-02 09:59:01 -070057static int (*perf_lock_rel)(unsigned long handle);
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -070058static int (*perf_hint)(int, char *, int, int);
Anurag Singh6ec12062012-10-02 09:59:01 -070059static struct list_node active_hint_list_head;
60
61static void *get_qcopt_handle()
62{
Anurag Singhfe93c1d2012-11-21 15:15:56 -080063 void *handle = NULL;
Anurag Singh6ec12062012-10-02 09:59:01 -070064
Anurag Singhfe93c1d2012-11-21 15:15:56 -080065 dlerror();
Anurag Singh6ec12062012-10-02 09:59:01 -070066
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -070067 handle = dlopen(PERF_HAL_PATH, RTLD_NOW);
68 if (!handle) {
69 ALOGE("Unable to open %s: %s\n", PERF_HAL_PATH,
70 dlerror());
Anurag Singh6ec12062012-10-02 09:59:01 -070071 }
72
Anurag Singhfe93c1d2012-11-21 15:15:56 -080073 return handle;
74}
75
76static void __attribute__ ((constructor)) initialize(void)
77{
78 qcopt_handle = get_qcopt_handle();
79
80 if (!qcopt_handle) {
81 ALOGE("Failed to get qcopt handle.\n");
82 } else {
83 /*
84 * qc-opt handle obtained. Get the perflock acquire/release
85 * function pointers.
86 */
87 perf_lock_acq = dlsym(qcopt_handle, "perf_lock_acq");
88
89 if (!perf_lock_acq) {
90 ALOGE("Unable to get perf_lock_acq function handle.\n");
91 }
92
93 perf_lock_rel = dlsym(qcopt_handle, "perf_lock_rel");
94
95 if (!perf_lock_rel) {
96 ALOGE("Unable to get perf_lock_rel function handle.\n");
97 }
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -070098
99 perf_hint = dlsym(qcopt_handle, "perf_hint");
100
101 if (!perf_hint) {
102 ALOGE("Unable to get perf_hint function handle.\n");
103 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800104 }
105}
106
107static void __attribute__ ((destructor)) cleanup(void)
108{
109 if (qcopt_handle) {
110 if (dlclose(qcopt_handle))
111 ALOGE("Error occurred while closing qc-opt library.");
112 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700113}
114
115int sysfs_read(char *path, char *s, int num_bytes)
116{
117 char buf[80];
118 int count;
119 int ret = 0;
120 int fd = open(path, O_RDONLY);
121
122 if (fd < 0) {
123 strerror_r(errno, buf, sizeof(buf));
124 ALOGE("Error opening %s: %s\n", path, buf);
125
126 return -1;
127 }
128
129 if ((count = read(fd, s, num_bytes - 1)) < 0) {
130 strerror_r(errno, buf, sizeof(buf));
131 ALOGE("Error writing to %s: %s\n", path, buf);
132
133 ret = -1;
134 } else {
135 s[count] = '\0';
136 }
137
138 close(fd);
139
140 return ret;
141}
142
143int sysfs_write(char *path, char *s)
144{
145 char buf[80];
146 int len;
147 int ret = 0;
148 int fd = open(path, O_WRONLY);
149
150 if (fd < 0) {
151 strerror_r(errno, buf, sizeof(buf));
152 ALOGE("Error opening %s: %s\n", path, buf);
153 return -1 ;
154 }
155
156 len = write(fd, s, strlen(s));
157 if (len < 0) {
158 strerror_r(errno, buf, sizeof(buf));
159 ALOGE("Error writing to %s: %s\n", path, buf);
160
161 ret = -1;
162 }
163
164 close(fd);
165
166 return ret;
167}
168
169int get_scaling_governor(char governor[], int size)
170{
171 if (sysfs_read(SCALING_GOVERNOR_PATH, governor,
172 size) == -1) {
173 // Can't obtain the scaling governor. Return.
174 return -1;
175 } else {
176 // Strip newline at the end.
177 int len = strlen(governor);
178
179 len--;
180
181 while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r'))
182 governor[len--] = '\0';
183 }
184
185 return 0;
186}
187
Sravan Kumar Ambapuram58387472015-05-20 22:22:54 +0530188int get_scaling_governor_check_cores(char governor[], int size,int core_num)
189{
190
191 if (sysfs_read(scaling_gov_path[core_num], governor,
192 size) == -1) {
193 // Can't obtain the scaling governor. Return.
194 return -1;
195 }
196
197 // Strip newline at the end.
198 int len = strlen(governor);
199 len--;
200 while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r'))
201 governor[len--] = '\0';
202
203 return 0;
204}
205
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700206int is_interactive_governor(char* governor) {
207 if (strncmp(governor, INTERACTIVE_GOVERNOR, (strlen(INTERACTIVE_GOVERNOR)+1)) == 0)
208 return 1;
209 return 0;
210}
211
Vince Leung699c1bc2013-08-14 17:40:36 -0700212void interaction(int duration, int num_args, int opt_list[])
Vince Leung68574662013-07-16 16:47:00 -0700213{
214#ifdef INTERACTION_BOOST
215 static int lock_handle = 0;
Vince Leung699c1bc2013-08-14 17:40:36 -0700216
217 if (duration < 0 || num_args < 1 || opt_list[0] == NULL)
218 return;
219
Vince Leung68574662013-07-16 16:47:00 -0700220 if (qcopt_handle) {
221 if (perf_lock_acq) {
Vince Leung699c1bc2013-08-14 17:40:36 -0700222 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
Vince Leung68574662013-07-16 16:47:00 -0700223 if (lock_handle == -1)
224 ALOGE("Failed to acquire lock.");
225 }
226 }
227#endif
228}
229
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700230int interaction_with_handle(int lock_handle, int duration, int num_args, int opt_list[])
231{
232 if (duration < 0 || num_args < 1 || opt_list[0] == NULL)
233 return 0;
234
235 if (qcopt_handle) {
236 if (perf_lock_acq) {
237 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
238 if (lock_handle == -1)
239 ALOGE("Failed to acquire lock.");
240 }
241 }
242 return lock_handle;
243}
244
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -0700245//this is interaction_with_handle using perf_hint instead of
246//perf_lock_acq
247int perf_hint_enable(int hint_id , int duration)
248{
249 int lock_handle = 0;
250
251 if (duration < 0)
252 return 0;
253
254 if (qcopt_handle) {
255 if (perf_hint) {
256 lock_handle = perf_hint(hint_id, NULL, duration, -1);
257 if (lock_handle == -1)
258 ALOGE("Failed to acquire lock.");
259 }
260 }
261 return lock_handle;
262}
263
264
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700265void release_request(int lock_handle) {
266 if (qcopt_handle && perf_lock_rel)
267 perf_lock_rel(lock_handle);
268}
269
Anurag Singh6ec12062012-10-02 09:59:01 -0700270void perform_hint_action(int hint_id, int resource_values[], int num_resources)
271{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800272 if (qcopt_handle) {
273 if (perf_lock_acq) {
274 /* Acquire an indefinite lock for the requested resources. */
275 int lock_handle = perf_lock_acq(0, 0, resource_values,
Anurag Singh6ec12062012-10-02 09:59:01 -0700276 num_resources);
277
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800278 if (lock_handle == -1) {
279 ALOGE("Failed to acquire lock.");
280 } else {
281 /* Add this handle to our internal hint-list. */
282 struct hint_data *new_hint =
283 (struct hint_data *)malloc(sizeof(struct hint_data));
Anurag Singh6ec12062012-10-02 09:59:01 -0700284
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800285 if (new_hint) {
286 if (!active_hint_list_head.compare) {
287 active_hint_list_head.compare =
288 (int (*)(void *, void *))hint_compare;
289 active_hint_list_head.dump = (void (*)(void *))hint_dump;
290 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700291
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800292 new_hint->hint_id = hint_id;
293 new_hint->perflock_handle = lock_handle;
Anurag Singh6ec12062012-10-02 09:59:01 -0700294
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800295 if (add_list_node(&active_hint_list_head, new_hint) == NULL) {
296 free(new_hint);
Anurag Singh6ec12062012-10-02 09:59:01 -0700297 /* Can't keep track of this lock. Release it. */
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800298 if (perf_lock_rel)
299 perf_lock_rel(lock_handle);
300
Anurag Singh6ec12062012-10-02 09:59:01 -0700301 ALOGE("Failed to process hint.");
302 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800303 } else {
304 /* Can't keep track of this lock. Release it. */
305 if (perf_lock_rel)
306 perf_lock_rel(lock_handle);
307
308 ALOGE("Failed to process hint.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700309 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700310 }
311 }
312 }
313}
314
315void undo_hint_action(int hint_id)
316{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800317 if (qcopt_handle) {
318 if (perf_lock_rel) {
319 /* Get hint-data associated with this hint-id */
320 struct list_node *found_node;
321 struct hint_data temp_hint_data = {
322 .hint_id = hint_id
323 };
Anurag Singh6ec12062012-10-02 09:59:01 -0700324
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800325 found_node = find_node(&active_hint_list_head,
326 &temp_hint_data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700327
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800328 if (found_node) {
329 /* Release this lock. */
330 struct hint_data *found_hint_data =
331 (struct hint_data *)(found_node->data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700332
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800333 if (found_hint_data) {
334 if (perf_lock_rel(found_hint_data->perflock_handle) == -1)
Anurag Singh6ec12062012-10-02 09:59:01 -0700335 ALOGE("Perflock release failed.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700336 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800337
338 if (found_node->data) {
339 /* We can free the hint-data for this node. */
340 free(found_node->data);
341 }
342
343 remove_list_node(&active_hint_list_head, found_node);
Anurag Singh6ec12062012-10-02 09:59:01 -0700344 } else {
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800345 ALOGE("Invalid hint ID.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700346 }
347 }
348 }
349}
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800350
351/*
352 * Used to release initial lock holding
353 * two cores online when the display is on
354 */
355void undo_initial_hint_action()
356{
357 if (qcopt_handle) {
358 if (perf_lock_rel) {
359 perf_lock_rel(1);
360 }
361 }
362}