blob: f89035fd1c4cca709f17855a228aeff13e8637ab [file] [log] [blame]
Lais Andrade3f7ecc52020-03-25 23:57:08 +00001/*
2 * Copyright (C) 2020 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 "PowerHalController"
Lais Andrade3f7ecc52020-03-25 23:57:08 +000018#include <android/hardware/power/1.1/IPower.h>
19#include <android/hardware/power/Boost.h>
20#include <android/hardware/power/IPower.h>
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080021#include <android/hardware/power/IPowerHintSession.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000022#include <android/hardware/power/Mode.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000023#include <powermanager/PowerHalController.h>
24#include <powermanager/PowerHalLoader.h>
Lais Andradeb59a9b52020-05-07 17:23:42 +010025#include <utils/Log.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000026
Lais Andradeb59a9b52020-05-07 17:23:42 +010027using namespace android::hardware::power;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000028
29namespace android {
30
Lais Andradeb59a9b52020-05-07 17:23:42 +010031namespace power {
32
Lais Andrade3f7ecc52020-03-25 23:57:08 +000033// -------------------------------------------------------------------------------------------------
34
Lais Andradeb59a9b52020-05-07 17:23:42 +010035std::unique_ptr<HalWrapper> HalConnector::connect() {
Matt Buckleyc3894a42022-09-01 21:17:15 +000036 if (sp<IPower> halAidl = PowerHalLoader::loadAidl()) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010037 return std::make_unique<AidlHalWrapper>(halAidl);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000038 }
Matt Buckleyc3894a42022-09-01 21:17:15 +000039 // If V1_0 isn't defined, none of them are
40 if (sp<V1_0::IPower> halHidlV1_0 = PowerHalLoader::loadHidlV1_0()) {
41 if (sp<V1_3::IPower> halHidlV1_3 = PowerHalLoader::loadHidlV1_3()) {
42 return std::make_unique<HidlHalWrapperV1_3>(halHidlV1_3);
43 }
44 if (sp<V1_2::IPower> halHidlV1_2 = PowerHalLoader::loadHidlV1_2()) {
45 return std::make_unique<HidlHalWrapperV1_2>(halHidlV1_2);
46 }
47 if (sp<V1_1::IPower> halHidlV1_1 = PowerHalLoader::loadHidlV1_1()) {
48 return std::make_unique<HidlHalWrapperV1_1>(halHidlV1_1);
49 }
Lais Andradeb59a9b52020-05-07 17:23:42 +010050 return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000051 }
52 return nullptr;
53}
54
Lais Andradeb59a9b52020-05-07 17:23:42 +010055void HalConnector::reset() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000056 PowerHalLoader::unloadAll();
57}
58
59// -------------------------------------------------------------------------------------------------
60
61void PowerHalController::init() {
62 initHal();
63}
64
Lais Andradeb59a9b52020-05-07 17:23:42 +010065// Check validity of current handle to the power HAL service, and create a new
66// one if necessary.
67std::shared_ptr<HalWrapper> PowerHalController::initHal() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000068 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
69 if (mConnectedHal == nullptr) {
70 mConnectedHal = mHalConnector->connect();
71 if (mConnectedHal == nullptr) {
72 // Unable to connect to Power HAL service. Fallback to default.
73 return mDefaultHal;
74 }
75 }
76 return mConnectedHal;
77}
78
Lais Andradeb59a9b52020-05-07 17:23:42 +010079// Check if a call to Power HAL function failed; if so, log the failure and
80// invalidate the current Power HAL handle.
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080081template <typename T>
82HalResult<T> PowerHalController::processHalResult(HalResult<T> result, const char* fnName) {
83 if (result.isFailed()) {
84 ALOGE("%s failed: %s", fnName, result.errorMessage());
Lais Andrade3f7ecc52020-03-25 23:57:08 +000085 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
86 // Drop Power HAL handle. This will force future api calls to reconnect.
87 mConnectedHal = nullptr;
88 mHalConnector->reset();
89 }
90 return result;
91}
92
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080093HalResult<void> PowerHalController::setBoost(Boost boost, int32_t durationMs) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010094 std::shared_ptr<HalWrapper> handle = initHal();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000095 auto result = handle->setBoost(boost, durationMs);
96 return processHalResult(result, "setBoost");
97}
98
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080099HalResult<void> PowerHalController::setMode(Mode mode, bool enabled) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100100 std::shared_ptr<HalWrapper> handle = initHal();
Lais Andrade3f7ecc52020-03-25 23:57:08 +0000101 auto result = handle->setMode(mode, enabled);
102 return processHalResult(result, "setMode");
103}
104
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800105HalResult<sp<IPowerHintSession>> PowerHalController::createHintSession(
106 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
107 std::shared_ptr<HalWrapper> handle = initHal();
108 auto result = handle->createHintSession(tgid, uid, threadIds, durationNanos);
109 return processHalResult(result, "createHintSession");
110}
111
112HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() {
113 std::shared_ptr<HalWrapper> handle = initHal();
114 auto result = handle->getHintSessionPreferredRate();
115 return processHalResult(result, "getHintSessionPreferredRate");
116}
117
Lais Andradeb59a9b52020-05-07 17:23:42 +0100118} // namespace power
119
120} // namespace android