Merge "Set up common sepolicy for CHRE" into main
diff --git a/aoc/sepolicy/aocd.te b/aoc/sepolicy/aocd.te
index b2bfd13..464e46b 100644
--- a/aoc/sepolicy/aocd.te
+++ b/aoc/sepolicy/aocd.te
@@ -20,4 +20,5 @@
 
 # set properties
 set_prop(aocd, vendor_aoc_prop)
-set_prop(aocd, vendor_timeout_aoc_prop)
\ No newline at end of file
+set_prop(aocd, vendor_timeout_aoc_prop)
+get_prop(aocd, vendor_volte_mif_off)
\ No newline at end of file
diff --git a/aoc/sepolicy/property.te b/aoc/sepolicy/property.te
index c2f5695..e6d715b 100644
--- a/aoc/sepolicy/property.te
+++ b/aoc/sepolicy/property.te
@@ -1,3 +1,4 @@
 # AoC
 vendor_internal_prop(vendor_aoc_prop)
-vendor_internal_prop(vendor_timeout_aoc_prop)
\ No newline at end of file
+vendor_internal_prop(vendor_timeout_aoc_prop)
+vendor_internal_prop(vendor_volte_mif_off)
diff --git a/aoc/sepolicy/property_contexts b/aoc/sepolicy/property_contexts
index 3c2acb6..475272a 100644
--- a/aoc/sepolicy/property_contexts
+++ b/aoc/sepolicy/property_contexts
@@ -1,3 +1,4 @@
 # AoC
 vendor.aoc.firmware.version                     u:object_r:vendor_aoc_prop:s0
