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/PowerStatsEnergyConsumer.h>
21 #include <DevfreqStateResidencyDataProvider.h>
22 #include <Gs201CommonDataProviders.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::DevfreqStateResidencyDataProvider;
33 using aidl::android::hardware::power::stats::DisplayStateResidencyDataProvider;
34 using aidl::android::hardware::power::stats::EnergyConsumerType;
35 using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
36 
addDisplay(std::shared_ptr<PowerStats> p)37 void addDisplay(std::shared_ptr<PowerStats> p) {
38     // Add display residency stats
39     struct stat buffer;
40     if (!stat("/sys/class/drm/card0/device/primary-panel/time_in_state", &buffer)) {
41         // time_in_state exists
42         addDisplayMrr(p);
43     } else {
44         // time_in_state doesn't exist
45         std::vector<std::string> states = {
46             "Off",
47             "LP: 1080x2400@30",
48             "On: 1080x2400@60",
49             "On: 1080x2400@90",
50             "HBM: 1080x2400@60",
51             "HBM: 1080x2400@90"};
52 
53         p->addStateResidencyDataProvider(std::make_unique<DisplayStateResidencyDataProvider>("Display",
54                 "/sys/class/backlight/panel0-backlight/state",
55                 states));
56     }
57 
58     // Add display energy consumer
59     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndEntityConsumer(p,
60             EnergyConsumerType::DISPLAY, "display", {"VSYS_PWR_DISPLAY"}, "Display",
61             {{"LP: 1080x2400@30", 1},
62              {"On: 1080x2400@60", 2},
63              {"On: 1080x2400@90", 3}}));
64 }
65 
addGPUGs202(std::shared_ptr<PowerStats> p)66 void addGPUGs202(std::shared_ptr<PowerStats> p) {
67     std::map<std::string, int32_t> stateCoeffs;
68 
69     // Add GPU state residency
70     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
71             "GPU",
72             "/sys/devices/platform/28000000.mali"));
73 
74     // Add GPU energy consumer
75     stateCoeffs = {
76         {"202000",  890},
77         {"251000", 1102},
78         {"302000", 1308},
79         {"351000", 1522},
80         {"400000", 1772},
81         {"434000", 1931},
82         {"471000", 2105},
83         {"510000", 2292},
84         {"572000", 2528},
85         {"633000", 2811},
86         {"701000", 3127},
87         {"762000", 3452},
88         {"848000", 4044}};
89 
90     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndAttrConsumer(
91             p,
92             EnergyConsumerType::OTHER,
93             "GPU",
94             {"S2S_VDD_G3D", "S8S_VDD_G3D_L2"},
95             {{UID_TIME_IN_STATE, "/sys/devices/platform/28000000.mali/uid_time_in_state"}},
96             stateCoeffs));
97 }
98 
main()99 int main() {
100     struct stat buffer;
101 
102     LOG(INFO) << "Pixel PowerStats HAL AIDL Service is starting.";
103 
104     // single thread
105     ABinderProcess_setThreadPoolMaxThreadCount(0);
106 
107     std::shared_ptr<PowerStats> p = ndk::SharedRefBase::make<PowerStats>();
108 
109     setEnergyMeter(p);
110     addAoC(p);
111     addCPUclusters(p);
112     addDisplay(p);
113     addSoC(p);
114     addGNSS(p);
115     addMobileRadio(p);
116     addPCIe(p);
117     addWlan(p);
118     addTPU(p);
119     addUfs(p);
120     if (!stat("/sys/devices/platform/10970000.hsi2c/i2c-8/8-0008/power_stats", &buffer)) {
121         addNFC(p, "/sys/devices/platform/10970000.hsi2c/i2c-8/8-0008/power_stats");
122     }
123     addPowerDomains(p);
124     addDevfreq(p);
125     addGPUGs202(p);
126     addDvfsStats(p);
127 
128     const std::string instance = std::string() + PowerStats::descriptor + "/default";
129     binder_status_t status = AServiceManager_addService(p->asBinder().get(), instance.c_str());
130     LOG_ALWAYS_FATAL_IF(status != STATUS_OK);
131 
132     ABinderProcess_joinThreadPool();
133     return EXIT_FAILURE;  // should not reach
134 }
135