blob: 9a12b70c7d4f656d842ad331602039a48f629483 [file] [log] [blame]
Wey-Yi Guy0975cc82010-07-31 08:34:07 -07001/******************************************************************************
2 *
Wey-Yi Guy4e318262011-12-27 11:21:32 -08003 * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
Wey-Yi Guy0975cc82010-07-31 08:34:07 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34
35#include <net/mac80211.h>
36
Johannes Berg84abd2c2012-03-06 13:30:47 -080037#include "iwl-agn.h"
Wey-Yi Guy0975cc82010-07-31 08:34:07 -070038#include "iwl-eeprom.h"
39#include "iwl-dev.h"
40#include "iwl-core.h"
41#include "iwl-io.h"
42#include "iwl-commands.h"
43#include "iwl-debug.h"
44#include "iwl-agn-tt.h"
45
46/* default Thermal Throttling transaction table
47 * Current state | Throttling Down | Throttling Up
48 *=============================================================================
49 * Condition Nxt State Condition Nxt State Condition Nxt State
50 *-----------------------------------------------------------------------------
51 * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
52 * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
53 * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
54 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
55 *=============================================================================
56 */
57static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
58 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
59 {IWL_TI_1, 105, CT_KILL_THRESHOLD - 1},
60 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
61};
62static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
63 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
64 {IWL_TI_2, 110, CT_KILL_THRESHOLD - 1},
65 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
66};
67static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
68 {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
69 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX},
70 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
71};
72static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
73 {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
74 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
75 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
76};
77
78/* Advance Thermal Throttling default restriction table */
79static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
80 {IWL_ANT_OK_MULTI, IWL_ANT_OK_MULTI, true },
81 {IWL_ANT_OK_SINGLE, IWL_ANT_OK_MULTI, true },
82 {IWL_ANT_OK_SINGLE, IWL_ANT_OK_SINGLE, false },
83 {IWL_ANT_OK_NONE, IWL_ANT_OK_NONE, false }
84};
85
86bool iwl_tt_is_low_power_state(struct iwl_priv *priv)
87{
88 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
89
90 if (tt->state >= IWL_TI_1)
91 return true;
92 return false;
93}
94
95u8 iwl_tt_current_power_mode(struct iwl_priv *priv)
96{
97 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
98
99 return tt->tt_power_mode;
100}
101
102bool iwl_ht_enabled(struct iwl_priv *priv)
103{
104 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
105 struct iwl_tt_restriction *restriction;
106
107 if (!priv->thermal_throttle.advanced_tt)
108 return true;
109 restriction = tt->restriction + tt->state;
110 return restriction->is_ht;
111}
112
113static bool iwl_within_ct_kill_margin(struct iwl_priv *priv)
114{
115 s32 temp = priv->temperature; /* degrees CELSIUS except specified */
116 bool within_margin = false;
117
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700118 if (!priv->thermal_throttle.advanced_tt)
119 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
120 CT_KILL_THRESHOLD_LEGACY) ? true : false;
121 else
122 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
123 CT_KILL_THRESHOLD) ? true : false;
124 return within_margin;
125}
126
127bool iwl_check_for_ct_kill(struct iwl_priv *priv)
128{
129 bool is_ct_kill = false;
130
131 if (iwl_within_ct_kill_margin(priv)) {
132 iwl_tt_enter_ct_kill(priv);
133 is_ct_kill = true;
134 }
135 return is_ct_kill;
136}
137
138enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
139{
140 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
141 struct iwl_tt_restriction *restriction;
142
143 if (!priv->thermal_throttle.advanced_tt)
144 return IWL_ANT_OK_MULTI;
145 restriction = tt->restriction + tt->state;
146 return restriction->tx_stream;
147}
148
149enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
150{
151 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
152 struct iwl_tt_restriction *restriction;
153
154 if (!priv->thermal_throttle.advanced_tt)
155 return IWL_ANT_OK_MULTI;
156 restriction = tt->restriction + tt->state;
157 return restriction->rx_stream;
158}
159
160#define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
161#define CT_KILL_WAITING_DURATION (300) /* 300ms duration */
162
163/*
164 * toggle the bit to wake up uCode and check the temperature
165 * if the temperature is below CT, uCode will stay awake and send card
166 * state notification with CT_KILL bit clear to inform Thermal Throttling
167 * Management to change state. Otherwise, uCode will go back to sleep
168 * without doing anything, driver should continue the 5 seconds timer
169 * to wake up uCode for temperature check until temperature drop below CT
170 */
171static void iwl_tt_check_exit_ct_kill(unsigned long data)
172{
173 struct iwl_priv *priv = (struct iwl_priv *)data;
174 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
175 unsigned long flags;
176
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700177 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700178 return;
179
180 if (tt->state == IWL_TI_CT_KILL) {
181 if (priv->thermal_throttle.ct_kill_toggle) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200182 iwl_write32(trans(priv), CSR_UCODE_DRV_GP1_CLR,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700183 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
184 priv->thermal_throttle.ct_kill_toggle = false;
185 } else {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200186 iwl_write32(trans(priv), CSR_UCODE_DRV_GP1_SET,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700187 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
188 priv->thermal_throttle.ct_kill_toggle = true;
189 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200190 iwl_read32(trans(priv), CSR_UCODE_DRV_GP1);
191 spin_lock_irqsave(&trans(priv)->reg_lock, flags);
Stanislaw Gruszkabfe4b802012-03-07 09:52:24 -0800192 if (likely(iwl_grab_nic_access(trans(priv))))
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200193 iwl_release_nic_access(trans(priv));
194 spin_unlock_irqrestore(&trans(priv)->reg_lock, flags);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700195
196 /* Reschedule the ct_kill timer to occur in
197 * CT_KILL_EXIT_DURATION seconds to ensure we get a
198 * thermal update */
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700199 IWL_DEBUG_TEMP(priv, "schedule ct_kill exit timer\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700200 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
201 jiffies + CT_KILL_EXIT_DURATION * HZ);
202 }
203}
204
205static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
206 bool stop)
207{
208 if (stop) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700209 IWL_DEBUG_TEMP(priv, "Stop all queues\n");
Emmanuel Grumbach859cfb02011-09-15 11:46:31 -0700210 if (priv->mac80211_registered)
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700211 ieee80211_stop_queues(priv->hw);
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700212 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700213 "Schedule 5 seconds CT_KILL Timer\n");
214 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
215 jiffies + CT_KILL_EXIT_DURATION * HZ);
216 } else {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700217 IWL_DEBUG_TEMP(priv, "Wake all queues\n");
Emmanuel Grumbach859cfb02011-09-15 11:46:31 -0700218 if (priv->mac80211_registered)
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700219 ieee80211_wake_queues(priv->hw);
220 }
221}
222
223static void iwl_tt_ready_for_ct_kill(unsigned long data)
224{
225 struct iwl_priv *priv = (struct iwl_priv *)data;
226 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
227
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700228 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700229 return;
230
231 /* temperature timer expired, ready to go into CT_KILL state */
232 if (tt->state != IWL_TI_CT_KILL) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700233 IWL_DEBUG_TEMP(priv, "entering CT_KILL state when "
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700234 "temperature timer expired\n");
235 tt->state = IWL_TI_CT_KILL;
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700236 set_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700237 iwl_perform_ct_kill_task(priv, true);
238 }
239}
240
241static void iwl_prepare_ct_kill_task(struct iwl_priv *priv)
242{
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700243 IWL_DEBUG_TEMP(priv, "Prepare to enter IWL_TI_CT_KILL\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700244 /* make request to retrieve statistics information */
245 iwl_send_statistics_request(priv, CMD_SYNC, false);
246 /* Reschedule the ct_kill wait timer */
247 mod_timer(&priv->thermal_throttle.ct_kill_waiting_tm,
248 jiffies + msecs_to_jiffies(CT_KILL_WAITING_DURATION));
249}
250
251#define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
252#define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
253#define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
254
255/*
256 * Legacy thermal throttling
257 * 1) Avoid NIC destruction due to high temperatures
258 * Chip will identify dangerously high temperatures that can
259 * harm the device and will power down
260 * 2) Avoid the NIC power down due to high temperature
261 * Throttle early enough to lower the power consumption before
262 * drastic steps are needed
263 */
264static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
265{
266 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
267 enum iwl_tt_state old_state;
268
269#ifdef CONFIG_IWLWIFI_DEBUG
270 if ((tt->tt_previous_temp) &&
271 (temp > tt->tt_previous_temp) &&
272 ((temp - tt->tt_previous_temp) >
273 IWL_TT_INCREASE_MARGIN)) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700274 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700275 "Temperature increase %d degree Celsius\n",
276 (temp - tt->tt_previous_temp));
277 }
278#endif
279 old_state = tt->state;
280 /* in Celsius */
281 if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
282 tt->state = IWL_TI_CT_KILL;
283 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
284 tt->state = IWL_TI_2;
285 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
286 tt->state = IWL_TI_1;
287 else
288 tt->state = IWL_TI_0;
289
290#ifdef CONFIG_IWLWIFI_DEBUG
291 tt->tt_previous_temp = temp;
292#endif
293 /* stop ct_kill_waiting_tm timer */
294 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
295 if (tt->state != old_state) {
296 switch (tt->state) {
297 case IWL_TI_0:
298 /*
299 * When the system is ready to go back to IWL_TI_0
300 * we only have to call iwl_power_update_mode() to
301 * do so.
302 */
303 break;
304 case IWL_TI_1:
305 tt->tt_power_mode = IWL_POWER_INDEX_3;
306 break;
307 case IWL_TI_2:
308 tt->tt_power_mode = IWL_POWER_INDEX_4;
309 break;
310 default:
311 tt->tt_power_mode = IWL_POWER_INDEX_5;
312 break;
313 }
Johannes Bergb1eea292012-03-06 13:30:42 -0800314 mutex_lock(&priv->mutex);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700315 if (old_state == IWL_TI_CT_KILL)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700316 clear_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700317 if (tt->state != IWL_TI_CT_KILL &&
318 iwl_power_update_mode(priv, true)) {
319 /* TT state not updated
320 * try again during next temperature read
321 */
322 if (old_state == IWL_TI_CT_KILL)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700323 set_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700324 tt->state = old_state;
325 IWL_ERR(priv, "Cannot update power mode, "
326 "TT state not updated\n");
327 } else {
328 if (tt->state == IWL_TI_CT_KILL) {
329 if (force) {
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700330 set_bit(STATUS_CT_KILL,
331 &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700332 iwl_perform_ct_kill_task(priv, true);
333 } else {
334 iwl_prepare_ct_kill_task(priv);
335 tt->state = old_state;
336 }
337 } else if (old_state == IWL_TI_CT_KILL &&
338 tt->state != IWL_TI_CT_KILL)
339 iwl_perform_ct_kill_task(priv, false);
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700340 IWL_DEBUG_TEMP(priv, "Temperature state changed %u\n",
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700341 tt->state);
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700342 IWL_DEBUG_TEMP(priv, "Power Index change to %u\n",
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700343 tt->tt_power_mode);
344 }
Johannes Bergb1eea292012-03-06 13:30:42 -0800345 mutex_unlock(&priv->mutex);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700346 }
347}
348
349/*
350 * Advance thermal throttling
351 * 1) Avoid NIC destruction due to high temperatures
352 * Chip will identify dangerously high temperatures that can
353 * harm the device and will power down
354 * 2) Avoid the NIC power down due to high temperature
355 * Throttle early enough to lower the power consumption before
356 * drastic steps are needed
357 * Actions include relaxing the power down sleep thresholds and
358 * decreasing the number of TX streams
359 * 3) Avoid throughput performance impact as much as possible
360 *
361 *=============================================================================
362 * Condition Nxt State Condition Nxt State Condition Nxt State
363 *-----------------------------------------------------------------------------
364 * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
365 * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
366 * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
367 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
368 *=============================================================================
369 */
370static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
371{
372 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
373 int i;
374 bool changed = false;
375 enum iwl_tt_state old_state;
376 struct iwl_tt_trans *transaction;
377
378 old_state = tt->state;
379 for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
380 /* based on the current TT state,
381 * find the curresponding transaction table
382 * each table has (IWL_TI_STATE_MAX - 1) entries
383 * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
384 * will advance to the correct table.
385 * then based on the current temperature
386 * find the next state need to transaction to
387 * go through all the possible (IWL_TI_STATE_MAX - 1) entries
388 * in the current table to see if transaction is needed
389 */
390 transaction = tt->transaction +
391 ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
392 if (temp >= transaction->tt_low &&
393 temp <= transaction->tt_high) {
394#ifdef CONFIG_IWLWIFI_DEBUG
395 if ((tt->tt_previous_temp) &&
396 (temp > tt->tt_previous_temp) &&
397 ((temp - tt->tt_previous_temp) >
398 IWL_TT_INCREASE_MARGIN)) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700399 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700400 "Temperature increase %d "
401 "degree Celsius\n",
402 (temp - tt->tt_previous_temp));
403 }
404 tt->tt_previous_temp = temp;
405#endif
406 if (old_state !=
407 transaction->next_state) {
408 changed = true;
409 tt->state =
410 transaction->next_state;
411 }
412 break;
413 }
414 }
415 /* stop ct_kill_waiting_tm timer */
416 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
417 if (changed) {
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700418 if (tt->state >= IWL_TI_1) {
419 /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
420 tt->tt_power_mode = IWL_POWER_INDEX_5;
Johannes Berg246ed352010-08-23 10:46:32 +0200421
422 if (!iwl_ht_enabled(priv)) {
423 struct iwl_rxon_context *ctx;
424
425 for_each_context(priv, ctx) {
426 struct iwl_rxon_cmd *rxon;
427
428 rxon = &ctx->staging;
429
430 /* disable HT */
431 rxon->flags &= ~(
432 RXON_FLG_CHANNEL_MODE_MSK |
433 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
434 RXON_FLG_HT40_PROT_MSK |
435 RXON_FLG_HT_PROT_MSK);
436 }
437 } else {
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700438 /* check HT capability and set
439 * according to the system HT capability
440 * in case get disabled before */
441 iwl_set_rxon_ht(priv, &priv->current_ht_config);
442 }
443
444 } else {
445 /*
446 * restore system power setting -- it will be
447 * recalculated automatically.
448 */
449
450 /* check HT capability and set
451 * according to the system HT capability
452 * in case get disabled before */
453 iwl_set_rxon_ht(priv, &priv->current_ht_config);
454 }
Johannes Bergb1eea292012-03-06 13:30:42 -0800455 mutex_lock(&priv->mutex);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700456 if (old_state == IWL_TI_CT_KILL)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700457 clear_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700458 if (tt->state != IWL_TI_CT_KILL &&
459 iwl_power_update_mode(priv, true)) {
460 /* TT state not updated
461 * try again during next temperature read
462 */
463 IWL_ERR(priv, "Cannot update power mode, "
464 "TT state not updated\n");
465 if (old_state == IWL_TI_CT_KILL)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700466 set_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700467 tt->state = old_state;
468 } else {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700469 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700470 "Thermal Throttling to new state: %u\n",
471 tt->state);
472 if (old_state != IWL_TI_CT_KILL &&
473 tt->state == IWL_TI_CT_KILL) {
474 if (force) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700475 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700476 "Enter IWL_TI_CT_KILL\n");
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700477 set_bit(STATUS_CT_KILL,
478 &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700479 iwl_perform_ct_kill_task(priv, true);
480 } else {
481 iwl_prepare_ct_kill_task(priv);
482 tt->state = old_state;
483 }
484 } else if (old_state == IWL_TI_CT_KILL &&
485 tt->state != IWL_TI_CT_KILL) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700486 IWL_DEBUG_TEMP(priv, "Exit IWL_TI_CT_KILL\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700487 iwl_perform_ct_kill_task(priv, false);
488 }
489 }
Johannes Bergb1eea292012-03-06 13:30:42 -0800490 mutex_unlock(&priv->mutex);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700491 }
492}
493
494/* Card State Notification indicated reach critical temperature
495 * if PSP not enable, no Thermal Throttling function will be performed
496 * just set the GP1 bit to acknowledge the event
497 * otherwise, go into IWL_TI_CT_KILL state
498 * since Card State Notification will not provide any temperature reading
499 * for Legacy mode
500 * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
501 * for advance mode
502 * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
503 */
504static void iwl_bg_ct_enter(struct work_struct *work)
505{
506 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
507 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
508
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700509 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700510 return;
511
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -0700512 if (!iwl_is_ready(priv->shrd))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700513 return;
514
515 if (tt->state != IWL_TI_CT_KILL) {
516 IWL_ERR(priv, "Device reached critical temperature "
517 "- ucode going to sleep!\n");
518 if (!priv->thermal_throttle.advanced_tt)
519 iwl_legacy_tt_handler(priv,
520 IWL_MINIMAL_POWER_THRESHOLD,
521 true);
522 else
523 iwl_advance_tt_handler(priv,
524 CT_KILL_THRESHOLD + 1, true);
525 }
526}
527
528/* Card State Notification indicated out of critical temperature
529 * since Card State Notification will not provide any temperature reading
530 * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
531 * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
532 */
533static void iwl_bg_ct_exit(struct work_struct *work)
534{
535 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
536 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
537
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700538 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700539 return;
540
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -0700541 if (!iwl_is_ready(priv->shrd))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700542 return;
543
544 /* stop ct_kill_exit_tm timer */
545 del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
546
547 if (tt->state == IWL_TI_CT_KILL) {
548 IWL_ERR(priv,
549 "Device temperature below critical"
550 "- ucode awake!\n");
551 /*
552 * exit from CT_KILL state
553 * reset the current temperature reading
554 */
555 priv->temperature = 0;
556 if (!priv->thermal_throttle.advanced_tt)
557 iwl_legacy_tt_handler(priv,
558 IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
559 true);
560 else
561 iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
562 true);
563 }
564}
565
566void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
567{
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700568 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700569 return;
570
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700571 IWL_DEBUG_TEMP(priv, "Queueing critical temperature enter.\n");
Johannes Berg1ee158d2012-02-17 10:07:44 -0800572 queue_work(priv->workqueue, &priv->ct_enter);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700573}
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700574
575void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
576{
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700577 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700578 return;
579
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700580 IWL_DEBUG_TEMP(priv, "Queueing critical temperature exit.\n");
Johannes Berg1ee158d2012-02-17 10:07:44 -0800581 queue_work(priv->workqueue, &priv->ct_exit);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700582}
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700583
584static void iwl_bg_tt_work(struct work_struct *work)
585{
586 struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
587 s32 temp = priv->temperature; /* degrees CELSIUS except specified */
588
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700589 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700590 return;
591
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700592 if (!priv->thermal_throttle.advanced_tt)
593 iwl_legacy_tt_handler(priv, temp, false);
594 else
595 iwl_advance_tt_handler(priv, temp, false);
596}
597
598void iwl_tt_handler(struct iwl_priv *priv)
599{
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700600 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700601 return;
602
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700603 IWL_DEBUG_TEMP(priv, "Queueing thermal throttling work.\n");
Johannes Berg1ee158d2012-02-17 10:07:44 -0800604 queue_work(priv->workqueue, &priv->tt_work);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700605}
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700606
607/* Thermal throttling initialization
608 * For advance thermal throttling:
609 * Initialize Thermal Index and temperature threshold table
610 * Initialize thermal throttling restriction table
611 */
612void iwl_tt_initialize(struct iwl_priv *priv)
613{
614 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
615 int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
616 struct iwl_tt_trans *transaction;
617
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700618 IWL_DEBUG_TEMP(priv, "Initialize Thermal Throttling\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700619
620 memset(tt, 0, sizeof(struct iwl_tt_mgmt));
621
622 tt->state = IWL_TI_0;
623 init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
624 priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
625 priv->thermal_throttle.ct_kill_exit_tm.function =
626 iwl_tt_check_exit_ct_kill;
627 init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
628 priv->thermal_throttle.ct_kill_waiting_tm.data =
629 (unsigned long)priv;
630 priv->thermal_throttle.ct_kill_waiting_tm.function =
631 iwl_tt_ready_for_ct_kill;
632 /* setup deferred ct kill work */
633 INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
634 INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
635 INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
636
Don Fry38622412011-12-16 07:07:36 -0800637 if (cfg(priv)->base_params->adv_thermal_throttle) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700638 IWL_DEBUG_TEMP(priv, "Advanced Thermal Throttling\n");
Emmanuel Grumbach7f90dce2011-09-22 15:14:53 -0700639 tt->restriction = kcalloc(IWL_TI_STATE_MAX,
640 sizeof(struct iwl_tt_restriction),
641 GFP_KERNEL);
642 tt->transaction = kcalloc(IWL_TI_STATE_MAX *
643 (IWL_TI_STATE_MAX - 1),
644 sizeof(struct iwl_tt_trans),
645 GFP_KERNEL);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700646 if (!tt->restriction || !tt->transaction) {
647 IWL_ERR(priv, "Fallback to Legacy Throttling\n");
648 priv->thermal_throttle.advanced_tt = false;
649 kfree(tt->restriction);
650 tt->restriction = NULL;
651 kfree(tt->transaction);
652 tt->transaction = NULL;
653 } else {
654 transaction = tt->transaction +
655 (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
656 memcpy(transaction, &tt_range_0[0], size);
657 transaction = tt->transaction +
658 (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
659 memcpy(transaction, &tt_range_1[0], size);
660 transaction = tt->transaction +
661 (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
662 memcpy(transaction, &tt_range_2[0], size);
663 transaction = tt->transaction +
664 (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
665 memcpy(transaction, &tt_range_3[0], size);
666 size = sizeof(struct iwl_tt_restriction) *
667 IWL_TI_STATE_MAX;
668 memcpy(tt->restriction,
669 &restriction_range[0], size);
670 priv->thermal_throttle.advanced_tt = true;
671 }
672 } else {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700673 IWL_DEBUG_TEMP(priv, "Legacy Thermal Throttling\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700674 priv->thermal_throttle.advanced_tt = false;
675 }
676}
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700677
678/* cleanup thermal throttling management related memory and timer */
679void iwl_tt_exit(struct iwl_priv *priv)
680{
681 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
682
683 /* stop ct_kill_exit_tm timer if activated */
684 del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
685 /* stop ct_kill_waiting_tm timer if activated */
686 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
687 cancel_work_sync(&priv->tt_work);
688 cancel_work_sync(&priv->ct_enter);
689 cancel_work_sync(&priv->ct_exit);
690
691 if (priv->thermal_throttle.advanced_tt) {
692 /* free advance thermal throttling memory */
693 kfree(tt->restriction);
694 tt->restriction = NULL;
695 kfree(tt->transaction);
696 tt->transaction = NULL;
697 }
698}