blob: 1e29c4a4c9b0807cf2ea8362571b206cbef3c2cb [file] [log] [blame]
David Ngee7c4c52018-03-22 23:49:12 -07001/*
Deevana Murthy Bandaruc6a9d5f2018-07-23 15:11:37 +05302 * Copyright (c) 2015,2018 The Linux Foundation. All rights reserved.
David Ngee7c4c52018-03-22 23:49:12 -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.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * 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 */
29
30#define LOG_NIDEBUG 0
31
32#include <errno.h>
33#include <string.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <fcntl.h>
37#include <dlfcn.h>
38#include <stdlib.h>
Deevana Murthy Bandaruc6a9d5f2018-07-23 15:11:37 +053039#include <unistd.h>
Deevana Murthy Bandaru9ff55b32018-08-09 12:26:52 +053040#include <pthread.h>
David Ngee7c4c52018-03-22 23:49:12 -070041
42#define LOG_TAG "QTI PowerHAL"
43#include <utils/Log.h>
44#include <hardware/hardware.h>
45#include <hardware/power.h>
46
47#include "utils.h"
48#include "metadata-defs.h"
49#include "hint-data.h"
50#include "performance.h"
51#include "power-common.h"
52
Deevana Murthy Bandaru9ff55b32018-08-09 12:26:52 +053053#define MIN_VAL(X,Y) ((X>Y)?(Y):(X))
54
David Ngee7c4c52018-03-22 23:49:12 -070055static int saved_interactive_mode = -1;
56static int display_hint_sent;
57static int video_encode_hint_sent;
Deevana Murthy Bandaru9ff55b32018-08-09 12:26:52 +053058pthread_mutex_t camera_hint_mutex = PTHREAD_MUTEX_INITIALIZER;
59static int camera_hint_ref_count;
David Ngee7c4c52018-03-22 23:49:12 -070060static void process_video_encode_hint(void *metadata);
Deevana Murthy Bandaruc6a9d5f2018-07-23 15:11:37 +053061static int display_fd;
62#define SYS_DISPLAY_PWR "/sys/kernel/hbtp/display_pwr"
David Ngee7c4c52018-03-22 23:49:12 -070063
Deevana Murthy Bandaru9ff55b32018-08-09 12:26:52 +053064static bool is_target_SDM439() /* Returns value=1 if target is Hathi else value 0 */
65{
66 int fd;
67 bool is_target_SDM439 = false;
68 char buf[10] = {0};
69 fd = open("/sys/devices/soc0/soc_id", O_RDONLY);
70 if (fd >= 0) {
71 if (read(fd, buf, sizeof(buf) - 1) == -1) {
72 ALOGW("Unable to read soc_id");
73 is_target_SDM439 = false;
74 } else {
75 int soc_id = atoi(buf);
76 if (soc_id == 353 || soc_id == 363 || soc_id == 354 || soc_id == 364) {
77 is_target_SDM439 = true; /* Above SOCID for SDM439/429 */
78 }
79 }
80 }
81 close(fd);
82 return is_target_SDM439;
83}
84
David Ngee7c4c52018-03-22 23:49:12 -070085int power_hint_override(struct power_module *module, power_hint_t hint,
86 void *data)
87{
88
89 switch(hint) {
90 case POWER_HINT_VSYNC:
91 break;
92 case POWER_HINT_VIDEO_ENCODE:
93 {
94 process_video_encode_hint(data);
95 return HINT_HANDLED;
96 }
97 }
98 return HINT_NONE;
99}
100
101int set_interactive_override(struct power_module *module, int on)
102{
103 char governor[80];
104 char tmp_str[NODE_MAX];
105 struct video_encode_metadata_t video_encode_metadata;
Deevana Murthy Bandaruc6a9d5f2018-07-23 15:11:37 +0530106 int rc = 0;
107
108 static const char *display_on = "1";
109 static const char *display_off = "0";
110 char err_buf[80];
111 static int init_interactive_hint = 0;
112 static int set_i_count = 0;
David Ngee7c4c52018-03-22 23:49:12 -0700113
114 ALOGI("Got set_interactive hint");
115
116 if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU0) == -1) {
117 if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU1) == -1) {
118 if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU2) == -1) {
119 if (get_scaling_governor_check_cores(governor, sizeof(governor),CPU3) == -1) {
120 ALOGE("Can't obtain scaling governor.");
121 return HINT_HANDLED;
122 }
123 }
124 }
125 }
126
127 if (!on) {
128 /* Display off. */
129 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
130 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
131 int resource_values[] = {INT_OP_CLUSTER0_TIMER_RATE, BIG_LITTLE_TR_MS_50,
132 INT_OP_CLUSTER1_TIMER_RATE, BIG_LITTLE_TR_MS_50,
133 INT_OP_NOTIFY_ON_MIGRATE, 0x00};
134
135 if (!display_hint_sent) {
136 perform_hint_action(DISPLAY_STATE_HINT_ID,
137 resource_values, sizeof(resource_values)/sizeof(resource_values[0]));
138 display_hint_sent = 1;
139 }
140 } /* Perf time rate set for CORE0,CORE4 8952 target*/
141
142 } else {
143 /* Display on. */
144 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
145 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
146
147 undo_hint_action(DISPLAY_STATE_HINT_ID);
148 display_hint_sent = 0;
149 }
150 }
151 saved_interactive_mode = !!on;
Deevana Murthy Bandaruc6a9d5f2018-07-23 15:11:37 +0530152
153 set_i_count ++;
154 ALOGI("Got set_interactive hint on= %d, count= %d\n", on, set_i_count);
155
156 if (init_interactive_hint == 0)
157 {
158 //First time the display is turned off
159 display_fd = TEMP_FAILURE_RETRY(open(SYS_DISPLAY_PWR, O_RDWR));
160 if (display_fd < 0) {
161 strerror_r(errno,err_buf,sizeof(err_buf));
162 ALOGE("Error opening %s: %s\n", SYS_DISPLAY_PWR, err_buf);
163 return HINT_HANDLED;
164 }
165 else
166 init_interactive_hint = 1;
167 }
168 else
169 if (!on ) {
170 /* Display off. */
171 rc = TEMP_FAILURE_RETRY(write(display_fd, display_off, strlen(display_off)));
172 if (rc < 0) {
173 strerror_r(errno,err_buf,sizeof(err_buf));
174 ALOGE("Error writing %s to %s: %s\n", display_off, SYS_DISPLAY_PWR, err_buf);
175 }
176 }
177 else {
178 /* Display on */
179 rc = TEMP_FAILURE_RETRY(write(display_fd, display_on, strlen(display_on)));
180 if (rc < 0) {
181 strerror_r(errno,err_buf,sizeof(err_buf));
182 ALOGE("Error writing %s to %s: %s\n", display_on, SYS_DISPLAY_PWR, err_buf);
183 }
184 }
185
David Ngee7c4c52018-03-22 23:49:12 -0700186 return HINT_HANDLED;
187}
188
189/* Video Encode Hint */
190static void process_video_encode_hint(void *metadata)
191{
Deevana Murthy Bandaru9ff55b32018-08-09 12:26:52 +0530192 char governor[80] = {0};
193 int resource_values[20] = {0};
194 int num_resources = 0;
David Ngee7c4c52018-03-22 23:49:12 -0700195 struct video_encode_metadata_t video_encode_metadata;
196
197 ALOGI("Got process_video_encode_hint");
198
199 if (get_scaling_governor_check_cores(governor,
200 sizeof(governor),CPU0) == -1) {
201 if (get_scaling_governor_check_cores(governor,
202 sizeof(governor),CPU1) == -1) {
203 if (get_scaling_governor_check_cores(governor,
204 sizeof(governor),CPU2) == -1) {
205 if (get_scaling_governor_check_cores(governor,
206 sizeof(governor),CPU3) == -1) {
207 ALOGE("Can't obtain scaling governor.");
208 return;
209 }
210 }
211 }
212 }
213
214 /* Initialize encode metadata struct fields. */
215 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
216 video_encode_metadata.state = -1;
217 video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID;
218
219 if (metadata) {
220 if (parse_video_encode_metadata((char *)metadata,
221 &video_encode_metadata) == -1) {
222 ALOGE("Error occurred while parsing metadata.");
223 return;
224 }
225 } else {
226 return;
227 }
228
229 if (video_encode_metadata.state == 1) {
Deevana Murthy Bandaru9ff55b32018-08-09 12:26:52 +0530230 if((strncmp(governor, SCHEDUTIL_GOVERNOR,
231 strlen(SCHEDUTIL_GOVERNOR)) == 0) &&
232 (strlen(governor) == strlen(SCHEDUTIL_GOVERNOR))) {
233 if(is_target_SDM439()) {
234 /* sample_ms = 10mS
235 * SLB for Core0 = -6
236 * SLB for Core1 = -6
237 * SLB for Core2 = -6
238 * SLB for Core3 = -6
239 * hispeed load = 95
240 * hispeed freq = 998Mhz */
241 int res[] = {0x41820000, 0xa,
242 0x40c68100, 0xfffffffa,
243 0x40c68110, 0xfffffffa,
244 0x40c68120, 0xfffffffa,
245 0x40c68130, 0xfffffffa,
246 0x41440100, 0x5f,
247 0x4143c100, 0x3e6,
248 };
249 memcpy(resource_values, res, MIN_VAL(sizeof(resource_values), sizeof(res)));
250 num_resources = sizeof(res)/sizeof(res[0]);
251 pthread_mutex_lock(&camera_hint_mutex);
252 camera_hint_ref_count++;
253 if (camera_hint_ref_count == 1) {
254 if (!video_encode_hint_sent) {
255 perform_hint_action(video_encode_metadata.hint_id,
256 resource_values, num_resources);
257 video_encode_hint_sent = 1;
258 }
259 }
260 pthread_mutex_unlock(&camera_hint_mutex);
261 }
262 else {
263 /* sample_ms = 10mS */
264 int res[] = {0x41820000, 0xa,
265 };
266 memcpy(resource_values, res, MIN_VAL(sizeof(resource_values), sizeof(res)));
267 num_resources = sizeof(res)/sizeof(res[0]);
268 pthread_mutex_lock(&camera_hint_mutex);
269 camera_hint_ref_count++;
270 if (camera_hint_ref_count == 1) {
271 if (!video_encode_hint_sent) {
272 perform_hint_action(video_encode_metadata.hint_id,
273 resource_values, num_resources);
274 video_encode_hint_sent = 1;
275 }
276 }
277 pthread_mutex_unlock(&camera_hint_mutex);
David Ngee7c4c52018-03-22 23:49:12 -0700278 }
279 }
Deevana Murthy Bandaru9ff55b32018-08-09 12:26:52 +0530280 else if ((strncmp(governor, INTERACTIVE_GOVERNOR,
David Ngee7c4c52018-03-22 23:49:12 -0700281 strlen(INTERACTIVE_GOVERNOR)) == 0) &&
282 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
Deevana Murthy Bandaru9ff55b32018-08-09 12:26:52 +0530283 /* Sched_load and migration_notif*/
284 int res[] = {INT_OP_CLUSTER0_USE_SCHED_LOAD,
285 0x1,
286 INT_OP_CLUSTER1_USE_SCHED_LOAD,
287 0x1,
288 INT_OP_CLUSTER0_USE_MIGRATION_NOTIF,
289 0x1,
290 INT_OP_CLUSTER1_USE_MIGRATION_NOTIF,
291 0x1,
292 INT_OP_CLUSTER0_TIMER_RATE,
293 BIG_LITTLE_TR_MS_40,
294 INT_OP_CLUSTER1_TIMER_RATE,
295 BIG_LITTLE_TR_MS_40
296 };
297 memcpy(resource_values, res, MIN_VAL(sizeof(resource_values), sizeof(res)));
298 num_resources = sizeof(res)/sizeof(res[0]);
299 pthread_mutex_lock(&camera_hint_mutex);
300 camera_hint_ref_count++;
301 if (!video_encode_hint_sent) {
302 perform_hint_action(video_encode_metadata.hint_id,
303 resource_values,num_resources);
304 video_encode_hint_sent = 1;
305 }
306 pthread_mutex_unlock(&camera_hint_mutex);
307 }
308 } else if (video_encode_metadata.state == 0) {
309 if (((strncmp(governor, INTERACTIVE_GOVERNOR,
310 strlen(INTERACTIVE_GOVERNOR)) == 0) &&
311 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) ||
312 ((strncmp(governor, SCHEDUTIL_GOVERNOR,
313 strlen(SCHEDUTIL_GOVERNOR)) == 0) &&
314 (strlen(governor) == strlen(SCHEDUTIL_GOVERNOR)))) {
315 pthread_mutex_lock(&camera_hint_mutex);
316 camera_hint_ref_count--;
317 if (!camera_hint_ref_count) {
318 undo_hint_action(video_encode_metadata.hint_id);
319 video_encode_hint_sent = 0;
320 }
321 pthread_mutex_unlock(&camera_hint_mutex);
David Ngee7c4c52018-03-22 23:49:12 -0700322 return ;
323 }
324 }
325 return;
326}
327