blob: acca44a450864cc446d3a0a653c20e85314465fd [file] [log] [blame]
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001/******************************************************************************
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 Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030010 *
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.
14 *
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.
19 *
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,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
27 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020035 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030036 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
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
47 * distribution.
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.
51 *
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.
63 *
64 *****************************************************************************/
65
66#include "mvm.h"
67#include "iwl-config.h"
68#include "iwl-io.h"
69#include "iwl-csr.h"
70#include "iwl-prph.h"
71
Luciano Coelhoa0a09242014-09-04 12:29:15 +030072#define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030073
74static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
75{
76 u32 duration = mvm->thermal_throttle.params->ct_kill_duration;
77
Luciano Coelhob689fa72014-08-20 17:26:58 +030078 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
79 return;
80
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030081 IWL_ERR(mvm, "Enter CT Kill\n");
82 iwl_mvm_set_hw_ctkill_state(mvm, true);
Luciano Coelhoefc36db2014-08-20 17:58:20 +030083
84 /* Don't schedule an exit work if we're in test mode, since
85 * the temperature will not change unless we manually set it
86 * again (or disable testing).
87 */
88 if (!mvm->temperature_test)
89 schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
90 round_jiffies_relative(duration * HZ));
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030091}
92
93static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
94{
Luciano Coelhob689fa72014-08-20 17:26:58 +030095 if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
96 return;
97
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030098 IWL_ERR(mvm, "Exit CT Kill\n");
99 iwl_mvm_set_hw_ctkill_state(mvm, false);
100}
101
Luciano Coelhoa0a09242014-09-04 12:29:15 +0300102static bool iwl_mvm_temp_notif(struct iwl_notif_wait_data *notif_wait,
103 struct iwl_rx_packet *pkt, void *data)
104{
105 struct iwl_mvm *mvm =
106 container_of(notif_wait, struct iwl_mvm, notif_wait);
107 int *temp = data;
108 struct iwl_dts_measurement_notif *notif;
109 int len = iwl_rx_packet_payload_len(pkt);
110
111 if (WARN_ON_ONCE(len != sizeof(*notif))) {
112 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
113 return true;
114 }
115
116 notif = (void *)pkt->data;
117
118 *temp = le32_to_cpu(notif->temp);
119
120 /* shouldn't be negative, but since it's s32, make sure it isn't */
121 if (WARN_ON_ONCE(*temp < 0))
122 *temp = 0;
123
124 IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", *temp);
125 return true;
126}
127
128static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
129{
130 struct iwl_dts_measurement_cmd cmd = {
131 .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
132 };
133
134 return iwl_mvm_send_cmd_pdu(mvm, CMD_DTS_MEASUREMENT_TRIGGER, 0,
135 sizeof(cmd), &cmd);
136}
137
Luciano Coelhoc549e392014-09-04 15:58:47 +0300138int iwl_mvm_get_temp(struct iwl_mvm *mvm)
Luciano Coelhoa0a09242014-09-04 12:29:15 +0300139{
140 struct iwl_notification_wait wait_temp_notif;
141 static const u8 temp_notif[] = { DTS_MEASUREMENT_NOTIFICATION };
142 int ret, temp;
143
144 lockdep_assert_held(&mvm->mutex);
145
146 iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
147 temp_notif, ARRAY_SIZE(temp_notif),
148 iwl_mvm_temp_notif, &temp);
149
150 ret = iwl_mvm_get_temp_cmd(mvm);
151 if (ret) {
152 IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
153 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
154 return ret;
155 }
156
157 ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
158 IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
159 if (ret) {
160 IWL_ERR(mvm, "Getting the temperature timed out\n");
161 return ret;
162 }
163
164 return temp;
165}
166
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300167static void check_exit_ctkill(struct work_struct *work)
168{
169 struct iwl_mvm_tt_mgmt *tt;
170 struct iwl_mvm *mvm;
171 u32 duration;
172 s32 temp;
173
174 tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
175 mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
176
177 duration = tt->params->ct_kill_duration;
178
Luciano Coelhoa0a09242014-09-04 12:29:15 +0300179 mutex_lock(&mvm->mutex);
180
181 if (__iwl_mvm_mac_start(mvm))
Eliad Peller576eeee2014-07-01 18:38:38 +0300182 goto reschedule;
183
Luciano Coelhoa0a09242014-09-04 12:29:15 +0300184 /* make sure the device is available for direct read/writes */
185 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
186 __iwl_mvm_mac_stop(mvm);
187 goto reschedule;
188 }
189
190 temp = iwl_mvm_get_temp(mvm);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300191
Eliad Peller576eeee2014-07-01 18:38:38 +0300192 iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
193
Luciano Coelhoa0a09242014-09-04 12:29:15 +0300194 __iwl_mvm_mac_stop(mvm);
195
196 if (temp < 0)
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300197 goto reschedule;
Luciano Coelhoa0a09242014-09-04 12:29:15 +0300198
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300199 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
200
201 if (temp <= tt->params->ct_kill_exit) {
Luciano Coelhoa0a09242014-09-04 12:29:15 +0300202 mutex_unlock(&mvm->mutex);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300203 iwl_mvm_exit_ctkill(mvm);
204 return;
205 }
206
207reschedule:
Luciano Coelhoa0a09242014-09-04 12:29:15 +0300208 mutex_unlock(&mvm->mutex);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300209 schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
210 round_jiffies(duration * HZ));
211}
212
213static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
214 struct ieee80211_vif *vif)
215{
216 struct iwl_mvm *mvm = _data;
217 enum ieee80211_smps_mode smps_mode;
218
219 lockdep_assert_held(&mvm->mutex);
220
221 if (mvm->thermal_throttle.dynamic_smps)
222 smps_mode = IEEE80211_SMPS_DYNAMIC;
223 else
224 smps_mode = IEEE80211_SMPS_AUTOMATIC;
225
Eytan Lifshitzfded3132013-06-04 12:28:51 +0300226 if (vif->type != NL80211_IFTYPE_STATION)
227 return;
228
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300229 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
230}
231
232static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
233{
234 struct ieee80211_sta *sta;
235 struct iwl_mvm_sta *mvmsta;
236 int i, err;
237
238 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
239 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
240 lockdep_is_held(&mvm->mutex));
241 if (IS_ERR_OR_NULL(sta))
242 continue;
Johannes Berg5b577a92013-11-14 18:20:04 +0100243 mvmsta = iwl_mvm_sta_from_mac80211(sta);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300244 if (enable == mvmsta->tt_tx_protection)
245 continue;
Johannes Berge126b5d2013-06-28 13:39:18 +0200246 err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300247 if (err) {
248 IWL_ERR(mvm, "Failed to %s Tx protection\n",
249 enable ? "enable" : "disable");
250 } else {
251 IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
252 enable ? "Enable" : "Disable");
253 mvmsta->tt_tx_protection = enable;
254 }
255 }
256}
257
Ido Yariv0c0e2c72014-01-16 21:12:02 -0500258void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300259{
260 struct iwl_host_cmd cmd = {
261 .id = REPLY_THERMAL_MNG_BACKOFF,
262 .len = { sizeof(u32), },
263 .data = { &backoff, },
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300264 };
265
Ido Yariv0c0e2c72014-01-16 21:12:02 -0500266 backoff = max(backoff, mvm->thermal_throttle.min_backoff);
267
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300268 if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
269 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
270 backoff);
271 mvm->thermal_throttle.tx_backoff = backoff;
272 } else {
273 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
274 }
275}
276
277void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
278{
279 const struct iwl_tt_params *params = mvm->thermal_throttle.params;
280 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
281 s32 temperature = mvm->temperature;
eytan lifshitzdafe6c42013-06-18 14:28:56 +0300282 bool throttle_enable = false;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300283 int i;
284 u32 tx_backoff;
285
286 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
287
288 if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
289 iwl_mvm_enter_ctkill(mvm);
290 return;
291 }
292
Luciano Coelhob689fa72014-08-20 17:26:58 +0300293 if (params->support_ct_kill &&
294 temperature <= tt->params->ct_kill_exit) {
295 iwl_mvm_exit_ctkill(mvm);
296 return;
297 }
298
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300299 if (params->support_dynamic_smps) {
300 if (!tt->dynamic_smps &&
301 temperature >= params->dynamic_smps_entry) {
302 IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
303 tt->dynamic_smps = true;
304 ieee80211_iterate_active_interfaces_atomic(
305 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
306 iwl_mvm_tt_smps_iterator, mvm);
eytan lifshitzdafe6c42013-06-18 14:28:56 +0300307 throttle_enable = true;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300308 } else if (tt->dynamic_smps &&
309 temperature <= params->dynamic_smps_exit) {
310 IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
311 tt->dynamic_smps = false;
312 ieee80211_iterate_active_interfaces_atomic(
313 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
314 iwl_mvm_tt_smps_iterator, mvm);
315 }
316 }
317
318 if (params->support_tx_protection) {
eytan lifshitzdafe6c42013-06-18 14:28:56 +0300319 if (temperature >= params->tx_protection_entry) {
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300320 iwl_mvm_tt_tx_protection(mvm, true);
eytan lifshitzdafe6c42013-06-18 14:28:56 +0300321 throttle_enable = true;
322 } else if (temperature <= params->tx_protection_exit) {
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300323 iwl_mvm_tt_tx_protection(mvm, false);
eytan lifshitzdafe6c42013-06-18 14:28:56 +0300324 }
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300325 }
326
327 if (params->support_tx_backoff) {
Eytan Lifshitz1b8ebbd2014-04-01 16:44:21 +0300328 tx_backoff = tt->min_backoff;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300329 for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
330 if (temperature < params->tx_backoff[i].temperature)
331 break;
Eytan Lifshitz1b8ebbd2014-04-01 16:44:21 +0300332 tx_backoff = max(tt->min_backoff,
333 params->tx_backoff[i].backoff);
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300334 }
Eytan Lifshitz1b8ebbd2014-04-01 16:44:21 +0300335 if (tx_backoff != tt->min_backoff)
eytan lifshitzdafe6c42013-06-18 14:28:56 +0300336 throttle_enable = true;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300337 if (tt->tx_backoff != tx_backoff)
338 iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
339 }
eytan lifshitzdafe6c42013-06-18 14:28:56 +0300340
341 if (!tt->throttle && throttle_enable) {
342 IWL_WARN(mvm,
343 "Due to high temperature thermal throttling initiated\n");
344 tt->throttle = true;
Eytan Lifshitz19a04bd2014-04-02 21:37:57 +0300345 } else if (tt->throttle && !tt->dynamic_smps &&
346 tt->tx_backoff == tt->min_backoff &&
eytan lifshitzdafe6c42013-06-18 14:28:56 +0300347 temperature <= params->tx_protection_exit) {
348 IWL_WARN(mvm,
349 "Temperature is back to normal thermal throttling stopped\n");
350 tt->throttle = false;
351 }
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300352}
353
354static const struct iwl_tt_params iwl7000_tt_params = {
355 .ct_kill_entry = 118,
356 .ct_kill_exit = 96,
357 .ct_kill_duration = 5,
358 .dynamic_smps_entry = 114,
359 .dynamic_smps_exit = 110,
360 .tx_protection_entry = 114,
361 .tx_protection_exit = 108,
362 .tx_backoff = {
363 {.temperature = 112, .backoff = 200},
364 {.temperature = 113, .backoff = 600},
365 {.temperature = 114, .backoff = 1200},
366 {.temperature = 115, .backoff = 2000},
367 {.temperature = 116, .backoff = 4000},
368 {.temperature = 117, .backoff = 10000},
369 },
370 .support_ct_kill = true,
371 .support_dynamic_smps = true,
372 .support_tx_protection = true,
373 .support_tx_backoff = true,
374};
375
Eytan Lifshitz6be497f2013-07-24 14:49:18 +0300376static const struct iwl_tt_params iwl7000_high_temp_tt_params = {
377 .ct_kill_entry = 118,
378 .ct_kill_exit = 96,
379 .ct_kill_duration = 5,
380 .dynamic_smps_entry = 114,
381 .dynamic_smps_exit = 110,
382 .tx_protection_entry = 114,
383 .tx_protection_exit = 108,
384 .tx_backoff = {
385 {.temperature = 112, .backoff = 300},
386 {.temperature = 113, .backoff = 800},
387 {.temperature = 114, .backoff = 1500},
388 {.temperature = 115, .backoff = 3000},
389 {.temperature = 116, .backoff = 5000},
390 {.temperature = 117, .backoff = 10000},
391 },
392 .support_ct_kill = true,
393 .support_dynamic_smps = true,
394 .support_tx_protection = true,
395 .support_tx_backoff = true,
396};
397
Ido Yariv0c0e2c72014-01-16 21:12:02 -0500398void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff)
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300399{
400 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
401
402 IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
Eytan Lifshitz6be497f2013-07-24 14:49:18 +0300403
404 if (mvm->cfg->high_temp)
405 tt->params = &iwl7000_high_temp_tt_params;
406 else
407 tt->params = &iwl7000_tt_params;
408
eytan lifshitzdafe6c42013-06-18 14:28:56 +0300409 tt->throttle = false;
Ido Yariv0c0e2c72014-01-16 21:12:02 -0500410 tt->min_backoff = min_backoff;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300411 INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
412}
413
414void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
415{
416 cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
417 IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
418}