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 #pragma once
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include <android/hardware/thermal/2.0/IThermal.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace thermal {
27 namespace V2_0 {
28 namespace implementation {
29 
30 using ::android::hardware::hidl_enum_range;
31 using CoolingType_2_0 = ::android::hardware::thermal::V2_0::CoolingType;
32 using TemperatureType_2_0 = ::android::hardware::thermal::V2_0::TemperatureType;
33 using ::android::hardware::thermal::V2_0::ThrottlingSeverity;
34 constexpr size_t kThrottlingSeverityCount = std::distance(
35     hidl_enum_range<ThrottlingSeverity>().begin(), hidl_enum_range<ThrottlingSeverity>().end());
36 using ThrottlingArray = std::array<float, static_cast<size_t>(kThrottlingSeverityCount)>;
37 using CdevArray = std::array<int, static_cast<size_t>(kThrottlingSeverityCount)>;
38 constexpr std::chrono::milliseconds kMinPollIntervalMs = std::chrono::milliseconds(2000);
39 constexpr std::chrono::milliseconds kUeventPollTimeoutMs = std::chrono::milliseconds(300000);
40 
41 enum FormulaOption : uint32_t {
42     COUNT_THRESHOLD = 0,
43     WEIGHTED_AVG,
44     MAXIMUM,
45     MINIMUM,
46 };
47 
48 struct VirtualSensorInfo {
49     std::vector<std::string> linked_sensors;
50     std::vector<float> coefficients;
51     float offset;
52     std::string trigger_sensor;
53     FormulaOption formula;
54 };
55 
56 struct VirtualPowerRailInfo {
57     std::vector<std::string> linked_power_rails;
58     std::vector<float> coefficients;
59     float offset;
60     FormulaOption formula;
61 };
62 
63 // The method when the ODPM power is lower than threshold
64 enum ReleaseLogic : uint32_t {
65     INCREASE = 0,      // Increase throttling by step
66     DECREASE,          // Decrease throttling by step
67     STEPWISE,          // Support both increase and decrease logix
68     RELEASE_TO_FLOOR,  // Release throttling to floor directly
69     NONE,
70 };
71 
72 struct BindedCdevInfo {
73     CdevArray limit_info;
74     ThrottlingArray power_thresholds;
75     ReleaseLogic release_logic;
76     ThrottlingArray cdev_weight_for_pid;
77     CdevArray cdev_ceiling;
78     CdevArray cdev_floor_with_power_link;
79     std::string power_rail;
80     // The flag for activate release logic when power is higher than power threshold
81     bool high_power_check;
82     // The flag for only triggering throttling until all power samples are collected
83     bool throttling_with_power_link;
84 };
85 
86 struct ThrottlingInfo {
87     ThrottlingArray k_po;
88     ThrottlingArray k_pu;
89     ThrottlingArray k_i;
90     ThrottlingArray k_d;
91     ThrottlingArray i_max;
92     ThrottlingArray max_alloc_power;
93     ThrottlingArray min_alloc_power;
94     ThrottlingArray s_power;
95     ThrottlingArray i_cutoff;
96     std::unordered_map<std::string, BindedCdevInfo> binded_cdev_info_map;
97 };
98 
99 struct SensorInfo {
100     TemperatureType_2_0 type;
101     ThrottlingArray hot_thresholds;
102     ThrottlingArray cold_thresholds;
103     ThrottlingArray hot_hysteresis;
104     ThrottlingArray cold_hysteresis;
105     std::string temp_path;
106     float vr_threshold;
107     float multiplier;
108     std::chrono::milliseconds polling_delay;
109     std::chrono::milliseconds passive_delay;
110     bool send_cb;
111     bool send_powerhint;
112     bool is_monitor;
113     std::unique_ptr<VirtualSensorInfo> virtual_sensor_info;
114     std::unique_ptr<ThrottlingInfo> throttling_info;
115 };
116 
117 struct CdevInfo {
118     CoolingType_2_0 type;
119     std::string read_path;
120     std::string write_path;
121     std::vector<float> state2power;
122     int max_state;
123 };
124 struct PowerRailInfo {
125     std::string rail;
126     int power_sample_count;
127     std::chrono::milliseconds power_sample_delay;
128     std::unique_ptr<VirtualPowerRailInfo> virtual_power_rail_info;
129 };
130 
131 std::unordered_map<std::string, SensorInfo> ParseSensorInfo(std::string_view config_path);
132 std::unordered_map<std::string, CdevInfo> ParseCoolingDevice(std::string_view config_path);
133 std::unordered_map<std::string, PowerRailInfo> ParsePowerRailInfo(std::string_view config_path);
134 
135 }  // namespace implementation
136 }  // namespace V2_0
137 }  // namespace thermal
138 }  // namespace hardware
139 }  // namespace android
140