1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "PowerHalController"
18*38e8c45fSAndroid Build Coastguard Worker #include <aidl/android/hardware/power/Boost.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <aidl/android/hardware/power/IPower.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <aidl/android/hardware/power/IPowerHintSession.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <aidl/android/hardware/power/Mode.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <android/hardware/power/1.1/IPower.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <powermanager/PowerHalController.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <powermanager/PowerHalLoader.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <utils/Log.h>
26*38e8c45fSAndroid Build Coastguard Worker
27*38e8c45fSAndroid Build Coastguard Worker using namespace android::hardware::power;
28*38e8c45fSAndroid Build Coastguard Worker
29*38e8c45fSAndroid Build Coastguard Worker namespace android {
30*38e8c45fSAndroid Build Coastguard Worker
31*38e8c45fSAndroid Build Coastguard Worker namespace power {
32*38e8c45fSAndroid Build Coastguard Worker
33*38e8c45fSAndroid Build Coastguard Worker // -------------------------------------------------------------------------------------------------
34*38e8c45fSAndroid Build Coastguard Worker
connect()35*38e8c45fSAndroid Build Coastguard Worker std::unique_ptr<HalWrapper> HalConnector::connect() {
36*38e8c45fSAndroid Build Coastguard Worker if (std::shared_ptr<aidl::android::hardware::power::IPower> halAidl =
37*38e8c45fSAndroid Build Coastguard Worker PowerHalLoader::loadAidl()) {
38*38e8c45fSAndroid Build Coastguard Worker return std::make_unique<AidlHalWrapper>(halAidl);
39*38e8c45fSAndroid Build Coastguard Worker }
40*38e8c45fSAndroid Build Coastguard Worker // If V1_0 isn't defined, none of them are
41*38e8c45fSAndroid Build Coastguard Worker if (sp<V1_0::IPower> halHidlV1_0 = PowerHalLoader::loadHidlV1_0()) {
42*38e8c45fSAndroid Build Coastguard Worker if (sp<V1_3::IPower> halHidlV1_3 = PowerHalLoader::loadHidlV1_3()) {
43*38e8c45fSAndroid Build Coastguard Worker return std::make_unique<HidlHalWrapperV1_3>(halHidlV1_3);
44*38e8c45fSAndroid Build Coastguard Worker }
45*38e8c45fSAndroid Build Coastguard Worker if (sp<V1_2::IPower> halHidlV1_2 = PowerHalLoader::loadHidlV1_2()) {
46*38e8c45fSAndroid Build Coastguard Worker return std::make_unique<HidlHalWrapperV1_2>(halHidlV1_2);
47*38e8c45fSAndroid Build Coastguard Worker }
48*38e8c45fSAndroid Build Coastguard Worker if (sp<V1_1::IPower> halHidlV1_1 = PowerHalLoader::loadHidlV1_1()) {
49*38e8c45fSAndroid Build Coastguard Worker return std::make_unique<HidlHalWrapperV1_1>(halHidlV1_1);
50*38e8c45fSAndroid Build Coastguard Worker }
51*38e8c45fSAndroid Build Coastguard Worker return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0);
52*38e8c45fSAndroid Build Coastguard Worker }
53*38e8c45fSAndroid Build Coastguard Worker return nullptr;
54*38e8c45fSAndroid Build Coastguard Worker }
55*38e8c45fSAndroid Build Coastguard Worker
reset()56*38e8c45fSAndroid Build Coastguard Worker void HalConnector::reset() {
57*38e8c45fSAndroid Build Coastguard Worker PowerHalLoader::unloadAll();
58*38e8c45fSAndroid Build Coastguard Worker }
59*38e8c45fSAndroid Build Coastguard Worker
getAidlVersion()60*38e8c45fSAndroid Build Coastguard Worker int32_t HalConnector::getAidlVersion() {
61*38e8c45fSAndroid Build Coastguard Worker return PowerHalLoader::getAidlVersion();
62*38e8c45fSAndroid Build Coastguard Worker }
63*38e8c45fSAndroid Build Coastguard Worker
64*38e8c45fSAndroid Build Coastguard Worker // -------------------------------------------------------------------------------------------------
65*38e8c45fSAndroid Build Coastguard Worker
init()66*38e8c45fSAndroid Build Coastguard Worker void PowerHalController::init() {
67*38e8c45fSAndroid Build Coastguard Worker initHal();
68*38e8c45fSAndroid Build Coastguard Worker }
69*38e8c45fSAndroid Build Coastguard Worker
70*38e8c45fSAndroid Build Coastguard Worker // Check validity of current handle to the power HAL service, and create a new
71*38e8c45fSAndroid Build Coastguard Worker // one if necessary.
initHal()72*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HalWrapper> PowerHalController::initHal() {
73*38e8c45fSAndroid Build Coastguard Worker std::lock_guard<std::mutex> lock(mConnectedHalMutex);
74*38e8c45fSAndroid Build Coastguard Worker if (mConnectedHal == nullptr) {
75*38e8c45fSAndroid Build Coastguard Worker mConnectedHal = mHalConnector->connect();
76*38e8c45fSAndroid Build Coastguard Worker if (mConnectedHal == nullptr) {
77*38e8c45fSAndroid Build Coastguard Worker // Unable to connect to Power HAL service. Fallback to default.
78*38e8c45fSAndroid Build Coastguard Worker return mDefaultHal;
79*38e8c45fSAndroid Build Coastguard Worker }
80*38e8c45fSAndroid Build Coastguard Worker }
81*38e8c45fSAndroid Build Coastguard Worker return mConnectedHal;
82*38e8c45fSAndroid Build Coastguard Worker }
83*38e8c45fSAndroid Build Coastguard Worker
84*38e8c45fSAndroid Build Coastguard Worker // Using statement expression macro instead of a method lets the static be
85*38e8c45fSAndroid Build Coastguard Worker // scoped to the outer method while dodging the need for a support lookup table
86*38e8c45fSAndroid Build Coastguard Worker // This only works for AIDL methods that do not vary supported/unsupported depending
87*38e8c45fSAndroid Build Coastguard Worker // on their arguments (not setBoost, setMode) which do their own support checks
88*38e8c45fSAndroid Build Coastguard Worker #define CACHE_SUPPORT(version, method) \
89*38e8c45fSAndroid Build Coastguard Worker ({ \
90*38e8c45fSAndroid Build Coastguard Worker static bool support = mHalConnector->getAidlVersion() >= version; \
91*38e8c45fSAndroid Build Coastguard Worker !support ? decltype(method)::unsupported() : ({ \
92*38e8c45fSAndroid Build Coastguard Worker auto result = method; \
93*38e8c45fSAndroid Build Coastguard Worker if (result.isUnsupported()) { \
94*38e8c45fSAndroid Build Coastguard Worker support = false; \
95*38e8c45fSAndroid Build Coastguard Worker } \
96*38e8c45fSAndroid Build Coastguard Worker std::move(result); \
97*38e8c45fSAndroid Build Coastguard Worker }); \
98*38e8c45fSAndroid Build Coastguard Worker })
99*38e8c45fSAndroid Build Coastguard Worker
100*38e8c45fSAndroid Build Coastguard Worker // Check if a call to Power HAL function failed; if so, log the failure and
101*38e8c45fSAndroid Build Coastguard Worker // invalidate the current Power HAL handle.
102*38e8c45fSAndroid Build Coastguard Worker template <typename T>
processHalResult(HalResult<T> && result,const char * fnName)103*38e8c45fSAndroid Build Coastguard Worker HalResult<T> PowerHalController::processHalResult(HalResult<T>&& result, const char* fnName) {
104*38e8c45fSAndroid Build Coastguard Worker if (result.isFailed()) {
105*38e8c45fSAndroid Build Coastguard Worker ALOGE("%s failed: %s", fnName, result.errorMessage());
106*38e8c45fSAndroid Build Coastguard Worker std::lock_guard<std::mutex> lock(mConnectedHalMutex);
107*38e8c45fSAndroid Build Coastguard Worker // Drop Power HAL handle. This will force future api calls to reconnect.
108*38e8c45fSAndroid Build Coastguard Worker mConnectedHal = nullptr;
109*38e8c45fSAndroid Build Coastguard Worker mHalConnector->reset();
110*38e8c45fSAndroid Build Coastguard Worker }
111*38e8c45fSAndroid Build Coastguard Worker return std::move(result);
112*38e8c45fSAndroid Build Coastguard Worker }
113*38e8c45fSAndroid Build Coastguard Worker
setBoost(aidl::android::hardware::power::Boost boost,int32_t durationMs)114*38e8c45fSAndroid Build Coastguard Worker HalResult<void> PowerHalController::setBoost(aidl::android::hardware::power::Boost boost,
115*38e8c45fSAndroid Build Coastguard Worker int32_t durationMs) {
116*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HalWrapper> handle = initHal();
117*38e8c45fSAndroid Build Coastguard Worker return processHalResult(handle->setBoost(boost, durationMs), "setBoost");
118*38e8c45fSAndroid Build Coastguard Worker }
119*38e8c45fSAndroid Build Coastguard Worker
setMode(aidl::android::hardware::power::Mode mode,bool enabled)120*38e8c45fSAndroid Build Coastguard Worker HalResult<void> PowerHalController::setMode(aidl::android::hardware::power::Mode mode,
121*38e8c45fSAndroid Build Coastguard Worker bool enabled) {
122*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HalWrapper> handle = initHal();
123*38e8c45fSAndroid Build Coastguard Worker return processHalResult(handle->setMode(mode, enabled), "setMode");
124*38e8c45fSAndroid Build Coastguard Worker }
125*38e8c45fSAndroid Build Coastguard Worker
126*38e8c45fSAndroid Build Coastguard Worker // Aidl-only methods
127*38e8c45fSAndroid Build Coastguard Worker
createHintSession(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos)128*38e8c45fSAndroid Build Coastguard Worker HalResult<std::shared_ptr<PowerHintSessionWrapper>> PowerHalController::createHintSession(
129*38e8c45fSAndroid Build Coastguard Worker int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
130*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HalWrapper> handle = initHal();
131*38e8c45fSAndroid Build Coastguard Worker return CACHE_SUPPORT(2,
132*38e8c45fSAndroid Build Coastguard Worker processHalResult(handle->createHintSession(tgid, uid, threadIds,
133*38e8c45fSAndroid Build Coastguard Worker durationNanos),
134*38e8c45fSAndroid Build Coastguard Worker "createHintSession"));
135*38e8c45fSAndroid Build Coastguard Worker }
136*38e8c45fSAndroid Build Coastguard Worker
createHintSessionWithConfig(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos,aidl::android::hardware::power::SessionTag tag,aidl::android::hardware::power::SessionConfig * config)137*38e8c45fSAndroid Build Coastguard Worker HalResult<std::shared_ptr<PowerHintSessionWrapper>> PowerHalController::createHintSessionWithConfig(
138*38e8c45fSAndroid Build Coastguard Worker int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
139*38e8c45fSAndroid Build Coastguard Worker aidl::android::hardware::power::SessionTag tag,
140*38e8c45fSAndroid Build Coastguard Worker aidl::android::hardware::power::SessionConfig* config) {
141*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HalWrapper> handle = initHal();
142*38e8c45fSAndroid Build Coastguard Worker return CACHE_SUPPORT(5,
143*38e8c45fSAndroid Build Coastguard Worker processHalResult(handle->createHintSessionWithConfig(tgid, uid, threadIds,
144*38e8c45fSAndroid Build Coastguard Worker durationNanos, tag,
145*38e8c45fSAndroid Build Coastguard Worker config),
146*38e8c45fSAndroid Build Coastguard Worker "createHintSessionWithConfig"));
147*38e8c45fSAndroid Build Coastguard Worker }
148*38e8c45fSAndroid Build Coastguard Worker
getHintSessionPreferredRate()149*38e8c45fSAndroid Build Coastguard Worker HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() {
150*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HalWrapper> handle = initHal();
151*38e8c45fSAndroid Build Coastguard Worker return CACHE_SUPPORT(2,
152*38e8c45fSAndroid Build Coastguard Worker processHalResult(handle->getHintSessionPreferredRate(),
153*38e8c45fSAndroid Build Coastguard Worker "getHintSessionPreferredRate"));
154*38e8c45fSAndroid Build Coastguard Worker }
155*38e8c45fSAndroid Build Coastguard Worker
getSessionChannel(int tgid,int uid)156*38e8c45fSAndroid Build Coastguard Worker HalResult<aidl::android::hardware::power::ChannelConfig> PowerHalController::getSessionChannel(
157*38e8c45fSAndroid Build Coastguard Worker int tgid, int uid) {
158*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HalWrapper> handle = initHal();
159*38e8c45fSAndroid Build Coastguard Worker return CACHE_SUPPORT(5,
160*38e8c45fSAndroid Build Coastguard Worker processHalResult(handle->getSessionChannel(tgid, uid),
161*38e8c45fSAndroid Build Coastguard Worker "getSessionChannel"));
162*38e8c45fSAndroid Build Coastguard Worker }
163*38e8c45fSAndroid Build Coastguard Worker
closeSessionChannel(int tgid,int uid)164*38e8c45fSAndroid Build Coastguard Worker HalResult<void> PowerHalController::closeSessionChannel(int tgid, int uid) {
165*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HalWrapper> handle = initHal();
166*38e8c45fSAndroid Build Coastguard Worker return CACHE_SUPPORT(5,
167*38e8c45fSAndroid Build Coastguard Worker processHalResult(handle->closeSessionChannel(tgid, uid),
168*38e8c45fSAndroid Build Coastguard Worker "closeSessionChannel"));
169*38e8c45fSAndroid Build Coastguard Worker }
170*38e8c45fSAndroid Build Coastguard Worker
getSupportInfo()171*38e8c45fSAndroid Build Coastguard Worker HalResult<aidl::android::hardware::power::SupportInfo> PowerHalController::getSupportInfo() {
172*38e8c45fSAndroid Build Coastguard Worker std::shared_ptr<HalWrapper> handle = initHal();
173*38e8c45fSAndroid Build Coastguard Worker return CACHE_SUPPORT(6, processHalResult(handle->getSupportInfo(), "getSupportInfo"));
174*38e8c45fSAndroid Build Coastguard Worker }
175*38e8c45fSAndroid Build Coastguard Worker
176*38e8c45fSAndroid Build Coastguard Worker } // namespace power
177*38e8c45fSAndroid Build Coastguard Worker
178*38e8c45fSAndroid Build Coastguard Worker } // namespace android
179