blob: 49a1c97c1ef1ee9d3ab062ab165208ca5cf0559f [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
Sravan Kumar Ambapuram58387472015-05-20 22:22:54 +053053char scaling_gov_path[4][80] ={
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"
58};
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{
Anurag Singhfe93c1d2012-11-21 15:15:56 -080070 void *handle = NULL;
Anurag Singh6ec12062012-10-02 09:59:01 -070071
Anurag Singhfe93c1d2012-11-21 15:15:56 -080072 dlerror();
Anurag Singh6ec12062012-10-02 09:59:01 -070073
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -070074 handle = dlopen(PERF_HAL_PATH, RTLD_NOW);
75 if (!handle) {
76 ALOGE("Unable to open %s: %s\n", PERF_HAL_PATH,
77 dlerror());
Anurag Singh6ec12062012-10-02 09:59:01 -070078 }
79
Anurag Singhfe93c1d2012-11-21 15:15:56 -080080 return handle;
81}
82
83static void __attribute__ ((constructor)) initialize(void)
84{
85 qcopt_handle = get_qcopt_handle();
86
87 if (!qcopt_handle) {
88 ALOGE("Failed to get qcopt handle.\n");
89 } else {
90 /*
91 * qc-opt handle obtained. Get the perflock acquire/release
92 * function pointers.
93 */
94 perf_lock_acq = dlsym(qcopt_handle, "perf_lock_acq");
95
96 if (!perf_lock_acq) {
97 ALOGE("Unable to get perf_lock_acq function handle.\n");
98 }
99
100 perf_lock_rel = dlsym(qcopt_handle, "perf_lock_rel");
101
102 if (!perf_lock_rel) {
103 ALOGE("Unable to get perf_lock_rel function handle.\n");
104 }
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -0700105
106 perf_hint = dlsym(qcopt_handle, "perf_hint");
107
108 if (!perf_hint) {
109 ALOGE("Unable to get perf_hint function handle.\n");
110 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800111 }
112}
113
114static void __attribute__ ((destructor)) cleanup(void)
115{
116 if (qcopt_handle) {
117 if (dlclose(qcopt_handle))
118 ALOGE("Error occurred while closing qc-opt library.");
119 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700120}
121
122int sysfs_read(char *path, char *s, int num_bytes)
123{
124 char buf[80];
125 int count;
126 int ret = 0;
127 int fd = open(path, O_RDONLY);
128
129 if (fd < 0) {
130 strerror_r(errno, buf, sizeof(buf));
131 ALOGE("Error opening %s: %s\n", path, buf);
132
133 return -1;
134 }
135
136 if ((count = read(fd, s, num_bytes - 1)) < 0) {
137 strerror_r(errno, buf, sizeof(buf));
138 ALOGE("Error writing to %s: %s\n", path, buf);
139
140 ret = -1;
141 } else {
142 s[count] = '\0';
143 }
144
145 close(fd);
146
147 return ret;
148}
149
150int sysfs_write(char *path, char *s)
151{
152 char buf[80];
153 int len;
154 int ret = 0;
155 int fd = open(path, O_WRONLY);
156
157 if (fd < 0) {
158 strerror_r(errno, buf, sizeof(buf));
159 ALOGE("Error opening %s: %s\n", path, buf);
160 return -1 ;
161 }
162
163 len = write(fd, s, strlen(s));
164 if (len < 0) {
165 strerror_r(errno, buf, sizeof(buf));
166 ALOGE("Error writing to %s: %s\n", path, buf);
167
168 ret = -1;
169 }
170
171 close(fd);
172
173 return ret;
174}
175
176int get_scaling_governor(char governor[], int size)
177{
178 if (sysfs_read(SCALING_GOVERNOR_PATH, governor,
179 size) == -1) {
180 // Can't obtain the scaling governor. Return.
181 return -1;
182 } else {
183 // Strip newline at the end.
184 int len = strlen(governor);
185
186 len--;
187
188 while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r'))
189 governor[len--] = '\0';
190 }
191
192 return 0;
193}
194
Sravan Kumar Ambapuram58387472015-05-20 22:22:54 +0530195int get_scaling_governor_check_cores(char governor[], int size,int core_num)
196{
197
198 if (sysfs_read(scaling_gov_path[core_num], governor,
199 size) == -1) {
200 // Can't obtain the scaling governor. Return.
201 return -1;
202 }
203
204 // Strip newline at the end.
205 int len = strlen(governor);
206 len--;
207 while (len >= 0 && (governor[len] == '\n' || governor[len] == '\r'))
208 governor[len--] = '\0';
209
210 return 0;
211}
212
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700213int is_interactive_governor(char* governor) {
214 if (strncmp(governor, INTERACTIVE_GOVERNOR, (strlen(INTERACTIVE_GOVERNOR)+1)) == 0)
215 return 1;
216 return 0;
217}
218
Michael Bestasfc62e7c2018-03-25 23:14:21 +0300219int is_ondemand_governor(char* governor) {
220 if (strncmp(governor, ONDEMAND_GOVERNOR, (strlen(ONDEMAND_GOVERNOR)+1)) == 0)
221 return 1;
222 return 0;
223}
224
225int is_msmdcvs_governor(char* governor) {
226 if (strncmp(governor, MSMDCVS_GOVERNOR, (strlen(MSMDCVS_GOVERNOR)+1)) == 0)
227 return 1;
228 return 0;
229}
230
Paul Keithd524aea2017-12-31 02:17:32 +0100231#ifndef INTERACTION_BOOST
232void interaction(int UNUSED(duration), int UNUSED(num_args), int UNUSED(opt_list[]))
233{
234#else
Vince Leung699c1bc2013-08-14 17:40:36 -0700235void interaction(int duration, int num_args, int opt_list[])
Vince Leung68574662013-07-16 16:47:00 -0700236{
Vince Leung68574662013-07-16 16:47:00 -0700237 static int lock_handle = 0;
Vince Leung699c1bc2013-08-14 17:40:36 -0700238
Paul Keithd524aea2017-12-31 02:17:32 +0100239 if (duration < 0 || num_args < 1 || opt_list[0] == 0)
Vince Leung699c1bc2013-08-14 17:40:36 -0700240 return;
241
Vince Leung68574662013-07-16 16:47:00 -0700242 if (qcopt_handle) {
243 if (perf_lock_acq) {
Vince Leung699c1bc2013-08-14 17:40:36 -0700244 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
Vince Leung68574662013-07-16 16:47:00 -0700245 if (lock_handle == -1)
246 ALOGE("Failed to acquire lock.");
247 }
248 }
249#endif
250}
251
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700252int interaction_with_handle(int lock_handle, int duration, int num_args, int opt_list[])
253{
Paul Keithd524aea2017-12-31 02:17:32 +0100254 if (duration < 0 || num_args < 1 || opt_list[0] == 0)
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700255 return 0;
256
257 if (qcopt_handle) {
258 if (perf_lock_acq) {
259 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
260 if (lock_handle == -1)
261 ALOGE("Failed to acquire lock.");
262 }
263 }
264 return lock_handle;
265}
266
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -0700267//this is interaction_with_handle using perf_hint instead of
268//perf_lock_acq
269int perf_hint_enable(int hint_id , int duration)
270{
271 int lock_handle = 0;
272
273 if (duration < 0)
274 return 0;
275
276 if (qcopt_handle) {
277 if (perf_hint) {
278 lock_handle = perf_hint(hint_id, NULL, duration, -1);
279 if (lock_handle == -1)
280 ALOGE("Failed to acquire lock.");
281 }
282 }
283 return lock_handle;
284}
285
286
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700287void release_request(int lock_handle) {
288 if (qcopt_handle && perf_lock_rel)
289 perf_lock_rel(lock_handle);
290}
291
Anurag Singh6ec12062012-10-02 09:59:01 -0700292void perform_hint_action(int hint_id, int resource_values[], int num_resources)
293{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800294 if (qcopt_handle) {
295 if (perf_lock_acq) {
296 /* Acquire an indefinite lock for the requested resources. */
297 int lock_handle = perf_lock_acq(0, 0, resource_values,
Anurag Singh6ec12062012-10-02 09:59:01 -0700298 num_resources);
299
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800300 if (lock_handle == -1) {
301 ALOGE("Failed to acquire lock.");
302 } else {
303 /* Add this handle to our internal hint-list. */
304 struct hint_data *new_hint =
305 (struct hint_data *)malloc(sizeof(struct hint_data));
Anurag Singh6ec12062012-10-02 09:59:01 -0700306
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800307 if (new_hint) {
308 if (!active_hint_list_head.compare) {
309 active_hint_list_head.compare =
310 (int (*)(void *, void *))hint_compare;
311 active_hint_list_head.dump = (void (*)(void *))hint_dump;
312 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700313
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800314 new_hint->hint_id = hint_id;
315 new_hint->perflock_handle = lock_handle;
Anurag Singh6ec12062012-10-02 09:59:01 -0700316
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800317 if (add_list_node(&active_hint_list_head, new_hint) == NULL) {
318 free(new_hint);
Anurag Singh6ec12062012-10-02 09:59:01 -0700319 /* Can't keep track of this lock. Release it. */
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800320 if (perf_lock_rel)
321 perf_lock_rel(lock_handle);
322
Anurag Singh6ec12062012-10-02 09:59:01 -0700323 ALOGE("Failed to process hint.");
324 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800325 } else {
326 /* Can't keep track of this lock. Release it. */
327 if (perf_lock_rel)
328 perf_lock_rel(lock_handle);
329
330 ALOGE("Failed to process hint.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700331 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700332 }
333 }
334 }
335}
336
337void undo_hint_action(int hint_id)
338{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800339 if (qcopt_handle) {
340 if (perf_lock_rel) {
341 /* Get hint-data associated with this hint-id */
342 struct list_node *found_node;
343 struct hint_data temp_hint_data = {
344 .hint_id = hint_id
345 };
Anurag Singh6ec12062012-10-02 09:59:01 -0700346
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800347 found_node = find_node(&active_hint_list_head,
348 &temp_hint_data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700349
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800350 if (found_node) {
351 /* Release this lock. */
352 struct hint_data *found_hint_data =
353 (struct hint_data *)(found_node->data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700354
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800355 if (found_hint_data) {
356 if (perf_lock_rel(found_hint_data->perflock_handle) == -1)
Anurag Singh6ec12062012-10-02 09:59:01 -0700357 ALOGE("Perflock release failed.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700358 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800359
360 if (found_node->data) {
361 /* We can free the hint-data for this node. */
362 free(found_node->data);
363 }
364
365 remove_list_node(&active_hint_list_head, found_node);
Anurag Singh6ec12062012-10-02 09:59:01 -0700366 } else {
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800367 ALOGE("Invalid hint ID.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700368 }
369 }
370 }
371}
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800372
373/*
374 * Used to release initial lock holding
375 * two cores online when the display is on
376 */
377void undo_initial_hint_action()
378{
379 if (qcopt_handle) {
380 if (perf_lock_rel) {
381 perf_lock_rel(1);
382 }
383 }
384}
Wei Wang5584ebb2016-06-07 18:44:31 -0700385
386long long calc_timespan_us(struct timespec start, struct timespec end)
387{
388 long long diff_in_us = 0;
389 diff_in_us += (end.tv_sec - start.tv_sec) * USINSEC;
390 diff_in_us += (end.tv_nsec - start.tv_nsec) / NSINUS;
391 return diff_in_us;
392}
Zhao Wei Liew6fe52522016-07-19 19:57:06 +0800393
394int get_soc_id(void)
395{
396 int fd;
397 int soc_id = -1;
398 char buf[10] = { 0 };
399
400 if (!access(SOC_ID_0, F_OK))
401 fd = open(SOC_ID_0, O_RDONLY);
402 else
403 fd = open(SOC_ID_1, O_RDONLY);
404
405 if (fd >= 0) {
406 if (read(fd, buf, sizeof(buf) - 1) == -1)
407 ALOGW("Unable to read soc_id");
408 else
409 soc_id = atoi(buf);
410 }
411
412 close(fd);
413 return soc_id;
414}