Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 1 | /* |
| 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 Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 18 | #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 Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 22 | #include <android/hardware/power/1.1/IPower.h> |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 23 | #include <powermanager/PowerHalController.h> |
| 24 | #include <powermanager/PowerHalLoader.h> |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 25 | #include <utils/Log.h> |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 26 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 27 | using namespace android::hardware::power; |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 31 | namespace power { |
| 32 | |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 33 | // ------------------------------------------------------------------------------------------------- |
| 34 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 35 | std::unique_ptr<HalWrapper> HalConnector::connect() { |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 36 | if (std::shared_ptr<aidl::android::hardware::power::IPower> halAidl = |
| 37 | PowerHalLoader::loadAidl()) { |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 38 | return std::make_unique<AidlHalWrapper>(halAidl); |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 39 | } |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 40 | // 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 Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 51 | return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0); |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 52 | } |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 56 | void HalConnector::reset() { |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 57 | PowerHalLoader::unloadAll(); |
| 58 | } |
| 59 | |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 60 | int32_t HalConnector::getAidlVersion() { |
| 61 | return PowerHalLoader::getAidlVersion(); |
| 62 | } |
| 63 | |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 64 | // ------------------------------------------------------------------------------------------------- |
| 65 | |
| 66 | void PowerHalController::init() { |
| 67 | initHal(); |
| 68 | } |
| 69 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 70 | // Check validity of current handle to the power HAL service, and create a new |
| 71 | // one if necessary. |
| 72 | std::shared_ptr<HalWrapper> PowerHalController::initHal() { |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 73 | 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 Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 84 | // 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 Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 100 | // Check if a call to Power HAL function failed; if so, log the failure and |
| 101 | // invalidate the current Power HAL handle. |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 102 | template <typename T> |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 103 | HalResult<T> PowerHalController::processHalResult(HalResult<T>&& result, const char* fnName) { |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 104 | if (result.isFailed()) { |
| 105 | ALOGE("%s failed: %s", fnName, result.errorMessage()); |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 106 | 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 Buckley | f4e95dc | 2024-01-24 22:45:57 +0000 | [diff] [blame] | 111 | return std::move(result); |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 114 | HalResult<void> PowerHalController::setBoost(aidl::android::hardware::power::Boost boost, |
| 115 | int32_t durationMs) { |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 116 | std::shared_ptr<HalWrapper> handle = initHal(); |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 117 | return processHalResult(handle->setBoost(boost, durationMs), "setBoost"); |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 120 | HalResult<void> PowerHalController::setMode(aidl::android::hardware::power::Mode mode, |
| 121 | bool enabled) { |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 122 | std::shared_ptr<HalWrapper> handle = initHal(); |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 123 | return processHalResult(handle->setMode(mode, enabled), "setMode"); |
Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 126 | // Aidl-only methods |
| 127 | |
| 128 | HalResult<std::shared_ptr<PowerHintSessionWrapper>> PowerHalController::createHintSession( |
| 129 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) { |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 130 | std::shared_ptr<HalWrapper> handle = initHal(); |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 131 | return CACHE_SUPPORT(2, |
| 132 | processHalResult(handle->createHintSession(tgid, uid, threadIds, |
| 133 | durationNanos), |
| 134 | "createHintSession")); |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 137 | HalResult<std::shared_ptr<PowerHintSessionWrapper>> PowerHalController::createHintSessionWithConfig( |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 138 | 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 Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 142 | return CACHE_SUPPORT(5, |
| 143 | processHalResult(handle->createHintSessionWithConfig(tgid, uid, threadIds, |
| 144 | durationNanos, tag, |
| 145 | config), |
| 146 | "createHintSessionWithConfig")); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() { |
| 150 | std::shared_ptr<HalWrapper> handle = initHal(); |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 151 | return CACHE_SUPPORT(2, |
| 152 | processHalResult(handle->getHintSessionPreferredRate(), |
| 153 | "getHintSessionPreferredRate")); |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | HalResult<aidl::android::hardware::power::ChannelConfig> PowerHalController::getSessionChannel( |
| 157 | int tgid, int uid) { |
| 158 | std::shared_ptr<HalWrapper> handle = initHal(); |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 159 | return CACHE_SUPPORT(5, |
| 160 | processHalResult(handle->getSessionChannel(tgid, uid), |
| 161 | "getSessionChannel")); |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | HalResult<void> PowerHalController::closeSessionChannel(int tgid, int uid) { |
| 165 | std::shared_ptr<HalWrapper> handle = initHal(); |
Matt Buckley | 6c18e6d | 2024-02-07 23:39:50 +0000 | [diff] [blame^] | 166 | return CACHE_SUPPORT(5, |
| 167 | processHalResult(handle->closeSessionChannel(tgid, uid), |
| 168 | "closeSessionChannel")); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 171 | } // namespace power |
| 172 | |
| 173 | } // namespace android |