blob: 85fe590ee7214528a897754590010c1106457c28 [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
37#include "iwl-eeprom.h"
38#include "iwl-dev.h"
39#include "iwl-core.h"
40#include "iwl-io.h"
41#include "iwl-commands.h"
42#include "iwl-debug.h"
43#include "iwl-agn-tt.h"
44
45/* default Thermal Throttling transaction table
46 * Current state | Throttling Down | Throttling Up
47 *=============================================================================
48 * Condition Nxt State Condition Nxt State Condition Nxt State
49 *-----------------------------------------------------------------------------
50 * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
51 * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
52 * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
53 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
54 *=============================================================================
55 */
56static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
57 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
58 {IWL_TI_1, 105, CT_KILL_THRESHOLD - 1},
59 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
60};
61static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
62 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
63 {IWL_TI_2, 110, CT_KILL_THRESHOLD - 1},
64 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
65};
66static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
67 {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
68 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX},
69 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
70};
71static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
72 {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
73 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
74 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
75};
76
77/* Advance Thermal Throttling default restriction table */
78static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
79 {IWL_ANT_OK_MULTI, IWL_ANT_OK_MULTI, true },
80 {IWL_ANT_OK_SINGLE, IWL_ANT_OK_MULTI, true },
81 {IWL_ANT_OK_SINGLE, IWL_ANT_OK_SINGLE, false },
82 {IWL_ANT_OK_NONE, IWL_ANT_OK_NONE, false }
83};
84
85bool iwl_tt_is_low_power_state(struct iwl_priv *priv)
86{
87 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
88
89 if (tt->state >= IWL_TI_1)
90 return true;
91 return false;
92}
93
94u8 iwl_tt_current_power_mode(struct iwl_priv *priv)
95{
96 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
97
98 return tt->tt_power_mode;
99}
100
101bool iwl_ht_enabled(struct iwl_priv *priv)
102{
103 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
104 struct iwl_tt_restriction *restriction;
105
106 if (!priv->thermal_throttle.advanced_tt)
107 return true;
108 restriction = tt->restriction + tt->state;
109 return restriction->is_ht;
110}
111
112static bool iwl_within_ct_kill_margin(struct iwl_priv *priv)
113{
114 s32 temp = priv->temperature; /* degrees CELSIUS except specified */
115 bool within_margin = false;
116
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700117 if (!priv->thermal_throttle.advanced_tt)
118 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
119 CT_KILL_THRESHOLD_LEGACY) ? true : false;
120 else
121 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
122 CT_KILL_THRESHOLD) ? true : false;
123 return within_margin;
124}
125
126bool iwl_check_for_ct_kill(struct iwl_priv *priv)
127{
128 bool is_ct_kill = false;
129
130 if (iwl_within_ct_kill_margin(priv)) {
131 iwl_tt_enter_ct_kill(priv);
132 is_ct_kill = true;
133 }
134 return is_ct_kill;
135}
136
137enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
138{
139 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
140 struct iwl_tt_restriction *restriction;
141
142 if (!priv->thermal_throttle.advanced_tt)
143 return IWL_ANT_OK_MULTI;
144 restriction = tt->restriction + tt->state;
145 return restriction->tx_stream;
146}
147
148enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
149{
150 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
151 struct iwl_tt_restriction *restriction;
152
153 if (!priv->thermal_throttle.advanced_tt)
154 return IWL_ANT_OK_MULTI;
155 restriction = tt->restriction + tt->state;
156 return restriction->rx_stream;
157}
158
159#define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
160#define CT_KILL_WAITING_DURATION (300) /* 300ms duration */
161
162/*
163 * toggle the bit to wake up uCode and check the temperature
164 * if the temperature is below CT, uCode will stay awake and send card
165 * state notification with CT_KILL bit clear to inform Thermal Throttling
166 * Management to change state. Otherwise, uCode will go back to sleep
167 * without doing anything, driver should continue the 5 seconds timer
168 * to wake up uCode for temperature check until temperature drop below CT
169 */
170static void iwl_tt_check_exit_ct_kill(unsigned long data)
171{
172 struct iwl_priv *priv = (struct iwl_priv *)data;
173 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
174 unsigned long flags;
175
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700176 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700177 return;
178
179 if (tt->state == IWL_TI_CT_KILL) {
180 if (priv->thermal_throttle.ct_kill_toggle) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200181 iwl_write32(trans(priv), CSR_UCODE_DRV_GP1_CLR,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700182 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
183 priv->thermal_throttle.ct_kill_toggle = false;
184 } else {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200185 iwl_write32(trans(priv), CSR_UCODE_DRV_GP1_SET,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700186 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
187 priv->thermal_throttle.ct_kill_toggle = true;
188 }
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200189 iwl_read32(trans(priv), CSR_UCODE_DRV_GP1);
190 spin_lock_irqsave(&trans(priv)->reg_lock, flags);
191 if (!iwl_grab_nic_access(trans(priv)))
192 iwl_release_nic_access(trans(priv));
193 spin_unlock_irqrestore(&trans(priv)->reg_lock, flags);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700194
195 /* Reschedule the ct_kill timer to occur in
196 * CT_KILL_EXIT_DURATION seconds to ensure we get a
197 * thermal update */
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700198 IWL_DEBUG_TEMP(priv, "schedule ct_kill exit timer\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700199 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
200 jiffies + CT_KILL_EXIT_DURATION * HZ);
201 }
202}
203
204static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
205 bool stop)
206{
207 if (stop) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700208 IWL_DEBUG_TEMP(priv, "Stop all queues\n");
Emmanuel Grumbach859cfb02011-09-15 11:46:31 -0700209 if (priv->mac80211_registered)
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700210 ieee80211_stop_queues(priv->hw);
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700211 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700212 "Schedule 5 seconds CT_KILL Timer\n");
213 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
214 jiffies + CT_KILL_EXIT_DURATION * HZ);
215 } else {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700216 IWL_DEBUG_TEMP(priv, "Wake all queues\n");
Emmanuel Grumbach859cfb02011-09-15 11:46:31 -0700217 if (priv->mac80211_registered)
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700218 ieee80211_wake_queues(priv->hw);
219 }
220}
221
222static void iwl_tt_ready_for_ct_kill(unsigned long data)
223{
224 struct iwl_priv *priv = (struct iwl_priv *)data;
225 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
226
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700227 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700228 return;
229
230 /* temperature timer expired, ready to go into CT_KILL state */
231 if (tt->state != IWL_TI_CT_KILL) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700232 IWL_DEBUG_TEMP(priv, "entering CT_KILL state when "
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700233 "temperature timer expired\n");
234 tt->state = IWL_TI_CT_KILL;
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700235 set_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700236 iwl_perform_ct_kill_task(priv, true);
237 }
238}
239
240static void iwl_prepare_ct_kill_task(struct iwl_priv *priv)
241{
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700242 IWL_DEBUG_TEMP(priv, "Prepare to enter IWL_TI_CT_KILL\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700243 /* make request to retrieve statistics information */
244 iwl_send_statistics_request(priv, CMD_SYNC, false);
245 /* Reschedule the ct_kill wait timer */
246 mod_timer(&priv->thermal_throttle.ct_kill_waiting_tm,
247 jiffies + msecs_to_jiffies(CT_KILL_WAITING_DURATION));
248}
249
250#define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
251#define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
252#define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
253
254/*
255 * Legacy thermal throttling
256 * 1) Avoid NIC destruction due to high temperatures
257 * Chip will identify dangerously high temperatures that can
258 * harm the device and will power down
259 * 2) Avoid the NIC power down due to high temperature
260 * Throttle early enough to lower the power consumption before
261 * drastic steps are needed
262 */
263static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
264{
265 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
266 enum iwl_tt_state old_state;
267
268#ifdef CONFIG_IWLWIFI_DEBUG
269 if ((tt->tt_previous_temp) &&
270 (temp > tt->tt_previous_temp) &&
271 ((temp - tt->tt_previous_temp) >
272 IWL_TT_INCREASE_MARGIN)) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700273 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700274 "Temperature increase %d degree Celsius\n",
275 (temp - tt->tt_previous_temp));
276 }
277#endif
278 old_state = tt->state;
279 /* in Celsius */
280 if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
281 tt->state = IWL_TI_CT_KILL;
282 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
283 tt->state = IWL_TI_2;
284 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
285 tt->state = IWL_TI_1;
286 else
287 tt->state = IWL_TI_0;
288
289#ifdef CONFIG_IWLWIFI_DEBUG
290 tt->tt_previous_temp = temp;
291#endif
292 /* stop ct_kill_waiting_tm timer */
293 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
294 if (tt->state != old_state) {
295 switch (tt->state) {
296 case IWL_TI_0:
297 /*
298 * When the system is ready to go back to IWL_TI_0
299 * we only have to call iwl_power_update_mode() to
300 * do so.
301 */
302 break;
303 case IWL_TI_1:
304 tt->tt_power_mode = IWL_POWER_INDEX_3;
305 break;
306 case IWL_TI_2:
307 tt->tt_power_mode = IWL_POWER_INDEX_4;
308 break;
309 default:
310 tt->tt_power_mode = IWL_POWER_INDEX_5;
311 break;
312 }
Johannes Bergb1eea292012-03-06 13:30:42 -0800313 mutex_lock(&priv->mutex);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700314 if (old_state == IWL_TI_CT_KILL)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700315 clear_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700316 if (tt->state != IWL_TI_CT_KILL &&
317 iwl_power_update_mode(priv, true)) {
318 /* TT state not updated
319 * try again during next temperature read
320 */
321 if (old_state == IWL_TI_CT_KILL)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700322 set_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700323 tt->state = old_state;
324 IWL_ERR(priv, "Cannot update power mode, "
325 "TT state not updated\n");
326 } else {
327 if (tt->state == IWL_TI_CT_KILL) {
328 if (force) {
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700329 set_bit(STATUS_CT_KILL,
330 &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700331 iwl_perform_ct_kill_task(priv, true);
332 } else {
333 iwl_prepare_ct_kill_task(priv);
334 tt->state = old_state;
335 }
336 } else if (old_state == IWL_TI_CT_KILL &&
337 tt->state != IWL_TI_CT_KILL)
338 iwl_perform_ct_kill_task(priv, false);
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700339 IWL_DEBUG_TEMP(priv, "Temperature state changed %u\n",
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700340 tt->state);
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700341 IWL_DEBUG_TEMP(priv, "Power Index change to %u\n",
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700342 tt->tt_power_mode);
343 }
Johannes Bergb1eea292012-03-06 13:30:42 -0800344 mutex_unlock(&priv->mutex);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700345 }
346}
347
348/*
349 * Advance thermal throttling
350 * 1) Avoid NIC destruction due to high temperatures
351 * Chip will identify dangerously high temperatures that can
352 * harm the device and will power down
353 * 2) Avoid the NIC power down due to high temperature
354 * Throttle early enough to lower the power consumption before
355 * drastic steps are needed
356 * Actions include relaxing the power down sleep thresholds and
357 * decreasing the number of TX streams
358 * 3) Avoid throughput performance impact as much as possible
359 *
360 *=============================================================================
361 * Condition Nxt State Condition Nxt State Condition Nxt State
362 *-----------------------------------------------------------------------------
363 * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
364 * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
365 * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
366 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
367 *=============================================================================
368 */
369static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
370{
371 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
372 int i;
373 bool changed = false;
374 enum iwl_tt_state old_state;
375 struct iwl_tt_trans *transaction;
376
377 old_state = tt->state;
378 for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
379 /* based on the current TT state,
380 * find the curresponding transaction table
381 * each table has (IWL_TI_STATE_MAX - 1) entries
382 * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
383 * will advance to the correct table.
384 * then based on the current temperature
385 * find the next state need to transaction to
386 * go through all the possible (IWL_TI_STATE_MAX - 1) entries
387 * in the current table to see if transaction is needed
388 */
389 transaction = tt->transaction +
390 ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
391 if (temp >= transaction->tt_low &&
392 temp <= transaction->tt_high) {
393#ifdef CONFIG_IWLWIFI_DEBUG
394 if ((tt->tt_previous_temp) &&
395 (temp > tt->tt_previous_temp) &&
396 ((temp - tt->tt_previous_temp) >
397 IWL_TT_INCREASE_MARGIN)) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700398 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700399 "Temperature increase %d "
400 "degree Celsius\n",
401 (temp - tt->tt_previous_temp));
402 }
403 tt->tt_previous_temp = temp;
404#endif
405 if (old_state !=
406 transaction->next_state) {
407 changed = true;
408 tt->state =
409 transaction->next_state;
410 }
411 break;
412 }
413 }
414 /* stop ct_kill_waiting_tm timer */
415 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
416 if (changed) {
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700417 if (tt->state >= IWL_TI_1) {
418 /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
419 tt->tt_power_mode = IWL_POWER_INDEX_5;
Johannes Berg246ed352010-08-23 10:46:32 +0200420
421 if (!iwl_ht_enabled(priv)) {
422 struct iwl_rxon_context *ctx;
423
424 for_each_context(priv, ctx) {
425 struct iwl_rxon_cmd *rxon;
426
427 rxon = &ctx->staging;
428
429 /* disable HT */
430 rxon->flags &= ~(
431 RXON_FLG_CHANNEL_MODE_MSK |
432 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
433 RXON_FLG_HT40_PROT_MSK |
434 RXON_FLG_HT_PROT_MSK);
435 }
436 } else {
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700437 /* check HT capability and set
438 * according to the system HT capability
439 * in case get disabled before */
440 iwl_set_rxon_ht(priv, &priv->current_ht_config);
441 }
442
443 } else {
444 /*
445 * restore system power setting -- it will be
446 * recalculated automatically.
447 */
448
449 /* check HT capability and set
450 * according to the system HT capability
451 * in case get disabled before */
452 iwl_set_rxon_ht(priv, &priv->current_ht_config);
453 }
Johannes Bergb1eea292012-03-06 13:30:42 -0800454 mutex_lock(&priv->mutex);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700455 if (old_state == IWL_TI_CT_KILL)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700456 clear_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700457 if (tt->state != IWL_TI_CT_KILL &&
458 iwl_power_update_mode(priv, true)) {
459 /* TT state not updated
460 * try again during next temperature read
461 */
462 IWL_ERR(priv, "Cannot update power mode, "
463 "TT state not updated\n");
464 if (old_state == IWL_TI_CT_KILL)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700465 set_bit(STATUS_CT_KILL, &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700466 tt->state = old_state;
467 } else {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700468 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700469 "Thermal Throttling to new state: %u\n",
470 tt->state);
471 if (old_state != IWL_TI_CT_KILL &&
472 tt->state == IWL_TI_CT_KILL) {
473 if (force) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700474 IWL_DEBUG_TEMP(priv,
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700475 "Enter IWL_TI_CT_KILL\n");
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700476 set_bit(STATUS_CT_KILL,
477 &priv->shrd->status);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700478 iwl_perform_ct_kill_task(priv, true);
479 } else {
480 iwl_prepare_ct_kill_task(priv);
481 tt->state = old_state;
482 }
483 } else if (old_state == IWL_TI_CT_KILL &&
484 tt->state != IWL_TI_CT_KILL) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700485 IWL_DEBUG_TEMP(priv, "Exit IWL_TI_CT_KILL\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700486 iwl_perform_ct_kill_task(priv, false);
487 }
488 }
Johannes Bergb1eea292012-03-06 13:30:42 -0800489 mutex_unlock(&priv->mutex);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700490 }
491}
492
493/* Card State Notification indicated reach critical temperature
494 * if PSP not enable, no Thermal Throttling function will be performed
495 * just set the GP1 bit to acknowledge the event
496 * otherwise, go into IWL_TI_CT_KILL state
497 * since Card State Notification will not provide any temperature reading
498 * for Legacy mode
499 * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
500 * for advance mode
501 * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
502 */
503static void iwl_bg_ct_enter(struct work_struct *work)
504{
505 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
506 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
507
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700508 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700509 return;
510
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -0700511 if (!iwl_is_ready(priv->shrd))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700512 return;
513
514 if (tt->state != IWL_TI_CT_KILL) {
515 IWL_ERR(priv, "Device reached critical temperature "
516 "- ucode going to sleep!\n");
517 if (!priv->thermal_throttle.advanced_tt)
518 iwl_legacy_tt_handler(priv,
519 IWL_MINIMAL_POWER_THRESHOLD,
520 true);
521 else
522 iwl_advance_tt_handler(priv,
523 CT_KILL_THRESHOLD + 1, true);
524 }
525}
526
527/* Card State Notification indicated out of critical temperature
528 * since Card State Notification will not provide any temperature reading
529 * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
530 * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
531 */
532static void iwl_bg_ct_exit(struct work_struct *work)
533{
534 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
535 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
536
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700537 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700538 return;
539
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -0700540 if (!iwl_is_ready(priv->shrd))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700541 return;
542
543 /* stop ct_kill_exit_tm timer */
544 del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
545
546 if (tt->state == IWL_TI_CT_KILL) {
547 IWL_ERR(priv,
548 "Device temperature below critical"
549 "- ucode awake!\n");
550 /*
551 * exit from CT_KILL state
552 * reset the current temperature reading
553 */
554 priv->temperature = 0;
555 if (!priv->thermal_throttle.advanced_tt)
556 iwl_legacy_tt_handler(priv,
557 IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
558 true);
559 else
560 iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
561 true);
562 }
563}
564
565void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
566{
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700567 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700568 return;
569
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700570 IWL_DEBUG_TEMP(priv, "Queueing critical temperature enter.\n");
Johannes Berg1ee158d2012-02-17 10:07:44 -0800571 queue_work(priv->workqueue, &priv->ct_enter);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700572}
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700573
574void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
575{
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700576 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700577 return;
578
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700579 IWL_DEBUG_TEMP(priv, "Queueing critical temperature exit.\n");
Johannes Berg1ee158d2012-02-17 10:07:44 -0800580 queue_work(priv->workqueue, &priv->ct_exit);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700581}
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700582
583static void iwl_bg_tt_work(struct work_struct *work)
584{
585 struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
586 s32 temp = priv->temperature; /* degrees CELSIUS except specified */
587
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700588 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700589 return;
590
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700591 if (!priv->thermal_throttle.advanced_tt)
592 iwl_legacy_tt_handler(priv, temp, false);
593 else
594 iwl_advance_tt_handler(priv, temp, false);
595}
596
597void iwl_tt_handler(struct iwl_priv *priv)
598{
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700599 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700600 return;
601
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700602 IWL_DEBUG_TEMP(priv, "Queueing thermal throttling work.\n");
Johannes Berg1ee158d2012-02-17 10:07:44 -0800603 queue_work(priv->workqueue, &priv->tt_work);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700604}
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700605
606/* Thermal throttling initialization
607 * For advance thermal throttling:
608 * Initialize Thermal Index and temperature threshold table
609 * Initialize thermal throttling restriction table
610 */
611void iwl_tt_initialize(struct iwl_priv *priv)
612{
613 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
614 int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
615 struct iwl_tt_trans *transaction;
616
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700617 IWL_DEBUG_TEMP(priv, "Initialize Thermal Throttling\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700618
619 memset(tt, 0, sizeof(struct iwl_tt_mgmt));
620
621 tt->state = IWL_TI_0;
622 init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
623 priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
624 priv->thermal_throttle.ct_kill_exit_tm.function =
625 iwl_tt_check_exit_ct_kill;
626 init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
627 priv->thermal_throttle.ct_kill_waiting_tm.data =
628 (unsigned long)priv;
629 priv->thermal_throttle.ct_kill_waiting_tm.function =
630 iwl_tt_ready_for_ct_kill;
631 /* setup deferred ct kill work */
632 INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
633 INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
634 INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
635
Don Fry38622412011-12-16 07:07:36 -0800636 if (cfg(priv)->base_params->adv_thermal_throttle) {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700637 IWL_DEBUG_TEMP(priv, "Advanced Thermal Throttling\n");
Emmanuel Grumbach7f90dce2011-09-22 15:14:53 -0700638 tt->restriction = kcalloc(IWL_TI_STATE_MAX,
639 sizeof(struct iwl_tt_restriction),
640 GFP_KERNEL);
641 tt->transaction = kcalloc(IWL_TI_STATE_MAX *
642 (IWL_TI_STATE_MAX - 1),
643 sizeof(struct iwl_tt_trans),
644 GFP_KERNEL);
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700645 if (!tt->restriction || !tt->transaction) {
646 IWL_ERR(priv, "Fallback to Legacy Throttling\n");
647 priv->thermal_throttle.advanced_tt = false;
648 kfree(tt->restriction);
649 tt->restriction = NULL;
650 kfree(tt->transaction);
651 tt->transaction = NULL;
652 } else {
653 transaction = tt->transaction +
654 (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
655 memcpy(transaction, &tt_range_0[0], size);
656 transaction = tt->transaction +
657 (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
658 memcpy(transaction, &tt_range_1[0], size);
659 transaction = tt->transaction +
660 (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
661 memcpy(transaction, &tt_range_2[0], size);
662 transaction = tt->transaction +
663 (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
664 memcpy(transaction, &tt_range_3[0], size);
665 size = sizeof(struct iwl_tt_restriction) *
666 IWL_TI_STATE_MAX;
667 memcpy(tt->restriction,
668 &restriction_range[0], size);
669 priv->thermal_throttle.advanced_tt = true;
670 }
671 } else {
Wey-Yi Guyf38f8842011-06-06 14:26:39 -0700672 IWL_DEBUG_TEMP(priv, "Legacy Thermal Throttling\n");
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700673 priv->thermal_throttle.advanced_tt = false;
674 }
675}
Wey-Yi Guy0975cc82010-07-31 08:34:07 -0700676
677/* cleanup thermal throttling management related memory and timer */
678void iwl_tt_exit(struct iwl_priv *priv)
679{
680 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
681
682 /* stop ct_kill_exit_tm timer if activated */
683 del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
684 /* stop ct_kill_waiting_tm timer if activated */
685 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
686 cancel_work_sync(&priv->tt_work);
687 cancel_work_sync(&priv->ct_enter);
688 cancel_work_sync(&priv->ct_exit);
689
690 if (priv->thermal_throttle.advanced_tt) {
691 /* free advance thermal throttling memory */
692 kfree(tt->restriction);
693 tt->restriction = NULL;
694 kfree(tt->transaction);
695 tt->transaction = NULL;
696 }
697}