bcl: Add Mitigation Logger
Mitigation Logger logs battery related information for 1 second when it
is triggered by under voltage or over current interrupts. Information
collected is to help debug system brownout.
Bug: 228383769
Test: Boot and Test
Signed-off-by: George Lee <geolee@google.com>
Change-Id: I24e5075b12a9bb3f16241de268254bde38c133d0
diff --git a/battery_mitigation/Android.bp b/battery_mitigation/Android.bp
new file mode 100644
index 0000000..3c0b882
--- /dev/null
+++ b/battery_mitigation/Android.bp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+soong_namespace {
+ imports: [
+ "hardware/google/pixel",
+ ],
+}
+
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+cc_binary {
+ vendor: true,
+ name: "battery_mitigation",
+ relative_install_path: "hw",
+ proprietary: true,
+ init_rc: ["battery_mitigation.rc"],
+ shared_libs: [
+ "libpixelmitigation",
+ "libbase",
+ "libbinder_ndk",
+ "libcutils",
+ "libhardware",
+ "libhidlbase",
+ "liblog",
+ "libutils",
+ "android.hardware.thermal@2.0"
+ ],
+ srcs: [
+ "battery_mitigation.cpp",
+ ],
+ cflags: [
+ "-Wall",
+ "-Werror",
+ ],
+}
diff --git a/battery_mitigation/battery_mitigation.cpp b/battery_mitigation/battery_mitigation.cpp
new file mode 100644
index 0000000..2fa6721
--- /dev/null
+++ b/battery_mitigation/battery_mitigation.cpp
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "battery-mitigation"
+
+#include <battery_mitigation/BatteryMitigation.h>
+
+using android::hardware::google::pixel::BatteryMitigation;
+using android::hardware::google::pixel::MitigationConfig;
+
+android::sp<BatteryMitigation> bmSp;
+
+const struct MitigationConfig::Config cfg = {
+ .SystemPath = {
+ "/dev/thermal/tz-by-name/batoilo/temp",
+ "/dev/thermal/tz-by-name/smpl_gm/temp",
+ "/dev/thermal/tz-by-name/soc/temp",
+ "/dev/thermal/tz-by-name/vdroop1/temp",
+ "/dev/thermal/tz-by-name/vdroop2/temp",
+ "/dev/thermal/tz-by-name/ocp_gpu/temp",
+ "/dev/thermal/tz-by-name/ocp_tpu/temp",
+ "/dev/thermal/tz-by-name/soft_ocp_cpu2/temp",
+ "/dev/thermal/tz-by-name/soft_ocp_cpu1/temp",
+ "/dev/thermal/tz-by-name/battery/temp",
+ "/dev/thermal/tz-by-name/battery_cycle/temp",
+ "/sys/bus/iio/devices/iio:device0/lpf_power",
+ "/sys/bus/iio/devices/iio:device1/lpf_power",
+ "/dev/thermal/cdev-by-name/thermal-cpufreq-2/cur_state",
+ "/dev/thermal/cdev-by-name/thermal-cpufreq-1/cur_state",
+ "/dev/thermal/cdev-by-name/thermal-gpufreq-0/cur_state",
+ "/dev/thermal/cdev-by-name/tpu_cooling/cur_state",
+ "/dev/thermal/cdev-by-name/CAM/cur_state",
+ "/dev/thermal/cdev-by-name/DISP/cur_state",
+ "/dev/thermal/cdev-by-name/gxp-cooling/cur_state",
+ "/sys/class/power_supply/battery/voltage_now",
+ "/sys/class/power_supply/battery/current_now",
+ },
+ .FilteredZones = {
+ "batoilo",
+ "vdroop1",
+ "vdroop2",
+ "smpl_gm",
+ },
+ .SystemName = {
+ "batoilo", "smpl_gm", "soc", "vdroop1", "vdroop2", "ocp_gpu",
+ "ocp_tpu", "soft_ocp_cpu2", "soft_ocp_cpu1", "battery", "battery_cycle",
+ "main", "sub", "CPU2", "CPU1", "GPU", "TPU", "CAM", "DISP", "NPU",
+ "voltage_now", "current_now",
+ },
+ .LogFilePath = "/data/vendor/mitigation/thismeal.txt",
+ .TimestampFormat = "%Y-%m-%d %H:%M:%S",
+};
+
+const char kReadyFilePath[] = "/sys/devices/virtual/pmic/mitigation/instruction/ready";
+const char kReadyProperty[] = "vendor.brownout.mitigation.ready";
+const char kLastMealPath[] = "/data/vendor/mitigation/lastmeal.txt";
+const char kBRRequestedProperty[] = "vendor.startup_bugreport_requested";
+const std::regex kTimestampRegex("^\\S+\\s[0-9]+:[0-9]+:[0-9]+\\S+$");
+
+int main(int /*argc*/, char ** /*argv*/) {
+ auto batteryMitigationStartTime = std::chrono::system_clock::now();
+ bmSp = new BatteryMitigation(cfg);
+ if (!bmSp) {
+ return 0;
+ }
+ bool mitigationLogTimeValid = bmSp->isMitigationLogTimeValid(batteryMitigationStartTime,
+ cfg.LogFilePath,
+ cfg.TimestampFormat,
+ kTimestampRegex);
+ int startupBugreport = android::base::GetIntProperty(kBRRequestedProperty, 0);
+ if (startupBugreport && mitigationLogTimeValid) {
+ std::ifstream src(cfg.LogFilePath, std::ios::in);
+ std::ofstream dst(kLastMealPath, std::ios::out);
+ dst << src.rdbuf();
+ }
+ bool isBatteryMitigationReady = false;
+ std::string ready_str;
+ int val = 0;
+ while (!isBatteryMitigationReady) {
+ if (!android::base::ReadFileToString(kReadyFilePath, &ready_str)) {
+ continue;
+ }
+ ready_str = android::base::Trim(ready_str);
+ if (!android::base::ParseInt(ready_str, &val)) {
+ continue;
+ }
+ if (val == 1) {
+ isBatteryMitigationReady = true;
+ }
+ }
+ android::base::SetProperty(kReadyProperty, "1");
+ while (true) {
+ pause();
+ }
+ return 0;
+}
diff --git a/battery_mitigation/battery_mitigation.rc b/battery_mitigation/battery_mitigation.rc
new file mode 100644
index 0000000..4c4e2c1
--- /dev/null
+++ b/battery_mitigation/battery_mitigation.rc
@@ -0,0 +1,93 @@
+on property:vendor.thermal.link_ready=1
+ mkdir /data/vendor/mitigation 0755 system system
+ chown system system /data/vendor/mitigation
+ start vendor.battery_mitigation
+
+on property:ro.boot.bootreason=reboot,uvlo,pmic,if
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,pmic,if
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,uvlo,pmic,main
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,uvlo,pmic,sub
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck1m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck2m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck3m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck4m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck5m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck6m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck7m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck8m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck9m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck10m
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck1s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck2s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck3s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck4s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck5s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck6s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck7s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck8s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck9s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buck10s
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buckds
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buckas
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buckcs
+ setprop vendor.startup_bugreport_requested 1
+
+on property:ro.boot.bootreason=reboot,ocp,buckbs
+ setprop vendor.startup_bugreport_requested 1
+
+service vendor.battery_mitigation /vendor/bin/hw/battery_mitigation
+ user system
+ group system
+ capabilities WAKE_ALARM BLOCK_SUSPEND
diff --git a/battery_mitigation/bcl.mk b/battery_mitigation/bcl.mk
new file mode 100644
index 0000000..09bb5fc
--- /dev/null
+++ b/battery_mitigation/bcl.mk
@@ -0,0 +1,4 @@
+PRODUCT_PACKAGES += battery_mitigation
+
+BOARD_VENDOR_SEPOLICY_DIRS += device/google/gs-common/battery_mitigation/sepolicy
+PRODUCT_SOONG_NAMESPACES += device/google/gs-common/battery_mitigation
diff --git a/battery_mitigation/sepolicy/battery_mitigation.te b/battery_mitigation/sepolicy/battery_mitigation.te
new file mode 100644
index 0000000..56b8373
--- /dev/null
+++ b/battery_mitigation/sepolicy/battery_mitigation.te
@@ -0,0 +1,21 @@
+type battery_mitigation, domain;
+type battery_mitigation_exec, exec_type, vendor_file_type, file_type;
+init_daemon_domain(battery_mitigation)
+get_prop(battery_mitigation, boot_status_prop)
+get_prop(battery_mitigation, vendor_startup_bugreport_requested_prop)
+set_prop(battery_mitigation, vendor_mitigation_ready_prop)
+
+hal_client_domain(battery_mitigation, hal_thermal);
+hal_client_domain(battery_mitigation, hal_health);
+
+r_dir_file(battery_mitigation, sysfs_batteryinfo)
+r_dir_file(battery_mitigation, sysfs_iio_devices)
+r_dir_file(battery_mitigation, sysfs_thermal)
+r_dir_file(battery_mitigation, thermal_link_device)
+r_dir_file(battery_mitigation, sysfs_odpm)
+allow battery_mitigation sysfs_bcl:dir r_dir_perms;
+allow battery_mitigation sysfs_bcl:file r_file_perms;
+allow battery_mitigation sysfs_bcl:lnk_file r_file_perms;
+allow battery_mitigation sysfs_thermal:lnk_file r_file_perms;
+allow battery_mitigation mitigation_vendor_data_file:dir rw_dir_perms;
+allow battery_mitigation mitigation_vendor_data_file:file create_file_perms;
diff --git a/battery_mitigation/sepolicy/file.te b/battery_mitigation/sepolicy/file.te
new file mode 100644
index 0000000..06bedad
--- /dev/null
+++ b/battery_mitigation/sepolicy/file.te
@@ -0,0 +1,3 @@
+type mitigation_vendor_data_file, file_type, data_file_type;
+type sysfs_bcl, sysfs_type, fs_type;
+type sysfs_odpm, sysfs_type, fs_type;
diff --git a/battery_mitigation/sepolicy/file_contexts b/battery_mitigation/sepolicy/file_contexts
new file mode 100644
index 0000000..2e88ba0
--- /dev/null
+++ b/battery_mitigation/sepolicy/file_contexts
@@ -0,0 +1,2 @@
+/vendor/bin/hw/battery_mitigation u:object_r:battery_mitigation_exec:s0
+/data/vendor/mitigation(/.*)? u:object_r:mitigation_vendor_data_file:s0
diff --git a/battery_mitigation/sepolicy/genfs_contexts b/battery_mitigation/sepolicy/genfs_contexts
new file mode 100644
index 0000000..66d63dd
--- /dev/null
+++ b/battery_mitigation/sepolicy/genfs_contexts
@@ -0,0 +1 @@
+genfscon sysfs /devices/virtual/pmic/mitigation u:object_r:sysfs_bcl:s0
diff --git a/battery_mitigation/sepolicy/property.te b/battery_mitigation/sepolicy/property.te
new file mode 100644
index 0000000..1e70d47
--- /dev/null
+++ b/battery_mitigation/sepolicy/property.te
@@ -0,0 +1,3 @@
+# Battery Mitigation
+vendor_internal_prop(vendor_mitigation_ready_prop)
+vendor_internal_prop(vendor_startup_bugreport_requested_prop)
diff --git a/battery_mitigation/sepolicy/property_contexts b/battery_mitigation/sepolicy/property_contexts
new file mode 100644
index 0000000..51f6aed
--- /dev/null
+++ b/battery_mitigation/sepolicy/property_contexts
@@ -0,0 +1,3 @@
+# Battery Mitigation
+vendor.brownout.mitigation.ready u:object_r:vendor_mitigation_ready_prop:s0
+vendor.startup_bugreport_requested u:object_r:vendor_startup_bugreport_requested_prop:s0