blob: 4cd94a227b669baa53a217fe7a6adbc55d9a5cfd [file] [log] [blame]
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -07001/*
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
31#include <android-base/file.h>
32#include <android-base/logging.h>
33#include <android-base/properties.h>
34#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
36#include <hidl/HidlTransportSupport.h>
37
38#include "thermalConfig.h"
39#include "thermalUtils.h"
40
41namespace android {
42namespace hardware {
43namespace thermal {
44namespace V2_0 {
45namespace implementation {
46
47ThermalUtils::ThermalUtils(const ueventCB &inp_cb):
48 cfg(),
49 cmnInst(),
50 monitor(std::bind(&ThermalUtils::ueventParse, this,
51 std::placeholders::_1,
52 std::placeholders::_2)),
53 cb(inp_cb)
54{
55 int ret = 0;
56 std::vector<struct therm_sensor> sensorList;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -080057 std::vector<struct target_therm_cfg> therm_cfg = cfg.fetchConfig();
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070058
59 is_sensor_init = false;
60 is_cdev_init = false;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -080061 ret = cmnInst.initThermalZones(therm_cfg);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070062 if (ret > 0) {
63 is_sensor_init = true;
64 sensorList = cmnInst.fetch_sensor_list();
65 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -080066 for (struct therm_sensor sens: sensorList) {
67 thermalConfig[sens.sensor_name] = sens;
68 cmnInst.read_temperature(sens);
Ram Chandrasekar56329992020-12-28 16:44:40 -080069 cmnInst.estimateSeverity(sens);
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -080070 cmnInst.initThreshold(sens);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070071 }
72 monitor.start();
73 }
74 ret = cmnInst.initCdev();
75 if (ret > 0) {
76 is_cdev_init = true;
77 cdevList = cmnInst.fetch_cdev_list();
78 }
79}
80
Ram Chandrasekar56329992020-12-28 16:44:40 -080081void ThermalUtils::Notify(struct therm_sensor& sens)
82{
83 int severity = cmnInst.estimateSeverity(sens);
84 if (severity != -1) {
85 LOG(INFO) << "sensor: " << sens.sensor_name <<" temperature: "
86 << sens.t.value << " old: " <<
87 (int)sens.lastThrottleStatus << " new: " <<
88 (int)sens.t.throttlingStatus << std::endl;
89 cb(sens.t);
90 cmnInst.initThreshold(sens);
91 }
92}
93
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070094void ThermalUtils::ueventParse(std::string sensor_name, int temp)
95{
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070096 LOG(INFO) << "uevent triggered for sensor: " << sensor_name
97 << std::endl;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -080098 if (thermalConfig.find(sensor_name) == thermalConfig.end()) {
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -070099 LOG(DEBUG) << "sensor is not monitored:" << sensor_name
100 << std::endl;
101 return;
102 }
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700103 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800104 struct therm_sensor& sens = thermalConfig[sensor_name];
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700105 sens.t.value = (float)temp / (float)sens.mulFactor;
Ram Chandrasekar56329992020-12-28 16:44:40 -0800106 return Notify(sens);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700107}
108
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700109int ThermalUtils::readTemperatures(hidl_vec<Temperature_1_0>& temp)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700110{
111 std::unordered_map<std::string, struct therm_sensor>::iterator it;
112 int ret = 0, idx = 0;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700113 std::vector<Temperature_1_0> _temp_v;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700114
115 if (!is_sensor_init)
116 return 0;
Ram Chandrasekar56329992020-12-28 16:44:40 -0800117 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700118 for (it = thermalConfig.begin(); it != thermalConfig.end();
119 it++, idx++) {
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800120 struct therm_sensor& sens = it->second;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700121 Temperature_1_0 _temp;
122
123 /* v1 supports only CPU, GPU, Battery and SKIN */
124 if (sens.t.type > TemperatureType::SKIN)
125 continue;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800126 ret = cmnInst.read_temperature(sens);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700127 if (ret < 0)
128 return ret;
Ram Chandrasekar56329992020-12-28 16:44:40 -0800129 Notify(sens);
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700130 _temp.currentValue = sens.t.value;
131 _temp.name = sens.t.name;
132 _temp.type = (TemperatureType_1_0)sens.t.type;
133 _temp.throttlingThreshold = sens.thresh.hotThrottlingThresholds[
Ram Chandrasekarf61c6142020-08-31 15:05:42 -0700134 (size_t)ThrottlingSeverity::SEVERE];
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700135 _temp.shutdownThreshold = sens.thresh.hotThrottlingThresholds[
Ram Chandrasekarf61c6142020-08-31 15:05:42 -0700136 (size_t)ThrottlingSeverity::SHUTDOWN];
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700137 _temp.vrThrottlingThreshold = sens.thresh.vrThrottlingThreshold;
138 _temp_v.push_back(_temp);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700139 }
140
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700141 temp = _temp_v;
142 return temp.size();
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700143}
144
145int ThermalUtils::readTemperatures(bool filterType, TemperatureType type,
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700146 hidl_vec<Temperature>& temp)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700147{
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700148 std::unordered_map<std::string, struct therm_sensor>::iterator it;
149 int ret = 0;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700150 std::vector<Temperature> _temp;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700151
Ram Chandrasekar56329992020-12-28 16:44:40 -0800152 std::lock_guard<std::mutex> _lock(sens_cb_mutex);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700153 for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) {
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800154 struct therm_sensor& sens = it->second;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700155
156 if (filterType && sens.t.type != type)
157 continue;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800158 ret = cmnInst.read_temperature(sens);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700159 if (ret < 0)
160 return ret;
Ram Chandrasekar56329992020-12-28 16:44:40 -0800161 Notify(sens);
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700162 _temp.push_back(sens.t);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700163 }
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700164
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700165 temp = _temp;
166 return temp.size();
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700167}
168
169int ThermalUtils::readTemperatureThreshold(bool filterType, TemperatureType type,
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700170 hidl_vec<TemperatureThreshold>& thresh)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700171{
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700172 std::unordered_map<std::string, struct therm_sensor>::iterator it;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700173 std::vector<TemperatureThreshold> _thresh;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700174
175 for (it = thermalConfig.begin(); it != thermalConfig.end(); it++) {
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800176 struct therm_sensor& sens = it->second;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700177
178 if (filterType && sens.t.type != type)
179 continue;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700180 _thresh.push_back(sens.thresh);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700181 }
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700182
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700183 thresh = _thresh;
184 return thresh.size();
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700185}
186
187int ThermalUtils::readCdevStates(bool filterType, cdevType type,
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700188 hidl_vec<CoolingDevice>& cdev_out)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700189{
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700190 int ret = 0;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700191 std::vector<CoolingDevice> _cdev;
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700192
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700193 for (struct therm_cdev cdev: cdevList) {
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700194
195 if (filterType && cdev.c.type != type)
196 continue;
Ram Chandrasekardbc52fc2020-11-17 13:57:40 -0800197 ret = cmnInst.read_cdev_state(cdev);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700198 if (ret < 0)
199 return ret;
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700200 _cdev.push_back(cdev.c);
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700201 }
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700202
Ram Chandrasekarf020a1d2020-08-25 14:25:14 -0700203 cdev_out = _cdev;
204
205 return cdev_out.size();
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700206}
207
Ram Chandrasekar72110b52020-08-06 14:39:40 -0700208int ThermalUtils::fetchCpuUsages(hidl_vec<CpuUsage>& cpu_usages)
Ram Chandrasekardac8f7d2020-06-10 12:23:28 -0700209{
210 return cmnInst.get_cpu_usages(cpu_usages);
211}
212
213} // namespace implementation
214} // namespace V2_0
215} // namespace thermal
216} // namespace hardware
217} // namespace android