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 
19 #include <dataproviders/DisplayStateResidencyDataProvider.h>
20 #include <dataproviders/GenericStateResidencyDataProvider.h>
21 #include <dataproviders/PowerStatsEnergyConsumer.h>
22 #include <Gs101CommonDataProviders.h>
23 #include <PowerStatsAidl.h>
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>
30 #include <sys/stat.h>
31 
32 using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider;
33 using aidl::android::hardware::power::stats::EnergyConsumerType;
34 using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider;
35 using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
36 
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 
addDisplay(std::shared_ptr<PowerStats> p)44 void addDisplay(std::shared_ptr<PowerStats> p) {
45     // Add display residency stats
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         };
63 
64         p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>("Display",
65                 "/sys/class/backlight/panel0-backlight/state",
66                 states));
67     }
68 
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 
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,
84             EnergyConsumerType::DISPLAY, "display", {channelName}, "Display",
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 
addUwb(std::shared_ptr<PowerStats> p)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 
main()127 int main() {
128     struct stat buffer;
129 
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);
138     addDisplay(p);
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");
141     }
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 }
149