Benjamin Schwartz | c798cb3 | 2021-03-18 17:55:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "android.hardware.power.stats-service.pixel" |
| 18 | |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 19 | #include <dataproviders/DisplayStateResidencyDataProvider.h> |
Benjamin Schwartz | eeaf5ea | 2021-04-14 14:49:31 -0700 | [diff] [blame] | 20 | #include <dataproviders/GenericStateResidencyDataProvider.h> |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 21 | #include <dataproviders/PowerStatsEnergyConsumer.h> |
Benjamin Schwartz | c798cb3 | 2021-03-18 17:55:40 -0700 | [diff] [blame] | 22 | #include <Gs101CommonDataProviders.h> |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 23 | #include <PowerStatsAidl.h> |
Benjamin Schwartz | c798cb3 | 2021-03-18 17:55:40 -0700 | [diff] [blame] | 24 | |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/properties.h> |
| 27 | #include <android/binder_manager.h> |
| 28 | #include <android/binder_process.h> |
| 29 | #include <log/log.h> |
chungkai | e593aaa | 2022-04-06 10:11:12 +0000 | [diff] [blame] | 30 | #include <sys/stat.h> |
Benjamin Schwartz | c798cb3 | 2021-03-18 17:55:40 -0700 | [diff] [blame] | 31 | |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 32 | using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider; |
| 33 | using aidl::android::hardware::power::stats::EnergyConsumerType; |
Benjamin Schwartz | eeaf5ea | 2021-04-14 14:49:31 -0700 | [diff] [blame] | 34 | using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider; |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 35 | using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer; |
| 36 | |
samou | f1820db | 2021-07-29 07:38:05 +0000 | [diff] [blame] | 37 | const char kBootRevision[] = "ro.boot.revision"; |
| 38 | std::map<std::string, std::string> displayChannelNames = { |
| 39 | {"PROTO1.0", "PPVAR_VSYS_PWR_DISP"}, |
| 40 | {"EVT1.0", "PPVAR_VSYS_PWR_DISP"}, |
| 41 | {"EVT1.1", "VSYS_PWR_DISPLAY"}, |
| 42 | }; |
| 43 | |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 44 | void addDisplay(std::shared_ptr<PowerStats> p) { |
| 45 | // Add display residency stats |
Darren Hsu | d1ebd6f | 2024-01-29 11:31:47 +0800 | [diff] [blame] | 46 | struct stat buffer; |
| 47 | if (!stat("/sys/class/drm/card0/device/primary-panel/time_in_state", &buffer)) { |
| 48 | // time_in_state exists |
| 49 | addDisplayMrr(p); |
| 50 | } else { |
| 51 | // time_in_state doesn't exist |
| 52 | std::vector<std::string> states = { |
| 53 | "Off", |
| 54 | "LP: 1440x3120@10", |
| 55 | "LP: 1440x3120@30", |
| 56 | "On: 1440x3120@10", |
| 57 | "On: 1440x3120@30", |
| 58 | "On: 1440x3120@60", |
| 59 | "On: 1440x3120@90", |
| 60 | "On: 1440x3120@120", |
| 61 | "HBM: 1440x3120@60", |
| 62 | }; |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 63 | |
Darren Hsu | d1ebd6f | 2024-01-29 11:31:47 +0800 | [diff] [blame] | 64 | p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>("Display", |
| 65 | "/sys/class/backlight/panel0-backlight/state", |
| 66 | states)); |
| 67 | } |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 68 | |
samou | f1820db | 2021-07-29 07:38:05 +0000 | [diff] [blame] | 69 | std::string rev = android::base::GetProperty(kBootRevision, ""); |
| 70 | |
| 71 | std::string channelName; |
| 72 | if (displayChannelNames.find(rev) == displayChannelNames.end()) { |
| 73 | channelName = displayChannelNames["EVT1.1"]; |
| 74 | } else { |
| 75 | channelName = displayChannelNames[rev]; |
| 76 | } |
| 77 | |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 78 | // Add display energy consumer |
| 79 | /* |
| 80 | * TODO(b/167216667): Add correct display power model here. Must read from display rail |
| 81 | * and include proper coefficients for display states. |
| 82 | */ |
| 83 | p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndEntityConsumer(p, |
samou | f1820db | 2021-07-29 07:38:05 +0000 | [diff] [blame] | 84 | EnergyConsumerType::DISPLAY, "display", {channelName}, "Display", |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 85 | {{"LP: 1440x3120@10", 1}, |
| 86 | {"LP: 1440x3120@30", 2}, |
| 87 | {"On: 1440x3120@10", 3}, |
| 88 | {"On: 1440x3120@30", 4}, |
| 89 | {"On: 1440x3120@60", 5}, |
| 90 | {"On: 1440x3120@90", 6}, |
| 91 | {"On: 1440x3120@120", 7}, |
| 92 | {"HBM: 1440x3120@60", 8}})); |
| 93 | } |
| 94 | |
Benjamin Schwartz | eeaf5ea | 2021-04-14 14:49:31 -0700 | [diff] [blame] | 95 | void addUwb(std::shared_ptr<PowerStats> p) { |
| 96 | // A constant to represent the number of nanoseconds in one millisecond. |
| 97 | const int NS_TO_MS = 1000000; |
| 98 | |
| 99 | // ACPM stats are reported in nanoseconds. The transform function |
| 100 | // converts nanoseconds to milliseconds. |
| 101 | std::function<uint64_t(uint64_t)> uwbNsToMs = [](uint64_t a) { return a / NS_TO_MS; }; |
| 102 | const GenericStateResidencyDataProvider::StateResidencyConfig stateConfig = { |
| 103 | .entryCountSupported = true, |
| 104 | .entryCountPrefix = "count:", |
| 105 | .totalTimeSupported = true, |
| 106 | .totalTimePrefix = "dur ns:", |
| 107 | .totalTimeTransform = uwbNsToMs, |
| 108 | .lastEntrySupported = false, |
| 109 | }; |
| 110 | |
| 111 | const std::vector<std::pair<std::string, std::string>> stateHeaders = { |
| 112 | std::make_pair("Off", "Off state:"), |
| 113 | std::make_pair("Run", "Run state:"), |
| 114 | std::make_pair("Idle", "Idle state:"), |
| 115 | std::make_pair("Tx", "Tx state:"), |
| 116 | std::make_pair("Rx", "Rx state:"), |
| 117 | }; |
| 118 | |
| 119 | std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs; |
| 120 | cfgs.emplace_back(generateGenericStateResidencyConfigs(stateConfig, stateHeaders), |
| 121 | "UWB", ""); |
| 122 | |
| 123 | p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>( |
| 124 | "/sys/devices/platform/10d30000.spi/spi_master/spi10/spi10.0/uwb/power_stats", cfgs)); |
| 125 | } |
| 126 | |
Benjamin Schwartz | c798cb3 | 2021-03-18 17:55:40 -0700 | [diff] [blame] | 127 | int main() { |
chungkai | e593aaa | 2022-04-06 10:11:12 +0000 | [diff] [blame] | 128 | struct stat buffer; |
| 129 | |
Benjamin Schwartz | c798cb3 | 2021-03-18 17:55:40 -0700 | [diff] [blame] | 130 | LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting."; |
| 131 | |
| 132 | // single thread |
| 133 | ABinderProcess_setThreadPoolMaxThreadCount(0); |
| 134 | |
| 135 | std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>(); |
| 136 | |
| 137 | addGs101CommonDataProviders(p); |
Benjamin Schwartz | e04673c | 2021-04-05 22:26:29 -0700 | [diff] [blame] | 138 | addDisplay(p); |
Jacky Liu | 78c2601 | 2024-02-02 12:59:20 +0800 | [diff] [blame] | 139 | if (!stat("/sys/devices/platform/10960000.hsi2c/i2c-7/7-0008/power_stats", &buffer)) { |
| 140 | addNFC(p, "/sys/devices/platform/10960000.hsi2c/i2c-7/7-0008/power_stats"); |
chungkai | e593aaa | 2022-04-06 10:11:12 +0000 | [diff] [blame] | 141 | } |
Benjamin Schwartz | c798cb3 | 2021-03-18 17:55:40 -0700 | [diff] [blame] | 142 | const std::string instance = std::string() + PowerStats::descriptor + "/default"; |
| 143 | binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str()); |
| 144 | LOG_ALWAYS_FATAL_IF(status != STATUS_OK); |
| 145 | |
| 146 | ABinderProcess_joinThreadPool(); |
| 147 | return EXIT_FAILURE; // should not reach |
| 148 | } |