Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 22 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 28 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Bavyasritha Alahari | 0a344f4 | 2023-04-26 17:16:51 -0700 | [diff] [blame] | 31 | /* Changes from Qualcomm Innovation Center are provided under the following license: |
| 32 | |
| 33 | Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved. |
| 34 | SPDX-License-Identifier: BSD-3-Clause-Clear */ |
| 35 | |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 36 | #include <android-base/file.h> |
| 37 | #include <android-base/logging.h> |
| 38 | #include <android-base/properties.h> |
| 39 | #include <android-base/stringprintf.h> |
| 40 | #include <android-base/strings.h> |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 41 | |
| 42 | #include "thermalConfig.h" |
| 43 | #include "thermalUtils.h" |
| 44 | |
Bavyasritha Alahari | 0a344f4 | 2023-04-26 17:16:51 -0700 | [diff] [blame] | 45 | namespace aidl { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 46 | namespace android { |
| 47 | namespace hardware { |
| 48 | namespace thermal { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 49 | |
| 50 | ThermalUtils::ThermalUtils(const ueventCB &inp_cb): |
| 51 | cfg(), |
| 52 | cmnInst(), |
| 53 | monitor(std::bind(&ThermalUtils::ueventParse, this, |
| 54 | std::placeholders::_1, |
| 55 | std::placeholders::_2)), |
| 56 | cb(inp_cb) |
| 57 | { |
| 58 | int ret = 0; |
| 59 | std::vector<struct therm_sensor> sensorList; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 60 | std::vector<struct target_therm_cfg> therm_cfg = cfg.fetchConfig(); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 61 | |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 62 | is_cdev_init = false; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 63 | ret = cmnInst.initThermalZones(therm_cfg); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 64 | if (ret > 0) { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 65 | sensorList = cmnInst.fetch_sensor_list(); |
| 66 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 67 | for (struct therm_sensor sens: sensorList) { |
| 68 | thermalConfig[sens.sensor_name] = sens; |
| 69 | cmnInst.read_temperature(sens); |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 70 | cmnInst.estimateSeverity(sens); |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 71 | cmnInst.initThreshold(sens); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 72 | } |
| 73 | monitor.start(); |
| 74 | } |
| 75 | ret = cmnInst.initCdev(); |
| 76 | if (ret > 0) { |
| 77 | is_cdev_init = true; |
| 78 | cdevList = cmnInst.fetch_cdev_list(); |
| 79 | } |
| 80 | } |
| 81 | |
Michael Bestas | 04aeca6 | 2025-07-06 23:44:50 +0300 | [diff] [blame] | 82 | bool ThermalUtils::isSensorInitialized() |
| 83 | { |
| 84 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
| 85 | |
| 86 | if (thermalConfig.begin() == thermalConfig.end()) |
| 87 | return false; |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool ThermalUtils::isSensorInitialized(TemperatureType type) |
| 93 | { |
| 94 | std::unordered_map<std::string, struct therm_sensor>::iterator it; |
| 95 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
| 96 | |
| 97 | if (thermalConfig.begin() == thermalConfig.end()) |
| 98 | return false; |
| 99 | |
| 100 | for (it = thermalConfig.begin(); it != thermalConfig.end(); |
| 101 | it++) { |
| 102 | struct therm_sensor& sens = it->second; |
| 103 | if (sens.t.type == type) |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | return false; |
| 108 | } |
| 109 | |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 110 | void ThermalUtils::Notify(struct therm_sensor& sens) |
| 111 | { |
| 112 | int severity = cmnInst.estimateSeverity(sens); |
| 113 | if (severity != -1) { |
| 114 | LOG(INFO) << "sensor: " << sens.sensor_name <<" temperature: " |
| 115 | << sens.t.value << " old: " << |
| 116 | (int)sens.lastThrottleStatus << " new: " << |
| 117 | (int)sens.t.throttlingStatus << std::endl; |
| 118 | cb(sens.t); |
| 119 | cmnInst.initThreshold(sens); |
| 120 | } |
| 121 | } |
| 122 | |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 123 | void ThermalUtils::ueventParse(std::string sensor_name, int temp) |
| 124 | { |
Rashid Zafar | e10611c | 2023-06-21 12:09:36 -0700 | [diff] [blame] | 125 | LOG(DEBUG) << "uevent triggered for sensor: " << sensor_name |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 126 | << std::endl; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 127 | if (thermalConfig.find(sensor_name) == thermalConfig.end()) { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 128 | LOG(DEBUG) << "sensor is not monitored:" << sensor_name |
| 129 | << std::endl; |
| 130 | return; |
| 131 | } |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 132 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 133 | struct therm_sensor& sens = thermalConfig[sensor_name]; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 134 | sens.t.value = (float)temp / (float)sens.mulFactor; |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 135 | return Notify(sens); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Bavyasritha Alahari | 0a344f4 | 2023-04-26 17:16:51 -0700 | [diff] [blame] | 138 | int ThermalUtils::readTemperatures(std::vector<Temperature>& temp) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 139 | { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 140 | std::unordered_map<std::string, struct therm_sensor>::iterator it; |
| 141 | int ret = 0; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 142 | std::vector<Temperature> _temp; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 143 | |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 144 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 145 | for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) { |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 146 | struct therm_sensor& sens = it->second; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 147 | |
Bavyasritha Alahari | 0a344f4 | 2023-04-26 17:16:51 -0700 | [diff] [blame] | 148 | ret = cmnInst.read_temperature(sens); |
| 149 | if (ret < 0) |
| 150 | return ret; |
| 151 | Notify(sens); |
| 152 | _temp.push_back(sens.t); |
| 153 | } |
| 154 | |
| 155 | temp = _temp; |
| 156 | return temp.size(); |
| 157 | } |
| 158 | |
| 159 | int ThermalUtils::readTemperatures(TemperatureType type, |
| 160 | std::vector<Temperature>& temp) |
| 161 | { |
| 162 | std::unordered_map<std::string, struct therm_sensor>::iterator it; |
| 163 | int ret = 0; |
| 164 | std::vector<Temperature> _temp; |
| 165 | |
| 166 | std::lock_guard<std::mutex> _lock(sens_cb_mutex); |
| 167 | for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) { |
| 168 | struct therm_sensor& sens = it->second; |
| 169 | |
| 170 | if (sens.t.type != type) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 171 | continue; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 172 | ret = cmnInst.read_temperature(sens); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 173 | if (ret < 0) |
| 174 | return ret; |
Ram Chandrasekar | 5632999 | 2020-12-28 16:44:40 -0800 | [diff] [blame] | 175 | Notify(sens); |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 176 | _temp.push_back(sens.t); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 177 | } |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 178 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 179 | temp = _temp; |
| 180 | return temp.size(); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Bavyasritha Alahari | 0a344f4 | 2023-04-26 17:16:51 -0700 | [diff] [blame] | 183 | int ThermalUtils::readTemperatureThreshold(std::vector<TemperatureThreshold>& thresh) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 184 | { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 185 | std::unordered_map<std::string, struct therm_sensor>::iterator it; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 186 | std::vector<TemperatureThreshold> _thresh; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 187 | |
| 188 | for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) { |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 189 | struct therm_sensor& sens = it->second; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 190 | |
Bavyasritha Alahari | 0a344f4 | 2023-04-26 17:16:51 -0700 | [diff] [blame] | 191 | _thresh.push_back(sens.thresh); |
| 192 | } |
| 193 | |
| 194 | thresh = _thresh; |
| 195 | return thresh.size(); |
| 196 | } |
| 197 | |
| 198 | int ThermalUtils::readTemperatureThreshold(TemperatureType type, |
| 199 | std::vector<TemperatureThreshold>& thresh) |
| 200 | { |
| 201 | std::unordered_map<std::string, struct therm_sensor>::iterator it; |
| 202 | std::vector<TemperatureThreshold> _thresh; |
| 203 | |
| 204 | for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) { |
| 205 | struct therm_sensor& sens = it->second; |
| 206 | |
| 207 | if (sens.t.type != type) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 208 | continue; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 209 | _thresh.push_back(sens.thresh); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 210 | } |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 211 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 212 | thresh = _thresh; |
| 213 | return thresh.size(); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Bavyasritha Alahari | 0a344f4 | 2023-04-26 17:16:51 -0700 | [diff] [blame] | 216 | int ThermalUtils::readCdevStates(std::vector<CoolingDevice>& cdev_out) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 217 | { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 218 | int ret = 0; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 219 | std::vector<CoolingDevice> _cdev; |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 220 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 221 | for (struct therm_cdev cdev: cdevList) { |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 222 | |
Bavyasritha Alahari | 0a344f4 | 2023-04-26 17:16:51 -0700 | [diff] [blame] | 223 | ret = cmnInst.read_cdev_state(cdev); |
| 224 | if (ret < 0) |
| 225 | return ret; |
| 226 | _cdev.push_back(cdev.c); |
| 227 | } |
| 228 | |
| 229 | cdev_out = _cdev; |
| 230 | |
| 231 | return cdev_out.size(); |
| 232 | } |
| 233 | |
| 234 | int ThermalUtils::readCdevStates(cdevType type, |
| 235 | std::vector<CoolingDevice>& cdev_out) |
| 236 | { |
| 237 | int ret = 0; |
| 238 | std::vector<CoolingDevice> _cdev; |
| 239 | |
| 240 | for (struct therm_cdev cdev: cdevList) { |
| 241 | |
| 242 | if (cdev.c.type != type) |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 243 | continue; |
Ram Chandrasekar | dbc52fc | 2020-11-17 13:57:40 -0800 | [diff] [blame] | 244 | ret = cmnInst.read_cdev_state(cdev); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 245 | if (ret < 0) |
| 246 | return ret; |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 247 | _cdev.push_back(cdev.c); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 248 | } |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 249 | |
Ram Chandrasekar | f020a1d | 2020-08-25 14:25:14 -0700 | [diff] [blame] | 250 | cdev_out = _cdev; |
| 251 | |
| 252 | return cdev_out.size(); |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Ram Chandrasekar | dac8f7d | 2020-06-10 12:23:28 -0700 | [diff] [blame] | 255 | } // namespace thermal |
| 256 | } // namespace hardware |
| 257 | } // namespace android |
Rashid Zafar | e10611c | 2023-06-21 12:09:36 -0700 | [diff] [blame] | 258 | } // namespace aidl |