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