blob: 6fa40af6a5fc76b9eb700580bf0d6a862bde8ff9 [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"
Rashed Abdel-Tawab00b21852017-11-03 12:44:16 -070042#include "power-helper.h"
Anurag Singh6ec12062012-10-02 09:59:01 -070043
44#define LOG_TAG "QCOM PowerHAL"
Rashed Abdel-Tawab9c14c9f2017-12-26 23:11:03 +020045#include <log/log.h>
Anurag Singh6ec12062012-10-02 09:59:01 -070046
Wei Wang5584ebb2016-06-07 18:44:31 -070047#define USINSEC 1000000L
48#define NSINUS 1000L
49
Zhao Wei Liew6fe52522016-07-19 19:57:06 +080050#define SOC_ID_0 "/sys/devices/soc0/soc_id"
51#define SOC_ID_1 "/sys/devices/system/soc/soc0/id"
52
Corinna Vinschened3426d2018-04-10 11:57:37 +020053const char *scaling_gov_path[4] = {
54 "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor",
55 "/sys/devices/system/cpu/cpu1/cpufreq/scaling_governor",
56 "/sys/devices/system/cpu/cpu2/cpufreq/scaling_governor",
57 "/sys/devices/system/cpu/cpu3/cpufreq/scaling_governor"
Sravan Kumar Ambapuram58387472015-05-20 22:22:54 +053058};
59
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -070060#define PERF_HAL_PATH "libqti-perfd-client.so"
Anurag Singh6ec12062012-10-02 09:59:01 -070061static void *qcopt_handle;
Anurag Singh6ec12062012-10-02 09:59:01 -070062static int (*perf_lock_acq)(unsigned long handle, int duration,
63 int list[], int numArgs);
Anurag Singh6ec12062012-10-02 09:59:01 -070064static int (*perf_lock_rel)(unsigned long handle);
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -070065static int (*perf_hint)(int, char *, int, int);
Anurag Singh6ec12062012-10-02 09:59:01 -070066static struct list_node active_hint_list_head;
67
68static void *get_qcopt_handle()
69{
Corinna Vinschen158576f2018-04-09 22:55:59 +020070 char qcopt_lib_path[PATH_MAX] = {0};
Anurag Singhfe93c1d2012-11-21 15:15:56 -080071 void *handle = NULL;
Anurag Singh6ec12062012-10-02 09:59:01 -070072
Anurag Singhfe93c1d2012-11-21 15:15:56 -080073 dlerror();
Anurag Singh6ec12062012-10-02 09:59:01 -070074
Corinna Vinschen158576f2018-04-09 22:55:59 +020075 if (property_get("ro.vendor.extension_library", qcopt_lib_path,
76 NULL)) {
77 handle = dlopen(qcopt_lib_path, RTLD_NOW);
78 if (!handle) {
79 ALOGE("Unable to open %s: %s\n", qcopt_lib_path,
80 dlerror());
81 }
82 }
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -070083 if (!handle) {
Corinna Vinschen158576f2018-04-09 22:55:59 +020084 handle = dlopen(PERF_HAL_PATH, RTLD_NOW);
85 if (!handle) {
86 ALOGE("Unable to open %s: %s\n", PERF_HAL_PATH,
87 dlerror());
88 }
Anurag Singh6ec12062012-10-02 09:59:01 -070089 }
90
Anurag Singhfe93c1d2012-11-21 15:15:56 -080091 return handle;
92}
93
94static void __attribute__ ((constructor)) initialize(void)
95{
96 qcopt_handle = get_qcopt_handle();
97
98 if (!qcopt_handle) {
99 ALOGE("Failed to get qcopt handle.\n");
100 } else {
101 /*
102 * qc-opt handle obtained. Get the perflock acquire/release
103 * function pointers.
104 */
105 perf_lock_acq = dlsym(qcopt_handle, "perf_lock_acq");
106
107 if (!perf_lock_acq) {
108 ALOGE("Unable to get perf_lock_acq function handle.\n");
109 }
110
111 perf_lock_rel = dlsym(qcopt_handle, "perf_lock_rel");
112
113 if (!perf_lock_rel) {
114 ALOGE("Unable to get perf_lock_rel function handle.\n");
115 }
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -0700116
117 perf_hint = dlsym(qcopt_handle, "perf_hint");
118
119 if (!perf_hint) {
120 ALOGE("Unable to get perf_hint function handle.\n");
121 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800122 }
123}
124
125static void __attribute__ ((destructor)) cleanup(void)
126{
127 if (qcopt_handle) {
128 if (dlclose(qcopt_handle))
129 ALOGE("Error occurred while closing qc-opt library.");
130 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700131}
132
Corinna Vinschened3426d2018-04-10 11:57:37 +0200133int sysfs_read(const char *path, char *s, int num_bytes)
Anurag Singh6ec12062012-10-02 09:59:01 -0700134{
135 char buf[80];
136 int count;
137 int ret = 0;
138 int fd = open(path, O_RDONLY);
139
140 if (fd < 0) {
141 strerror_r(errno, buf, sizeof(buf));
142 ALOGE("Error opening %s: %s\n", path, buf);
143
144 return -1;
145 }
146
147 if ((count = read(fd, s, num_bytes - 1)) < 0) {
148 strerror_r(errno, buf, sizeof(buf));
149 ALOGE("Error writing to %s: %s\n", path, buf);
150
151 ret = -1;
152 } else {
153 s[count] = '\0';
154 }
155
156 close(fd);
157
158 return ret;
159}
160
Corinna Vinschened3426d2018-04-10 11:57:37 +0200161int sysfs_write(const char *path, char *s)
Anurag Singh6ec12062012-10-02 09:59:01 -0700162{
163 char buf[80];
164 int len;
165 int ret = 0;
166 int fd = open(path, O_WRONLY);
167
168 if (fd < 0) {
169 strerror_r(errno, buf, sizeof(buf));
170 ALOGE("Error opening %s: %s\n", path, buf);
171 return -1 ;
172 }
173
174 len = write(fd, s, strlen(s));
175 if (len < 0) {
176 strerror_r(errno, buf, sizeof(buf));
177 ALOGE("Error writing to %s: %s\n", path, buf);
178
179 ret = -1;
180 }
181
182 close(fd);
183
184 return ret;
185}
186
187int get_scaling_governor(char governor[], int size)
188{
189 if (sysfs_read(SCALING_GOVERNOR_PATH, governor,
190 size) == -1) {
191 // Can't obtain the scaling governor. Return.
192 return -1;
193 } else {
194 // Strip newline at the end.
195 int len = strlen(governor);
196
197 len--;
198
199 while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r'))
200 governor[len--] = '\0';
201 }
202
203 return 0;
204}
205
Sravan Kumar Ambapuram58387472015-05-20 22:22:54 +0530206int get_scaling_governor_check_cores(char governor[], int size,int core_num)
207{
208
209 if (sysfs_read(scaling_gov_path[core_num], governor,
210 size) == -1) {
211 // Can't obtain the scaling governor. Return.
212 return -1;
213 }
214
215 // Strip newline at the end.
216 int len = strlen(governor);
217 len--;
218 while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r'))
219 governor[len--] = '\0';
220
221 return 0;
222}
223
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700224int is_interactive_governor(char* governor) {
225 if (strncmp(governor, INTERACTIVE_GOVERNOR, (strlen(INTERACTIVE_GOVERNOR)+1)) == 0)
226 return 1;
227 return 0;
228}
229
Michael Bestasfc62e7c2018-03-25 23:14:21 +0300230int is_ondemand_governor(char* governor) {
231 if (strncmp(governor, ONDEMAND_GOVERNOR, (strlen(ONDEMAND_GOVERNOR)+1)) == 0)
232 return 1;
233 return 0;
234}
235
Paul Keithd524aea2017-12-31 02:17:32 +0100236#ifndef INTERACTION_BOOST
237void interaction(int UNUSED(duration), int UNUSED(num_args), int UNUSED(opt_list[]))
238{
239#else
Vince Leung699c1bc2013-08-14 17:40:36 -0700240void interaction(int duration, int num_args, int opt_list[])
Vince Leung68574662013-07-16 16:47:00 -0700241{
Vince Leung68574662013-07-16 16:47:00 -0700242 static int lock_handle = 0;
Vince Leung699c1bc2013-08-14 17:40:36 -0700243
Paul Keithd524aea2017-12-31 02:17:32 +0100244 if (duration < 0 || num_args < 1 || opt_list[0] == 0)
Vince Leung699c1bc2013-08-14 17:40:36 -0700245 return;
246
Vince Leung68574662013-07-16 16:47:00 -0700247 if (qcopt_handle) {
248 if (perf_lock_acq) {
Vince Leung699c1bc2013-08-14 17:40:36 -0700249 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
Vince Leung68574662013-07-16 16:47:00 -0700250 if (lock_handle == -1)
251 ALOGE("Failed to acquire lock.");
252 }
253 }
254#endif
255}
256
dianlujitao1f5b9872019-02-19 18:06:41 +0800257#ifndef INTERACTION_BOOST
258int interaction_with_handle(int UNUSED(lock_handle), int UNUSED(duration), int UNUSED(num_args), int UNUSED(opt_list[]))
259{
260 return 0;
261#else
262int interaction_with_handle(int lock_handle, int duration, int num_args, int opt_list[])
263{
264 if (duration < 0 || num_args < 1 || opt_list[0] == 0)
265 return 0;
266
267 if (qcopt_handle) {
268 if (perf_lock_acq) {
269 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
270 if (lock_handle == -1)
271 ALOGE("Failed to acquire lock.");
272 }
273 }
274 return lock_handle;
275#endif
276}
277
278//this is interaction_with_handle using perf_hint instead of
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -0700279//perf_lock_acq
280int perf_hint_enable(int hint_id , int duration)
281{
282 int lock_handle = 0;
283
284 if (duration < 0)
285 return 0;
286
287 if (qcopt_handle) {
288 if (perf_hint) {
289 lock_handle = perf_hint(hint_id, NULL, duration, -1);
290 if (lock_handle == -1)
291 ALOGE("Failed to acquire lock.");
292 }
293 }
294 return lock_handle;
295}
296
BeYkeRYktbd52e812018-12-13 06:42:35 +0900297//Same as perf_hint_enable, but with the ability to
298//choose the type
299int perf_hint_enable_with_type(int hint_id, int duration, int type)
300{
301 int lock_handle = 0;
302
303 if (qcopt_handle) {
304 if (perf_hint) {
305 lock_handle = perf_hint(hint_id, NULL, duration, type);
306 if (lock_handle == -1)
307 ALOGE("Failed to acquire lock.");
308 }
309 }
310 return lock_handle;
311}
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -0700312
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700313void release_request(int lock_handle) {
314 if (qcopt_handle && perf_lock_rel)
315 perf_lock_rel(lock_handle);
316}
317
Corinna Vinschen282e8892018-08-26 21:16:23 +0200318int perform_hint_action(int hint_id, int resource_values[], int num_resources)
Anurag Singh6ec12062012-10-02 09:59:01 -0700319{
Corinna Vinschen282e8892018-08-26 21:16:23 +0200320 if (qcopt_handle && perf_lock_acq) {
321 /* Acquire an indefinite lock for the requested resources. */
322 int lock_handle = perf_lock_acq(0, 0, resource_values,
323 num_resources);
Anurag Singh6ec12062012-10-02 09:59:01 -0700324
Corinna Vinschen282e8892018-08-26 21:16:23 +0200325 if (lock_handle == -1) {
326 ALOGE("Failed to acquire lock.");
327 return -EINVAL;
328 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700329
Corinna Vinschen282e8892018-08-26 21:16:23 +0200330 /* Add this handle to our internal hint-list. */
331 struct hint_data *new_hint =
332 (struct hint_data *)malloc(sizeof(struct hint_data));
Anurag Singh6ec12062012-10-02 09:59:01 -0700333
Corinna Vinschen282e8892018-08-26 21:16:23 +0200334 if (!new_hint) {
335 /* Can't keep track of this lock. Release it. */
336 if (perf_lock_rel)
337 perf_lock_rel(lock_handle);
338 ALOGE("Failed to process hint.");
339 return -ENOMEM;
340 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700341
Corinna Vinschen282e8892018-08-26 21:16:23 +0200342 if (!active_hint_list_head.compare) {
343 active_hint_list_head.compare =
344 (int (*)(void *, void *))hint_compare;
345 active_hint_list_head.dump = (void (*)(void *))hint_dump;
346 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800347
Corinna Vinschen282e8892018-08-26 21:16:23 +0200348 new_hint->hint_id = hint_id;
349 new_hint->perflock_handle = lock_handle;
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800350
Corinna Vinschen282e8892018-08-26 21:16:23 +0200351 if (add_list_node(&active_hint_list_head, new_hint) == NULL) {
352 free(new_hint);
353 /* Can't keep track of this lock. Release it. */
354 if (perf_lock_rel)
355 perf_lock_rel(lock_handle);
356 ALOGE("Failed to process hint.");
357 return -ENOMEM;
Anurag Singh6ec12062012-10-02 09:59:01 -0700358 }
359 }
Corinna Vinschen282e8892018-08-26 21:16:23 +0200360 return 0;
Anurag Singh6ec12062012-10-02 09:59:01 -0700361}
362
363void undo_hint_action(int hint_id)
364{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800365 if (qcopt_handle) {
366 if (perf_lock_rel) {
367 /* Get hint-data associated with this hint-id */
368 struct list_node *found_node;
369 struct hint_data temp_hint_data = {
370 .hint_id = hint_id
371 };
Anurag Singh6ec12062012-10-02 09:59:01 -0700372
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800373 found_node = find_node(&active_hint_list_head,
374 &temp_hint_data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700375
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800376 if (found_node) {
377 /* Release this lock. */
378 struct hint_data *found_hint_data =
379 (struct hint_data *)(found_node->data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700380
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800381 if (found_hint_data) {
382 if (perf_lock_rel(found_hint_data->perflock_handle) == -1)
Anurag Singh6ec12062012-10-02 09:59:01 -0700383 ALOGE("Perflock release failed.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700384 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800385
386 if (found_node->data) {
387 /* We can free the hint-data for this node. */
388 free(found_node->data);
389 }
390
391 remove_list_node(&active_hint_list_head, found_node);
Anurag Singh6ec12062012-10-02 09:59:01 -0700392 } else {
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800393 ALOGE("Invalid hint ID.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700394 }
395 }
396 }
397}
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800398
399/*
400 * Used to release initial lock holding
401 * two cores online when the display is on
402 */
403void undo_initial_hint_action()
404{
405 if (qcopt_handle) {
406 if (perf_lock_rel) {
407 perf_lock_rel(1);
408 }
409 }
410}
Wei Wang5584ebb2016-06-07 18:44:31 -0700411
412long long calc_timespan_us(struct timespec start, struct timespec end)
413{
414 long long diff_in_us = 0;
415 diff_in_us += (end.tv_sec - start.tv_sec) * USINSEC;
416 diff_in_us += (end.tv_nsec - start.tv_nsec) / NSINUS;
417 return diff_in_us;
418}
Zhao Wei Liew6fe52522016-07-19 19:57:06 +0800419
420int get_soc_id(void)
421{
422 int fd;
423 int soc_id = -1;
424 char buf[10] = { 0 };
425
426 if (!access(SOC_ID_0, F_OK))
427 fd = open(SOC_ID_0, O_RDONLY);
428 else
429 fd = open(SOC_ID_1, O_RDONLY);
430
431 if (fd >= 0) {
432 if (read(fd, buf, sizeof(buf) - 1) == -1)
433 ALOGW("Unable to read soc_id");
434 else
435 soc_id = atoi(buf);
436 }
437
438 close(fd);
439 return soc_id;
440}