blob: 5f950a1a40255c5fc1a862d49a8d8932a255df16 [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
Paul Keithd524aea2017-12-31 02:17:32 +0100219#ifndef INTERACTION_BOOST
220void interaction(int UNUSED(duration), int UNUSED(num_args), int UNUSED(opt_list[]))
221{
222#else
Vince Leung699c1bc2013-08-14 17:40:36 -0700223void interaction(int duration, int num_args, int opt_list[])
Vince Leung68574662013-07-16 16:47:00 -0700224{
Vince Leung68574662013-07-16 16:47:00 -0700225 static int lock_handle = 0;
Vince Leung699c1bc2013-08-14 17:40:36 -0700226
Paul Keithd524aea2017-12-31 02:17:32 +0100227 if (duration < 0 || num_args < 1 || opt_list[0] == 0)
Vince Leung699c1bc2013-08-14 17:40:36 -0700228 return;
229
Vince Leung68574662013-07-16 16:47:00 -0700230 if (qcopt_handle) {
231 if (perf_lock_acq) {
Vince Leung699c1bc2013-08-14 17:40:36 -0700232 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
Vince Leung68574662013-07-16 16:47:00 -0700233 if (lock_handle == -1)
234 ALOGE("Failed to acquire lock.");
235 }
236 }
237#endif
238}
239
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700240int interaction_with_handle(int lock_handle, int duration, int num_args, int opt_list[])
241{
Paul Keithd524aea2017-12-31 02:17:32 +0100242 if (duration < 0 || num_args < 1 || opt_list[0] == 0)
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700243 return 0;
244
245 if (qcopt_handle) {
246 if (perf_lock_acq) {
247 lock_handle = perf_lock_acq(lock_handle, duration, opt_list, num_args);
248 if (lock_handle == -1)
249 ALOGE("Failed to acquire lock.");
250 }
251 }
252 return lock_handle;
253}
254
Ananth Raghavan Subramanian0c226b72017-06-27 21:28:43 -0700255//this is interaction_with_handle using perf_hint instead of
256//perf_lock_acq
257int perf_hint_enable(int hint_id , int duration)
258{
259 int lock_handle = 0;
260
261 if (duration < 0)
262 return 0;
263
264 if (qcopt_handle) {
265 if (perf_hint) {
266 lock_handle = perf_hint(hint_id, NULL, duration, -1);
267 if (lock_handle == -1)
268 ALOGE("Failed to acquire lock.");
269 }
270 }
271 return lock_handle;
272}
273
274
Ananth Raghavan Subramaniane432dbf2017-03-24 16:30:12 -0700275void release_request(int lock_handle) {
276 if (qcopt_handle && perf_lock_rel)
277 perf_lock_rel(lock_handle);
278}
279
Anurag Singh6ec12062012-10-02 09:59:01 -0700280void perform_hint_action(int hint_id, int resource_values[], int num_resources)
281{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800282 if (qcopt_handle) {
283 if (perf_lock_acq) {
284 /* Acquire an indefinite lock for the requested resources. */
285 int lock_handle = perf_lock_acq(0, 0, resource_values,
Anurag Singh6ec12062012-10-02 09:59:01 -0700286 num_resources);
287
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800288 if (lock_handle == -1) {
289 ALOGE("Failed to acquire lock.");
290 } else {
291 /* Add this handle to our internal hint-list. */
292 struct hint_data *new_hint =
293 (struct hint_data *)malloc(sizeof(struct hint_data));
Anurag Singh6ec12062012-10-02 09:59:01 -0700294
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800295 if (new_hint) {
296 if (!active_hint_list_head.compare) {
297 active_hint_list_head.compare =
298 (int (*)(void *, void *))hint_compare;
299 active_hint_list_head.dump = (void (*)(void *))hint_dump;
300 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700301
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800302 new_hint->hint_id = hint_id;
303 new_hint->perflock_handle = lock_handle;
Anurag Singh6ec12062012-10-02 09:59:01 -0700304
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800305 if (add_list_node(&active_hint_list_head, new_hint) == NULL) {
306 free(new_hint);
Anurag Singh6ec12062012-10-02 09:59:01 -0700307 /* Can't keep track of this lock. Release it. */
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800308 if (perf_lock_rel)
309 perf_lock_rel(lock_handle);
310
Anurag Singh6ec12062012-10-02 09:59:01 -0700311 ALOGE("Failed to process hint.");
312 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800313 } else {
314 /* Can't keep track of this lock. Release it. */
315 if (perf_lock_rel)
316 perf_lock_rel(lock_handle);
317
318 ALOGE("Failed to process hint.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700319 }
Anurag Singh6ec12062012-10-02 09:59:01 -0700320 }
321 }
322 }
323}
324
325void undo_hint_action(int hint_id)
326{
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800327 if (qcopt_handle) {
328 if (perf_lock_rel) {
329 /* Get hint-data associated with this hint-id */
330 struct list_node *found_node;
331 struct hint_data temp_hint_data = {
332 .hint_id = hint_id
333 };
Anurag Singh6ec12062012-10-02 09:59:01 -0700334
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800335 found_node = find_node(&active_hint_list_head,
336 &temp_hint_data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700337
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800338 if (found_node) {
339 /* Release this lock. */
340 struct hint_data *found_hint_data =
341 (struct hint_data *)(found_node->data);
Anurag Singh6ec12062012-10-02 09:59:01 -0700342
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800343 if (found_hint_data) {
344 if (perf_lock_rel(found_hint_data->perflock_handle) == -1)
Anurag Singh6ec12062012-10-02 09:59:01 -0700345 ALOGE("Perflock release failed.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700346 }
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800347
348 if (found_node->data) {
349 /* We can free the hint-data for this node. */
350 free(found_node->data);
351 }
352
353 remove_list_node(&active_hint_list_head, found_node);
Anurag Singh6ec12062012-10-02 09:59:01 -0700354 } else {
Anurag Singhfe93c1d2012-11-21 15:15:56 -0800355 ALOGE("Invalid hint ID.");
Anurag Singh6ec12062012-10-02 09:59:01 -0700356 }
357 }
358 }
359}
Zhoulu Luo3b1fbdb2013-12-06 16:32:10 -0800360
361/*
362 * Used to release initial lock holding
363 * two cores online when the display is on
364 */
365void undo_initial_hint_action()
366{
367 if (qcopt_handle) {
368 if (perf_lock_rel) {
369 perf_lock_rel(1);
370 }
371 }
372}
Wei Wang5584ebb2016-06-07 18:44:31 -0700373
374long long calc_timespan_us(struct timespec start, struct timespec end)
375{
376 long long diff_in_us = 0;
377 diff_in_us += (end.tv_sec - start.tv_sec) * USINSEC;
378 diff_in_us += (end.tv_nsec - start.tv_nsec) / NSINUS;
379 return diff_in_us;
380}
Zhao Wei Liew6fe52522016-07-19 19:57:06 +0800381
382int get_soc_id(void)
383{
384 int fd;
385 int soc_id = -1;
386 char buf[10] = { 0 };
387
388 if (!access(SOC_ID_0, F_OK))
389 fd = open(SOC_ID_0, O_RDONLY);
390 else
391 fd = open(SOC_ID_1, O_RDONLY);
392
393 if (fd >= 0) {
394 if (read(fd, buf, sizeof(buf) - 1) == -1)
395 ALOGW("Unable to read soc_id");
396 else
397 soc_id = atoi(buf);
398 }
399
400 close(fd);
401 return soc_id;
402}