1 /*
2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3 *
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 #define LOG_NIDEBUG 0
30
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <dlfcn.h>
37 #include <stdlib.h>
38
39 #define LOG_TAG "QCOM PowerHAL"
40 #include <utils/Log.h>
41 #include <hardware/hardware.h>
42 #include <hardware/power.h>
43
44 #include "utils.h"
45 #include "metadata-defs.h"
46 #include "hint-data.h"
47 #include "performance.h"
48 #include "power-common.h"
49
50 static int display_hint_sent;
51 int launch_handle = -1;
52 int launch_mode;
53
54 #ifdef EXTRA_POWERHAL_HINTS
process_cam_preview_hint(void * metadata)55 static int process_cam_preview_hint(void *metadata)
56 {
57 char governor[80];
58 struct cam_preview_metadata_t cam_preview_metadata;
59
60 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
61 ALOGE("Can't obtain scaling governor.");
62
63 return HINT_NONE;
64 }
65
66 /* Initialize encode metadata struct fields */
67 memset(&cam_preview_metadata, 0, sizeof(struct cam_preview_metadata_t));
68 cam_preview_metadata.state = -1;
69 cam_preview_metadata.hint_id = CAM_PREVIEW_HINT_ID;
70
71 if (metadata) {
72 if (parse_cam_preview_metadata((char *)metadata, &cam_preview_metadata) ==
73 -1) {
74 ALOGE("Error occurred while parsing metadata.");
75 return HINT_NONE;
76 }
77 } else {
78 return HINT_NONE;
79 }
80
81 if (cam_preview_metadata.state == 1) {
82 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
83 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
84 /* sched and cpufreq params
85 * above_hispeed_delay for LVT - 40ms
86 * go hispeed load for LVT - 95
87 * hispeed freq for LVT - 556 MHz
88 * target load for LVT - 90
89 * above hispeed delay for sLVT - 40ms
90 * go hispeed load for sLVT - 95
91 * hispeed freq for sLVT - 556 MHz
92 * target load for sLVT - 90
93 * bus DCVS set to V2 config:
94 * low power ceil mpbs - 2500
95 * low power io percent - 50
96 */
97 int resource_values[] = {0x41400000, 0x4, 0x41410000, 0x5F, 0x41414000, 0x22C,
98 0x41420000, 0x5A, 0x41400100, 0x4, 0x41410100, 0x5F, 0x41414100, 0x22C,
99 0x41420100, 0x5A, 0x41810000, 0x9C4, 0x41814000, 0x32};
100
101 perform_hint_action(cam_preview_metadata.hint_id,
102 resource_values, sizeof(resource_values)/sizeof(resource_values[0]));
103 ALOGI("Cam Preview hint start");
104 return HINT_HANDLED;
105 } else if ((strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) &&
106 (strlen(governor) == strlen(SCHED_GOVERNOR))) {
107 /*
108 * lower bus BW to save power
109 * 0x41810000: low power ceil mpbs = 2500
110 * 0x41814000: low power io percent = 50
111 */
112 int resource_values[] = {0x41810000, 0x9C4, 0x41814000, 0x32};
113
114 perform_hint_action(
115 cam_preview_metadata.hint_id, resource_values,
116 sizeof(resource_values) / sizeof(resource_values[0]));
117 ALOGI("Cam Preview hint start");
118 return HINT_HANDLED;
119 }
120 } else if (cam_preview_metadata.state == 0) {
121 if (((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
122 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) ||
123 ((strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) &&
124 (strlen(governor) == strlen(SCHED_GOVERNOR)))) {
125 undo_hint_action(cam_preview_metadata.hint_id);
126 ALOGI("Cam Preview hint stop");
127 return HINT_HANDLED;
128 }
129 }
130 return HINT_NONE;
131 }
132 #endif
133
process_boost(int boost_handle,int duration)134 static int process_boost(int boost_handle, int duration)
135 {
136 char governor[80];
137 int eas_launch_resources[] = {0x40804000, 0xFFF, 0x40804100, 0xFFF,
138 0x40800000, 0xFFF, 0x40800100, 0xFFF,
139 0x41800000, 140, 0x40400000, 0x1};
140 int hmp_launch_resources[] = {0x40C00000, 0x1, 0x40804000, 0xFFF,
141 0x40804100, 0xFFF, 0x40800000, 0xFFF,
142 0x40800100, 0xFFF, 0x41800000, 140,
143 0x40400000, 0x1};
144 int* launch_resources;
145 size_t launch_resources_size;
146
147 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
148 ALOGE("Can't obtain scaling governor.");
149 return -1;
150 }
151 if (strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) {
152 launch_resources = eas_launch_resources;
153 launch_resources_size = sizeof(eas_launch_resources) / sizeof(eas_launch_resources[0]);
154 } else if (strncmp(governor, INTERACTIVE_GOVERNOR,
155 strlen(INTERACTIVE_GOVERNOR)) == 0) { /*HMP boost*/
156 launch_resources = hmp_launch_resources;
157 launch_resources_size = sizeof(hmp_launch_resources) / sizeof(hmp_launch_resources[0]);
158 }
159 boost_handle = interaction_with_handle(
160 boost_handle, duration, launch_resources_size, launch_resources);
161 return boost_handle;
162 }
163
process_video_encode_hint(void * metadata)164 static int process_video_encode_hint(void *metadata)
165 {
166 char governor[80];
167 struct video_encode_metadata_t video_encode_metadata;
168 static int boost_handle = -1;
169
170 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
171 ALOGE("Can't obtain scaling governor.");
172
173 return HINT_NONE;
174 }
175
176 /* Initialize encode metadata struct fields */
177 memset(&video_encode_metadata, 0, sizeof(struct video_encode_metadata_t));
178 video_encode_metadata.state = -1;
179 video_encode_metadata.hint_id = DEFAULT_VIDEO_ENCODE_HINT_ID;
180
181 if (metadata) {
182 if (parse_video_encode_metadata((char *)metadata, &video_encode_metadata) ==
183 -1) {
184 ALOGE("Error occurred while parsing metadata.");
185 return HINT_NONE;
186 }
187 } else {
188 return HINT_NONE;
189 }
190
191 if (video_encode_metadata.state == 1) {
192 int duration = 2000; // boosts 2s for starting encoding
193 boost_handle = process_boost(boost_handle, duration);
194 ALOGD("LAUNCH ENCODER-ON: %d MS", duration);
195 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
196 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
197 /* 1. cpufreq params
198 * -above_hispeed_delay for LVT - 40ms
199 * -go hispeed load for LVT - 95
200 * -hispeed freq for LVT - 556 MHz
201 * -target load for LVT - 90
202 * -above hispeed delay for sLVT - 40ms
203 * -go hispeed load for sLVT - 95
204 * -hispeed freq for sLVT - 806 MHz
205 * -target load for sLVT - 90
206 * 2. bus DCVS set to V2 config:
207 * -low power ceil mpbs - 2500
208 * -low power io percent - 50
209 * 3. hysteresis optimization
210 * -bus dcvs hysteresis tuning
211 * -sample_ms of 10 ms
212 * -disable ignore_hispeed_notif
213 * -sLVT hispeed freq to 806MHz
214 */
215 int resource_values[] = {
216 0x41810000, 0x9C4, 0x41814000, 0x32, 0x4180C000, 0x0, 0x41820000, 0xA,
217 0x41438100, 0x1, 0x41438000, 0x1 };
218
219 perform_hint_action(video_encode_metadata.hint_id,
220 resource_values, sizeof(resource_values)/sizeof(resource_values[0]));
221 ALOGI("Video Encode hint start");
222 return HINT_HANDLED;
223 } else if ((strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) &&
224 (strlen(governor) == strlen(SCHED_GOVERNOR))) {
225
226 /* 1. bus DCVS set to V2 config:
227 * 0x41810000: low power ceil mpbs - 2500
228 * 0x41814000: low power io percent - 50
229 * 2. hysteresis optimization
230 * 0x4180C000: bus dcvs hysteresis tuning
231 * 0x41820000: sample_ms of 10 ms
232 */
233 int resource_values[] = {0x41810000, 0x9C4, 0x41814000, 0x32,
234 0x4180C000, 0x0, 0x41820000, 0xA};
235
236 perform_hint_action(video_encode_metadata.hint_id,
237 resource_values, sizeof(resource_values)/sizeof(resource_values[0]));
238 ALOGI("Video Encode hint start");
239 return HINT_HANDLED;
240 }
241 } else if (video_encode_metadata.state == 0) {
242 // boost handle is intentionally not released, release_request(boost_handle);
243 if (((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
244 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) ||
245 ((strncmp(governor, SCHED_GOVERNOR, strlen(SCHED_GOVERNOR)) == 0) &&
246 (strlen(governor) == strlen(SCHED_GOVERNOR)))) {
247 undo_hint_action(video_encode_metadata.hint_id);
248 ALOGI("Video Encode hint stop");
249 return HINT_HANDLED;
250 }
251 }
252 return HINT_NONE;
253 }
254
process_activity_launch_hint(void * data)255 static int process_activity_launch_hint(void *data)
256 {
257 // boost will timeout in 5s
258 int duration = 5000;
259 if (sustained_performance_mode || vr_mode) {
260 return HINT_HANDLED;
261 }
262
263 ALOGD("LAUNCH HINT: %s", data ? "ON" : "OFF");
264 if (data && launch_mode == 0) {
265 launch_handle = process_boost(launch_handle, duration);
266 if (launch_handle > 0) {
267 launch_mode = 1;
268 ALOGI("Activity launch hint handled");
269 return HINT_HANDLED;
270 } else {
271 return HINT_NONE;
272 }
273 } else if (data == NULL && launch_mode == 1) {
274 release_request(launch_handle);
275 launch_mode = 0;
276 return HINT_HANDLED;
277 }
278 return HINT_NONE;
279 }
280
power_hint_override(struct power_module * module,power_hint_t hint,void * data)281 int power_hint_override(struct power_module *module, power_hint_t hint, void *data)
282 {
283 int ret_val = HINT_NONE;
284 switch(hint) {
285 #ifdef EXTRA_POWERHAL_HINTS
286 case POWER_HINT_CAM_PREVIEW:
287 ret_val = process_cam_preview_hint(data);
288 break;
289 #endif
290 case POWER_HINT_VIDEO_ENCODE:
291 ret_val = process_video_encode_hint(data);
292 break;
293 case POWER_HINT_LAUNCH:
294 ret_val = process_activity_launch_hint(data);
295 break;
296 default:
297 break;
298 }
299 return ret_val;
300 }
301
set_interactive_override(struct power_module * module,int on)302 int set_interactive_override(struct power_module *module, int on)
303 {
304 return HINT_HANDLED; /* Don't excecute this code path, not in use */
305 char governor[80];
306
307 if (get_scaling_governor(governor, sizeof(governor)) == -1) {
308 ALOGE("Can't obtain scaling governor.");
309
310 return HINT_NONE;
311 }
312
313 if (!on) {
314 /* Display off */
315 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
316 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
317 int resource_values[] = {}; /* dummy node */
318 if (!display_hint_sent) {
319 perform_hint_action(DISPLAY_STATE_HINT_ID,
320 resource_values, sizeof(resource_values)/sizeof(resource_values[0]));
321 display_hint_sent = 1;
322 ALOGI("Display Off hint start");
323 return HINT_HANDLED;
324 }
325 }
326 } else {
327 /* Display on */
328 if ((strncmp(governor, INTERACTIVE_GOVERNOR, strlen(INTERACTIVE_GOVERNOR)) == 0) &&
329 (strlen(governor) == strlen(INTERACTIVE_GOVERNOR))) {
330 undo_hint_action(DISPLAY_STATE_HINT_ID);
331 display_hint_sent = 0;
332 ALOGI("Display Off hint stop");
333 return HINT_HANDLED;
334 }
335 }
336 return HINT_NONE;
337 }
338