Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * This file is provided under a dual BSD/GPLv2 license. When using or |
| 4 | * redistributing this file, you may do so under either license. |
| 5 | * |
| 6 | * GPL LICENSE SUMMARY |
| 7 | * |
Emmanuel Grumbach | 51368bf | 2013-12-30 13:15:54 +0200 | [diff] [blame] | 8 | * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved. |
Johannes Berg | 8b4139d | 2014-07-24 14:05:26 +0200 | [diff] [blame] | 9 | * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH |
Chaya Rachel Ivgi | 0a3b711 | 2015-12-16 16:34:55 +0200 | [diff] [blame] | 10 | * Copyright(c) 2015 - 2016 Intel Deutschland GmbH |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of version 2 of the GNU General Public License as |
| 14 | * published by the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, but |
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, |
| 24 | * USA |
| 25 | * |
| 26 | * The full GNU General Public License is included in this distribution |
| 27 | * in the file called COPYING. |
| 28 | * |
| 29 | * Contact Information: |
Emmanuel Grumbach | d01c536 | 2015-11-17 15:39:56 +0200 | [diff] [blame] | 30 | * Intel Linux Wireless <linuxwifi@intel.com> |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 31 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
| 32 | * |
| 33 | * BSD LICENSE |
| 34 | * |
Emmanuel Grumbach | 51368bf | 2013-12-30 13:15:54 +0200 | [diff] [blame] | 35 | * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. |
Johannes Berg | 8b4139d | 2014-07-24 14:05:26 +0200 | [diff] [blame] | 36 | * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH |
Chaya Rachel Ivgi | 0a3b711 | 2015-12-16 16:34:55 +0200 | [diff] [blame] | 37 | * Copyright(c) 2015 - 2016 Intel Deutschland GmbH |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 38 | * All rights reserved. |
| 39 | * |
| 40 | * Redistribution and use in source and binary forms, with or without |
| 41 | * modification, are permitted provided that the following conditions |
| 42 | * are met: |
| 43 | * |
| 44 | * * Redistributions of source code must retain the above copyright |
| 45 | * notice, this list of conditions and the following disclaimer. |
| 46 | * * Redistributions in binary form must reproduce the above copyright |
| 47 | * notice, this list of conditions and the following disclaimer in |
| 48 | * the documentation and/or other materials provided with the |
| 49 | * distribution. |
| 50 | * * Neither the name Intel Corporation nor the names of its |
| 51 | * contributors may be used to endorse or promote products derived |
| 52 | * from this software without specific prior written permission. |
| 53 | * |
| 54 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 55 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 56 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 57 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 58 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 59 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 60 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 61 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 62 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 63 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 64 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 65 | * |
| 66 | *****************************************************************************/ |
| 67 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 68 | #include <linux/sort.h> |
| 69 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 70 | #include "mvm.h" |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 71 | |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 72 | #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 73 | |
Chaya Rachel Ivgi | 61d8c62 | 2017-06-27 14:13:02 +0300 | [diff] [blame] | 74 | void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 75 | { |
Luciano Coelho | 19789ab | 2014-12-15 14:15:15 +0200 | [diff] [blame] | 76 | struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 77 | u32 duration = tt->params.ct_kill_duration; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 78 | |
Luciano Coelho | b689fa7 | 2014-08-20 17:26:58 +0300 | [diff] [blame] | 79 | if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) |
| 80 | return; |
| 81 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 82 | IWL_ERR(mvm, "Enter CT Kill\n"); |
| 83 | iwl_mvm_set_hw_ctkill_state(mvm, true); |
Luciano Coelho | efc36db | 2014-08-20 17:58:20 +0300 | [diff] [blame] | 84 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 85 | if (!iwl_mvm_is_tt_in_fw(mvm)) { |
| 86 | tt->throttle = false; |
| 87 | tt->dynamic_smps = false; |
| 88 | } |
Luciano Coelho | 19789ab | 2014-12-15 14:15:15 +0200 | [diff] [blame] | 89 | |
Luciano Coelho | efc36db | 2014-08-20 17:58:20 +0300 | [diff] [blame] | 90 | /* Don't schedule an exit work if we're in test mode, since |
| 91 | * the temperature will not change unless we manually set it |
| 92 | * again (or disable testing). |
| 93 | */ |
| 94 | if (!mvm->temperature_test) |
Luciano Coelho | 19789ab | 2014-12-15 14:15:15 +0200 | [diff] [blame] | 95 | schedule_delayed_work(&tt->ct_kill_exit, |
Luciano Coelho | efc36db | 2014-08-20 17:58:20 +0300 | [diff] [blame] | 96 | round_jiffies_relative(duration * HZ)); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm) |
| 100 | { |
Luciano Coelho | b689fa7 | 2014-08-20 17:26:58 +0300 | [diff] [blame] | 101 | if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) |
| 102 | return; |
| 103 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 104 | IWL_ERR(mvm, "Exit CT Kill\n"); |
| 105 | iwl_mvm_set_hw_ctkill_state(mvm, false); |
| 106 | } |
| 107 | |
Luciano Coelho | fd1f755 | 2014-11-04 16:17:46 +0200 | [diff] [blame] | 108 | void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp) |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 109 | { |
Luciano Coelho | fd1f755 | 2014-11-04 16:17:46 +0200 | [diff] [blame] | 110 | /* ignore the notification if we are in test mode */ |
| 111 | if (mvm->temperature_test) |
| 112 | return; |
| 113 | |
| 114 | if (mvm->temperature == temp) |
| 115 | return; |
| 116 | |
| 117 | mvm->temperature = temp; |
| 118 | iwl_mvm_tt_handler(mvm); |
| 119 | } |
| 120 | |
| 121 | static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm, |
| 122 | struct iwl_rx_packet *pkt) |
| 123 | { |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 124 | struct iwl_dts_measurement_notif_v1 *notif_v1; |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 125 | int len = iwl_rx_packet_payload_len(pkt); |
Luciano Coelho | fd1f755 | 2014-11-04 16:17:46 +0200 | [diff] [blame] | 126 | int temp; |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 127 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 128 | /* we can use notif_v1 only, because v2 only adds an additional |
| 129 | * parameter, which is not used in this function. |
| 130 | */ |
| 131 | if (WARN_ON_ONCE(len < sizeof(*notif_v1))) { |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 132 | IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n"); |
Luciano Coelho | fd1f755 | 2014-11-04 16:17:46 +0200 | [diff] [blame] | 133 | return -EINVAL; |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 134 | } |
| 135 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 136 | notif_v1 = (void *)pkt->data; |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 137 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 138 | temp = le32_to_cpu(notif_v1->temp); |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 139 | |
| 140 | /* shouldn't be negative, but since it's s32, make sure it isn't */ |
Luciano Coelho | fd1f755 | 2014-11-04 16:17:46 +0200 | [diff] [blame] | 141 | if (WARN_ON_ONCE(temp < 0)) |
| 142 | temp = 0; |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 143 | |
Luciano Coelho | fd1f755 | 2014-11-04 16:17:46 +0200 | [diff] [blame] | 144 | IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp); |
| 145 | |
| 146 | return temp; |
| 147 | } |
| 148 | |
| 149 | static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait, |
| 150 | struct iwl_rx_packet *pkt, void *data) |
| 151 | { |
| 152 | struct iwl_mvm *mvm = |
| 153 | container_of(notif_wait, struct iwl_mvm, notif_wait); |
| 154 | int *temp = data; |
| 155 | int ret; |
| 156 | |
| 157 | ret = iwl_mvm_temp_notif_parse(mvm, pkt); |
| 158 | if (ret < 0) |
| 159 | return true; |
| 160 | |
| 161 | *temp = ret; |
| 162 | |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | |
Johannes Berg | 0416841 | 2015-06-23 21:22:09 +0200 | [diff] [blame] | 166 | void iwl_mvm_temp_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) |
Luciano Coelho | ea9af24 | 2014-11-06 10:34:49 +0200 | [diff] [blame] | 167 | { |
| 168 | struct iwl_rx_packet *pkt = rxb_addr(rxb); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 169 | struct iwl_dts_measurement_notif_v2 *notif_v2; |
| 170 | int len = iwl_rx_packet_payload_len(pkt); |
Luciano Coelho | ea9af24 | 2014-11-06 10:34:49 +0200 | [diff] [blame] | 171 | int temp; |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 172 | u32 ths_crossed; |
Luciano Coelho | ea9af24 | 2014-11-06 10:34:49 +0200 | [diff] [blame] | 173 | |
| 174 | /* the notification is handled synchronously in ctkill, so skip here */ |
| 175 | if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) |
Johannes Berg | 0416841 | 2015-06-23 21:22:09 +0200 | [diff] [blame] | 176 | return; |
Luciano Coelho | ea9af24 | 2014-11-06 10:34:49 +0200 | [diff] [blame] | 177 | |
| 178 | temp = iwl_mvm_temp_notif_parse(mvm, pkt); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 179 | |
| 180 | if (!iwl_mvm_is_tt_in_fw(mvm)) { |
| 181 | if (temp >= 0) |
| 182 | iwl_mvm_tt_temp_changed(mvm, temp); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if (WARN_ON_ONCE(len < sizeof(*notif_v2))) { |
| 187 | IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n"); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | notif_v2 = (void *)pkt->data; |
| 192 | ths_crossed = le32_to_cpu(notif_v2->threshold_idx); |
| 193 | |
| 194 | /* 0xFF in ths_crossed means the notification is not related |
| 195 | * to a trip, so we can ignore it here. |
| 196 | */ |
| 197 | if (ths_crossed == 0xFF) |
Johannes Berg | 0416841 | 2015-06-23 21:22:09 +0200 | [diff] [blame] | 198 | return; |
Luciano Coelho | ea9af24 | 2014-11-06 10:34:49 +0200 | [diff] [blame] | 199 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 200 | IWL_DEBUG_TEMP(mvm, "Temp = %d Threshold crossed = %d\n", |
| 201 | temp, ths_crossed); |
| 202 | |
| 203 | #ifdef CONFIG_THERMAL |
| 204 | if (WARN_ON(ths_crossed >= IWL_MAX_DTS_TRIPS)) |
| 205 | return; |
| 206 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 207 | if (mvm->tz_device.tzone) { |
| 208 | struct iwl_mvm_thermal_device *tz_dev = &mvm->tz_device; |
| 209 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 210 | thermal_notify_framework(tz_dev->tzone, |
| 211 | tz_dev->fw_trips_index[ths_crossed]); |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 212 | } |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 213 | #endif /* CONFIG_THERMAL */ |
Luciano Coelho | ea9af24 | 2014-11-06 10:34:49 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Chaya Rachel Ivgi | 0a3b711 | 2015-12-16 16:34:55 +0200 | [diff] [blame] | 216 | void iwl_mvm_ct_kill_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) |
| 217 | { |
| 218 | struct iwl_rx_packet *pkt = rxb_addr(rxb); |
| 219 | struct ct_kill_notif *notif; |
| 220 | int len = iwl_rx_packet_payload_len(pkt); |
| 221 | |
| 222 | if (WARN_ON_ONCE(len != sizeof(*notif))) { |
| 223 | IWL_ERR(mvm, "Invalid CT_KILL_NOTIFICATION\n"); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | notif = (struct ct_kill_notif *)pkt->data; |
| 228 | IWL_DEBUG_TEMP(mvm, "CT Kill notification temperature = %d\n", |
| 229 | notif->temperature); |
| 230 | |
| 231 | iwl_mvm_enter_ctkill(mvm); |
| 232 | } |
| 233 | |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 234 | static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm) |
| 235 | { |
| 236 | struct iwl_dts_measurement_cmd cmd = { |
| 237 | .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP), |
| 238 | }; |
Arik Nemtsov | 78efc70 | 2015-10-06 12:22:47 +0300 | [diff] [blame] | 239 | struct iwl_ext_dts_measurement_cmd extcmd = { |
| 240 | .control_mode = cpu_to_le32(DTS_AUTOMATIC), |
| 241 | }; |
Aviya Erenfeld | 09eef33 | 2015-09-01 19:34:38 +0300 | [diff] [blame] | 242 | u32 cmdid; |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 243 | |
Luca Coelho | 4b87e5a | 2016-09-12 16:03:30 +0300 | [diff] [blame] | 244 | cmdid = iwl_cmd_id(CMD_DTS_MEASUREMENT_TRIGGER_WIDE, |
| 245 | PHY_OPS_GROUP, 0); |
Arik Nemtsov | 78efc70 | 2015-10-06 12:22:47 +0300 | [diff] [blame] | 246 | |
| 247 | if (!fw_has_capa(&mvm->fw->ucode_capa, |
| 248 | IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE)) |
| 249 | return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(cmd), &cmd); |
| 250 | |
| 251 | return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(extcmd), &extcmd); |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 252 | } |
| 253 | |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 254 | int iwl_mvm_get_temp(struct iwl_mvm *mvm, s32 *temp) |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 255 | { |
| 256 | struct iwl_notification_wait wait_temp_notif; |
Aviya Erenfeld | 09eef33 | 2015-09-01 19:34:38 +0300 | [diff] [blame] | 257 | static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP, |
| 258 | DTS_MEASUREMENT_NOTIF_WIDE) }; |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 259 | int ret; |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 260 | |
| 261 | lockdep_assert_held(&mvm->mutex); |
| 262 | |
| 263 | iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif, |
| 264 | temp_notif, ARRAY_SIZE(temp_notif), |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 265 | iwl_mvm_temp_notif_wait, temp); |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 266 | |
| 267 | ret = iwl_mvm_get_temp_cmd(mvm); |
| 268 | if (ret) { |
| 269 | IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret); |
| 270 | iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif); |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif, |
| 275 | IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT); |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 276 | if (ret) |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 277 | IWL_ERR(mvm, "Getting the temperature timed out\n"); |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 278 | |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 279 | return ret; |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 280 | } |
| 281 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 282 | static void check_exit_ctkill(struct work_struct *work) |
| 283 | { |
| 284 | struct iwl_mvm_tt_mgmt *tt; |
| 285 | struct iwl_mvm *mvm; |
| 286 | u32 duration; |
| 287 | s32 temp; |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 288 | int ret; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 289 | |
| 290 | tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work); |
| 291 | mvm = container_of(tt, struct iwl_mvm, thermal_throttle); |
| 292 | |
Chaya Rachel Ivgi | 0a3b711 | 2015-12-16 16:34:55 +0200 | [diff] [blame] | 293 | if (iwl_mvm_is_tt_in_fw(mvm)) { |
| 294 | iwl_mvm_exit_ctkill(mvm); |
| 295 | |
| 296 | return; |
| 297 | } |
| 298 | |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 299 | duration = tt->params.ct_kill_duration; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 300 | |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 301 | mutex_lock(&mvm->mutex); |
| 302 | |
| 303 | if (__iwl_mvm_mac_start(mvm)) |
Eliad Peller | 576eeee | 2014-07-01 18:38:38 +0300 | [diff] [blame] | 304 | goto reschedule; |
| 305 | |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 306 | /* make sure the device is available for direct read/writes */ |
| 307 | if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) { |
| 308 | __iwl_mvm_mac_stop(mvm); |
| 309 | goto reschedule; |
| 310 | } |
| 311 | |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 312 | ret = iwl_mvm_get_temp(mvm, &temp); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 313 | |
Eliad Peller | 576eeee | 2014-07-01 18:38:38 +0300 | [diff] [blame] | 314 | iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL); |
| 315 | |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 316 | __iwl_mvm_mac_stop(mvm); |
| 317 | |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 318 | if (ret) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 319 | goto reschedule; |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 320 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 321 | IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp); |
| 322 | |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 323 | if (temp <= tt->params.ct_kill_exit) { |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 324 | mutex_unlock(&mvm->mutex); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 325 | iwl_mvm_exit_ctkill(mvm); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | reschedule: |
Luciano Coelho | a0a0924 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 330 | mutex_unlock(&mvm->mutex); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 331 | schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit, |
| 332 | round_jiffies(duration * HZ)); |
| 333 | } |
| 334 | |
| 335 | static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac, |
| 336 | struct ieee80211_vif *vif) |
| 337 | { |
| 338 | struct iwl_mvm *mvm = _data; |
| 339 | enum ieee80211_smps_mode smps_mode; |
| 340 | |
| 341 | lockdep_assert_held(&mvm->mutex); |
| 342 | |
| 343 | if (mvm->thermal_throttle.dynamic_smps) |
| 344 | smps_mode = IEEE80211_SMPS_DYNAMIC; |
| 345 | else |
| 346 | smps_mode = IEEE80211_SMPS_AUTOMATIC; |
| 347 | |
Eytan Lifshitz | fded313 | 2013-06-04 12:28:51 +0300 | [diff] [blame] | 348 | if (vif->type != NL80211_IFTYPE_STATION) |
| 349 | return; |
| 350 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 351 | iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode); |
| 352 | } |
| 353 | |
| 354 | static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable) |
| 355 | { |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 356 | struct iwl_mvm_sta *mvmsta; |
| 357 | int i, err; |
| 358 | |
Sara Sharon | 0ae9881 | 2017-01-04 14:53:58 +0200 | [diff] [blame] | 359 | for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) { |
Sara Sharon | 13303c0 | 2016-04-10 15:51:54 +0300 | [diff] [blame] | 360 | mvmsta = iwl_mvm_sta_from_staid_protected(mvm, i); |
| 361 | if (!mvmsta) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 362 | continue; |
Sara Sharon | 13303c0 | 2016-04-10 15:51:54 +0300 | [diff] [blame] | 363 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 364 | if (enable == mvmsta->tt_tx_protection) |
| 365 | continue; |
Johannes Berg | e126b5d | 2013-06-28 13:39:18 +0200 | [diff] [blame] | 366 | err = iwl_mvm_tx_protection(mvm, mvmsta, enable); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 367 | if (err) { |
| 368 | IWL_ERR(mvm, "Failed to %s Tx protection\n", |
| 369 | enable ? "enable" : "disable"); |
| 370 | } else { |
| 371 | IWL_DEBUG_TEMP(mvm, "%s Tx protection\n", |
| 372 | enable ? "Enable" : "Disable"); |
| 373 | mvmsta->tt_tx_protection = enable; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
Ido Yariv | 0c0e2c7 | 2014-01-16 21:12:02 -0500 | [diff] [blame] | 378 | void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 379 | { |
| 380 | struct iwl_host_cmd cmd = { |
| 381 | .id = REPLY_THERMAL_MNG_BACKOFF, |
| 382 | .len = { sizeof(u32), }, |
| 383 | .data = { &backoff, }, |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 384 | }; |
| 385 | |
Ido Yariv | 0c0e2c7 | 2014-01-16 21:12:02 -0500 | [diff] [blame] | 386 | backoff = max(backoff, mvm->thermal_throttle.min_backoff); |
| 387 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 388 | if (iwl_mvm_send_cmd(mvm, &cmd) == 0) { |
| 389 | IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n", |
| 390 | backoff); |
| 391 | mvm->thermal_throttle.tx_backoff = backoff; |
| 392 | } else { |
| 393 | IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n"); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void iwl_mvm_tt_handler(struct iwl_mvm *mvm) |
| 398 | { |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 399 | struct iwl_tt_params *params = &mvm->thermal_throttle.params; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 400 | struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; |
| 401 | s32 temperature = mvm->temperature; |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 402 | bool throttle_enable = false; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 403 | int i; |
| 404 | u32 tx_backoff; |
| 405 | |
| 406 | IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature); |
| 407 | |
| 408 | if (params->support_ct_kill && temperature >= params->ct_kill_entry) { |
| 409 | iwl_mvm_enter_ctkill(mvm); |
| 410 | return; |
| 411 | } |
| 412 | |
Luciano Coelho | b689fa7 | 2014-08-20 17:26:58 +0300 | [diff] [blame] | 413 | if (params->support_ct_kill && |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 414 | temperature <= params->ct_kill_exit) { |
Luciano Coelho | b689fa7 | 2014-08-20 17:26:58 +0300 | [diff] [blame] | 415 | iwl_mvm_exit_ctkill(mvm); |
| 416 | return; |
| 417 | } |
| 418 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 419 | if (params->support_dynamic_smps) { |
| 420 | if (!tt->dynamic_smps && |
| 421 | temperature >= params->dynamic_smps_entry) { |
| 422 | IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n"); |
| 423 | tt->dynamic_smps = true; |
| 424 | ieee80211_iterate_active_interfaces_atomic( |
| 425 | mvm->hw, IEEE80211_IFACE_ITER_NORMAL, |
| 426 | iwl_mvm_tt_smps_iterator, mvm); |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 427 | throttle_enable = true; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 428 | } else if (tt->dynamic_smps && |
| 429 | temperature <= params->dynamic_smps_exit) { |
| 430 | IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n"); |
| 431 | tt->dynamic_smps = false; |
| 432 | ieee80211_iterate_active_interfaces_atomic( |
| 433 | mvm->hw, IEEE80211_IFACE_ITER_NORMAL, |
| 434 | iwl_mvm_tt_smps_iterator, mvm); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if (params->support_tx_protection) { |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 439 | if (temperature >= params->tx_protection_entry) { |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 440 | iwl_mvm_tt_tx_protection(mvm, true); |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 441 | throttle_enable = true; |
| 442 | } else if (temperature <= params->tx_protection_exit) { |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 443 | iwl_mvm_tt_tx_protection(mvm, false); |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 444 | } |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | if (params->support_tx_backoff) { |
Eytan Lifshitz | 1b8ebbd | 2014-04-01 16:44:21 +0300 | [diff] [blame] | 448 | tx_backoff = tt->min_backoff; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 449 | for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) { |
| 450 | if (temperature < params->tx_backoff[i].temperature) |
| 451 | break; |
Eytan Lifshitz | 1b8ebbd | 2014-04-01 16:44:21 +0300 | [diff] [blame] | 452 | tx_backoff = max(tt->min_backoff, |
| 453 | params->tx_backoff[i].backoff); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 454 | } |
Eytan Lifshitz | 1b8ebbd | 2014-04-01 16:44:21 +0300 | [diff] [blame] | 455 | if (tx_backoff != tt->min_backoff) |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 456 | throttle_enable = true; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 457 | if (tt->tx_backoff != tx_backoff) |
| 458 | iwl_mvm_tt_tx_backoff(mvm, tx_backoff); |
| 459 | } |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 460 | |
| 461 | if (!tt->throttle && throttle_enable) { |
| 462 | IWL_WARN(mvm, |
| 463 | "Due to high temperature thermal throttling initiated\n"); |
| 464 | tt->throttle = true; |
Eytan Lifshitz | 19a04bd | 2014-04-02 21:37:57 +0300 | [diff] [blame] | 465 | } else if (tt->throttle && !tt->dynamic_smps && |
| 466 | tt->tx_backoff == tt->min_backoff && |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 467 | temperature <= params->tx_protection_exit) { |
| 468 | IWL_WARN(mvm, |
| 469 | "Temperature is back to normal thermal throttling stopped\n"); |
| 470 | tt->throttle = false; |
| 471 | } |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 472 | } |
| 473 | |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 474 | static const struct iwl_tt_params iwl_mvm_default_tt_params = { |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 475 | .ct_kill_entry = 118, |
| 476 | .ct_kill_exit = 96, |
| 477 | .ct_kill_duration = 5, |
| 478 | .dynamic_smps_entry = 114, |
| 479 | .dynamic_smps_exit = 110, |
| 480 | .tx_protection_entry = 114, |
| 481 | .tx_protection_exit = 108, |
| 482 | .tx_backoff = { |
| 483 | {.temperature = 112, .backoff = 200}, |
| 484 | {.temperature = 113, .backoff = 600}, |
| 485 | {.temperature = 114, .backoff = 1200}, |
| 486 | {.temperature = 115, .backoff = 2000}, |
| 487 | {.temperature = 116, .backoff = 4000}, |
| 488 | {.temperature = 117, .backoff = 10000}, |
| 489 | }, |
| 490 | .support_ct_kill = true, |
| 491 | .support_dynamic_smps = true, |
| 492 | .support_tx_protection = true, |
| 493 | .support_tx_backoff = true, |
| 494 | }; |
| 495 | |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 496 | /* budget in mWatt */ |
| 497 | static const u32 iwl_mvm_cdev_budgets[] = { |
| 498 | 2000, /* cooling state 0 */ |
| 499 | 1800, /* cooling state 1 */ |
| 500 | 1600, /* cooling state 2 */ |
| 501 | 1400, /* cooling state 3 */ |
| 502 | 1200, /* cooling state 4 */ |
| 503 | 1000, /* cooling state 5 */ |
| 504 | 900, /* cooling state 6 */ |
| 505 | 800, /* cooling state 7 */ |
| 506 | 700, /* cooling state 8 */ |
| 507 | 650, /* cooling state 9 */ |
| 508 | 600, /* cooling state 10 */ |
| 509 | 550, /* cooling state 11 */ |
| 510 | 500, /* cooling state 12 */ |
| 511 | 450, /* cooling state 13 */ |
| 512 | 400, /* cooling state 14 */ |
| 513 | 350, /* cooling state 15 */ |
| 514 | 300, /* cooling state 16 */ |
| 515 | 250, /* cooling state 17 */ |
| 516 | 200, /* cooling state 18 */ |
| 517 | 150, /* cooling state 19 */ |
| 518 | }; |
| 519 | |
| 520 | int iwl_mvm_ctdp_command(struct iwl_mvm *mvm, u32 op, u32 state) |
Chaya Rachel Ivgi | 00f481b | 2016-02-24 12:19:22 +0200 | [diff] [blame] | 521 | { |
| 522 | struct iwl_mvm_ctdp_cmd cmd = { |
| 523 | .operation = cpu_to_le32(op), |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 524 | .budget = cpu_to_le32(iwl_mvm_cdev_budgets[state]), |
Chaya Rachel Ivgi | 00f481b | 2016-02-24 12:19:22 +0200 | [diff] [blame] | 525 | .window_size = 0, |
| 526 | }; |
| 527 | int ret; |
| 528 | u32 status; |
| 529 | |
| 530 | lockdep_assert_held(&mvm->mutex); |
| 531 | |
Luca Coelho | d460f1f | 2017-09-02 11:25:40 +0300 | [diff] [blame] | 532 | status = 0; |
Chaya Rachel Ivgi | 00f481b | 2016-02-24 12:19:22 +0200 | [diff] [blame] | 533 | ret = iwl_mvm_send_cmd_pdu_status(mvm, WIDE_ID(PHY_OPS_GROUP, |
| 534 | CTDP_CONFIG_CMD), |
| 535 | sizeof(cmd), &cmd, &status); |
| 536 | |
| 537 | if (ret) { |
| 538 | IWL_ERR(mvm, "cTDP command failed (err=%d)\n", ret); |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | switch (op) { |
| 543 | case CTDP_CMD_OPERATION_START: |
| 544 | #ifdef CONFIG_THERMAL |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 545 | mvm->cooling_dev.cur_state = state; |
Chaya Rachel Ivgi | 00f481b | 2016-02-24 12:19:22 +0200 | [diff] [blame] | 546 | #endif /* CONFIG_THERMAL */ |
| 547 | break; |
| 548 | case CTDP_CMD_OPERATION_REPORT: |
| 549 | IWL_DEBUG_TEMP(mvm, "cTDP avg energy in mWatt = %d\n", status); |
| 550 | /* when the function is called with CTDP_CMD_OPERATION_REPORT |
| 551 | * option the function should return the average budget value |
| 552 | * that is received from the FW. |
| 553 | * The budget can't be less or equal to 0, so it's possible |
| 554 | * to distinguish between error values and budgets. |
| 555 | */ |
| 556 | return status; |
| 557 | case CTDP_CMD_OPERATION_STOP: |
| 558 | IWL_DEBUG_TEMP(mvm, "cTDP stopped successfully\n"); |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 565 | #ifdef CONFIG_THERMAL |
| 566 | static int compare_temps(const void *a, const void *b) |
| 567 | { |
| 568 | return ((s16)le16_to_cpu(*(__le16 *)a) - |
| 569 | (s16)le16_to_cpu(*(__le16 *)b)); |
| 570 | } |
| 571 | |
| 572 | int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm *mvm) |
| 573 | { |
| 574 | struct temp_report_ths_cmd cmd = {0}; |
| 575 | int ret, i, j, idx = 0; |
| 576 | |
| 577 | lockdep_assert_held(&mvm->mutex); |
| 578 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 579 | if (!mvm->tz_device.tzone) |
| 580 | return -EINVAL; |
| 581 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 582 | /* The driver holds array of temperature trips that are unsorted |
| 583 | * and uncompressed, the FW should get it compressed and sorted |
| 584 | */ |
| 585 | |
| 586 | /* compress temp_trips to cmd array, remove uninitialized values*/ |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 587 | for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) { |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 588 | if (mvm->tz_device.temp_trips[i] != S16_MIN) { |
| 589 | cmd.thresholds[idx++] = |
| 590 | cpu_to_le16(mvm->tz_device.temp_trips[i]); |
| 591 | } |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 592 | } |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 593 | cmd.num_temps = cpu_to_le32(idx); |
| 594 | |
| 595 | if (!idx) |
| 596 | goto send; |
| 597 | |
| 598 | /*sort cmd array*/ |
| 599 | sort(cmd.thresholds, idx, sizeof(s16), compare_temps, NULL); |
| 600 | |
| 601 | /* we should save the indexes of trips because we sort |
| 602 | * and compress the orginal array |
| 603 | */ |
| 604 | for (i = 0; i < idx; i++) { |
| 605 | for (j = 0; j < IWL_MAX_DTS_TRIPS; j++) { |
| 606 | if (le16_to_cpu(cmd.thresholds[i]) == |
| 607 | mvm->tz_device.temp_trips[j]) |
| 608 | mvm->tz_device.fw_trips_index[i] = j; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | send: |
| 613 | ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP, |
| 614 | TEMP_REPORTING_THRESHOLDS_CMD), |
| 615 | 0, sizeof(cmd), &cmd); |
| 616 | if (ret) |
| 617 | IWL_ERR(mvm, "TEMP_REPORT_THS_CMD command failed (err=%d)\n", |
| 618 | ret); |
| 619 | |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | static int iwl_mvm_tzone_get_temp(struct thermal_zone_device *device, |
| 624 | int *temperature) |
| 625 | { |
| 626 | struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata; |
| 627 | int ret; |
| 628 | int temp; |
| 629 | |
| 630 | mutex_lock(&mvm->mutex); |
| 631 | |
Johannes Berg | aab6930 | 2017-03-22 22:05:12 +0100 | [diff] [blame] | 632 | if (!iwl_mvm_firmware_running(mvm) || |
Johannes Berg | 702e975 | 2017-06-02 11:56:58 +0200 | [diff] [blame] | 633 | mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) { |
Luca Coelho | 1442a9a | 2017-09-18 14:39:26 +0300 | [diff] [blame] | 634 | ret = -ENODATA; |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 635 | goto out; |
| 636 | } |
| 637 | |
| 638 | ret = iwl_mvm_get_temp(mvm, &temp); |
| 639 | if (ret) |
| 640 | goto out; |
| 641 | |
| 642 | *temperature = temp * 1000; |
| 643 | |
| 644 | out: |
| 645 | mutex_unlock(&mvm->mutex); |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | static int iwl_mvm_tzone_get_trip_temp(struct thermal_zone_device *device, |
| 650 | int trip, int *temp) |
| 651 | { |
| 652 | struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata; |
| 653 | |
| 654 | if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) |
| 655 | return -EINVAL; |
| 656 | |
| 657 | *temp = mvm->tz_device.temp_trips[trip] * 1000; |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | static int iwl_mvm_tzone_get_trip_type(struct thermal_zone_device *device, |
| 663 | int trip, enum thermal_trip_type *type) |
| 664 | { |
| 665 | if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) |
| 666 | return -EINVAL; |
| 667 | |
| 668 | *type = THERMAL_TRIP_PASSIVE; |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device *device, |
| 674 | int trip, int temp) |
| 675 | { |
| 676 | struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata; |
| 677 | struct iwl_mvm_thermal_device *tzone; |
| 678 | int i, ret; |
| 679 | s16 temperature; |
| 680 | |
| 681 | mutex_lock(&mvm->mutex); |
| 682 | |
Johannes Berg | aab6930 | 2017-03-22 22:05:12 +0100 | [diff] [blame] | 683 | if (!iwl_mvm_firmware_running(mvm) || |
Johannes Berg | 702e975 | 2017-06-02 11:56:58 +0200 | [diff] [blame] | 684 | mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) { |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 685 | ret = -EIO; |
| 686 | goto out; |
| 687 | } |
| 688 | |
| 689 | if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) { |
| 690 | ret = -EINVAL; |
| 691 | goto out; |
| 692 | } |
| 693 | |
| 694 | if ((temp / 1000) > S16_MAX) { |
| 695 | ret = -EINVAL; |
| 696 | goto out; |
| 697 | } |
| 698 | |
| 699 | temperature = (s16)(temp / 1000); |
| 700 | tzone = &mvm->tz_device; |
| 701 | |
| 702 | if (!tzone) { |
| 703 | ret = -EIO; |
| 704 | goto out; |
| 705 | } |
| 706 | |
| 707 | /* no updates*/ |
| 708 | if (tzone->temp_trips[trip] == temperature) { |
| 709 | ret = 0; |
| 710 | goto out; |
| 711 | } |
| 712 | |
| 713 | /* already existing temperature */ |
| 714 | for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) { |
| 715 | if (tzone->temp_trips[i] == temperature) { |
| 716 | ret = -EINVAL; |
| 717 | goto out; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | tzone->temp_trips[trip] = temperature; |
| 722 | |
| 723 | ret = iwl_mvm_send_temp_report_ths_cmd(mvm); |
| 724 | out: |
| 725 | mutex_unlock(&mvm->mutex); |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | static struct thermal_zone_device_ops tzone_ops = { |
| 730 | .get_temp = iwl_mvm_tzone_get_temp, |
| 731 | .get_trip_temp = iwl_mvm_tzone_get_trip_temp, |
| 732 | .get_trip_type = iwl_mvm_tzone_get_trip_type, |
| 733 | .set_trip_temp = iwl_mvm_tzone_set_trip_temp, |
| 734 | }; |
| 735 | |
| 736 | /* make all trips writable */ |
| 737 | #define IWL_WRITABLE_TRIPS_MSK (BIT(IWL_MAX_DTS_TRIPS) - 1) |
| 738 | |
| 739 | static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm) |
| 740 | { |
| 741 | int i; |
| 742 | char name[] = "iwlwifi"; |
| 743 | |
| 744 | if (!iwl_mvm_is_tt_in_fw(mvm)) { |
| 745 | mvm->tz_device.tzone = NULL; |
| 746 | |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH); |
| 751 | |
| 752 | mvm->tz_device.tzone = thermal_zone_device_register(name, |
| 753 | IWL_MAX_DTS_TRIPS, |
| 754 | IWL_WRITABLE_TRIPS_MSK, |
| 755 | mvm, &tzone_ops, |
| 756 | NULL, 0, 0); |
| 757 | if (IS_ERR(mvm->tz_device.tzone)) { |
| 758 | IWL_DEBUG_TEMP(mvm, |
| 759 | "Failed to register to thermal zone (err = %ld)\n", |
| 760 | PTR_ERR(mvm->tz_device.tzone)); |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 761 | mvm->tz_device.tzone = NULL; |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 762 | return; |
| 763 | } |
| 764 | |
| 765 | /* 0 is a valid temperature, |
| 766 | * so initialize the array with S16_MIN which invalid temperature |
| 767 | */ |
| 768 | for (i = 0 ; i < IWL_MAX_DTS_TRIPS; i++) |
| 769 | mvm->tz_device.temp_trips[i] = S16_MIN; |
| 770 | } |
| 771 | |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 772 | static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device *cdev, |
| 773 | unsigned long *state) |
| 774 | { |
| 775 | *state = ARRAY_SIZE(iwl_mvm_cdev_budgets) - 1; |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device *cdev, |
| 781 | unsigned long *state) |
| 782 | { |
| 783 | struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata); |
| 784 | |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 785 | *state = mvm->cooling_dev.cur_state; |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 786 | |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device *cdev, |
| 791 | unsigned long new_state) |
| 792 | { |
| 793 | struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata); |
| 794 | int ret; |
| 795 | |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 796 | mutex_lock(&mvm->mutex); |
| 797 | |
Johannes Berg | aab6930 | 2017-03-22 22:05:12 +0100 | [diff] [blame] | 798 | if (!iwl_mvm_firmware_running(mvm) || |
Johannes Berg | 702e975 | 2017-06-02 11:56:58 +0200 | [diff] [blame] | 799 | mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) { |
Johannes Berg | d995440 | 2017-03-22 22:00:10 +0100 | [diff] [blame] | 800 | ret = -EIO; |
| 801 | goto unlock; |
| 802 | } |
| 803 | |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 804 | if (new_state >= ARRAY_SIZE(iwl_mvm_cdev_budgets)) { |
| 805 | ret = -EINVAL; |
| 806 | goto unlock; |
| 807 | } |
| 808 | |
| 809 | ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START, |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 810 | new_state); |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 811 | |
| 812 | unlock: |
| 813 | mutex_unlock(&mvm->mutex); |
| 814 | return ret; |
| 815 | } |
| 816 | |
Bhumika Goyal | b1a1efc | 2017-06-21 14:10:42 +0530 | [diff] [blame] | 817 | static const struct thermal_cooling_device_ops tcooling_ops = { |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 818 | .get_max_state = iwl_mvm_tcool_get_max_state, |
| 819 | .get_cur_state = iwl_mvm_tcool_get_cur_state, |
| 820 | .set_cur_state = iwl_mvm_tcool_set_cur_state, |
| 821 | }; |
| 822 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 823 | static void iwl_mvm_cooling_device_register(struct iwl_mvm *mvm) |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 824 | { |
| 825 | char name[] = "iwlwifi"; |
| 826 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 827 | if (!iwl_mvm_is_ctdp_supported(mvm)) |
| 828 | return; |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 829 | |
| 830 | BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH); |
| 831 | |
| 832 | mvm->cooling_dev.cdev = |
| 833 | thermal_cooling_device_register(name, |
| 834 | mvm, |
| 835 | &tcooling_ops); |
| 836 | |
| 837 | if (IS_ERR(mvm->cooling_dev.cdev)) { |
| 838 | IWL_DEBUG_TEMP(mvm, |
| 839 | "Failed to register to cooling device (err = %ld)\n", |
| 840 | PTR_ERR(mvm->cooling_dev.cdev)); |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 841 | mvm->cooling_dev.cdev = NULL; |
| 842 | return; |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 843 | } |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 846 | static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm *mvm) |
| 847 | { |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 848 | if (!iwl_mvm_is_tt_in_fw(mvm) || !mvm->tz_device.tzone) |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 849 | return; |
| 850 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 851 | IWL_DEBUG_TEMP(mvm, "Thermal zone device unregister\n"); |
Jens Axboe | 92549cd | 2017-01-17 14:22:24 -0800 | [diff] [blame] | 852 | if (mvm->tz_device.tzone) { |
| 853 | thermal_zone_device_unregister(mvm->tz_device.tzone); |
| 854 | mvm->tz_device.tzone = NULL; |
| 855 | } |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 856 | } |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 857 | |
| 858 | static void iwl_mvm_cooling_device_unregister(struct iwl_mvm *mvm) |
| 859 | { |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 860 | if (!iwl_mvm_is_ctdp_supported(mvm) || !mvm->cooling_dev.cdev) |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 861 | return; |
| 862 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 863 | IWL_DEBUG_TEMP(mvm, "Cooling device unregister\n"); |
Jens Axboe | 92549cd | 2017-01-17 14:22:24 -0800 | [diff] [blame] | 864 | if (mvm->cooling_dev.cdev) { |
| 865 | thermal_cooling_device_unregister(mvm->cooling_dev.cdev); |
| 866 | mvm->cooling_dev.cdev = NULL; |
| 867 | } |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 868 | } |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 869 | #endif /* CONFIG_THERMAL */ |
| 870 | |
| 871 | void iwl_mvm_thermal_initialize(struct iwl_mvm *mvm, u32 min_backoff) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 872 | { |
| 873 | struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; |
| 874 | |
| 875 | IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n"); |
Eytan Lifshitz | 6be497f | 2013-07-24 14:49:18 +0300 | [diff] [blame] | 876 | |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 877 | if (mvm->cfg->thermal_params) |
| 878 | tt->params = *mvm->cfg->thermal_params; |
Eytan Lifshitz | 6be497f | 2013-07-24 14:49:18 +0300 | [diff] [blame] | 879 | else |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 880 | tt->params = iwl_mvm_default_tt_params; |
Eytan Lifshitz | 6be497f | 2013-07-24 14:49:18 +0300 | [diff] [blame] | 881 | |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 882 | tt->throttle = false; |
Luciano Coelho | 19789ab | 2014-12-15 14:15:15 +0200 | [diff] [blame] | 883 | tt->dynamic_smps = false; |
Ido Yariv | 0c0e2c7 | 2014-01-16 21:12:02 -0500 | [diff] [blame] | 884 | tt->min_backoff = min_backoff; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 885 | INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 886 | |
| 887 | #ifdef CONFIG_THERMAL |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 888 | iwl_mvm_cooling_device_register(mvm); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 889 | iwl_mvm_thermal_zone_register(mvm); |
| 890 | #endif |
Liad Kaufman | de8ba41 | 2017-03-16 13:00:59 +0200 | [diff] [blame] | 891 | mvm->init_status |= IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 892 | } |
| 893 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 894 | void iwl_mvm_thermal_exit(struct iwl_mvm *mvm) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 895 | { |
Liad Kaufman | de8ba41 | 2017-03-16 13:00:59 +0200 | [diff] [blame] | 896 | if (!(mvm->init_status & IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE)) |
| 897 | return; |
| 898 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 899 | cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit); |
| 900 | IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n"); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 901 | |
| 902 | #ifdef CONFIG_THERMAL |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 903 | iwl_mvm_cooling_device_unregister(mvm); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 904 | iwl_mvm_thermal_zone_unregister(mvm); |
| 905 | #endif |
Liad Kaufman | de8ba41 | 2017-03-16 13:00:59 +0200 | [diff] [blame] | 906 | mvm->init_status &= ~IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 907 | } |