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 #include <PowerStatsAidl.h>
18 #include <Gs101CommonDataProviders.h>
19 #include "AocStateResidencyDataProvider.h"
20 #include "DevfreqStateResidencyDataProvider.h"
21 #include <DisplayMrrStateResidencyDataProvider.h>
22 #include "DvfsStateResidencyDataProvider.h"
23 #include "UfsStateResidencyDataProvider.h"
24 #include <dataproviders/GenericStateResidencyDataProvider.h>
25 #include <dataproviders/IioEnergyMeterDataProvider.h>
26 #include <dataproviders/PowerStatsEnergyConsumer.h>
27 #include <dataproviders/PowerStatsEnergyAttribution.h>
28 #include <dataproviders/PixelStateResidencyDataProvider.h>
29 
30 #include <android-base/logging.h>
31 #include <android-base/properties.h>
32 #include <android/binder_manager.h>
33 #include <android/binder_process.h>
34 #include <log/log.h>
35 
36 using aidl::android::hardware::power::stats::AocStateResidencyDataProvider;
37 using aidl::android::hardware::power::stats::DevfreqStateResidencyDataProvider;
38 using aidl::android::hardware::power::stats::DisplayMrrStateResidencyDataProvider;
39 using aidl::android::hardware::power::stats::DvfsStateResidencyDataProvider;
40 using aidl::android::hardware::power::stats::UfsStateResidencyDataProvider;
41 using aidl::android::hardware::power::stats::EnergyConsumerType;
42 using aidl::android::hardware::power::stats::GenericStateResidencyDataProvider;
43 using aidl::android::hardware::power::stats::IioEnergyMeterDataProvider;
44 using aidl::android::hardware::power::stats::PixelStateResidencyDataProvider;
45 using aidl::android::hardware::power::stats::PowerStatsEnergyConsumer;
46 
47 constexpr char kBootHwSoCRev[] = "ro.boot.hw.soc.rev";
48 
addAoC(std::shared_ptr<PowerStats> p)49 void addAoC(std::shared_ptr<PowerStats> p) {
50     // AoC clock is synced from "libaoc.c"
51     static const uint64_t AOC_CLOCK = 4096;
52     std::string prefix = "/sys/devices/platform/19000000.aoc/control/";
53 
54     // Add AoC cores (a32, ff1, hf0, and hf1)
55     std::vector<std::pair<std::string, std::string>> coreIds = {
56             {"AoC-A32", prefix + "a32_"},
57             {"AoC-FF1", prefix + "ff1_"},
58             {"AoC-HF1", prefix + "hf1_"},
59             {"AoC-HF0", prefix + "hf0_"},
60     };
61     std::vector<std::pair<std::string, std::string>> coreStates = {
62             {"DWN", "off"}, {"RET", "retention"}, {"WFI", "wfi"}};
63     p->addStateResidencyDataProvider(std::make_unique<AocStateResidencyDataProvider>(coreIds,
64             coreStates, AOC_CLOCK));
65 
66     // Add AoC voltage stats
67     std::vector<std::pair<std::string, std::string>> voltageIds = {
68             {"AoC-Voltage", prefix + "voltage_"},
69     };
70     std::vector<std::pair<std::string, std::string>> voltageStates = {{"NOM", "nominal"},
71                                                                       {"SUD", "super_underdrive"},
72                                                                       {"UUD", "ultra_underdrive"},
73                                                                       {"UD", "underdrive"}};
74     p->addStateResidencyDataProvider(
75             std::make_unique<AocStateResidencyDataProvider>(voltageIds, voltageStates, AOC_CLOCK));
76 
77     // Add AoC monitor mode
78     std::vector<std::pair<std::string, std::string>> monitorIds = {
79             {"AoC", prefix + "monitor_"},
80     };
81     std::vector<std::pair<std::string, std::string>> monitorStates = {
82             {"MON", "mode"},
83     };
84     p->addStateResidencyDataProvider(
85             std::make_unique<AocStateResidencyDataProvider>(monitorIds, monitorStates, AOC_CLOCK));
86 
87     // Add AoC restart count
88     const GenericStateResidencyDataProvider::StateResidencyConfig restartCountConfig = {
89             .entryCountSupported = true,
90             .entryCountPrefix = "",
91             .totalTimeSupported = false,
92             .lastEntrySupported = false,
93     };
94     const std::vector<std::pair<std::string, std::string>> restartCountHeaders = {
95             std::make_pair("RESTART", ""),
96     };
97     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
98     cfgs.emplace_back(
99             generateGenericStateResidencyConfigs(restartCountConfig, restartCountHeaders),
100             "AoC-Count", "");
101     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
102             "/sys/devices/platform/19000000.aoc/restart_count", cfgs));
103 }
104 
addDvfsStats(std::shared_ptr<PowerStats> p)105 void addDvfsStats(std::shared_ptr<PowerStats> p) {
106     // A constant to represent the number of nanoseconds in one millisecond
107     const int NS_TO_MS = 1000000;
108 
109     std::vector<DvfsStateResidencyDataProvider::Config> cfgs;
110 
111     cfgs.push_back({"MIF", {
112         std::make_pair("3172MHz", "3172000"),
113         std::make_pair("2730MHz", "2730000"),
114         std::make_pair("2535MHz", "2535000"),
115         std::make_pair("2288MHz", "2288000"),
116         std::make_pair("2028MHz", "2028000"),
117         std::make_pair("1716MHz", "1716000"),
118         std::make_pair("1539MHz", "1539000"),
119         std::make_pair("1352MHz", "1352000"),
120         std::make_pair("1014MHz", "1014000"),
121         std::make_pair("845MHz", "845000"),
122         std::make_pair("676MHz", "676000"),
123         std::make_pair("546MHz", "546000"),
124         std::make_pair("421MHz", "421000"),
125         std::make_pair("0MHz", "0"),
126     }});
127 
128     cfgs.push_back({"CL1", {
129         std::make_pair("2466MHz", "2466000"),
130         std::make_pair("2393MHz", "2393000"),
131         std::make_pair("2348MHz", "2348000"),
132         std::make_pair("2253MHz", "2253000"),
133         std::make_pair("2130MHz", "2130000"),
134         std::make_pair("1999MHz", "1999000"),
135         std::make_pair("1836MHz", "1836000"),
136         std::make_pair("1663MHz", "1663000"),
137         std::make_pair("1491MHz", "1491000"),
138         std::make_pair("1328MHz", "1328000"),
139         std::make_pair("1197MHz", "1197000"),
140         std::make_pair("1024MHz", "1024000"),
141         std::make_pair("910MHz", "910000"),
142         std::make_pair("799MHz", "799000"),
143         std::make_pair("696MHz", "696000"),
144         std::make_pair("533MHz", "533000"),
145         std::make_pair("400MHz", "400000"),
146         std::make_pair("0MHz", "0"),
147     }});
148 
149     // B0/B1 chips have different DVFS operating points than A0/A1 SoC
150     const int socRev = android::base::GetIntProperty(kBootHwSoCRev, 0);
151     if (socRev >= 2) {
152         cfgs.push_back({"CL0", {
153             std::make_pair("2196MHz", "2196000"),
154             std::make_pair("2098MHz", "2098000"),
155             std::make_pair("2024MHz", "2024000"),
156             std::make_pair("1950MHz", "1950000"),
157             std::make_pair("1803MHz", "1803000"),
158             std::make_pair("1704MHz", "1704000"),
159             std::make_pair("1598MHz", "1598000"),
160             std::make_pair("1401MHz", "1401000"),
161             std::make_pair("1328MHz", "1328000"),
162             std::make_pair("1197MHz", "1197000"),
163             std::make_pair("1098MHz", "1098000"),
164             std::make_pair("930MHz", "930000"),
165             std::make_pair("738MHz", "738000"),
166             std::make_pair("574MHz", "574000"),
167             std::make_pair("300MHz", "300000"),
168             std::make_pair("0MHz", "0"),
169         }});
170 
171         cfgs.push_back({"CL2", {
172             std::make_pair("3195MHz", "3195000"),
173             std::make_pair("3097MHz", "3097000"),
174             std::make_pair("2950MHz", "2950000"),
175             std::make_pair("2850MHz", "2850000"),
176             std::make_pair("2802MHz", "2802000"),
177             std::make_pair("2704MHz", "2704000"),
178             std::make_pair("2630MHz", "2630000"),
179             std::make_pair("2507MHz", "2507000"),
180             std::make_pair("2401MHz", "2401000"),
181             std::make_pair("2252MHz", "2252000"),
182             std::make_pair("2188MHz", "2188000"),
183             std::make_pair("2048MHz", "2048000"),
184             std::make_pair("1826MHz", "1826000"),
185             std::make_pair("1745MHz", "1745000"),
186             std::make_pair("1582MHz", "1582000"),
187             std::make_pair("1426MHz", "1426000"),
188             std::make_pair("1277MHz", "1277000"),
189             std::make_pair("1106MHz", "1106000"),
190             std::make_pair("984MHz", "984000"),
191             std::make_pair("851MHz", "851000"),
192             std::make_pair("500MHz", "500000"),
193             std::make_pair("0MHz", "0"),
194         }});
195 
196         cfgs.push_back({"TPU", {
197             std::make_pair("1230MHz", "1230000"),
198             std::make_pair("1066MHz", "1066000"),
199             std::make_pair("800MHz", "800000"),
200             std::make_pair("500MHz", "500000"),
201             std::make_pair("226MHz", "226000"),
202             std::make_pair("RET_SLOW", "6"),
203             std::make_pair("S_OFF", "5"),
204             std::make_pair("S_SLOW", "4"),
205             std::make_pair("DS_FAST", "3"),
206             std::make_pair("DS_SLOW", "2"),
207             std::make_pair("DS_OFF", "1"),
208             std::make_pair("OFF", "0"),
209         }});
210     } else {
211         cfgs.push_back({"CL0", {
212             std::make_pair("2196MHz", "2196000"),
213             std::make_pair("2098MHz", "2098000"),
214             std::make_pair("2024MHz", "2024000"),
215             std::make_pair("1950MHz", "1950000"),
216             std::make_pair("1868MHz", "1868000"),
217             std::make_pair("1745MHz", "1745000"),
218             std::make_pair("1598MHz", "1598000"),
219             std::make_pair("1459MHz", "1459000"),
220             std::make_pair("1328MHz", "1328000"),
221             std::make_pair("1197MHz", "1197000"),
222             std::make_pair("1098MHz", "1098000"),
223             std::make_pair("889MHz", "889000"),
224             std::make_pair("738MHz", "738000"),
225             std::make_pair("574MHz", "574000"),
226             std::make_pair("300MHz", "300000"),
227             std::make_pair("0MHz", "0"),
228         }});
229 
230         cfgs.push_back({"CL2", {
231             std::make_pair("3195MHz", "3195000"),
232             std::make_pair("3097MHz", "3097000"),
233             std::make_pair("2999MHz", "2999000"),
234             std::make_pair("2900MHz", "2900000"),
235             std::make_pair("2802MHz", "2802000"),
236             std::make_pair("2704MHz", "2704000"),
237             std::make_pair("2630MHz", "2630000"),
238             std::make_pair("2507MHz", "2507000"),
239             std::make_pair("2302MHz", "2302000"),
240             std::make_pair("2188MHz", "2188000"),
241             std::make_pair("2048MHz", "2048000"),
242             std::make_pair("1901MHz", "1901000"),
243             std::make_pair("1745MHz", "1745000"),
244             std::make_pair("1582MHz", "1582000"),
245             std::make_pair("1426MHz", "1426000"),
246             std::make_pair("1237MHz", "1237000"),
247             std::make_pair("1106MHz", "1106000"),
248             std::make_pair("984MHz", "984000"),
249             std::make_pair("848MHz", "848000"),
250             std::make_pair("500MHz", "500000"),
251             std::make_pair("0MHz", "0"),
252         }});
253 
254         cfgs.push_back({"TPU", {
255             std::make_pair("1393MHz", "1393000"),
256             std::make_pair("1180MHz", "1180000"),
257             std::make_pair("1049MHz", "1049000"),
258             std::make_pair("967MHz", "967000"),
259             std::make_pair("721MHz", "721000"),
260             std::make_pair("648MHz", "648000"),
261             std::make_pair("455MHz", "455000"),
262             std::make_pair("250MHz", "250000"),
263             std::make_pair("RET_SLOW", "6"),
264             std::make_pair("S_OFF", "5"),
265             std::make_pair("S_SLOW", "4"),
266             std::make_pair("DS_FAST", "3"),
267             std::make_pair("DS_SLOW", "2"),
268             std::make_pair("DS_OFF", "1"),
269             std::make_pair("OFF", "0"),
270         }});
271     }
272 
273     p->addStateResidencyDataProvider(std::make_unique<DvfsStateResidencyDataProvider>(
274             "/sys/devices/platform/acpm_stats/fvp_stats", NS_TO_MS, cfgs));
275 }
276 
addSoC(std::shared_ptr<PowerStats> p)277 void addSoC(std::shared_ptr<PowerStats> p) {
278     // A constant to represent the number of nanoseconds in one millisecond.
279     const int NS_TO_MS = 1000000;
280 
281     // ACPM stats are reported in nanoseconds. The transform function
282     // converts nanoseconds to milliseconds.
283     std::function<uint64_t(uint64_t)> acpmNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
284     const GenericStateResidencyDataProvider::StateResidencyConfig lpmStateConfig = {
285             .entryCountSupported = true,
286             .entryCountPrefix = "success_count:",
287             .totalTimeSupported = true,
288             .totalTimePrefix = "total_time_ns:",
289             .totalTimeTransform = acpmNsToMs,
290             .lastEntrySupported = true,
291             .lastEntryPrefix = "last_entry_time_ns:",
292             .lastEntryTransform = acpmNsToMs,
293     };
294     const GenericStateResidencyDataProvider::StateResidencyConfig downStateConfig = {
295             .entryCountSupported = true,
296             .entryCountPrefix = "down_count:",
297             .totalTimeSupported = true,
298             .totalTimePrefix = "total_down_time_ns:",
299             .totalTimeTransform = acpmNsToMs,
300             .lastEntrySupported = true,
301             .lastEntryPrefix = "last_down_time_ns:",
302             .lastEntryTransform = acpmNsToMs,
303     };
304     const GenericStateResidencyDataProvider::StateResidencyConfig reqStateConfig = {
305             .entryCountSupported = true,
306             .entryCountPrefix = "req_up_count:",
307             .totalTimeSupported = true,
308             .totalTimePrefix = "total_req_up_time_ns:",
309             .totalTimeTransform = acpmNsToMs,
310             .lastEntrySupported = true,
311             .lastEntryPrefix = "last_req_up_time_ns:",
312             .lastEntryTransform = acpmNsToMs,
313 
314     };
315     const std::vector<std::pair<std::string, std::string>> powerStateHeaders = {
316             std::make_pair("SICD", "SICD"),
317             std::make_pair("SLEEP", "SLEEP"),
318             std::make_pair("SLEEP_SLCMON", "SLEEP_SLCMON"),
319             std::make_pair("STOP", "STOP"),
320     };
321     const std::vector<std::pair<std::string, std::string>> mifReqStateHeaders = {
322             std::make_pair("AOC", "AOC"),
323             std::make_pair("GSA", "GSA"),
324     };
325     const std::vector<std::pair<std::string, std::string>> slcReqStateHeaders = {
326             std::make_pair("AOC", "AOC"),
327     };
328 
329     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
330     cfgs.emplace_back(generateGenericStateResidencyConfigs(lpmStateConfig, powerStateHeaders),
331             "LPM", "LPM:");
332     cfgs.emplace_back(generateGenericStateResidencyConfigs(downStateConfig, powerStateHeaders),
333             "MIF", "MIF:");
334     cfgs.emplace_back(generateGenericStateResidencyConfigs(reqStateConfig, mifReqStateHeaders),
335             "MIF-REQ", "MIF_REQ:");
336     cfgs.emplace_back(generateGenericStateResidencyConfigs(downStateConfig, powerStateHeaders),
337             "SLC", "SLC:");
338     cfgs.emplace_back(generateGenericStateResidencyConfigs(reqStateConfig, slcReqStateHeaders),
339             "SLC-REQ", "SLC_REQ:");
340 
341     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
342             "/sys/devices/platform/acpm_stats/soc_stats", cfgs));
343 }
344 
setEnergyMeter(std::shared_ptr<PowerStats> p)345 void setEnergyMeter(std::shared_ptr<PowerStats> p) {
346     std::vector<const std::string> deviceNames { "s2mpg10-odpm", "s2mpg11-odpm" };
347     p->setEnergyMeterDataProvider(std::make_unique<IioEnergyMeterDataProvider>(deviceNames, true));
348 }
349 
addCPUclusters(std::shared_ptr<PowerStats> p)350 void addCPUclusters(std::shared_ptr<PowerStats> p) {
351     // A constant to represent the number of nanoseconds in one millisecond.
352     const int NS_TO_MS = 1000000;
353 
354     std::function<uint64_t(uint64_t)> acpmNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
355     const GenericStateResidencyDataProvider::StateResidencyConfig cpuStateConfig = {
356             .entryCountSupported = true,
357             .entryCountPrefix = "down_count:",
358             .totalTimeSupported = true,
359             .totalTimePrefix = "total_down_time_ns:",
360             .totalTimeTransform = acpmNsToMs,
361             .lastEntrySupported = true,
362             .lastEntryPrefix = "last_down_time_ns:",
363             .lastEntryTransform = acpmNsToMs,
364     };
365 
366     const std::vector<std::pair<std::string, std::string>> cpuStateHeaders = {
367             std::make_pair("DOWN", ""),
368     };
369 
370     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
371     for (std::string name : {"CORE00", "CORE01", "CORE02", "CORE03", "CORE10", "CORE11",
372                                 "CORE20", "CORE21", "CLUSTER0", "CLUSTER1", "CLUSTER2"}) {
373         cfgs.emplace_back(generateGenericStateResidencyConfigs(cpuStateConfig, cpuStateHeaders),
374             name, name);
375     }
376 
377     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
378             "/sys/devices/platform/acpm_stats/core_stats", cfgs));
379 
380     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
381             EnergyConsumerType::CPU_CLUSTER, "CPUCL0", {"S4M_VDD_CPUCL0"}));
382     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
383             EnergyConsumerType::CPU_CLUSTER, "CPUCL1", {"S3M_VDD_CPUCL1"}));
384     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
385             EnergyConsumerType::CPU_CLUSTER, "CPUCL2", {"S2M_VDD_CPUCL2"}));
386 }
387 
addGPU(std::shared_ptr<PowerStats> p)388 void addGPU(std::shared_ptr<PowerStats> p) {
389     // Add gpu energy consumer
390     std::map<std::string, int32_t> stateCoeffs;
391     const int socRev = android::base::GetIntProperty(kBootHwSoCRev, 0);
392 
393     // B0/B1 chips have different GPU DVFS operating points than A0/A1 SoC
394     if (socRev >= 2) {
395         stateCoeffs = {
396             {"151000",  642},
397             {"202000",  890},
398             {"251000", 1102},
399             {"302000", 1308},
400             {"351000", 1522},
401             {"400000", 1772},
402             {"471000", 2105},
403             {"510000", 2292},
404             {"572000", 2528},
405             {"701000", 3127},
406             {"762000", 3452},
407             {"848000", 4044}};
408     } else {
409         stateCoeffs = {
410             {"151000",  843},
411             {"302000", 1529},
412             {"455000", 2298},
413             {"572000", 2866},
414             {"670000", 3191}};
415     }
416 
417     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndAttrConsumer(p,
418             EnergyConsumerType::OTHER, "GPU", {"S2S_VDD_G3D"},
419             {{UID_TIME_IN_STATE, "/sys/devices/platform/1c500000.mali/uid_time_in_state"}},
420             stateCoeffs));
421 
422     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>("GPU",
423             "/sys/bus/platform/devices/1c500000.mali"));
424 }
425 
addMobileRadio(std::shared_ptr<PowerStats> p)426 void addMobileRadio(std::shared_ptr<PowerStats> p)
427 {
428     // A constant to represent the number of microseconds in one millisecond.
429     const int US_TO_MS = 1000;
430 
431     // modem power_stats are reported in microseconds. The transform function
432     // converts microseconds to milliseconds.
433     std::function<uint64_t(uint64_t)> modemUsToMs = [](uint64_t a) { return a / US_TO_MS; };
434     const GenericStateResidencyDataProvider::StateResidencyConfig powerStateConfig = {
435             .entryCountSupported = true,
436             .entryCountPrefix = "count:",
437             .totalTimeSupported = true,
438             .totalTimePrefix = "duration_usec:",
439             .totalTimeTransform = modemUsToMs,
440             .lastEntrySupported = true,
441             .lastEntryPrefix = "last_entry_timestamp_usec:",
442             .lastEntryTransform = modemUsToMs,
443     };
444     const std::vector<std::pair<std::string, std::string>> powerStateHeaders = {
445             std::make_pair("SLEEP", "SLEEP:"),
446     };
447 
448     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
449     cfgs.emplace_back(generateGenericStateResidencyConfigs(powerStateConfig, powerStateHeaders),
450             "MODEM", "");
451 
452     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
453             "/sys/devices/platform/cpif/modem/power_stats", cfgs));
454 
455     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
456             EnergyConsumerType::MOBILE_RADIO, "MODEM", {"VSYS_PWR_MODEM", "VSYS_PWR_RFFE"}));
457 }
458 
addGNSS(std::shared_ptr<PowerStats> p)459 void addGNSS(std::shared_ptr<PowerStats> p)
460 {
461     // A constant to represent the number of microseconds in one millisecond.
462     const int US_TO_MS = 1000;
463 
464     // gnss power_stats are reported in microseconds. The transform function
465     // converts microseconds to milliseconds.
466     std::function<uint64_t(uint64_t)> gnssUsToMs = [](uint64_t a) { return a / US_TO_MS; };
467 
468     const GenericStateResidencyDataProvider::StateResidencyConfig gnssStateConfig = {
469         .entryCountSupported = true,
470         .entryCountPrefix = "count:",
471         .totalTimeSupported = true,
472         .totalTimePrefix = "duration_usec:",
473         .totalTimeTransform = gnssUsToMs,
474         .lastEntrySupported = true,
475         .lastEntryPrefix = "last_entry_timestamp_usec:",
476         .lastEntryTransform = gnssUsToMs,
477     };
478 
479     const std::vector<std::pair<std::string, std::string>> gnssStateHeaders = {
480         std::make_pair("ON", "GPS_ON:"),
481         std::make_pair("OFF", "GPS_OFF:"),
482     };
483 
484     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
485     cfgs.emplace_back(generateGenericStateResidencyConfigs(gnssStateConfig, gnssStateHeaders),
486             "GPS", "");
487 
488     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
489             "/dev/bbd_pwrstat", cfgs));
490 
491     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterConsumer(p,
492             EnergyConsumerType::GNSS, "GPS", {"L9S_GNSS_CORE"}));
493 }
494 
addPCIe(std::shared_ptr<PowerStats> p)495 void addPCIe(std::shared_ptr<PowerStats> p) {
496     // Add PCIe power entities for Modem and WiFi
497     const GenericStateResidencyDataProvider::StateResidencyConfig pcieStateConfig = {
498         .entryCountSupported = true,
499         .entryCountPrefix = "Cumulative count:",
500         .totalTimeSupported = true,
501         .totalTimePrefix = "Cumulative duration msec:",
502         .lastEntrySupported = true,
503         .lastEntryPrefix = "Last entry timestamp msec:",
504     };
505     const std::vector<std::pair<std::string, std::string>> pcieStateHeaders = {
506         std::make_pair("UP", "Link up:"),
507         std::make_pair("DOWN", "Link down:"),
508     };
509 
510     // Add PCIe - Modem
511     const std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> pcieModemCfgs = {
512         {generateGenericStateResidencyConfigs(pcieStateConfig, pcieStateHeaders), "PCIe-Modem",
513                 "Version: 1"}
514     };
515 
516     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
517             "/sys/devices/platform/11920000.pcie/power_stats", pcieModemCfgs));
518 
519     // Add PCIe - WiFi
520     const std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> pcieWifiCfgs = {
521         {generateGenericStateResidencyConfigs(pcieStateConfig, pcieStateHeaders),
522             "PCIe-WiFi", "Version: 1"}
523     };
524 
525     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
526             "/sys/devices/platform/14520000.pcie/power_stats", pcieWifiCfgs));
527 }
528 
addWifi(std::shared_ptr<PowerStats> p)529 void addWifi(std::shared_ptr<PowerStats> p) {
530     // The transform function converts microseconds to milliseconds.
531     std::function<uint64_t(uint64_t)> usecToMs = [](uint64_t a) { return a / 1000; };
532     const GenericStateResidencyDataProvider::StateResidencyConfig stateConfig = {
533         .entryCountSupported = true,
534         .entryCountPrefix = "count:",
535         .totalTimeSupported = true,
536         .totalTimePrefix = "duration_usec:",
537         .totalTimeTransform = usecToMs,
538         .lastEntrySupported = true,
539         .lastEntryPrefix = "last_entry_timestamp_usec:",
540         .lastEntryTransform = usecToMs,
541     };
542     const GenericStateResidencyDataProvider::StateResidencyConfig pcieStateConfig = {
543         .entryCountSupported = true,
544         .entryCountPrefix = "count:",
545         .totalTimeSupported = true,
546         .totalTimePrefix = "duration_usec:",
547         .totalTimeTransform = usecToMs,
548         .lastEntrySupported = false,
549     };
550 
551     const std::vector<std::pair<std::string, std::string>> stateHeaders = {
552         std::make_pair("AWAKE", "AWAKE:"),
553         std::make_pair("ASLEEP", "ASLEEP:"),
554 
555     };
556     const std::vector<std::pair<std::string, std::string>> pcieStateHeaders = {
557         std::make_pair("L0", "L0:"),
558         std::make_pair("L1", "L1:"),
559         std::make_pair("L1_1", "L1_1:"),
560         std::make_pair("L1_2", "L1_2:"),
561         std::make_pair("L2", "L2:"),
562     };
563 
564     const std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs = {
565         {generateGenericStateResidencyConfigs(stateConfig, stateHeaders), "WIFI", "WIFI"},
566         {generateGenericStateResidencyConfigs(pcieStateConfig, pcieStateHeaders), "WIFI-PCIE",
567                 "WIFI-PCIE"}
568     };
569 
570     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>("/sys/wifi/power_stats",
571             cfgs));
572 }
573 
addUfs(std::shared_ptr<PowerStats> p)574 void addUfs(std::shared_ptr<PowerStats> p) {
575     p->addStateResidencyDataProvider(std::make_unique<UfsStateResidencyDataProvider>("/sys/bus/platform/devices/14700000.ufs/ufs_stats/"));
576 }
577 
addPowerDomains(std::shared_ptr<PowerStats> p)578 void addPowerDomains(std::shared_ptr<PowerStats> p) {
579     // A constant to represent the number of nanoseconds in one millisecond.
580     const int NS_TO_MS = 1000000;
581 
582     std::function<uint64_t(uint64_t)> acpmNsToMs = [](uint64_t a) { return a / NS_TO_MS; };
583     const GenericStateResidencyDataProvider::StateResidencyConfig cpuStateConfig = {
584             .entryCountSupported = true,
585             .entryCountPrefix = "on_count:",
586             .totalTimeSupported = true,
587             .totalTimePrefix = "total_on_time_ns:",
588             .totalTimeTransform = acpmNsToMs,
589             .lastEntrySupported = true,
590             .lastEntryPrefix = "last_on_time_ns:",
591             .lastEntryTransform = acpmNsToMs,
592     };
593 
594     const std::vector<std::pair<std::string, std::string>> cpuStateHeaders = {
595             std::make_pair("ON", ""),
596     };
597 
598     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
599     for (std::string name : {"pd-tpu", "pd-bo", "pd-tnr", "pd-gdc", "pd-mcsc", "pd-ipp",
600                                 "pd-g3aa", "pd-dns", "pd-itp", "pd-pdp", "pd-csis",
601                                 "pd-mfc", "pd-g2d", "pd-dpu", "pd-disp", "pd-hsi0",
602                                 "pd-embedded_g3d", "pd-g3d", "pd-eh"}) {
603         cfgs.emplace_back(generateGenericStateResidencyConfigs(cpuStateConfig, cpuStateHeaders),
604             name, name + ":");
605     }
606 
607     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
608             "/sys/devices/platform/acpm_stats/pd_stats", cfgs));
609 }
610 
addDevfreq(std::shared_ptr<PowerStats> p)611 void addDevfreq(std::shared_ptr<PowerStats> p) {
612     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
613             "INT",
614             "/sys/devices/platform/17000020.devfreq_int/devfreq/17000020.devfreq_int"));
615 
616     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
617             "INTCAM",
618             "/sys/devices/platform/17000030.devfreq_intcam/devfreq/17000030.devfreq_intcam"));
619 
620     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
621             "DISP",
622             "/sys/devices/platform/17000040.devfreq_disp/devfreq/17000040.devfreq_disp"));
623 
624     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
625             "CAM",
626             "/sys/devices/platform/17000050.devfreq_cam/devfreq/17000050.devfreq_cam"));
627 
628     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
629             "TNR",
630             "/sys/devices/platform/17000060.devfreq_tnr/devfreq/17000060.devfreq_tnr"));
631 
632     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
633             "MFC",
634             "/sys/devices/platform/17000070.devfreq_mfc/devfreq/17000070.devfreq_mfc"));
635 
636     p->addStateResidencyDataProvider(std::make_unique<DevfreqStateResidencyDataProvider>(
637             "BO",
638             "/sys/devices/platform/17000080.devfreq_bo/devfreq/17000080.devfreq_bo"));
639 }
640 
addTPU(std::shared_ptr<PowerStats> p)641 void addTPU(std::shared_ptr<PowerStats> p) {
642     std::map<std::string, int32_t> stateCoeffs;
643 
644     stateCoeffs = {
645         {"226000",  49.25},
646         {"500000",  73.80},
647         {"800000",  86.99},
648         {"1066000", 103.93},
649         {"1230000", 108.10}};
650 
651     p->addEnergyConsumer(PowerStatsEnergyConsumer::createMeterAndAttrConsumer(p,
652             EnergyConsumerType::OTHER, "TPU", {"S10M_VDD_TPU"},
653             {{UID_TIME_IN_STATE, "/sys/class/edgetpu/edgetpu-soc/device/tpu_usage"}},
654             stateCoeffs));
655 }
656 
657 /**
658  * Unlike other data providers, which source power entity state residency data from the kernel,
659  * this data provider acts as a general-purpose channel for state residency data providers
660  * that live in user space. Entities are defined here and user space clients of this provider's
661  * vendor service register callbacks to provide state residency data for their given pwoer entity.
662  */
addPixelStateResidencyDataProvider(std::shared_ptr<PowerStats> p)663 void addPixelStateResidencyDataProvider(std::shared_ptr<PowerStats> p) {
664 
665     auto pixelSdp = std::make_unique<PixelStateResidencyDataProvider>();
666 
667     pixelSdp->addEntity("Bluetooth", {{0, "Idle"}, {1, "Active"}, {2, "Tx"}, {3, "Rx"}});
668 
669     pixelSdp->start();
670 
671     p->addStateResidencyDataProvider(std::move(pixelSdp));
672 }
673 
addDisplayMrr(std::shared_ptr<PowerStats> p)674 void addDisplayMrr(std::shared_ptr<PowerStats> p) {
675     p->addStateResidencyDataProvider(std::make_unique<DisplayMrrStateResidencyDataProvider>(
676             "Display", "/sys/class/drm/card0/device/primary-panel/"));
677 }
678 
addGs101CommonDataProviders(std::shared_ptr<PowerStats> p)679 void addGs101CommonDataProviders(std::shared_ptr<PowerStats> p) {
680     setEnergyMeter(p);
681 
682     addPixelStateResidencyDataProvider(p);
683     addAoC(p);
684     addDvfsStats(p);
685     addSoC(p);
686     addCPUclusters(p);
687     addGPU(p);
688     addMobileRadio(p);
689     addGNSS(p);
690     addPCIe(p);
691     addWifi(p);
692     addUfs(p);
693     addPowerDomains(p);
694     addDevfreq(p);
695     addTPU(p);
696 }
697 
addNFC(std::shared_ptr<PowerStats> p,const std::string & path)698 void addNFC(std::shared_ptr<PowerStats> p, const std::string& path) {
699     const GenericStateResidencyDataProvider::StateResidencyConfig nfcStateConfig = {
700         .entryCountSupported = true,
701         .entryCountPrefix = "Cumulative count:",
702         .totalTimeSupported = true,
703         .totalTimePrefix = "Cumulative duration msec:",
704         .lastEntrySupported = true,
705         .lastEntryPrefix = "Last entry timestamp msec:",
706     };
707     const std::vector<std::pair<std::string, std::string>> nfcStateHeaders = {
708         std::make_pair("IDLE", "Idle mode:"),
709         std::make_pair("ACTIVE", "Active mode:"),
710         std::make_pair("ACTIVE-RW", "Active Reader/Writer mode:"),
711     };
712 
713     std::vector<GenericStateResidencyDataProvider::PowerEntityConfig> cfgs;
714     cfgs.emplace_back(generateGenericStateResidencyConfigs(nfcStateConfig, nfcStateHeaders),
715             "NFC", "NFC subsystem");
716 
717     p->addStateResidencyDataProvider(std::make_unique<GenericStateResidencyDataProvider>(
718             path, cfgs));
719 }
720