1 /******************************************************************************
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.
8 * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 *****************************************************************************/
68 #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
70 static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
72 u32 duration = mvm->thermal_throttle.params->ct_kill_duration;
74 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
77 IWL_ERR(mvm, "Enter CT Kill\n");
78 iwl_mvm_set_hw_ctkill_state(mvm, true);
80 /* Don't schedule an exit work if we're in test mode, since
81 * the temperature will not change unless we manually set it
82 * again (or disable testing).
84 if (!mvm->temperature_test)
85 schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
86 round_jiffies_relative(duration * HZ));
89 static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
91 if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
94 IWL_ERR(mvm, "Exit CT Kill\n");
95 iwl_mvm_set_hw_ctkill_state(mvm, false);
98 static bool iwl_mvm_temp_notif(struct iwl_notif_wait_data *notif_wait,
99 struct iwl_rx_packet *pkt, void *data)
101 struct iwl_mvm *mvm =
102 container_of(notif_wait, struct iwl_mvm, notif_wait);
104 struct iwl_dts_measurement_notif *notif;
105 int len = iwl_rx_packet_payload_len(pkt);
107 if (WARN_ON_ONCE(len != sizeof(*notif))) {
108 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
112 notif = (void *)pkt->data;
114 *temp = le32_to_cpu(notif->temp);
116 /* shouldn't be negative, but since it's s32, make sure it isn't */
117 if (WARN_ON_ONCE(*temp < 0))
120 IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", *temp);
124 static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
126 struct iwl_dts_measurement_cmd cmd = {
127 .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
130 return iwl_mvm_send_cmd_pdu(mvm, CMD_DTS_MEASUREMENT_TRIGGER, 0,
134 int iwl_mvm_get_temp(struct iwl_mvm *mvm)
136 struct iwl_notification_wait wait_temp_notif;
137 static const u8 temp_notif[] = { DTS_MEASUREMENT_NOTIFICATION };
140 lockdep_assert_held(&mvm->mutex);
142 iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
143 temp_notif, ARRAY_SIZE(temp_notif),
144 iwl_mvm_temp_notif, &temp);
146 ret = iwl_mvm_get_temp_cmd(mvm);
148 IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
149 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
153 ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
154 IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
156 IWL_ERR(mvm, "Getting the temperature timed out\n");
163 static void check_exit_ctkill(struct work_struct *work)
165 struct iwl_mvm_tt_mgmt *tt;
170 tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
171 mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
173 duration = tt->params->ct_kill_duration;
175 mutex_lock(&mvm->mutex);
177 if (__iwl_mvm_mac_start(mvm))
180 /* make sure the device is available for direct read/writes */
181 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
182 __iwl_mvm_mac_stop(mvm);
186 temp = iwl_mvm_get_temp(mvm);
188 iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
190 __iwl_mvm_mac_stop(mvm);
195 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
197 if (temp <= tt->params->ct_kill_exit) {
198 mutex_unlock(&mvm->mutex);
199 iwl_mvm_exit_ctkill(mvm);
204 mutex_unlock(&mvm->mutex);
205 schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
206 round_jiffies(duration * HZ));
209 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
210 struct ieee80211_vif *vif)
212 struct iwl_mvm *mvm = _data;
213 enum ieee80211_smps_mode smps_mode;
215 lockdep_assert_held(&mvm->mutex);
217 if (mvm->thermal_throttle.dynamic_smps)
218 smps_mode = IEEE80211_SMPS_DYNAMIC;
220 smps_mode = IEEE80211_SMPS_AUTOMATIC;
222 if (vif->type != NL80211_IFTYPE_STATION)
225 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
228 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
230 struct ieee80211_sta *sta;
231 struct iwl_mvm_sta *mvmsta;
234 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
235 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
236 lockdep_is_held(&mvm->mutex));
237 if (IS_ERR_OR_NULL(sta))
239 mvmsta = iwl_mvm_sta_from_mac80211(sta);
240 if (enable == mvmsta->tt_tx_protection)
242 err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
244 IWL_ERR(mvm, "Failed to %s Tx protection\n",
245 enable ? "enable" : "disable");
247 IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
248 enable ? "Enable" : "Disable");
249 mvmsta->tt_tx_protection = enable;
254 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
256 struct iwl_host_cmd cmd = {
257 .id = REPLY_THERMAL_MNG_BACKOFF,
258 .len = { sizeof(u32), },
259 .data = { &backoff, },
262 backoff = max(backoff, mvm->thermal_throttle.min_backoff);
264 if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
265 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
267 mvm->thermal_throttle.tx_backoff = backoff;
269 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
273 void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
275 const struct iwl_tt_params *params = mvm->thermal_throttle.params;
276 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
277 s32 temperature = mvm->temperature;
278 bool throttle_enable = false;
282 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
284 if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
285 iwl_mvm_enter_ctkill(mvm);
289 if (params->support_ct_kill &&
290 temperature <= tt->params->ct_kill_exit) {
291 iwl_mvm_exit_ctkill(mvm);
295 if (params->support_dynamic_smps) {
296 if (!tt->dynamic_smps &&
297 temperature >= params->dynamic_smps_entry) {
298 IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
299 tt->dynamic_smps = true;
300 ieee80211_iterate_active_interfaces_atomic(
301 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
302 iwl_mvm_tt_smps_iterator, mvm);
303 throttle_enable = true;
304 } else if (tt->dynamic_smps &&
305 temperature <= params->dynamic_smps_exit) {
306 IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
307 tt->dynamic_smps = false;
308 ieee80211_iterate_active_interfaces_atomic(
309 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
310 iwl_mvm_tt_smps_iterator, mvm);
314 if (params->support_tx_protection) {
315 if (temperature >= params->tx_protection_entry) {
316 iwl_mvm_tt_tx_protection(mvm, true);
317 throttle_enable = true;
318 } else if (temperature <= params->tx_protection_exit) {
319 iwl_mvm_tt_tx_protection(mvm, false);
323 if (params->support_tx_backoff) {
324 tx_backoff = tt->min_backoff;
325 for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
326 if (temperature < params->tx_backoff[i].temperature)
328 tx_backoff = max(tt->min_backoff,
329 params->tx_backoff[i].backoff);
331 if (tx_backoff != tt->min_backoff)
332 throttle_enable = true;
333 if (tt->tx_backoff != tx_backoff)
334 iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
337 if (!tt->throttle && throttle_enable) {
339 "Due to high temperature thermal throttling initiated\n");
341 } else if (tt->throttle && !tt->dynamic_smps &&
342 tt->tx_backoff == tt->min_backoff &&
343 temperature <= params->tx_protection_exit) {
345 "Temperature is back to normal thermal throttling stopped\n");
346 tt->throttle = false;
350 static const struct iwl_tt_params iwl7000_tt_params = {
351 .ct_kill_entry = 118,
353 .ct_kill_duration = 5,
354 .dynamic_smps_entry = 114,
355 .dynamic_smps_exit = 110,
356 .tx_protection_entry = 114,
357 .tx_protection_exit = 108,
359 {.temperature = 112, .backoff = 200},
360 {.temperature = 113, .backoff = 600},
361 {.temperature = 114, .backoff = 1200},
362 {.temperature = 115, .backoff = 2000},
363 {.temperature = 116, .backoff = 4000},
364 {.temperature = 117, .backoff = 10000},
366 .support_ct_kill = true,
367 .support_dynamic_smps = true,
368 .support_tx_protection = true,
369 .support_tx_backoff = true,
372 static const struct iwl_tt_params iwl7000_high_temp_tt_params = {
373 .ct_kill_entry = 118,
375 .ct_kill_duration = 5,
376 .dynamic_smps_entry = 114,
377 .dynamic_smps_exit = 110,
378 .tx_protection_entry = 114,
379 .tx_protection_exit = 108,
381 {.temperature = 112, .backoff = 300},
382 {.temperature = 113, .backoff = 800},
383 {.temperature = 114, .backoff = 1500},
384 {.temperature = 115, .backoff = 3000},
385 {.temperature = 116, .backoff = 5000},
386 {.temperature = 117, .backoff = 10000},
388 .support_ct_kill = true,
389 .support_dynamic_smps = true,
390 .support_tx_protection = true,
391 .support_tx_backoff = true,
394 void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff)
396 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
398 IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
400 if (mvm->cfg->high_temp)
401 tt->params = &iwl7000_high_temp_tt_params;
403 tt->params = &iwl7000_tt_params;
405 tt->throttle = false;
406 tt->min_backoff = min_backoff;
407 INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
410 void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
412 cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
413 IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");