1 /* Copyright (c) 2015,2018 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     return kErrorNotSupported;
53   }
54 
55   DLOGI("CPU Hint Pre-enable Window %d", pre_enable_window);
56   pre_enable_window_ = pre_enable_window;
57 
58   if (vendor_ext_lib_.Open(path)) {
59     if (!vendor_ext_lib_.Sym("perf_lock_acq", reinterpret_cast<void **>(&fn_lock_acquire_)) ||
60         !vendor_ext_lib_.Sym("perf_lock_rel", reinterpret_cast<void **>(&fn_lock_release_))) {
61       DLOGW("Failed to load symbols for Vendor Extension Library");
62       return kErrorNotSupported;
63     }
64     DLOGI("Successfully Loaded Vendor Extension Library symbols");
65     enabled_ = true;
66   } else {
67     DLOGW("Failed to open %s : %s", path, vendor_ext_lib_.Error());
68   }
69 
70   return kErrorNone;
71 }
72 
Set()73 void CPUHint::Set() {
74   if (!enabled_) {
75     return;
76   }
77   if (lock_acquired_) {
78     return;
79   }
80   if (frame_countdown_) {
81     --frame_countdown_;
82     return;
83   }
84 
85   int hint = HINT;
86   lock_handle_ = fn_lock_acquire_(0 /*handle*/, 0/*duration*/,
87                                   &hint, sizeof(hint) / sizeof(int));
88   if (lock_handle_ >= 0) {
89     lock_acquired_ = true;
90   }
91 }
92 
Reset()93 void CPUHint::Reset() {
94   if (!enabled_) {
95     return;
96   }
97 
98   frame_countdown_ = pre_enable_window_;
99 
100   if (!lock_acquired_) {
101     return;
102   }
103 
104   fn_lock_release_(lock_handle_);
105   lock_acquired_ = false;
106 }
107 
108 }  // namespace sdm
109