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