1 /*
2  * Copyright (C) 2018 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 #ifndef HARDWARE_GOOGLE_PIXEL_PIXELSTATS_UEVENTLISTENER_H
18 #define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_UEVENTLISTENER_H
19 
20 #include <aidl/android/frameworks/stats/IStats.h>
21 #include <android-base/chrono_utils.h>
22 #include <pixelstats/BatteryCapacityReporter.h>
23 #include <pixelstats/ChargeStatsReporter.h>
24 #include <pixelstats/BatteryFGReporter.h>
25 #include <pixelstats/WaterEventReporter.h>
26 
27 
28 namespace android {
29 namespace hardware {
30 namespace google {
31 namespace pixel {
32 
33 using aidl::android::frameworks::stats::IStats;
34 /**
35  * A class to listen for uevents and report reliability events to
36  * the PixelStats HAL.
37  * Runs in a background thread if created with ListenForeverInNewThread().
38  * Alternatively, process one message at a time with ProcessUevent().
39  */
40 class UeventListener {
41   public:
42     struct UeventPaths {
43         const char *const AudioUevent;
44         const char *const SsocDetailsPath;
45         const char *const OverheatPath;
46         const char *const ChargeMetricsPath;
47         const char *const TypeCPartnerUevent;
48         const char *const TypeCPartnerVidPath;
49         const char *const TypeCPartnerPidPath;
50         const char *const WirelessChargerPtmcUevent;  // Deprecated.
51         const char *const WirelessChargerPtmcPath;    // Deprecated.
52         const char *const FwUpdatePath;
53         const std::vector<std::string> FGAbnlPath;
54     };
55     constexpr static const char *const ssoc_details_path =
56             "/sys/class/power_supply/battery/ssoc_details";
57     constexpr static const char *const overheat_path_default =
58             "/sys/devices/platform/soc/soc:google,overheat_mitigation";
59     constexpr static const char *const charge_metrics_path_default =
60             "/sys/class/power_supply/battery/charge_stats";
61     constexpr static const char *const typec_partner_vid_path_default =
62             "/sys/class/typec/port0-partner/identity/id_header";
63     constexpr static const char *const typec_partner_pid_path_default =
64             "/sys/class/typec/port0-partner/identity/product";
65     constexpr static const char *const typec_partner_uevent_default = "DEVTYPE=typec_partner";
66 
67     UeventListener(const std::string audio_uevent, const std::string ssoc_details_path = "",
68                    const std::string overheat_path = overheat_path_default,
69                    const std::string charge_metrics_path = charge_metrics_path_default,
70                    const std::string typec_partner_vid_path = typec_partner_vid_path_default,
71                    const std::string typec_partner_pid_path = typec_partner_pid_path_default,
72                    const std::string fw_update_path = "",
73                    const std::vector<std::string> fg_abnl_path = {""});
74     UeventListener(const struct UeventPaths &paths);
75 
76     bool ProcessUevent();  // Process a single Uevent.
77     void ListenForever();  // Process Uevents forever
78 
79   private:
80     bool ReadFileToInt(const std::string &path, int *val);
81     bool ReadFileToInt(const char *path, int *val);
82     void ReportMicStatusUevents(const std::shared_ptr<IStats> &stats_client, const char *devpath,
83                                 const char *mic_status);
84     void ReportMicBrokenOrDegraded(const std::shared_ptr<IStats> &stats_client, const int mic,
85                                    const bool isBroken);
86     void ReportUsbPortOverheatEvent(const std::shared_ptr<IStats> &stats_client,
87                                     const char *driver);
88     void ReportChargeStats(const std::shared_ptr<IStats> &stats_client, const std::string line,
89                            const std::string wline_at, const std::string wline_ac,
90                            const std::string pca_line);
91     void ReportVoltageTierStats(const std::shared_ptr<IStats> &stats_client, const char *line,
92                                 const bool has_wireless, const std::string wfile_contents);
93     void ReportChargeMetricsEvent(const std::shared_ptr<IStats> &stats_client, const char *driver);
94     void ReportBatteryCapacityFGEvent(const std::shared_ptr<IStats> &stats_client,
95                                       const char *subsystem);
96     void ReportTypeCPartnerId(const std::shared_ptr<IStats> &stats_client);
97     void ReportGpuEvent(const std::shared_ptr<IStats> &stats_client, const char *driver,
98                         const char *gpu_event_type, const char *gpu_event_info);
99     void ReportThermalAbnormalEvent(const std::shared_ptr<IStats> &stats_client,
100                                     const char *devpath, const char *thermal_abnormal_event_type,
101                                     const char *thermal_abnormal_event_info);
102     void ReportFGMetricsEvent(const std::shared_ptr<IStats> &stats_client, const char *driver);
103     void ReportWaterEvent(const std::shared_ptr<IStats> &stats_client,
104                           const char *driver, const char *devpath);
105 
106     const std::string kAudioUevent;
107     const std::string kBatterySSOCPath;
108     const std::string kUsbPortOverheatPath;
109     const std::string kChargeMetricsPath;
110     const std::string kTypeCPartnerUevent;
111     const std::string kTypeCPartnerVidPath;
112     const std::string kTypeCPartnerPidPath;
113     const std::string kFwUpdatePath;
114     const std::vector<std::string> kFGAbnlPath;
115 
116 
117     const std::unordered_map<std::string, PixelAtoms::GpuEvent::GpuEventType>
118             kGpuEventTypeStrToEnum{
119                     {"KMD_ERROR",
120                      PixelAtoms::GpuEvent::GpuEventType::GpuEvent_GpuEventType_MALI_KMD_ERROR},
121                     {"GPU_RESET",
122                      PixelAtoms::GpuEvent::GpuEventType::GpuEvent_GpuEventType_MALI_GPU_RESET}};
123 
124     const std::unordered_map<std::string, PixelAtoms::GpuEvent::GpuEventInfo>
125             kGpuEventInfoStrToEnum{
126                     {"CSG_REQ_STATUS_UPDATE",
127                      PixelAtoms::GpuEvent::GpuEventInfo::
128                              GpuEvent_GpuEventInfo_MALI_CSG_REQ_STATUS_UPDATE},
129                     {"CSG_SUSPEND",
130                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_CSG_SUSPEND},
131                     {"CSG_SLOTS_SUSPEND", PixelAtoms::GpuEvent::GpuEventInfo::
132                                                   GpuEvent_GpuEventInfo_MALI_CSG_SLOTS_SUSPEND},
133                     {"CSG_GROUP_SUSPEND", PixelAtoms::GpuEvent::GpuEventInfo::
134                                                   GpuEvent_GpuEventInfo_MALI_CSG_GROUP_SUSPEND},
135                     {"CSG_EP_CFG",
136                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_CSG_EP_CFG},
137                     {"CSG_SLOTS_START", PixelAtoms::GpuEvent::GpuEventInfo::
138                                                 GpuEvent_GpuEventInfo_MALI_CSG_SLOTS_START},
139                     {"GROUP_TERM",
140                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_GROUP_TERM},
141                     {"QUEUE_START",
142                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_QUEUE_START},
143                     {"QUEUE_STOP",
144                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_QUEUE_STOP},
145                     {"QUEUE_STOP_ACK",
146                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_QUEUE_STOP_ACK},
147                     {"CSG_SLOT_READY",
148                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_CSG_SLOT_READY},
149                     {"L2_PM_TIMEOUT",
150                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_L2_PM_TIMEOUT},
151                     {"PM_TIMEOUT",
152                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_PM_TIMEOUT},
153                     {"CSF_RESET_OK",
154                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_CSF_RESET_OK},
155                     {"CSF_RESET_FAILED", PixelAtoms::GpuEvent::GpuEventInfo::
156                                                  GpuEvent_GpuEventInfo_MALI_CSF_RESET_FAILED},
157                     {"TILER_OOM",
158                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_TILER_OOM},
159                     {"PROGRESS_TIMER",
160                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_PROGRESS_TIMER},
161                     {"CS_ERROR",
162                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_CS_ERROR},
163                     {"FW_ERROR",
164                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_FW_ERROR},
165                     {"PMODE_EXIT_TIMEOUT", PixelAtoms::GpuEvent::GpuEventInfo::
166                                                    GpuEvent_GpuEventInfo_MALI_PMODE_EXIT_TIMEOUT},
167                     {"PMODE_ENTRY_FAILURE", PixelAtoms::GpuEvent::GpuEventInfo::
168                                                     GpuEvent_GpuEventInfo_MALI_PMODE_ENTRY_FAILURE},
169                     {"GPU_PAGE_FAULT",
170                      PixelAtoms::GpuEvent::GpuEventInfo::GpuEvent_GpuEventInfo_MALI_GPU_PAGE_FAULT},
171                     {"MMU_AS_ACTIVE_STUCK", PixelAtoms::GpuEvent::GpuEventInfo::
172                                                     GpuEvent_GpuEventInfo_MALI_MMU_AS_ACTIVE_STUCK},
173                     {"TRACE_BUF_INVALID_SLOT",
174                      PixelAtoms::GpuEvent::GpuEventInfo::
175                              GpuEvent_GpuEventInfo_MALI_TRACE_BUF_INVALID_SLOT}};
176 
177     const std::unordered_map<std::string,
178                              PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType>
179             kThermalAbnormalityTypeStrToEnum{
180                     {"UNKNOWN", PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
181                                         ThermalSensorAbnormalityDetected_AbnormalityType_UNKNOWN},
182                     {"SENSOR_STUCK",
183                      PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
184                              ThermalSensorAbnormalityDetected_AbnormalityType_SENSOR_STUCK},
185                     {"EXTREME_HIGH_TEMP",
186                      PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
187                              ThermalSensorAbnormalityDetected_AbnormalityType_EXTREME_HIGH_TEMP},
188                     {"EXTREME_LOW_TEMP",
189                      PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
190                              ThermalSensorAbnormalityDetected_AbnormalityType_EXTREME_LOW_TEMP},
191                     {"HIGH_RISING_SPEED",
192                      PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
193                              ThermalSensorAbnormalityDetected_AbnormalityType_HIGH_RISING_SPEED},
194                     {"TEMP_READ_FAIL",
195                      PixelAtoms::ThermalSensorAbnormalityDetected::AbnormalityType::
196                              ThermalSensorAbnormalityDetected_AbnormalityType_TEMP_READ_FAIL},
197             };
198 
199     BatteryCapacityReporter battery_capacity_reporter_;
200     ChargeStatsReporter charge_stats_reporter_;
201     BatteryFGReporter battery_fg_reporter_;
202     WaterEventReporter water_event_reporter_;
203 
204     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
205     // store everything in the values array at the index of the field number
206     // -2.
207     const int kVendorAtomOffset = 2;
208 
209     int uevent_fd_;
210     int log_fd_;
211 };
212 
213 }  // namespace pixel
214 }  // namespace google
215 }  // namespace hardware
216 }  // namespace android
217 
218 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_UEVENTLISTENER_H
219