blob: 40fd09749147a57637be6153ed27333d4ce147d1 [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"
Xiang Wang99f6f3c2023-05-22 13:12:16 -070018#include <aidl/android/hardware/power/Boost.h>
19#include <aidl/android/hardware/power/IPower.h>
20#include <aidl/android/hardware/power/IPowerHintSession.h>
21#include <aidl/android/hardware/power/Mode.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000022#include <android/hardware/power/1.1/IPower.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() {
Xiang Wang99f6f3c2023-05-22 13:12:16 -070036 if (std::shared_ptr<aidl::android::hardware::power::IPower> halAidl =
37 PowerHalLoader::loadAidl()) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010038 return std::make_unique<AidlHalWrapper>(halAidl);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000039 }
Matt Buckleyc3894a42022-09-01 21:17:15 +000040 // If V1_0 isn't defined, none of them are
41 if (sp<V1_0::IPower> halHidlV1_0 = PowerHalLoader::loadHidlV1_0()) {
42 if (sp<V1_3::IPower> halHidlV1_3 = PowerHalLoader::loadHidlV1_3()) {
43 return std::make_unique<HidlHalWrapperV1_3>(halHidlV1_3);
44 }
45 if (sp<V1_2::IPower> halHidlV1_2 = PowerHalLoader::loadHidlV1_2()) {
46 return std::make_unique<HidlHalWrapperV1_2>(halHidlV1_2);
47 }
48 if (sp<V1_1::IPower> halHidlV1_1 = PowerHalLoader::loadHidlV1_1()) {
49 return std::make_unique<HidlHalWrapperV1_1>(halHidlV1_1);
50 }
Lais Andradeb59a9b52020-05-07 17:23:42 +010051 return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000052 }
53 return nullptr;
54}
55
Lais Andradeb59a9b52020-05-07 17:23:42 +010056void HalConnector::reset() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000057 PowerHalLoader::unloadAll();
58}
59
Matt Buckley6c18e6d2024-02-07 23:39:50 +000060int32_t HalConnector::getAidlVersion() {
61 return PowerHalLoader::getAidlVersion();
62}
63
Lais Andrade3f7ecc52020-03-25 23:57:08 +000064// -------------------------------------------------------------------------------------------------
65
66void PowerHalController::init() {
67 initHal();
68}
69
Lais Andradeb59a9b52020-05-07 17:23:42 +010070// Check validity of current handle to the power HAL service, and create a new
71// one if necessary.
72std::shared_ptr<HalWrapper> PowerHalController::initHal() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000073 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
74 if (mConnectedHal == nullptr) {
75 mConnectedHal = mHalConnector->connect();
76 if (mConnectedHal == nullptr) {
77 // Unable to connect to Power HAL service. Fallback to default.
78 return mDefaultHal;
79 }
80 }
81 return mConnectedHal;
82}
83
Matt Buckley6c18e6d2024-02-07 23:39:50 +000084// Using statement expression macro instead of a method lets the static be
85// scoped to the outer method while dodging the need for a support lookup table
86// This only works for AIDL methods that do not vary supported/unsupported depending
87// on their arguments (not setBoost, setMode) which do their own support checks
88#define CACHE_SUPPORT(version, method) \
89 ({ \
90 static bool support = mHalConnector->getAidlVersion() >= version; \
91 !support ? decltype(method)::unsupported() : ({ \
92 auto result = method; \
93 if (result.isUnsupported()) { \
94 support = false; \
95 } \
96 std::move(result); \
97 }); \
98 })
99
Lais Andradeb59a9b52020-05-07 17:23:42 +0100100// Check if a call to Power HAL function failed; if so, log the failure and
101// invalidate the current Power HAL handle.
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800102template <typename T>
Matt Buckleydb4192a2023-12-21 20:00:32 +0000103HalResult<T> PowerHalController::processHalResult(HalResult<T>&& result, const char* fnName) {
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800104 if (result.isFailed()) {
105 ALOGE("%s failed: %s", fnName, result.errorMessage());
Lais Andrade3f7ecc52020-03-25 23:57:08 +0000106 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
107 // Drop Power HAL handle. This will force future api calls to reconnect.
108 mConnectedHal = nullptr;
109 mHalConnector->reset();
110 }
Matt Buckleyf4e95dc2024-01-24 22:45:57 +0000111 return std::move(result);
Lais Andrade3f7ecc52020-03-25 23:57:08 +0000112}
113
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700114HalResult<void> PowerHalController::setBoost(aidl::android::hardware::power::Boost boost,
115 int32_t durationMs) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100116 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckleydb4192a2023-12-21 20:00:32 +0000117 return processHalResult(handle->setBoost(boost, durationMs), "setBoost");
Lais Andrade3f7ecc52020-03-25 23:57:08 +0000118}
119
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700120HalResult<void> PowerHalController::setMode(aidl::android::hardware::power::Mode mode,
121 bool enabled) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100122 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckleydb4192a2023-12-21 20:00:32 +0000123 return processHalResult(handle->setMode(mode, enabled), "setMode");
Lais Andrade3f7ecc52020-03-25 23:57:08 +0000124}
125
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000126// Aidl-only methods
127
128HalResult<std::shared_ptr<PowerHintSessionWrapper>> PowerHalController::createHintSession(
129 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800130 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000131 return CACHE_SUPPORT(2,
132 processHalResult(handle->createHintSession(tgid, uid, threadIds,
133 durationNanos),
134 "createHintSession"));
Matt Buckleydb4192a2023-12-21 20:00:32 +0000135}
136
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000137HalResult<std::shared_ptr<PowerHintSessionWrapper>> PowerHalController::createHintSessionWithConfig(
Matt Buckleydb4192a2023-12-21 20:00:32 +0000138 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
139 aidl::android::hardware::power::SessionTag tag,
140 aidl::android::hardware::power::SessionConfig* config) {
141 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000142 return CACHE_SUPPORT(5,
143 processHalResult(handle->createHintSessionWithConfig(tgid, uid, threadIds,
144 durationNanos, tag,
145 config),
146 "createHintSessionWithConfig"));
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800147}
148
149HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() {
150 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000151 return CACHE_SUPPORT(2,
152 processHalResult(handle->getHintSessionPreferredRate(),
153 "getHintSessionPreferredRate"));
Matt Buckleydb4192a2023-12-21 20:00:32 +0000154}
155
156HalResult<aidl::android::hardware::power::ChannelConfig> PowerHalController::getSessionChannel(
157 int tgid, int uid) {
158 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000159 return CACHE_SUPPORT(5,
160 processHalResult(handle->getSessionChannel(tgid, uid),
161 "getSessionChannel"));
Matt Buckleydb4192a2023-12-21 20:00:32 +0000162}
163
164HalResult<void> PowerHalController::closeSessionChannel(int tgid, int uid) {
165 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000166 return CACHE_SUPPORT(5,
167 processHalResult(handle->closeSessionChannel(tgid, uid),
168 "closeSessionChannel"));
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800169}
170
Lais Andradeb59a9b52020-05-07 17:23:42 +0100171} // namespace power
172
173} // namespace android