blob: 87fd4bfb28dc9068715c9f3206a517c0a2690207 [file] [log] [blame]
Sathish Ambley519f68e2017-07-19 10:26:51 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "VrHALImpl"
18
19#include <cutils/log.h>
20
21#include <dlfcn.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
29#include <hardware/vr.h>
30#include <hardware/hardware.h>
31
Roopesh Rajashekharaiah Nataraja5d88d4e2018-03-20 18:08:06 -070032#include <thermal_client.h>
Sathish Ambley519f68e2017-07-19 10:26:51 -070033#include "vr_int.h"
34
35
36static void *dlhandle;
37static int (*p_thermal_client_config_query)(char *, struct config_instance **);
38static int (*p_thermal_client_config_set)(struct config_instance *, unsigned int);
39static void (*p_thermal_client_config_cleanup)(struct config_instance *, unsigned int);
40
41static int max_string_size = 36;
42static int error_state = 0; //global error state - don't do anything if set!
43
Rohit Bandi2df5b732017-09-18 12:37:11 -070044#define DEBUG 0
Sathish Ambley519f68e2017-07-19 10:26:51 -070045
46static thermal_cfg_info_t non_vr_thermal_cfgs;
47static thermal_cfg_info_t vr_thermal_cfgs;
48
49int __attribute__ ((weak)) load_thermal_cfg_info(thermal_cfg_info_t *non_vr,
50 thermal_cfg_info_t *vr) {
Rohit Bandi2df5b732017-09-18 12:37:11 -070051 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -070052 non_vr->num_cfgs = 0;
53 vr->num_cfgs = 0;
54
55 return 0;
56}
57
58/**
59 * Debug function for printing out a log instance
60 */
61static void log_config_instance(struct config_instance *instance ){
62 ALOGI("logging config_instance 0x%p", instance);
63 ALOGI("config_instance: cfg_desc = %s", instance->cfg_desc);
64 ALOGI("config_instance: algo_type = %s", instance->algo_type);
65 ALOGI("config_instance: fields_mask = 0x%x", instance->fields_mask);
66 ALOGI("config_instance: num_fields = %u", instance->num_fields);
67 for (uint32_t i = 0; i < instance->num_fields; i++) {
68 ALOGI("config_instance: field_data[%d]", i);
69 ALOGI("\tfield_name = %s", instance->fields[i].field_name);
70 ALOGI("\tdata_type = %u", instance->fields[i].data_type);
71 ALOGI("\tnum_data = %u", instance->fields[i].num_data);
72 switch (instance->fields[i].data_type){
73 case FIELD_INT: ALOGI("\tdata = %d", *(int*)(instance->fields[i].data));
74 break;
75 case FIELD_STR: ALOGI("\tdata = %s", (char*)(instance->fields[i].data));
76 break;
77 default: ALOGI("\tdata = 0x%p", instance->fields[i].data);
78 break;
79 }
80 }
81}
82
83/**
84 * Debug function for printing out all instances of "ss" and "monitor" algos
85 */
86static void query_thermal_config(){
Rohit Bandi2df5b732017-09-18 12:37:11 -070087 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -070088 struct config_instance *instances;
89
90 int num_configs = (*p_thermal_client_config_query)("ss", &instances);
91 if (num_configs <= 0) {
92 return;
93 }
94 for (int i = 0; i < num_configs; i++) {
95 log_config_instance(&(instances[i]));
96 }
97 if (num_configs > 0) {
98 (*p_thermal_client_config_cleanup)(instances, num_configs);
99 }
100
101 num_configs = (*p_thermal_client_config_query)("monitor", &instances);
102 if (num_configs <= 0) {
103 return;
104 }
105 for (int i = 0; i < num_configs; i++) {
106 log_config_instance(&(instances[i]));
107 }
108 if (num_configs > 0) {
109 (*p_thermal_client_config_cleanup)(instances, num_configs);
110 }
111}
112
113/**
114 * Load the thermal client library
115 * returns 0 on success
116 */
Rohit Bandi2df5b732017-09-18 12:37:11 -0700117static int load_thermal_client(void){
118 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -0700119 char *thermal_client_so = "vendor/lib64/libthermalclient.so";
120
121 dlhandle = dlopen(thermal_client_so, RTLD_NOW | RTLD_LOCAL);
122 if (dlhandle) {
123 dlerror();
124 p_thermal_client_config_query = (int (*) (char *, struct config_instance **))
125 dlsym(dlhandle, "thermal_client_config_query");
126 if (dlerror()) {
127 ALOGE("Unable to load thermal_client_config_query");
128 goto error_handle;
129 }
130
131 p_thermal_client_config_set = (int (*) (struct config_instance *, unsigned int))
132 dlsym(dlhandle, "thermal_client_config_set");
133 if (dlerror()) {
134 ALOGE("Unable to load thermal_client_config_set");
135 goto error_handle;
136 }
137
138 p_thermal_client_config_cleanup = (void (*) (struct config_instance *, unsigned int))
139 dlsym(dlhandle, "thermal_client_config_cleanup");
140 if (dlerror()) {
141 ALOGE("Unable to load thermal_client_config_cleanup");
142 goto error_handle;
143 }
144 } else {
145 ALOGE("unable to open %s", thermal_client_so);
146 return -1;
147 }
148 return 0;
149
150error_handle:
151 ALOGE("Error opening functions from %s", thermal_client_so);
152 p_thermal_client_config_query = NULL;
153 p_thermal_client_config_set = NULL;
154 p_thermal_client_config_cleanup = NULL;
155 dlclose(dlhandle);
156 dlhandle = NULL;
157 return -1;
158}
159
160/**
Sathish Ambley519f68e2017-07-19 10:26:51 -0700161 * Free the config_instance as allocated in allocate_config_instance
162 */
163static void free_config_instance(struct config_instance *config){
164
Rohit Bandi2df5b732017-09-18 12:37:11 -0700165 if (config) {
166 if (config->fields) {
167 free(config->fields[0].data);
168 free(config->fields[0].field_name);
169 free(config->fields);
170 }
171 free(config->algo_type);
172 free(config->cfg_desc);
173 free(config);
174 }
175}
176
177/**
178 * Allocate a new struct config_instance for modifying the disable field
179 */
180static struct config_instance *allocate_config_instance(){
181 ALOGD("Entering %s",__func__);
182 struct config_instance *config = (struct config_instance *)malloc(sizeof(struct config_instance));
183 if (!config) {
184 ALOGE("Unable to allocate memory for config");
185 return NULL;
186 }
187 memset(config, 0, sizeof(*config));
188
189 config->cfg_desc = (char *)malloc(sizeof(char) * max_string_size);
190 if (!config->cfg_desc) {
191 free_config_instance(config);
192 ALOGE("Unable to allocate memory for config->cfg_desc");
193 return NULL;
194 }
195 memset(config->cfg_desc, 0, sizeof(char)*max_string_size);
196
197 config->algo_type = (char *)malloc(sizeof(char) * max_string_size);
198 if (!config->algo_type) {
199 free_config_instance(config);
200 ALOGE("Unable to allocate memory for config->algo_type");
201 return NULL;
202 }
203 memset(config->algo_type, 0, sizeof(char) * max_string_size);
204
205 config->fields = (struct field_data *)malloc(sizeof(struct field_data));
206 if (!config->fields) {
207 free_config_instance(config);
208 ALOGE("Unable to allocate memory for config->fields");
209 return NULL;
210 }
211 memset(config->fields, 0, sizeof(*config->fields));
212
213 config->fields[0].field_name = (char*)malloc(sizeof(char) * max_string_size);
214 if (!config->fields[0].field_name) {
215 free_config_instance(config);
216 ALOGE("Unable to allocate memory for config->fields[0].field_name");
217 return NULL;
218 }
219 memset(config->fields[0].field_name, 0, sizeof(char) * max_string_size);
220
221 config->fields[0].data = (void*)malloc(sizeof(int));
222 if (!config->fields[0].data) {
223 free_config_instance(config);
224 ALOGE("Unable to allocate memory for config->fields[0].data");
225 return NULL;
226 }
227
228 return config;
Sathish Ambley519f68e2017-07-19 10:26:51 -0700229}
230
231/**
232 * disable a thermal config
233 * returns 1 on success, anything else is a failure
234 */
235static int disable_config(char *config_name, char *algo_type){
Rohit Bandi2df5b732017-09-18 12:37:11 -0700236 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -0700237 int result = 0;
238 if (error_state) {
239 return 0;
240 }
241 struct config_instance *config = allocate_config_instance();
Rohit Bandi2df5b732017-09-18 12:37:11 -0700242 if (!config) {
243 return 0;
244 }
Sathish Ambley519f68e2017-07-19 10:26:51 -0700245 strlcpy(config->cfg_desc, config_name, max_string_size);
246 strlcpy(config->algo_type, algo_type, max_string_size);
247 strlcpy(config->fields[0].field_name, "disable", max_string_size);
248
249 config->fields_mask |= DISABLE_FIELD;
250 config->num_fields = 1;
251 config->fields[0].data_type = FIELD_INT;
252 config->fields[0].num_data = 1;
253 *(int*)(config->fields[0].data) = 1; //DISABLE
254
255
256 result = (*p_thermal_client_config_set)(config, 1);
257 if (DEBUG) {
258 ALOGE("disable profile: name = %s, algo_type = %s, success = %d", config_name, algo_type, result);
259 }
260 free_config_instance(config);
261
262 return result;
263}
264
265/**
266 * enable a thermal config
267 * returns 1 on success, anything else is failure
268 */
269static int enable_config(char *config_name, char *algo_type){
Rohit Bandi2df5b732017-09-18 12:37:11 -0700270 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -0700271 int result = 0;
272 if (error_state) {
273 return 0;
274 }
275 struct config_instance *config = allocate_config_instance();
Rohit Bandi2df5b732017-09-18 12:37:11 -0700276 if (!config) {
277 return 0;
278 }
Sathish Ambley519f68e2017-07-19 10:26:51 -0700279 strlcpy(config->cfg_desc, config_name, max_string_size);
280 strlcpy(config->algo_type, algo_type, max_string_size);
281 strlcpy(config->fields[0].field_name, "disable", max_string_size);
282
283 config->fields_mask |= DISABLE_FIELD;
284 config->num_fields = 1;
285 config->fields[0].data_type = FIELD_INT;
286 config->fields[0].num_data = 1;
287 *(int*)(config->fields[0].data) = 0; //ENABLE
288
289 result = (*p_thermal_client_config_set)(config, 1);
290 if (DEBUG) {
291 ALOGE("enable profile: name = %s, algo_type = %s, success = %d",
292 config_name, algo_type, result);
293 }
294
295 free_config_instance(config);
296
297 return result;
298}
299
300/**
301 * Call this if there is a compoenent-fatal error
302 * Attempts to clean up any outstanding thermal config state
303 */
304static void error_cleanup(){
Rohit Bandi2df5b732017-09-18 12:37:11 -0700305 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -0700306 //disable VR configs, best-effort so ignore return values
307 for (unsigned int i = 0; i < vr_thermal_cfgs.num_cfgs; i++) {
308 disable_config(vr_thermal_cfgs.cfgs[i].config_name, vr_thermal_cfgs.cfgs[i].algo_name);
309 }
310
311 // enable non-VR profile, best-effort so ignore return values
312 for (unsigned int i = 0; i < non_vr_thermal_cfgs.num_cfgs; i++) {
313 enable_config(non_vr_thermal_cfgs.cfgs[i].config_name, non_vr_thermal_cfgs.cfgs[i].algo_name);
314 }
315
316 // set global error flag
317 error_state = 1;
318}
319
320/*
321 * Set global display/GPU/scheduler configuration to used for VR apps.
322 */
323static void set_vr_thermal_configuration() {
Rohit Bandi2df5b732017-09-18 12:37:11 -0700324 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -0700325 int result = 1;
326 if (error_state) {
327 return;
328 }
329 //disable non-VR configs
330 for (unsigned int i = 0; i < non_vr_thermal_cfgs.num_cfgs; i++) {
331 result = disable_config(non_vr_thermal_cfgs.cfgs[i].config_name, non_vr_thermal_cfgs.cfgs[i].algo_name);
332 if (result != 1)
333 {
334 goto error;
335 }
336 }
337
338 //enable VR configs
339 for (unsigned int i = 0; i < vr_thermal_cfgs.num_cfgs; i++) {
340 result = enable_config(vr_thermal_cfgs.cfgs[i].config_name, vr_thermal_cfgs.cfgs[i].algo_name);
341 if (result != 1)
342 {
343 goto error;
344 }
345 }
346
347 if (DEBUG) {
348 query_thermal_config();
349 }
350
351 return;
352
353error:
354 error_cleanup();
355 return;
356}
357
358/*
359 * Reset to default global display/GPU/scheduler configuration.
360 */
361static void unset_vr_thermal_configuration() {
Rohit Bandi2df5b732017-09-18 12:37:11 -0700362 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -0700363 int result = 1;
364 if (error_state) {
365 return;
366 }
367
368 //disable VR configs
369 for (unsigned int i = 0; i < vr_thermal_cfgs.num_cfgs; i++) {
370 result = disable_config(vr_thermal_cfgs.cfgs[i].config_name, vr_thermal_cfgs.cfgs[i].algo_name);
371 if (result != 1) {
372 goto error;
373 }
374 }
375
376 // enable non-VR profile
377 for (unsigned int i = 0; i < non_vr_thermal_cfgs.num_cfgs; i++) {
378 result = enable_config(non_vr_thermal_cfgs.cfgs[i].config_name, non_vr_thermal_cfgs.cfgs[i].algo_name);
379 if (result != 1) {
380 goto error;
381 }
382 }
383
384 if (DEBUG) {
385 query_thermal_config();
386 }
387
388 return;
389
390error:
391 error_cleanup();
392 return;
393}
394
Rohit Bandi2df5b732017-09-18 12:37:11 -0700395static void vr_init(struct vr_module *module){
396 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -0700397 int success = load_thermal_client();
398 if (success != 0) {
399 ALOGE("failed to load thermal client");
400 error_state = 1;
401 }
402 success = load_thermal_cfg_info(&non_vr_thermal_cfgs, &vr_thermal_cfgs);
403 if (success != 0) {
404 ALOGE("failed to load thermal configs");
405 error_state = 1;
406 }
407
408}
409
Rohit Bandi2df5b732017-09-18 12:37:11 -0700410static void vr_set_vr_mode(struct vr_module *module, bool enabled){
411 ALOGD("Entering %s",__func__);
Sathish Ambley519f68e2017-07-19 10:26:51 -0700412 if (enabled) {
413 set_vr_thermal_configuration();
414 } else {
415 unset_vr_thermal_configuration();
416 }
417}
418
419static struct hw_module_methods_t vr_module_methods = {
420 .open = NULL, // There are no devices for this HAL interface.
421};
422
423
424vr_module_t HAL_MODULE_INFO_SYM = {
425 .common = {
426 .tag = HARDWARE_MODULE_TAG,
427 .module_api_version = VR_MODULE_API_VERSION_1_0,
428 .hal_api_version = HARDWARE_HAL_API_VERSION,
429 .id = VR_HARDWARE_MODULE_ID,
430 .name = "VR HAL",
431 .author = "Code Aurora Forum",
432 .methods = &vr_module_methods,
433 },
434
435 .init = vr_init,
436 .set_vr_mode = vr_set_vr_mode,
437};