1 /* Copyright (c) 2015, The Linux Foundataion. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 
30 #include <cutils/properties.h>
31 #include <dlfcn.h>
32 #include <utils/debug.h>
33 
34 #include "cpuhint.h"
35 #include "hwc_debugger.h"
36 
37 #define __CLASS__ "CPUHint"
38 
39 namespace sdm {
40 
~CPUHint()41 CPUHint::~CPUHint() {
42   if (lib_handle_) {
43     dlclose(lib_handle_);
44     lib_handle_ = NULL;
45   }
46 }
47 
Init(HWCDebugHandler * debug_handler)48 DisplayError CPUHint::Init(HWCDebugHandler *debug_handler) {
49   char path[PROPERTY_VALUE_MAX];
50   if (debug_handler->GetProperty("ro.vendor.extension_library", path) != kErrorNone) {
51     DLOGI("Vendor Extension Library not enabled");
52     return kErrorNotSupported;
53   }
54 
55   int pre_enable_window = -1;
56   debug_handler->GetProperty("sdm.perf_hint_window", &pre_enable_window);
57   if (pre_enable_window <= 0) {
58     DLOGI("Invalid CPU Hint Pre-enable Window %d", pre_enable_window);
59     return kErrorNotSupported;
60   }
61 
62   DLOGI("CPU Hint Pre-enable Window %d", pre_enable_window);
63   pre_enable_window_ = pre_enable_window;
64   lib_handle_ = dlopen(path, RTLD_NOW);
65 
66   if (lib_handle_) {
67     *(reinterpret_cast<void **>(&fn_lock_acquire_)) = dlsym(lib_handle_, "perf_lock_acq");
68     *(reinterpret_cast<void **>(&fn_lock_release_)) = dlsym(lib_handle_, "perf_lock_rel");
69     if (!fn_lock_acquire_ || !fn_lock_release_) {
70       DLOGW("Failed to load symbols for Vendor Extension Library");
71       return kErrorNotSupported;
72     }
73     DLOGI("Successfully Loaded Vendor Extension Library symbols");
74     enabled_ = true;
75   } else {
76     DLOGW("Failed to open %s : %s", path, dlerror());
77   }
78 
79   return kErrorNone;
80 }
81 
Set()82 void CPUHint::Set() {
83   if (!enabled_) {
84     return;
85   }
86   if (lock_acquired_) {
87     return;
88   }
89   if (frame_countdown_) {
90     --frame_countdown_;
91     return;
92   }
93 
94   int hint = HINT;
95   lock_handle_ = fn_lock_acquire_(0 /*handle*/, 0/*duration*/,
96                                   &hint, sizeof(hint) / sizeof(int));
97   if (lock_handle_ >= 0) {
98     lock_acquired_ = true;
99   }
100 }
101 
Reset()102 void CPUHint::Reset() {
103   if (!enabled_) {
104     return;
105   }
106 
107   frame_countdown_ = pre_enable_window_;
108 
109   if (!lock_acquired_) {
110     return;
111   }
112 
113   fn_lock_release_(lock_handle_);
114   lock_acquired_ = false;
115 }
116 
117 }  // namespace sdm
118