-persist.vendor.aoc.status_request_timed_out     u:object_r:vendor_timeout_aoc_prop:s0
\ No newline at end of file
+persist.vendor.aoc.status_request_timed_out     u:object_r:vendor_timeout_aoc_prop:s0
+persist.vendor.radio.volte_mif_off              u:object_r:vendor_volte_mif_off:s0
\ No newline at end of file
diff --git a/battery_mitigation/battery_mitigation.cpp b/battery_mitigation/battery_mitigation.cpp
index fb81b20..a4e28b9 100644
--- a/battery_mitigation/battery_mitigation.cpp
+++ b/battery_mitigation/battery_mitigation.cpp
@@ -73,6 +73,7 @@
 };
 
 const struct MitigationConfig::EventThreadConfig eventThreadCfg = {
+    .BrownoutStatsEnablePath = "/sys/devices/virtual/pmic/mitigation/br_stats/enable_br_stats",
     .NumericSysfsStatPaths = {
         {"cpu0_freq", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"},
         {"cpu1_freq", "/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq"},
diff --git a/battery_mitigation/battery_mitigation.rc b/battery_mitigation/battery_mitigation.rc
index 6c8f5cc..bc95fd3 100644
--- a/battery_mitigation/battery_mitigation.rc
+++ b/battery_mitigation/battery_mitigation.rc
@@ -1,6 +1,7 @@
 on property:vendor.thermal.link_ready=1
     mkdir /data/vendor/mitigation 0755 system system
     chown system system /data/vendor/mitigation
+    chown system system /sys/devices/virtual/pmic/mitigation/br_stats/enable_br_stats
     start vendor.battery_mitigation
 
 on property:vendor.thermal.link_ready=1 && property:ro.boot.bootreason=reboot,uvlo,pmic,if && property:sys.boot.reason=reboot,uvlo,pmic,if
diff --git a/battery_mitigation/sepolicy/vendor/battery_mitigation.te b/battery_mitigation/sepolicy/vendor/battery_mitigation.te
index 4b5bc48..ee0780a 100644
--- a/battery_mitigation/sepolicy/vendor/battery_mitigation.te
+++ b/battery_mitigation/sepolicy/vendor/battery_mitigation.te
@@ -19,7 +19,7 @@
 r_dir_file(battery_mitigation, sysfs_odpm)
 r_dir_file(battery_mitigation, sysfs_power_stats)
 allow battery_mitigation sysfs_bcl:dir r_dir_perms;
-allow battery_mitigation sysfs_bcl:file r_file_perms;
+allow battery_mitigation sysfs_bcl:file rw_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;
diff --git a/display_logbuffer/Android.bp b/display_logbuffer/Android.bp
new file mode 100644
index 0000000..e2859d9
--- /dev/null
+++ b/display_logbuffer/Android.bp
@@ -0,0 +1,19 @@
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+cc_binary {
+    name: "dump_display_logbuffer",
+    srcs: ["dump_display_logbuffer.cpp"],
+    init_rc: ["init.display_logbuffer.rc"],
+    cflags: [
+        "-Wall",
+        "-Wextra",
+        "-Werror",
+    ],
+    shared_libs: [
+        "libdump",
+    ],
+    vendor: true,
+    relative_install_path: "dump",
+}
diff --git a/display_logbuffer/dump.mk b/display_logbuffer/dump.mk
new file mode 100644
index 0000000..9808c1d
--- /dev/null
+++ b/display_logbuffer/dump.mk
@@ -0,0 +1,3 @@
+BOARD_VENDOR_SEPOLICY_DIRS += device/google/gs-common/display_logbuffer/sepolicy
+
+PRODUCT_PACKAGES += dump_display_logbuffer
diff --git a/display_logbuffer/dump_display_logbuffer.cpp b/display_logbuffer/dump_display_logbuffer.cpp
new file mode 100644
index 0000000..7477c8c
--- /dev/null
+++ b/display_logbuffer/dump_display_logbuffer.cpp
@@ -0,0 +1,32 @@
+/*
+ * Copyright 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.
+ */
+#include <dump/pixel_dump.h>
+#include <unistd.h>
+
+const char* logbuffer_paths[][2] = {
+    {"DSIM0", "/dev/logbuffer_dsim0"},
+    {"DSIM1", "/dev/logbuffer_dsim1"},
+};
+
+int main() {
+    for (auto &logbuffer_path : logbuffer_paths) {
+        if(!access(logbuffer_path[1], R_OK)) {
+            dumpFileContent(logbuffer_path[0], logbuffer_path[1]);
+        }
+    }
+
+    return 0;
+}
\ No newline at end of file
diff --git a/display_logbuffer/init.display_logbuffer.rc b/display_logbuffer/init.display_logbuffer.rc
new file mode 100644
index 0000000..2a39ce3
--- /dev/null
+++ b/display_logbuffer/init.display_logbuffer.rc
@@ -0,0 +1,3 @@
+on property:ro.build.type=userdebug
+    chown system system /dev/logbuffer_dsim0
+    chown system system /dev/logbuffer_dsim1
diff --git a/display_logbuffer/sepolicy/device.te b/display_logbuffer/sepolicy/device.te
new file mode 100644
index 0000000..94432f4
--- /dev/null
+++ b/display_logbuffer/sepolicy/device.te
@@ -0,0 +1 @@
+type display_log_device, dev_type;
\ No newline at end of file
diff --git a/display_logbuffer/sepolicy/dump_display_logbuffer.te b/display_logbuffer/sepolicy/dump_display_logbuffer.te
new file mode 100644
index 0000000..34da48c
--- /dev/null
+++ b/display_logbuffer/sepolicy/dump_display_logbuffer.te
@@ -0,0 +1,3 @@
+pixel_bugreport(dump_display_logbuffer)
+
+allow dump_display_logbuffer display_log_device:chr_file r_file_perms;
\ No newline at end of file
diff --git a/display_logbuffer/sepolicy/file_contexts b/display_logbuffer/sepolicy/file_contexts
new file mode 100644
index 0000000..7425214
--- /dev/null
+++ b/display_logbuffer/sepolicy/file_contexts
@@ -0,0 +1,4 @@
+/vendor/bin/dump/dump_display_logbuffer      u:object_r:dump_display_logbuffer_exec:s0
+
+/dev/logbuffer_dsim0                         u:object_r:display_log_device:s0
+/dev/logbuffer_dsim1                         u:object_r:display_log_device:s0
diff --git a/powerstats/TpuDvfsStateResidencyDataProvider.cpp b/powerstats/TpuDvfsStateResidencyDataProvider.cpp
new file mode 100644
index 0000000..e1ce56c
--- /dev/null
+++ b/powerstats/TpuDvfsStateResidencyDataProvider.cpp
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2023 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.
+ */
+#include "TpuDvfsStateResidencyDataProvider.h"
+
+#include <android-base/logging.h>
+
+static const std::string ENTITY_NAME = "TPU-DVFS";
+
+namespace aidl {
+namespace android {
+namespace hardware {
+namespace power {
+namespace stats {
+
+TpuDvfsStateResidencyDataProvider::TpuDvfsStateResidencyDataProvider(
+        const std::string& path,
+        std::vector<std::string> frequencies,
+        uint64_t clockRate)
+    : mPath(path), mFrequencies(std::move(frequencies)), mClockRate(clockRate) {}
+
+bool TpuDvfsStateResidencyDataProvider::getStateResidencies(
+        std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
+    // Using FILE* instead of std::ifstream for performance reasons
+    std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(mPath.c_str(), "r"), fclose);
+    if (!fp) {
+        PLOG(ERROR) << "Failed to open file " << mPath;
+        return false;
+    }
+
+    std::vector<StateResidency> stateResidencies;
+    for (int i = 0; i < mFrequencies.size(); i++) {
+        StateResidency s = {.id = i, .totalTimeInStateMs = 0};
+        stateResidencies.push_back(s);
+    }
+
+    char *line = nullptr;
+    size_t len = 0;
+    std::istringstream ssLine;
+    std::string split;
+    int32_t lineIdx = 0;
+    std::vector<int32_t> stateIdxMap;
+    int32_t colIdx;
+    std::vector<std::string>::const_iterator found;
+    while (getline(&line, &len, fp.get()) != -1) {
+        ssLine.clear();
+        ssLine.str(line);
+        colIdx = 0;
+        for (std::string split; std::getline(ssLine, split, ' ');) {
+            // Skip first column
+            if (split.find(':') != std::string::npos)
+                continue;
+
+            if (lineIdx == 0) {
+                // Build the state index map by first row
+                split.erase(split.find_last_not_of(" \n\r\t") + 1);
+                found = std::find(mFrequencies.begin(), mFrequencies.end(), split);
+                if (found != mFrequencies.end()) {
+                    stateIdxMap.push_back(found - mFrequencies.begin());
+                } else {
+                    PLOG(ERROR) << "TPU frequency " << split << " is not found in " << mPath;
+                    stateIdxMap.push_back(0);
+                }
+            } else {
+                // Add up time in frequency per uid
+                stateResidencies[stateIdxMap[colIdx]].totalTimeInStateMs +=
+                        std::atoll(split.c_str()) / mClockRate;
+            }
+            colIdx++;
+        }
+        lineIdx++;
+    }
+
+    residencies->emplace(ENTITY_NAME, stateResidencies);
+    return true;
+}
+
+std::unordered_map<std::string, std::vector<State>> TpuDvfsStateResidencyDataProvider::getInfo() {
+    std::vector<State> states;
+    for (int32_t id = 0; id < mFrequencies.size(); id++) {
+        State s =
+            {.id = id, .name = std::to_string(std::atol(mFrequencies[id].c_str()) / 1000) + "MHz"};
+        states.push_back(s);
+    }
+
+    return {{ENTITY_NAME, states}};
+}
+
+}  // namespace stats
+}  // namespace power
+}  // namespace hardware
+}  // namespace android
+}  // namespace aidl
diff --git a/powerstats/include/TpuDvfsStateResidencyDataProvider.h b/powerstats/include/TpuDvfsStateResidencyDataProvider.h
new file mode 100644
index 0000000..9ce3bd3
--- /dev/null
+++ b/powerstats/include/TpuDvfsStateResidencyDataProvider.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2023 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.
+ */
+#pragma once
+
+#include <PowerStatsAidl.h>
+
+namespace aidl {
+namespace android {
+namespace hardware {
+namespace power {
+namespace stats {
+
+class TpuDvfsStateResidencyDataProvider : public PowerStats::IStateResidencyDataProvider {
+  public:
+    TpuDvfsStateResidencyDataProvider(
+            const std::string& path, std::vector<std::string> frequencies, uint64_t clockRate);
+    ~TpuDvfsStateResidencyDataProvider() = default;
+
+    /*
+     * See IStateResidencyDataProvider::getStateResidencies
+     */
+    bool getStateResidencies(
+        std::unordered_map<std::string, std::vector<StateResidency>> *residencies) override;
+
+    /*
+     * See IStateResidencyDataProvider::getInfo
+     */
+    std::unordered_map<std::string, std::vector<State>> getInfo() override;
+
+  private:
+    const std::string mPath;
+    const std::vector<std::string> mFrequencies;
+    const uint64_t mClockRate;
+};
+
+}  // namespace stats
+}  // namespace power
+}  // namespace hardware
+}  // namespace android
+}  // namespace aidl