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 | a0a09243 | 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 | |
| 74 | static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm) |
| 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 | a0a09243 | 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 | a0a09243 | 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 | a0a09243 | 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 | a0a09243 | 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 | a0a09243 | 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 | a0a09243 | 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 | a0a09243 | 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 | a0a09243 | 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 | a0a09243 | 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 | |
| 207 | /* |
| 208 | * We are now handling a temperature notification from the firmware |
| 209 | * in ASYNC and hold the mutex. thermal_notify_framework will call |
| 210 | * us back through get_temp() which ought to send a SYNC command to |
| 211 | * the firmware and hence to take the mutex. |
| 212 | * Avoid the deadlock by unlocking the mutex here. |
| 213 | */ |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 214 | if (mvm->tz_device.tzone) { |
| 215 | struct iwl_mvm_thermal_device *tz_dev = &mvm->tz_device; |
| 216 | |
| 217 | mutex_unlock(&mvm->mutex); |
| 218 | thermal_notify_framework(tz_dev->tzone, |
| 219 | tz_dev->fw_trips_index[ths_crossed]); |
| 220 | mutex_lock(&mvm->mutex); |
| 221 | } |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 222 | #endif /* CONFIG_THERMAL */ |
Luciano Coelho | ea9af24 | 2014-11-06 10:34:49 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Chaya Rachel Ivgi | 0a3b711 | 2015-12-16 16:34:55 +0200 | [diff] [blame] | 225 | void iwl_mvm_ct_kill_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) |
| 226 | { |
| 227 | struct iwl_rx_packet *pkt = rxb_addr(rxb); |
| 228 | struct ct_kill_notif *notif; |
| 229 | int len = iwl_rx_packet_payload_len(pkt); |
| 230 | |
| 231 | if (WARN_ON_ONCE(len != sizeof(*notif))) { |
| 232 | IWL_ERR(mvm, "Invalid CT_KILL_NOTIFICATION\n"); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | notif = (struct ct_kill_notif *)pkt->data; |
| 237 | IWL_DEBUG_TEMP(mvm, "CT Kill notification temperature = %d\n", |
| 238 | notif->temperature); |
| 239 | |
| 240 | iwl_mvm_enter_ctkill(mvm); |
| 241 | } |
| 242 | |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 243 | static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm) |
| 244 | { |
| 245 | struct iwl_dts_measurement_cmd cmd = { |
| 246 | .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP), |
| 247 | }; |
Arik Nemtsov | 78efc70 | 2015-10-06 12:22:47 +0300 | [diff] [blame] | 248 | struct iwl_ext_dts_measurement_cmd extcmd = { |
| 249 | .control_mode = cpu_to_le32(DTS_AUTOMATIC), |
| 250 | }; |
Aviya Erenfeld | 09eef33 | 2015-09-01 19:34:38 +0300 | [diff] [blame] | 251 | u32 cmdid; |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 252 | |
Aviya Erenfeld | 09eef33 | 2015-09-01 19:34:38 +0300 | [diff] [blame] | 253 | if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR)) |
| 254 | cmdid = iwl_cmd_id(CMD_DTS_MEASUREMENT_TRIGGER_WIDE, |
| 255 | PHY_OPS_GROUP, 0); |
| 256 | else |
| 257 | cmdid = CMD_DTS_MEASUREMENT_TRIGGER; |
Arik Nemtsov | 78efc70 | 2015-10-06 12:22:47 +0300 | [diff] [blame] | 258 | |
| 259 | if (!fw_has_capa(&mvm->fw->ucode_capa, |
| 260 | IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE)) |
| 261 | return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(cmd), &cmd); |
| 262 | |
| 263 | return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(extcmd), &extcmd); |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 264 | } |
| 265 | |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 266 | int iwl_mvm_get_temp(struct iwl_mvm *mvm, s32 *temp) |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 267 | { |
| 268 | struct iwl_notification_wait wait_temp_notif; |
Aviya Erenfeld | 09eef33 | 2015-09-01 19:34:38 +0300 | [diff] [blame] | 269 | static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP, |
| 270 | DTS_MEASUREMENT_NOTIF_WIDE) }; |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 271 | int ret; |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 272 | |
Aviya Erenfeld | 09eef33 | 2015-09-01 19:34:38 +0300 | [diff] [blame] | 273 | if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR)) |
| 274 | temp_notif[0] = DTS_MEASUREMENT_NOTIFICATION; |
| 275 | |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 276 | lockdep_assert_held(&mvm->mutex); |
| 277 | |
| 278 | iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif, |
| 279 | temp_notif, ARRAY_SIZE(temp_notif), |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 280 | iwl_mvm_temp_notif_wait, temp); |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 281 | |
| 282 | ret = iwl_mvm_get_temp_cmd(mvm); |
| 283 | if (ret) { |
| 284 | IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret); |
| 285 | iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif, |
| 290 | IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT); |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 291 | if (ret) |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 292 | IWL_ERR(mvm, "Getting the temperature timed out\n"); |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 293 | |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 294 | return ret; |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 295 | } |
| 296 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 297 | static void check_exit_ctkill(struct work_struct *work) |
| 298 | { |
| 299 | struct iwl_mvm_tt_mgmt *tt; |
| 300 | struct iwl_mvm *mvm; |
| 301 | u32 duration; |
| 302 | s32 temp; |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 303 | int ret; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 304 | |
| 305 | tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work); |
| 306 | mvm = container_of(tt, struct iwl_mvm, thermal_throttle); |
| 307 | |
Chaya Rachel Ivgi | 0a3b711 | 2015-12-16 16:34:55 +0200 | [diff] [blame] | 308 | if (iwl_mvm_is_tt_in_fw(mvm)) { |
| 309 | iwl_mvm_exit_ctkill(mvm); |
| 310 | |
| 311 | return; |
| 312 | } |
| 313 | |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 314 | duration = tt->params.ct_kill_duration; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 315 | |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 316 | mutex_lock(&mvm->mutex); |
| 317 | |
| 318 | if (__iwl_mvm_mac_start(mvm)) |
Eliad Peller | 576eeee | 2014-07-01 18:38:38 +0300 | [diff] [blame] | 319 | goto reschedule; |
| 320 | |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 321 | /* make sure the device is available for direct read/writes */ |
| 322 | if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) { |
| 323 | __iwl_mvm_mac_stop(mvm); |
| 324 | goto reschedule; |
| 325 | } |
| 326 | |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 327 | ret = iwl_mvm_get_temp(mvm, &temp); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 328 | |
Eliad Peller | 576eeee | 2014-07-01 18:38:38 +0300 | [diff] [blame] | 329 | iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL); |
| 330 | |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 331 | __iwl_mvm_mac_stop(mvm); |
| 332 | |
Chaya Rachel Ivgi | 7869318 | 2015-12-27 13:45:42 +0200 | [diff] [blame] | 333 | if (ret) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 334 | goto reschedule; |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 335 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 336 | IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp); |
| 337 | |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 338 | if (temp <= tt->params.ct_kill_exit) { |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 339 | mutex_unlock(&mvm->mutex); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 340 | iwl_mvm_exit_ctkill(mvm); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | reschedule: |
Luciano Coelho | a0a09243 | 2014-09-04 12:29:15 +0300 | [diff] [blame] | 345 | mutex_unlock(&mvm->mutex); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 346 | schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit, |
| 347 | round_jiffies(duration * HZ)); |
| 348 | } |
| 349 | |
| 350 | static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac, |
| 351 | struct ieee80211_vif *vif) |
| 352 | { |
| 353 | struct iwl_mvm *mvm = _data; |
| 354 | enum ieee80211_smps_mode smps_mode; |
| 355 | |
| 356 | lockdep_assert_held(&mvm->mutex); |
| 357 | |
| 358 | if (mvm->thermal_throttle.dynamic_smps) |
| 359 | smps_mode = IEEE80211_SMPS_DYNAMIC; |
| 360 | else |
| 361 | smps_mode = IEEE80211_SMPS_AUTOMATIC; |
| 362 | |
Eytan Lifshitz | fded313 | 2013-06-04 12:28:51 +0300 | [diff] [blame] | 363 | if (vif->type != NL80211_IFTYPE_STATION) |
| 364 | return; |
| 365 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 366 | iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode); |
| 367 | } |
| 368 | |
| 369 | static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable) |
| 370 | { |
| 371 | struct ieee80211_sta *sta; |
| 372 | struct iwl_mvm_sta *mvmsta; |
| 373 | int i, err; |
| 374 | |
| 375 | for (i = 0; i < IWL_MVM_STATION_COUNT; i++) { |
| 376 | sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i], |
| 377 | lockdep_is_held(&mvm->mutex)); |
| 378 | if (IS_ERR_OR_NULL(sta)) |
| 379 | continue; |
Johannes Berg | 5b577a9 | 2013-11-14 18:20:04 +0100 | [diff] [blame] | 380 | mvmsta = iwl_mvm_sta_from_mac80211(sta); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 381 | if (enable == mvmsta->tt_tx_protection) |
| 382 | continue; |
Johannes Berg | e126b5d | 2013-06-28 13:39:18 +0200 | [diff] [blame] | 383 | err = iwl_mvm_tx_protection(mvm, mvmsta, enable); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 384 | if (err) { |
| 385 | IWL_ERR(mvm, "Failed to %s Tx protection\n", |
| 386 | enable ? "enable" : "disable"); |
| 387 | } else { |
| 388 | IWL_DEBUG_TEMP(mvm, "%s Tx protection\n", |
| 389 | enable ? "Enable" : "Disable"); |
| 390 | mvmsta->tt_tx_protection = enable; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Ido Yariv | 0c0e2c7 | 2014-01-16 21:12:02 -0500 | [diff] [blame] | 395 | void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 396 | { |
| 397 | struct iwl_host_cmd cmd = { |
| 398 | .id = REPLY_THERMAL_MNG_BACKOFF, |
| 399 | .len = { sizeof(u32), }, |
| 400 | .data = { &backoff, }, |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 401 | }; |
| 402 | |
Ido Yariv | 0c0e2c7 | 2014-01-16 21:12:02 -0500 | [diff] [blame] | 403 | backoff = max(backoff, mvm->thermal_throttle.min_backoff); |
| 404 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 405 | if (iwl_mvm_send_cmd(mvm, &cmd) == 0) { |
| 406 | IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n", |
| 407 | backoff); |
| 408 | mvm->thermal_throttle.tx_backoff = backoff; |
| 409 | } else { |
| 410 | IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n"); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | void iwl_mvm_tt_handler(struct iwl_mvm *mvm) |
| 415 | { |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 416 | struct iwl_tt_params *params = &mvm->thermal_throttle.params; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 417 | struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; |
| 418 | s32 temperature = mvm->temperature; |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 419 | bool throttle_enable = false; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 420 | int i; |
| 421 | u32 tx_backoff; |
| 422 | |
| 423 | IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature); |
| 424 | |
| 425 | if (params->support_ct_kill && temperature >= params->ct_kill_entry) { |
| 426 | iwl_mvm_enter_ctkill(mvm); |
| 427 | return; |
| 428 | } |
| 429 | |
Luciano Coelho | b689fa7 | 2014-08-20 17:26:58 +0300 | [diff] [blame] | 430 | if (params->support_ct_kill && |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 431 | temperature <= params->ct_kill_exit) { |
Luciano Coelho | b689fa7 | 2014-08-20 17:26:58 +0300 | [diff] [blame] | 432 | iwl_mvm_exit_ctkill(mvm); |
| 433 | return; |
| 434 | } |
| 435 | |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 436 | if (params->support_dynamic_smps) { |
| 437 | if (!tt->dynamic_smps && |
| 438 | temperature >= params->dynamic_smps_entry) { |
| 439 | IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n"); |
| 440 | tt->dynamic_smps = true; |
| 441 | ieee80211_iterate_active_interfaces_atomic( |
| 442 | mvm->hw, IEEE80211_IFACE_ITER_NORMAL, |
| 443 | iwl_mvm_tt_smps_iterator, mvm); |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 444 | throttle_enable = true; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 445 | } else if (tt->dynamic_smps && |
| 446 | temperature <= params->dynamic_smps_exit) { |
| 447 | IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n"); |
| 448 | tt->dynamic_smps = false; |
| 449 | ieee80211_iterate_active_interfaces_atomic( |
| 450 | mvm->hw, IEEE80211_IFACE_ITER_NORMAL, |
| 451 | iwl_mvm_tt_smps_iterator, mvm); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | if (params->support_tx_protection) { |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 456 | if (temperature >= params->tx_protection_entry) { |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 457 | iwl_mvm_tt_tx_protection(mvm, true); |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 458 | throttle_enable = true; |
| 459 | } else if (temperature <= params->tx_protection_exit) { |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 460 | iwl_mvm_tt_tx_protection(mvm, false); |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 461 | } |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | if (params->support_tx_backoff) { |
Eytan Lifshitz | 1b8ebbd | 2014-04-01 16:44:21 +0300 | [diff] [blame] | 465 | tx_backoff = tt->min_backoff; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 466 | for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) { |
| 467 | if (temperature < params->tx_backoff[i].temperature) |
| 468 | break; |
Eytan Lifshitz | 1b8ebbd | 2014-04-01 16:44:21 +0300 | [diff] [blame] | 469 | tx_backoff = max(tt->min_backoff, |
| 470 | params->tx_backoff[i].backoff); |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 471 | } |
Eytan Lifshitz | 1b8ebbd | 2014-04-01 16:44:21 +0300 | [diff] [blame] | 472 | if (tx_backoff != tt->min_backoff) |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 473 | throttle_enable = true; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 474 | if (tt->tx_backoff != tx_backoff) |
| 475 | iwl_mvm_tt_tx_backoff(mvm, tx_backoff); |
| 476 | } |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 477 | |
| 478 | if (!tt->throttle && throttle_enable) { |
| 479 | IWL_WARN(mvm, |
| 480 | "Due to high temperature thermal throttling initiated\n"); |
| 481 | tt->throttle = true; |
Eytan Lifshitz | 19a04bd | 2014-04-02 21:37:57 +0300 | [diff] [blame] | 482 | } else if (tt->throttle && !tt->dynamic_smps && |
| 483 | tt->tx_backoff == tt->min_backoff && |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 484 | temperature <= params->tx_protection_exit) { |
| 485 | IWL_WARN(mvm, |
| 486 | "Temperature is back to normal thermal throttling stopped\n"); |
| 487 | tt->throttle = false; |
| 488 | } |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 489 | } |
| 490 | |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 491 | static const struct iwl_tt_params iwl_mvm_default_tt_params = { |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 492 | .ct_kill_entry = 118, |
| 493 | .ct_kill_exit = 96, |
| 494 | .ct_kill_duration = 5, |
| 495 | .dynamic_smps_entry = 114, |
| 496 | .dynamic_smps_exit = 110, |
| 497 | .tx_protection_entry = 114, |
| 498 | .tx_protection_exit = 108, |
| 499 | .tx_backoff = { |
| 500 | {.temperature = 112, .backoff = 200}, |
| 501 | {.temperature = 113, .backoff = 600}, |
| 502 | {.temperature = 114, .backoff = 1200}, |
| 503 | {.temperature = 115, .backoff = 2000}, |
| 504 | {.temperature = 116, .backoff = 4000}, |
| 505 | {.temperature = 117, .backoff = 10000}, |
| 506 | }, |
| 507 | .support_ct_kill = true, |
| 508 | .support_dynamic_smps = true, |
| 509 | .support_tx_protection = true, |
| 510 | .support_tx_backoff = true, |
| 511 | }; |
| 512 | |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 513 | /* budget in mWatt */ |
| 514 | static const u32 iwl_mvm_cdev_budgets[] = { |
| 515 | 2000, /* cooling state 0 */ |
| 516 | 1800, /* cooling state 1 */ |
| 517 | 1600, /* cooling state 2 */ |
| 518 | 1400, /* cooling state 3 */ |
| 519 | 1200, /* cooling state 4 */ |
| 520 | 1000, /* cooling state 5 */ |
| 521 | 900, /* cooling state 6 */ |
| 522 | 800, /* cooling state 7 */ |
| 523 | 700, /* cooling state 8 */ |
| 524 | 650, /* cooling state 9 */ |
| 525 | 600, /* cooling state 10 */ |
| 526 | 550, /* cooling state 11 */ |
| 527 | 500, /* cooling state 12 */ |
| 528 | 450, /* cooling state 13 */ |
| 529 | 400, /* cooling state 14 */ |
| 530 | 350, /* cooling state 15 */ |
| 531 | 300, /* cooling state 16 */ |
| 532 | 250, /* cooling state 17 */ |
| 533 | 200, /* cooling state 18 */ |
| 534 | 150, /* cooling state 19 */ |
| 535 | }; |
| 536 | |
| 537 | 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] | 538 | { |
| 539 | struct iwl_mvm_ctdp_cmd cmd = { |
| 540 | .operation = cpu_to_le32(op), |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 541 | .budget = cpu_to_le32(iwl_mvm_cdev_budgets[state]), |
Chaya Rachel Ivgi | 00f481b | 2016-02-24 12:19:22 +0200 | [diff] [blame] | 542 | .window_size = 0, |
| 543 | }; |
| 544 | int ret; |
| 545 | u32 status; |
| 546 | |
| 547 | lockdep_assert_held(&mvm->mutex); |
| 548 | |
| 549 | ret = iwl_mvm_send_cmd_pdu_status(mvm, WIDE_ID(PHY_OPS_GROUP, |
| 550 | CTDP_CONFIG_CMD), |
| 551 | sizeof(cmd), &cmd, &status); |
| 552 | |
| 553 | if (ret) { |
| 554 | IWL_ERR(mvm, "cTDP command failed (err=%d)\n", ret); |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | switch (op) { |
| 559 | case CTDP_CMD_OPERATION_START: |
| 560 | #ifdef CONFIG_THERMAL |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 561 | mvm->cooling_dev.cur_state = state; |
Chaya Rachel Ivgi | 00f481b | 2016-02-24 12:19:22 +0200 | [diff] [blame] | 562 | #endif /* CONFIG_THERMAL */ |
| 563 | break; |
| 564 | case CTDP_CMD_OPERATION_REPORT: |
| 565 | IWL_DEBUG_TEMP(mvm, "cTDP avg energy in mWatt = %d\n", status); |
| 566 | /* when the function is called with CTDP_CMD_OPERATION_REPORT |
| 567 | * option the function should return the average budget value |
| 568 | * that is received from the FW. |
| 569 | * The budget can't be less or equal to 0, so it's possible |
| 570 | * to distinguish between error values and budgets. |
| 571 | */ |
| 572 | return status; |
| 573 | case CTDP_CMD_OPERATION_STOP: |
| 574 | IWL_DEBUG_TEMP(mvm, "cTDP stopped successfully\n"); |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 581 | #ifdef CONFIG_THERMAL |
| 582 | static int compare_temps(const void *a, const void *b) |
| 583 | { |
| 584 | return ((s16)le16_to_cpu(*(__le16 *)a) - |
| 585 | (s16)le16_to_cpu(*(__le16 *)b)); |
| 586 | } |
| 587 | |
| 588 | int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm *mvm) |
| 589 | { |
| 590 | struct temp_report_ths_cmd cmd = {0}; |
| 591 | int ret, i, j, idx = 0; |
| 592 | |
| 593 | lockdep_assert_held(&mvm->mutex); |
| 594 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 595 | if (!mvm->tz_device.tzone) |
| 596 | return -EINVAL; |
| 597 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 598 | /* The driver holds array of temperature trips that are unsorted |
| 599 | * and uncompressed, the FW should get it compressed and sorted |
| 600 | */ |
| 601 | |
| 602 | /* compress temp_trips to cmd array, remove uninitialized values*/ |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 603 | for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) { |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 604 | if (mvm->tz_device.temp_trips[i] != S16_MIN) { |
| 605 | cmd.thresholds[idx++] = |
| 606 | cpu_to_le16(mvm->tz_device.temp_trips[i]); |
| 607 | } |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 608 | } |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 609 | cmd.num_temps = cpu_to_le32(idx); |
| 610 | |
| 611 | if (!idx) |
| 612 | goto send; |
| 613 | |
| 614 | /*sort cmd array*/ |
| 615 | sort(cmd.thresholds, idx, sizeof(s16), compare_temps, NULL); |
| 616 | |
| 617 | /* we should save the indexes of trips because we sort |
| 618 | * and compress the orginal array |
| 619 | */ |
| 620 | for (i = 0; i < idx; i++) { |
| 621 | for (j = 0; j < IWL_MAX_DTS_TRIPS; j++) { |
| 622 | if (le16_to_cpu(cmd.thresholds[i]) == |
| 623 | mvm->tz_device.temp_trips[j]) |
| 624 | mvm->tz_device.fw_trips_index[i] = j; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | send: |
| 629 | ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP, |
| 630 | TEMP_REPORTING_THRESHOLDS_CMD), |
| 631 | 0, sizeof(cmd), &cmd); |
| 632 | if (ret) |
| 633 | IWL_ERR(mvm, "TEMP_REPORT_THS_CMD command failed (err=%d)\n", |
| 634 | ret); |
| 635 | |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | static int iwl_mvm_tzone_get_temp(struct thermal_zone_device *device, |
| 640 | int *temperature) |
| 641 | { |
| 642 | struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata; |
| 643 | int ret; |
| 644 | int temp; |
| 645 | |
| 646 | mutex_lock(&mvm->mutex); |
| 647 | |
| 648 | if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) { |
| 649 | ret = -EIO; |
| 650 | goto out; |
| 651 | } |
| 652 | |
| 653 | ret = iwl_mvm_get_temp(mvm, &temp); |
| 654 | if (ret) |
| 655 | goto out; |
| 656 | |
| 657 | *temperature = temp * 1000; |
| 658 | |
| 659 | out: |
| 660 | mutex_unlock(&mvm->mutex); |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | static int iwl_mvm_tzone_get_trip_temp(struct thermal_zone_device *device, |
| 665 | int trip, int *temp) |
| 666 | { |
| 667 | struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata; |
| 668 | |
| 669 | if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) |
| 670 | return -EINVAL; |
| 671 | |
| 672 | *temp = mvm->tz_device.temp_trips[trip] * 1000; |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static int iwl_mvm_tzone_get_trip_type(struct thermal_zone_device *device, |
| 678 | int trip, enum thermal_trip_type *type) |
| 679 | { |
| 680 | if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) |
| 681 | return -EINVAL; |
| 682 | |
| 683 | *type = THERMAL_TRIP_PASSIVE; |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | static int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device *device, |
| 689 | int trip, int temp) |
| 690 | { |
| 691 | struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata; |
| 692 | struct iwl_mvm_thermal_device *tzone; |
| 693 | int i, ret; |
| 694 | s16 temperature; |
| 695 | |
| 696 | mutex_lock(&mvm->mutex); |
| 697 | |
| 698 | if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) { |
| 699 | ret = -EIO; |
| 700 | goto out; |
| 701 | } |
| 702 | |
| 703 | if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) { |
| 704 | ret = -EINVAL; |
| 705 | goto out; |
| 706 | } |
| 707 | |
| 708 | if ((temp / 1000) > S16_MAX) { |
| 709 | ret = -EINVAL; |
| 710 | goto out; |
| 711 | } |
| 712 | |
| 713 | temperature = (s16)(temp / 1000); |
| 714 | tzone = &mvm->tz_device; |
| 715 | |
| 716 | if (!tzone) { |
| 717 | ret = -EIO; |
| 718 | goto out; |
| 719 | } |
| 720 | |
| 721 | /* no updates*/ |
| 722 | if (tzone->temp_trips[trip] == temperature) { |
| 723 | ret = 0; |
| 724 | goto out; |
| 725 | } |
| 726 | |
| 727 | /* already existing temperature */ |
| 728 | for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) { |
| 729 | if (tzone->temp_trips[i] == temperature) { |
| 730 | ret = -EINVAL; |
| 731 | goto out; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | tzone->temp_trips[trip] = temperature; |
| 736 | |
| 737 | ret = iwl_mvm_send_temp_report_ths_cmd(mvm); |
| 738 | out: |
| 739 | mutex_unlock(&mvm->mutex); |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | static struct thermal_zone_device_ops tzone_ops = { |
| 744 | .get_temp = iwl_mvm_tzone_get_temp, |
| 745 | .get_trip_temp = iwl_mvm_tzone_get_trip_temp, |
| 746 | .get_trip_type = iwl_mvm_tzone_get_trip_type, |
| 747 | .set_trip_temp = iwl_mvm_tzone_set_trip_temp, |
| 748 | }; |
| 749 | |
| 750 | /* make all trips writable */ |
| 751 | #define IWL_WRITABLE_TRIPS_MSK (BIT(IWL_MAX_DTS_TRIPS) - 1) |
| 752 | |
| 753 | static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm) |
| 754 | { |
| 755 | int i; |
| 756 | char name[] = "iwlwifi"; |
| 757 | |
| 758 | if (!iwl_mvm_is_tt_in_fw(mvm)) { |
| 759 | mvm->tz_device.tzone = NULL; |
| 760 | |
| 761 | return; |
| 762 | } |
| 763 | |
| 764 | BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH); |
| 765 | |
| 766 | mvm->tz_device.tzone = thermal_zone_device_register(name, |
| 767 | IWL_MAX_DTS_TRIPS, |
| 768 | IWL_WRITABLE_TRIPS_MSK, |
| 769 | mvm, &tzone_ops, |
| 770 | NULL, 0, 0); |
| 771 | if (IS_ERR(mvm->tz_device.tzone)) { |
| 772 | IWL_DEBUG_TEMP(mvm, |
| 773 | "Failed to register to thermal zone (err = %ld)\n", |
| 774 | PTR_ERR(mvm->tz_device.tzone)); |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 775 | mvm->tz_device.tzone = NULL; |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | |
| 779 | /* 0 is a valid temperature, |
| 780 | * so initialize the array with S16_MIN which invalid temperature |
| 781 | */ |
| 782 | for (i = 0 ; i < IWL_MAX_DTS_TRIPS; i++) |
| 783 | mvm->tz_device.temp_trips[i] = S16_MIN; |
| 784 | } |
| 785 | |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 786 | static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device *cdev, |
| 787 | unsigned long *state) |
| 788 | { |
| 789 | *state = ARRAY_SIZE(iwl_mvm_cdev_budgets) - 1; |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device *cdev, |
| 795 | unsigned long *state) |
| 796 | { |
| 797 | struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata); |
| 798 | |
| 799 | if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) |
| 800 | return -EBUSY; |
| 801 | |
| 802 | *state = mvm->cooling_dev.cur_state; |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 803 | |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device *cdev, |
| 808 | unsigned long new_state) |
| 809 | { |
| 810 | struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata); |
| 811 | int ret; |
| 812 | |
| 813 | if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) |
| 814 | return -EIO; |
| 815 | |
| 816 | if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) |
| 817 | return -EBUSY; |
| 818 | |
| 819 | mutex_lock(&mvm->mutex); |
| 820 | |
| 821 | if (new_state >= ARRAY_SIZE(iwl_mvm_cdev_budgets)) { |
| 822 | ret = -EINVAL; |
| 823 | goto unlock; |
| 824 | } |
| 825 | |
| 826 | ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START, |
Chaya Rachel Ivgi | b358993 | 2016-02-14 14:03:10 +0200 | [diff] [blame] | 827 | new_state); |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 828 | |
| 829 | unlock: |
| 830 | mutex_unlock(&mvm->mutex); |
| 831 | return ret; |
| 832 | } |
| 833 | |
| 834 | static struct thermal_cooling_device_ops tcooling_ops = { |
| 835 | .get_max_state = iwl_mvm_tcool_get_max_state, |
| 836 | .get_cur_state = iwl_mvm_tcool_get_cur_state, |
| 837 | .set_cur_state = iwl_mvm_tcool_set_cur_state, |
| 838 | }; |
| 839 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 840 | static void iwl_mvm_cooling_device_register(struct iwl_mvm *mvm) |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 841 | { |
| 842 | char name[] = "iwlwifi"; |
| 843 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 844 | if (!iwl_mvm_is_ctdp_supported(mvm)) |
| 845 | return; |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 846 | |
| 847 | BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH); |
| 848 | |
| 849 | mvm->cooling_dev.cdev = |
| 850 | thermal_cooling_device_register(name, |
| 851 | mvm, |
| 852 | &tcooling_ops); |
| 853 | |
| 854 | if (IS_ERR(mvm->cooling_dev.cdev)) { |
| 855 | IWL_DEBUG_TEMP(mvm, |
| 856 | "Failed to register to cooling device (err = %ld)\n", |
| 857 | PTR_ERR(mvm->cooling_dev.cdev)); |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 858 | mvm->cooling_dev.cdev = NULL; |
| 859 | return; |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 860 | } |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 861 | } |
| 862 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 863 | static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm *mvm) |
| 864 | { |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 865 | 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] | 866 | return; |
| 867 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 868 | IWL_DEBUG_TEMP(mvm, "Thermal zone device unregister\n"); |
| 869 | thermal_zone_device_unregister(mvm->tz_device.tzone); |
| 870 | mvm->tz_device.tzone = NULL; |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 871 | } |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 872 | |
| 873 | static void iwl_mvm_cooling_device_unregister(struct iwl_mvm *mvm) |
| 874 | { |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 875 | if (!iwl_mvm_is_ctdp_supported(mvm) || !mvm->cooling_dev.cdev) |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 876 | return; |
| 877 | |
Emmanuel Grumbach | 91f66a3 | 2016-02-28 10:15:08 +0200 | [diff] [blame] | 878 | IWL_DEBUG_TEMP(mvm, "Cooling device unregister\n"); |
| 879 | thermal_cooling_device_unregister(mvm->cooling_dev.cdev); |
| 880 | mvm->cooling_dev.cdev = NULL; |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 881 | } |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 882 | #endif /* CONFIG_THERMAL */ |
| 883 | |
| 884 | void iwl_mvm_thermal_initialize(struct iwl_mvm *mvm, u32 min_backoff) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 885 | { |
| 886 | struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; |
| 887 | |
| 888 | IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n"); |
Eytan Lifshitz | 6be497f | 2013-07-24 14:49:18 +0300 | [diff] [blame] | 889 | |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 890 | if (mvm->cfg->thermal_params) |
| 891 | tt->params = *mvm->cfg->thermal_params; |
Eytan Lifshitz | 6be497f | 2013-07-24 14:49:18 +0300 | [diff] [blame] | 892 | else |
Chaya Rachel Ivgi | 3444682 | 2015-04-19 12:26:39 +0300 | [diff] [blame] | 893 | tt->params = iwl_mvm_default_tt_params; |
Eytan Lifshitz | 6be497f | 2013-07-24 14:49:18 +0300 | [diff] [blame] | 894 | |
eytan lifshitz | dafe6c4 | 2013-06-18 14:28:56 +0300 | [diff] [blame] | 895 | tt->throttle = false; |
Luciano Coelho | 19789ab | 2014-12-15 14:15:15 +0200 | [diff] [blame] | 896 | tt->dynamic_smps = false; |
Ido Yariv | 0c0e2c7 | 2014-01-16 21:12:02 -0500 | [diff] [blame] | 897 | tt->min_backoff = min_backoff; |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 898 | INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 899 | |
| 900 | #ifdef CONFIG_THERMAL |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 901 | iwl_mvm_cooling_device_register(mvm); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 902 | iwl_mvm_thermal_zone_register(mvm); |
| 903 | #endif |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 904 | } |
| 905 | |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 906 | void iwl_mvm_thermal_exit(struct iwl_mvm *mvm) |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 907 | { |
| 908 | cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit); |
| 909 | IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n"); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 910 | |
| 911 | #ifdef CONFIG_THERMAL |
Chaya Rachel Ivgi | 5c89e7b | 2016-01-05 10:34:47 +0200 | [diff] [blame] | 912 | iwl_mvm_cooling_device_unregister(mvm); |
Chaya Rachel Ivgi | c221daf | 2015-12-29 09:54:49 +0200 | [diff] [blame] | 913 | iwl_mvm_thermal_zone_unregister(mvm); |
| 914 | #endif |
Eytan Lifshitz | 9ee718a | 2013-05-19 19:14:41 +0300 | [diff] [blame] | 915 | } |