1 /* Copyright (c) 2015, 2021, 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
Init(HWCDebugHandler * debug_handler)41 DisplayError CPUHint::Init(HWCDebugHandler *debug_handler) {
42 char path[PROPERTY_VALUE_MAX];
43 if (debug_handler->GetProperty("ro.vendor.extension_library", path) != kErrorNone) {
44 DLOGI("Vendor Extension Library not enabled");
45 return kErrorNotSupported;
46 }
47
48 int pre_enable_window = -1;
49 debug_handler->GetProperty(PERF_HINT_WINDOW_PROP, &pre_enable_window);
50 if (pre_enable_window <= 0) {
51 DLOGI("Invalid CPU Hint Pre-enable Window %d", pre_enable_window);
52 }
53
54 DLOGI("CPU Hint Pre-enable Window %d", pre_enable_window);
55 pre_enable_window_ = pre_enable_window;
56
57 if (vendor_ext_lib_.Open(path)) {
58 if (!vendor_ext_lib_.Sym("perf_lock_acq", reinterpret_cast<void **>(&fn_lock_acquire_)) ||
59 !vendor_ext_lib_.Sym("perf_lock_rel", reinterpret_cast<void **>(&fn_lock_release_)) ||
60 !vendor_ext_lib_.Sym("perf_hint", reinterpret_cast<void **>(&fn_perf_hint_)) ||
61 !vendor_ext_lib_.Sym("perf_hint_offload", reinterpret_cast<void **> \
62 (&fn_perf_hint_offload_))) {
63 DLOGW("Failed to load symbols for Vendor Extension Library");
64 return kErrorNotSupported;
65 }
66 DLOGI("Successfully Loaded Vendor Extension Library symbols");
67 enabled_ = true;
68 } else {
69 DLOGW("Failed to open %s : %s", path, vendor_ext_lib_.Error());
70 }
71
72 return enabled_ ? kErrorNone : kErrorNotSupported;
73 }
74
Set()75 void CPUHint::Set() {
76 if (!enabled_) {
77 return;
78 }
79 if (lock_acquired_) {
80 return;
81 }
82 if (frame_countdown_) {
83 --frame_countdown_;
84 return;
85 }
86
87 int hint = HINT;
88 lock_handle_ = fn_lock_acquire_(0 /*handle*/, 0/*duration*/,
89 &hint, sizeof(hint) / sizeof(int));
90 if (lock_handle_ >= 0) {
91 lock_acquired_ = true;
92 }
93 }
94
Reset()95 void CPUHint::Reset() {
96 if (!enabled_) {
97 return;
98 }
99
100 frame_countdown_ = pre_enable_window_;
101
102 if (!lock_acquired_) {
103 return;
104 }
105
106 fn_lock_release_(lock_handle_);
107 lock_acquired_ = false;
108 }
109
ReqHints(int hint)110 void CPUHint::ReqHints(int hint) {
111 if(enabled_ && hint > 0) {
112 fn_perf_hint_(hint, NULL, 0, 0);
113 }
114 }
115
ReqHintsOffload(int hint,int duration)116 void CPUHint::ReqHintsOffload(int hint, int duration) {
117 if(enabled_ && hint > 0) {
118 fn_perf_hint_offload_(hint, NULL, duration, 0, 0, NULL);
119 }
120 }
121 } // namespace sdm
122