Commit | Line | Data |
---|---|---|
9ee718aa EL |
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 | * | |
51368bf7 | 8 | * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved. |
8b4139dc | 9 | * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH |
9ee718aa EL |
10 | * |
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 | * | |
51368bf7 | 34 | * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. |
8b4139dc | 35 | * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH |
9ee718aa EL |
36 | * 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" | |
9ee718aa | 67 | |
a0a09243 | 68 | #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ |
9ee718aa | 69 | |
a0a09243 | 70 | static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm) |
9ee718aa | 71 | { |
a0a09243 | 72 | u32 duration = mvm->thermal_throttle.params->ct_kill_duration; |
9ee718aa | 73 | |
a0a09243 LC |
74 | if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) |
75 | return; | |
9ee718aa | 76 | |
a0a09243 LC |
77 | IWL_ERR(mvm, "Enter CT Kill\n"); |
78 | iwl_mvm_set_hw_ctkill_state(mvm, true); | |
9ee718aa | 79 | |
a0a09243 LC |
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). | |
83 | */ | |
84 | if (!mvm->temperature_test) | |
85 | schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit, | |
86 | round_jiffies_relative(duration * HZ)); | |
9ee718aa EL |
87 | } |
88 | ||
a0a09243 | 89 | static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm) |
9ee718aa | 90 | { |
a0a09243 LC |
91 | if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) |
92 | return; | |
9ee718aa | 93 | |
a0a09243 LC |
94 | IWL_ERR(mvm, "Exit CT Kill\n"); |
95 | iwl_mvm_set_hw_ctkill_state(mvm, false); | |
9ee718aa EL |
96 | } |
97 | ||
fd1f7550 LC |
98 | void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp) |
99 | { | |
100 | /* ignore the notification if we are in test mode */ | |
101 | if (mvm->temperature_test) | |
102 | return; | |
103 | ||
104 | if (mvm->temperature == temp) | |
105 | return; | |
106 | ||
107 | mvm->temperature = temp; | |
108 | iwl_mvm_tt_handler(mvm); | |
109 | } | |
110 | ||
111 | static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm, | |
112 | struct iwl_rx_packet *pkt) | |
9ee718aa | 113 | { |
a0a09243 LC |
114 | struct iwl_dts_measurement_notif *notif; |
115 | int len = iwl_rx_packet_payload_len(pkt); | |
fd1f7550 | 116 | int temp; |
a0a09243 LC |
117 | |
118 | if (WARN_ON_ONCE(len != sizeof(*notif))) { | |
119 | IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n"); | |
fd1f7550 | 120 | return -EINVAL; |
a0a09243 | 121 | } |
9ee718aa | 122 | |
a0a09243 | 123 | notif = (void *)pkt->data; |
9ee718aa | 124 | |
fd1f7550 | 125 | temp = le32_to_cpu(notif->temp); |
9ee718aa | 126 | |
a0a09243 | 127 | /* shouldn't be negative, but since it's s32, make sure it isn't */ |
fd1f7550 LC |
128 | if (WARN_ON_ONCE(temp < 0)) |
129 | temp = 0; | |
130 | ||
131 | IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp); | |
132 | ||
133 | return temp; | |
134 | } | |
135 | ||
136 | static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait, | |
137 | struct iwl_rx_packet *pkt, void *data) | |
138 | { | |
139 | struct iwl_mvm *mvm = | |
140 | container_of(notif_wait, struct iwl_mvm, notif_wait); | |
141 | int *temp = data; | |
142 | int ret; | |
143 | ||
144 | ret = iwl_mvm_temp_notif_parse(mvm, pkt); | |
145 | if (ret < 0) | |
146 | return true; | |
147 | ||
148 | *temp = ret; | |
9ee718aa | 149 | |
a0a09243 | 150 | return true; |
9ee718aa EL |
151 | } |
152 | ||
a0a09243 | 153 | static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm) |
9ee718aa | 154 | { |
a0a09243 LC |
155 | struct iwl_dts_measurement_cmd cmd = { |
156 | .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP), | |
9ee718aa EL |
157 | }; |
158 | ||
a0a09243 LC |
159 | return iwl_mvm_send_cmd_pdu(mvm, CMD_DTS_MEASUREMENT_TRIGGER, 0, |
160 | sizeof(cmd), &cmd); | |
9ee718aa EL |
161 | } |
162 | ||
c549e390 | 163 | int iwl_mvm_get_temp(struct iwl_mvm *mvm) |
9ee718aa | 164 | { |
a0a09243 LC |
165 | struct iwl_notification_wait wait_temp_notif; |
166 | static const u8 temp_notif[] = { DTS_MEASUREMENT_NOTIFICATION }; | |
167 | int ret, temp; | |
9ee718aa | 168 | |
a0a09243 | 169 | lockdep_assert_held(&mvm->mutex); |
b689fa79 | 170 | |
a0a09243 LC |
171 | iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif, |
172 | temp_notif, ARRAY_SIZE(temp_notif), | |
fd1f7550 | 173 | iwl_mvm_temp_notif_wait, &temp); |
efc36dbd | 174 | |
a0a09243 LC |
175 | ret = iwl_mvm_get_temp_cmd(mvm); |
176 | if (ret) { | |
177 | IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret); | |
178 | iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif); | |
179 | return ret; | |
180 | } | |
9ee718aa | 181 | |
a0a09243 LC |
182 | ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif, |
183 | IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT); | |
184 | if (ret) { | |
185 | IWL_ERR(mvm, "Getting the temperature timed out\n"); | |
186 | return ret; | |
187 | } | |
b689fa79 | 188 | |
a0a09243 | 189 | return temp; |
9ee718aa EL |
190 | } |
191 | ||
192 | static void check_exit_ctkill(struct work_struct *work) | |
193 | { | |
194 | struct iwl_mvm_tt_mgmt *tt; | |
195 | struct iwl_mvm *mvm; | |
196 | u32 duration; | |
197 | s32 temp; | |
198 | ||
199 | tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work); | |
200 | mvm = container_of(tt, struct iwl_mvm, thermal_throttle); | |
201 | ||
202 | duration = tt->params->ct_kill_duration; | |
203 | ||
a0a09243 LC |
204 | mutex_lock(&mvm->mutex); |
205 | ||
206 | if (__iwl_mvm_mac_start(mvm)) | |
207 | goto reschedule; | |
208 | ||
576eeee9 | 209 | /* make sure the device is available for direct read/writes */ |
a0a09243 LC |
210 | if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) { |
211 | __iwl_mvm_mac_stop(mvm); | |
576eeee9 | 212 | goto reschedule; |
a0a09243 | 213 | } |
576eeee9 | 214 | |
a0a09243 | 215 | temp = iwl_mvm_get_temp(mvm); |
9ee718aa | 216 | |
576eeee9 EP |
217 | iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL); |
218 | ||
a0a09243 LC |
219 | __iwl_mvm_mac_stop(mvm); |
220 | ||
221 | if (temp < 0) | |
9ee718aa | 222 | goto reschedule; |
a0a09243 | 223 | |
9ee718aa EL |
224 | IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp); |
225 | ||
226 | if (temp <= tt->params->ct_kill_exit) { | |
a0a09243 | 227 | mutex_unlock(&mvm->mutex); |
9ee718aa EL |
228 | iwl_mvm_exit_ctkill(mvm); |
229 | return; | |
230 | } | |
231 | ||
232 | reschedule: | |
a0a09243 | 233 | mutex_unlock(&mvm->mutex); |
9ee718aa EL |
234 | schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit, |
235 | round_jiffies(duration * HZ)); | |
236 | } | |
237 | ||
238 | static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac, | |
239 | struct ieee80211_vif *vif) | |
240 | { | |
241 | struct iwl_mvm *mvm = _data; | |
242 | enum ieee80211_smps_mode smps_mode; | |
243 | ||
244 | lockdep_assert_held(&mvm->mutex); | |
245 | ||
246 | if (mvm->thermal_throttle.dynamic_smps) | |
247 | smps_mode = IEEE80211_SMPS_DYNAMIC; | |
248 | else | |
249 | smps_mode = IEEE80211_SMPS_AUTOMATIC; | |
250 | ||
fded313e EL |
251 | if (vif->type != NL80211_IFTYPE_STATION) |
252 | return; | |
253 | ||
9ee718aa EL |
254 | iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode); |
255 | } | |
256 | ||
257 | static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable) | |
258 | { | |
259 | struct ieee80211_sta *sta; | |
260 | struct iwl_mvm_sta *mvmsta; | |
261 | int i, err; | |
262 | ||
263 | for (i = 0; i < IWL_MVM_STATION_COUNT; i++) { | |
264 | sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i], | |
265 | lockdep_is_held(&mvm->mutex)); | |
266 | if (IS_ERR_OR_NULL(sta)) | |
267 | continue; | |
5b577a90 | 268 | mvmsta = iwl_mvm_sta_from_mac80211(sta); |
9ee718aa EL |
269 | if (enable == mvmsta->tt_tx_protection) |
270 | continue; | |
e126b5d9 | 271 | err = iwl_mvm_tx_protection(mvm, mvmsta, enable); |
9ee718aa EL |
272 | if (err) { |
273 | IWL_ERR(mvm, "Failed to %s Tx protection\n", | |
274 | enable ? "enable" : "disable"); | |
275 | } else { | |
276 | IWL_DEBUG_TEMP(mvm, "%s Tx protection\n", | |
277 | enable ? "Enable" : "Disable"); | |
278 | mvmsta->tt_tx_protection = enable; | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
0c0e2c71 | 283 | void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff) |
9ee718aa EL |
284 | { |
285 | struct iwl_host_cmd cmd = { | |
286 | .id = REPLY_THERMAL_MNG_BACKOFF, | |
287 | .len = { sizeof(u32), }, | |
288 | .data = { &backoff, }, | |
9ee718aa EL |
289 | }; |
290 | ||
0c0e2c71 IY |
291 | backoff = max(backoff, mvm->thermal_throttle.min_backoff); |
292 | ||
9ee718aa EL |
293 | if (iwl_mvm_send_cmd(mvm, &cmd) == 0) { |
294 | IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n", | |
295 | backoff); | |
296 | mvm->thermal_throttle.tx_backoff = backoff; | |
297 | } else { | |
298 | IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n"); | |
299 | } | |
300 | } | |
301 | ||
302 | void iwl_mvm_tt_handler(struct iwl_mvm *mvm) | |
303 | { | |
304 | const struct iwl_tt_params *params = mvm->thermal_throttle.params; | |
305 | struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; | |
306 | s32 temperature = mvm->temperature; | |
dafe6c43 | 307 | bool throttle_enable = false; |
9ee718aa EL |
308 | int i; |
309 | u32 tx_backoff; | |
310 | ||
311 | IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature); | |
312 | ||
313 | if (params->support_ct_kill && temperature >= params->ct_kill_entry) { | |
314 | iwl_mvm_enter_ctkill(mvm); | |
315 | return; | |
316 | } | |
317 | ||
b689fa79 LC |
318 | if (params->support_ct_kill && |
319 | temperature <= tt->params->ct_kill_exit) { | |
320 | iwl_mvm_exit_ctkill(mvm); | |
321 | return; | |
322 | } | |
323 | ||
9ee718aa EL |
324 | if (params->support_dynamic_smps) { |
325 | if (!tt->dynamic_smps && | |
326 | temperature >= params->dynamic_smps_entry) { | |
327 | IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n"); | |
328 | tt->dynamic_smps = true; | |
329 | ieee80211_iterate_active_interfaces_atomic( | |
330 | mvm->hw, IEEE80211_IFACE_ITER_NORMAL, | |
331 | iwl_mvm_tt_smps_iterator, mvm); | |
dafe6c43 | 332 | throttle_enable = true; |
9ee718aa EL |
333 | } else if (tt->dynamic_smps && |
334 | temperature <= params->dynamic_smps_exit) { | |
335 | IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n"); | |
336 | tt->dynamic_smps = false; | |
337 | ieee80211_iterate_active_interfaces_atomic( | |
338 | mvm->hw, IEEE80211_IFACE_ITER_NORMAL, | |
339 | iwl_mvm_tt_smps_iterator, mvm); | |
340 | } | |
341 | } | |
342 | ||
343 | if (params->support_tx_protection) { | |
dafe6c43 | 344 | if (temperature >= params->tx_protection_entry) { |
9ee718aa | 345 | iwl_mvm_tt_tx_protection(mvm, true); |
dafe6c43 | 346 | throttle_enable = true; |
347 | } else if (temperature <= params->tx_protection_exit) { | |
9ee718aa | 348 | iwl_mvm_tt_tx_protection(mvm, false); |
dafe6c43 | 349 | } |
9ee718aa EL |
350 | } |
351 | ||
352 | if (params->support_tx_backoff) { | |
1b8ebbd3 | 353 | tx_backoff = tt->min_backoff; |
9ee718aa EL |
354 | for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) { |
355 | if (temperature < params->tx_backoff[i].temperature) | |
356 | break; | |
1b8ebbd3 EL |
357 | tx_backoff = max(tt->min_backoff, |
358 | params->tx_backoff[i].backoff); | |
9ee718aa | 359 | } |
1b8ebbd3 | 360 | if (tx_backoff != tt->min_backoff) |
dafe6c43 | 361 | throttle_enable = true; |
9ee718aa EL |
362 | if (tt->tx_backoff != tx_backoff) |
363 | iwl_mvm_tt_tx_backoff(mvm, tx_backoff); | |
364 | } | |
dafe6c43 | 365 | |
366 | if (!tt->throttle && throttle_enable) { | |
367 | IWL_WARN(mvm, | |
368 | "Due to high temperature thermal throttling initiated\n"); | |
369 | tt->throttle = true; | |
19a04bdd EL |
370 | } else if (tt->throttle && !tt->dynamic_smps && |
371 | tt->tx_backoff == tt->min_backoff && | |
dafe6c43 | 372 | temperature <= params->tx_protection_exit) { |
373 | IWL_WARN(mvm, | |
374 | "Temperature is back to normal thermal throttling stopped\n"); | |
375 | tt->throttle = false; | |
376 | } | |
9ee718aa EL |
377 | } |
378 | ||
379 | static const struct iwl_tt_params iwl7000_tt_params = { | |
380 | .ct_kill_entry = 118, | |
381 | .ct_kill_exit = 96, | |
382 | .ct_kill_duration = 5, | |
383 | .dynamic_smps_entry = 114, | |
384 | .dynamic_smps_exit = 110, | |
385 | .tx_protection_entry = 114, | |
386 | .tx_protection_exit = 108, | |
387 | .tx_backoff = { | |
388 | {.temperature = 112, .backoff = 200}, | |
389 | {.temperature = 113, .backoff = 600}, | |
390 | {.temperature = 114, .backoff = 1200}, | |
391 | {.temperature = 115, .backoff = 2000}, | |
392 | {.temperature = 116, .backoff = 4000}, | |
393 | {.temperature = 117, .backoff = 10000}, | |
394 | }, | |
395 | .support_ct_kill = true, | |
396 | .support_dynamic_smps = true, | |
397 | .support_tx_protection = true, | |
398 | .support_tx_backoff = true, | |
399 | }; | |
400 | ||
6be497f2 EL |
401 | static const struct iwl_tt_params iwl7000_high_temp_tt_params = { |
402 | .ct_kill_entry = 118, | |
403 | .ct_kill_exit = 96, | |
404 | .ct_kill_duration = 5, | |
405 | .dynamic_smps_entry = 114, | |
406 | .dynamic_smps_exit = 110, | |
407 | .tx_protection_entry = 114, | |
408 | .tx_protection_exit = 108, | |
409 | .tx_backoff = { | |
410 | {.temperature = 112, .backoff = 300}, | |
411 | {.temperature = 113, .backoff = 800}, | |
412 | {.temperature = 114, .backoff = 1500}, | |
413 | {.temperature = 115, .backoff = 3000}, | |
414 | {.temperature = 116, .backoff = 5000}, | |
415 | {.temperature = 117, .backoff = 10000}, | |
416 | }, | |
417 | .support_ct_kill = true, | |
418 | .support_dynamic_smps = true, | |
419 | .support_tx_protection = true, | |
420 | .support_tx_backoff = true, | |
421 | }; | |
422 | ||
0c0e2c71 | 423 | void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff) |
9ee718aa EL |
424 | { |
425 | struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; | |
426 | ||
427 | IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n"); | |
6be497f2 EL |
428 | |
429 | if (mvm->cfg->high_temp) | |
430 | tt->params = &iwl7000_high_temp_tt_params; | |
431 | else | |
432 | tt->params = &iwl7000_tt_params; | |
433 | ||
dafe6c43 | 434 | tt->throttle = false; |
0c0e2c71 | 435 | tt->min_backoff = min_backoff; |
9ee718aa EL |
436 | INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill); |
437 | } | |
438 | ||
439 | void iwl_mvm_tt_exit(struct iwl_mvm *mvm) | |
440 | { | |
441 | cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit); | |
442 | IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n"); | |
443 | } |