1 /******************************************************************************
3 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
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.
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
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
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
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 *****************************************************************************/
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
34 #include <net/mac80211.h>
36 #include "iwl-eeprom.h"
40 #include "iwl-commands.h"
41 #include "iwl-debug.h"
42 #include "iwl-power.h"
45 * Setting power level allow the card to go to sleep when not busy.
47 * The power level is set to INDEX_1 (the least deep state) by
48 * default, and will, in the future, be the deepest state unless
49 * otherwise required by pm_qos network latency requirements.
51 * Using INDEX_1 without pm_qos is ok because mac80211 will disable
52 * PS when even checking every beacon for the TIM bit would exceed
53 * the required latency.
56 #define IWL_POWER_RANGE_0_MAX (2)
57 #define IWL_POWER_RANGE_1_MAX (10)
60 #define NOSLP cpu_to_le16(0), 0, 0
61 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
62 #define TU_TO_USEC 1024
63 #define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
64 #define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
69 /* default power management (not Tx power) table values */
70 /* for DTIM period 0 through IWL_POWER_RANGE_0_MAX */
71 static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
72 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
73 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
74 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
75 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
76 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
77 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
81 /* for DTIM period IWL_POWER_RANGE_0_MAX + 1 through IWL_POWER_RANGE_1_MAX */
82 static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
83 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
84 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
85 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
86 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
87 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
88 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
91 /* for DTIM period > IWL_POWER_RANGE_1_MAX */
92 static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
93 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
94 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
95 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
96 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
97 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
98 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
101 /* default Thermal Throttling transaction table
102 * Current state | Throttling Down | Throttling Up
103 *=============================================================================
104 * Condition Nxt State Condition Nxt State Condition Nxt State
105 *-----------------------------------------------------------------------------
106 * IWL_TI_0 T >= 115 CT_KILL 115>T>=105 TI_1 N/A N/A
107 * IWL_TI_1 T >= 115 CT_KILL 115>T>=110 TI_2 T<=95 TI_0
108 * IWL_TI_2 T >= 115 CT_KILL T<=100 TI_1
109 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
110 *=============================================================================
112 static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
113 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
114 {IWL_TI_1, 105, CT_KILL_THRESHOLD},
115 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
117 static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
118 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
119 {IWL_TI_2, 110, CT_KILL_THRESHOLD},
120 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
122 static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
123 {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
124 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
125 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
127 static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
128 {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
129 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
130 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
133 /* Advance Thermal Throttling default restriction table */
134 static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
135 {IWL_TX_MULTI, true, IWL_RX_MULTI},
136 {IWL_TX_SINGLE, true, IWL_RX_MULTI},
137 {IWL_TX_SINGLE, false, IWL_RX_SINGLE},
138 {IWL_TX_NONE, false, IWL_RX_NONE}
141 /* set card power command */
142 static int iwl_set_power(struct iwl_priv *priv, void *cmd)
144 return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
145 sizeof(struct iwl_powertable_cmd), cmd);
148 /* initialize to default */
149 static void iwl_power_init_handle(struct iwl_priv *priv)
151 struct iwl_power_mgr *pow_data;
152 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_NUM;
153 struct iwl_powertable_cmd *cmd;
157 IWL_DEBUG_POWER(priv, "Initialize power \n");
159 pow_data = &priv->power_data;
161 memset(pow_data, 0, sizeof(*pow_data));
163 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
164 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
165 memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
167 lctl = iwl_pcie_link_ctl(priv);
169 IWL_DEBUG_POWER(priv, "adjust power command flags\n");
171 for (i = 0; i < IWL_POWER_NUM; i++) {
172 cmd = &pow_data->pwr_range_0[i].cmd;
174 if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
175 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
177 cmd->flags |= IWL_POWER_PCI_PM_MSK;
181 /* adjust power command according to DTIM period and power level*/
182 static int iwl_update_power_cmd(struct iwl_priv *priv,
183 struct iwl_powertable_cmd *cmd, u16 mode)
185 struct iwl_power_vec_entry *range;
186 struct iwl_power_mgr *pow_data;
192 if (mode > IWL_POWER_INDEX_5) {
193 IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
197 pow_data = &priv->power_data;
199 if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
200 range = &pow_data->pwr_range_0[0];
201 else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
202 range = &pow_data->pwr_range_1[0];
204 range = &pow_data->pwr_range_2[0];
206 period = pow_data->dtim_period;
207 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
213 skip = !!range[mode].no_dtim;
217 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
218 max_sleep = le32_to_cpu(slp_itrvl);
219 if (max_sleep == 0xFF)
220 max_sleep = period * (skip + 1);
221 else if (max_sleep > period)
222 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
223 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
226 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
229 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
230 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
231 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
233 IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
234 IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
235 IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
236 IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
237 le32_to_cpu(cmd->sleep_interval[0]),
238 le32_to_cpu(cmd->sleep_interval[1]),
239 le32_to_cpu(cmd->sleep_interval[2]),
240 le32_to_cpu(cmd->sleep_interval[3]),
241 le32_to_cpu(cmd->sleep_interval[4]));
248 * compute the final power mode index
250 int iwl_power_update_mode(struct iwl_priv *priv, bool force)
252 struct iwl_power_mgr *setting = &(priv->power_data);
254 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
255 u16 uninitialized_var(final_mode);
258 /* Don't update the RX chain when chain noise calibration is running */
259 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
260 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
262 final_mode = priv->power_data.user_power_setting;
264 if (setting->power_disabled)
265 final_mode = IWL_POWER_MODE_CAM;
267 if (tt->state >= IWL_TI_1) {
268 /* TT power setting overwrite user & system power setting */
269 final_mode = tt->tt_power_mode;
271 if (iwl_is_ready_rf(priv) &&
272 ((setting->power_mode != final_mode) || force)) {
273 struct iwl_powertable_cmd cmd;
275 if (final_mode != IWL_POWER_MODE_CAM)
276 set_bit(STATUS_POWER_PMI, &priv->status);
278 iwl_update_power_cmd(priv, &cmd, final_mode);
279 cmd.keep_alive_beacons = 0;
281 if (final_mode == IWL_POWER_INDEX_5)
282 cmd.flags |= IWL_POWER_FAST_PD;
284 ret = iwl_set_power(priv, &cmd);
286 if (final_mode == IWL_POWER_MODE_CAM)
287 clear_bit(STATUS_POWER_PMI, &priv->status);
289 if (priv->cfg->ops->lib->update_chain_flags && update_chains)
290 priv->cfg->ops->lib->update_chain_flags(priv);
292 IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
293 "calibration running: %d\n",
294 priv->chain_noise_data.state);
296 setting->power_mode = final_mode;
301 EXPORT_SYMBOL(iwl_power_update_mode);
303 /* set user_power_setting */
304 int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
306 if (mode >= IWL_POWER_NUM)
309 priv->power_data.user_power_setting = mode;
311 return iwl_power_update_mode(priv, 0);
313 EXPORT_SYMBOL(iwl_power_set_user_mode);
315 bool iwl_ht_enabled(struct iwl_priv *priv)
317 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
318 struct iwl_tt_restriction *restriction;
320 if (!priv->power_data.adv_tt)
322 restriction = tt->restriction + tt->state;
323 return restriction->is_ht;
325 EXPORT_SYMBOL(iwl_ht_enabled);
327 u8 iwl_tx_ant_restriction(struct iwl_priv *priv)
329 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
330 struct iwl_tt_restriction *restriction;
332 if (!priv->power_data.adv_tt)
334 restriction = tt->restriction + tt->state;
335 return restriction->tx_stream;
337 EXPORT_SYMBOL(iwl_tx_ant_restriction);
339 u8 iwl_rx_ant_restriction(struct iwl_priv *priv)
341 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
342 struct iwl_tt_restriction *restriction;
344 if (!priv->power_data.adv_tt)
346 restriction = tt->restriction + tt->state;
347 return restriction->rx_stream;
349 EXPORT_SYMBOL(iwl_rx_ant_restriction);
351 #define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
354 * toggle the bit to wake up uCode and check the temperature
355 * if the temperature is below CT, uCode will stay awake and send card
356 * state notification with CT_KILL bit clear to inform Thermal Throttling
357 * Management to change state. Otherwise, uCode will go back to sleep
358 * without doing anything, driver should continue the 5 seconds timer
359 * to wake up uCode for temperature check until temperature drop below CT
361 static void iwl_tt_check_exit_ct_kill(unsigned long data)
363 struct iwl_priv *priv = (struct iwl_priv *)data;
364 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
367 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
370 if (tt->state == IWL_TI_CT_KILL) {
371 if (priv->power_data.ct_kill_toggle) {
372 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
373 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
374 priv->power_data.ct_kill_toggle = false;
376 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
377 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
378 priv->power_data.ct_kill_toggle = true;
380 iwl_read32(priv, CSR_UCODE_DRV_GP1);
381 spin_lock_irqsave(&priv->reg_lock, flags);
382 if (!iwl_grab_nic_access(priv))
383 iwl_release_nic_access(priv);
384 spin_unlock_irqrestore(&priv->reg_lock, flags);
386 /* Reschedule the ct_kill timer to occur in
387 * CT_KILL_EXIT_DURATION seconds to ensure we get a
389 mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
390 CT_KILL_EXIT_DURATION * HZ);
394 static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
398 IWL_DEBUG_POWER(priv, "Stop all queues\n");
399 if (priv->mac80211_registered)
400 ieee80211_stop_queues(priv->hw);
401 IWL_DEBUG_POWER(priv,
402 "Schedule 5 seconds CT_KILL Timer\n");
403 mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
404 CT_KILL_EXIT_DURATION * HZ);
406 IWL_DEBUG_POWER(priv, "Wake all queues\n");
407 if (priv->mac80211_registered)
408 ieee80211_wake_queues(priv->hw);
412 #define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
413 #define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
414 #define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
417 * Legacy thermal throttling
418 * 1) Avoid NIC destruction due to high temperatures
419 * Chip will identify dangerously high temperatures that can
420 * harm the device and will power down
421 * 2) Avoid the NIC power down due to high temperature
422 * Throttle early enough to lower the power consumption before
423 * drastic steps are needed
425 static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp)
427 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
428 enum iwl_tt_state old_state;
429 struct iwl_power_mgr *setting = &priv->power_data;
431 #ifdef CONFIG_IWLWIFI_DEBUG
432 if ((tt->tt_previous_temp) &&
433 (temp > tt->tt_previous_temp) &&
434 ((temp - tt->tt_previous_temp) >
435 IWL_TT_INCREASE_MARGIN)) {
436 IWL_DEBUG_POWER(priv,
437 "Temperature increase %d degree Celsius\n",
438 (temp - tt->tt_previous_temp));
441 old_state = tt->state;
443 if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
444 tt->state = IWL_TI_CT_KILL;
445 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
446 tt->state = IWL_TI_2;
447 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
448 tt->state = IWL_TI_1;
450 tt->state = IWL_TI_0;
452 #ifdef CONFIG_IWLWIFI_DEBUG
453 tt->tt_previous_temp = temp;
455 if (tt->state != old_state) {
456 if (old_state == IWL_TI_0) {
457 tt->sys_power_mode = setting->power_mode;
458 IWL_DEBUG_POWER(priv, "current power mode: %u\n",
459 setting->power_mode);
463 /* when system ready to go back to IWL_TI_0 state
464 * using system power mode instead of TT power mode
465 * revert back to the orginal power mode which was saved
466 * before enter Thermal Throttling state
467 * update priv->power_data.user_power_setting to the
468 * required power mode to make sure
469 * iwl_power_update_mode() will update power correctly.
471 priv->power_data.user_power_setting =
473 tt->tt_power_mode = tt->sys_power_mode;
476 tt->tt_power_mode = IWL_POWER_INDEX_3;
479 tt->tt_power_mode = IWL_POWER_INDEX_4;
482 tt->tt_power_mode = IWL_POWER_INDEX_5;
485 mutex_lock(&priv->mutex);
486 if (iwl_power_update_mode(priv, true)) {
487 /* TT state not updated
488 * try again during next temperature read
490 tt->state = old_state;
491 IWL_ERR(priv, "Cannot update power mode, "
492 "TT state not updated\n");
494 if (tt->state == IWL_TI_CT_KILL)
495 iwl_perform_ct_kill_task(priv, true);
496 else if (old_state == IWL_TI_CT_KILL &&
497 tt->state != IWL_TI_CT_KILL)
498 iwl_perform_ct_kill_task(priv, false);
499 IWL_DEBUG_POWER(priv, "Temperature state changed %u\n",
501 IWL_DEBUG_POWER(priv, "Power Index change to %u\n",
504 mutex_unlock(&priv->mutex);
509 * Advance thermal throttling
510 * 1) Avoid NIC destruction due to high temperatures
511 * Chip will identify dangerously high temperatures that can
512 * harm the device and will power down
513 * 2) Avoid the NIC power down due to high temperature
514 * Throttle early enough to lower the power consumption before
515 * drastic steps are needed
516 * Actions include relaxing the power down sleep thresholds and
517 * decreasing the number of TX streams
518 * 3) Avoid throughput performance impact as much as possible
520 *=============================================================================
521 * Condition Nxt State Condition Nxt State Condition Nxt State
522 *-----------------------------------------------------------------------------
523 * IWL_TI_0 T >= 115 CT_KILL 115>T>=105 TI_1 N/A N/A
524 * IWL_TI_1 T >= 115 CT_KILL 115>T>=110 TI_2 T<=95 TI_0
525 * IWL_TI_2 T >= 115 CT_KILL T<=100 TI_1
526 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
527 *=============================================================================
529 static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp)
531 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
533 bool changed = false;
534 enum iwl_tt_state old_state;
535 struct iwl_tt_trans *transaction;
537 old_state = tt->state;
538 for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
539 /* based on the current TT state,
540 * find the curresponding transaction table
541 * each table has (IWL_TI_STATE_MAX - 1) entries
542 * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
543 * will advance to the correct table.
544 * then based on the current temperature
545 * find the next state need to transaction to
546 * go through all the possible (IWL_TI_STATE_MAX - 1) entries
547 * in the current table to see if transaction is needed
549 transaction = tt->transaction +
550 ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
551 if (temp >= transaction->tt_low &&
552 temp <= transaction->tt_high) {
553 #ifdef CONFIG_IWLWIFI_DEBUG
554 if ((tt->tt_previous_temp) &&
555 (temp > tt->tt_previous_temp) &&
556 ((temp - tt->tt_previous_temp) >
557 IWL_TT_INCREASE_MARGIN)) {
558 IWL_DEBUG_POWER(priv,
559 "Temperature increase %d "
561 (temp - tt->tt_previous_temp));
563 tt->tt_previous_temp = temp;
566 transaction->next_state) {
569 transaction->next_state;
575 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
576 struct iwl_power_mgr *setting = &priv->power_data;
578 if (tt->state >= IWL_TI_1) {
579 /* if switching from IWL_TI_0 to other TT state
580 * save previous power setting in tt->sys_power_mode */
581 if (old_state == IWL_TI_0)
582 tt->sys_power_mode = setting->power_mode;
583 /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
584 tt->tt_power_mode = IWL_POWER_INDEX_5;
585 if (!iwl_ht_enabled(priv))
587 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
588 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
589 RXON_FLG_HT40_PROT_MSK |
590 RXON_FLG_HT_PROT_MSK);
592 /* check HT capability and set
593 * according to the system HT capability
594 * in case get disabled before */
595 iwl_set_rxon_ht(priv, &priv->current_ht_config);
599 /* restore system power setting */
600 /* the previous power mode was saved in
601 * tt->sys_power_mode when system move into
602 * Thermal Throttling state
603 * set power_data.user_power_setting to the previous
604 * system power mode to make sure power will get
607 priv->power_data.user_power_setting =
609 tt->tt_power_mode = tt->sys_power_mode;
610 /* check HT capability and set
611 * according to the system HT capability
612 * in case get disabled before */
613 iwl_set_rxon_ht(priv, &priv->current_ht_config);
615 mutex_lock(&priv->mutex);
616 if (iwl_power_update_mode(priv, true)) {
617 /* TT state not updated
618 * try again during next temperature read
620 IWL_ERR(priv, "Cannot update power mode, "
621 "TT state not updated\n");
622 tt->state = old_state;
624 IWL_DEBUG_POWER(priv,
625 "Thermal Throttling to new state: %u\n",
627 if (old_state != IWL_TI_CT_KILL &&
628 tt->state == IWL_TI_CT_KILL) {
629 IWL_DEBUG_POWER(priv, "Enter IWL_TI_CT_KILL\n");
630 iwl_perform_ct_kill_task(priv, true);
632 } else if (old_state == IWL_TI_CT_KILL &&
633 tt->state != IWL_TI_CT_KILL) {
634 IWL_DEBUG_POWER(priv, "Exit IWL_TI_CT_KILL\n");
635 iwl_perform_ct_kill_task(priv, false);
638 mutex_unlock(&priv->mutex);
642 /* Card State Notification indicated reach critical temperature
643 * if PSP not enable, no Thermal Throttling function will be performed
644 * just set the GP1 bit to acknowledge the event
645 * otherwise, go into IWL_TI_CT_KILL state
646 * since Card State Notification will not provide any temperature reading
648 * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
650 * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
652 static void iwl_bg_ct_enter(struct work_struct *work)
654 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
655 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
657 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
660 if (!iwl_is_ready(priv))
663 if (tt->state != IWL_TI_CT_KILL) {
664 IWL_ERR(priv, "Device reached critical temperature "
665 "- ucode going to sleep!\n");
666 if (!priv->power_data.adv_tt)
667 iwl_legacy_tt_handler(priv,
668 IWL_MINIMAL_POWER_THRESHOLD);
670 iwl_advance_tt_handler(priv,
671 CT_KILL_THRESHOLD + 1);
675 /* Card State Notification indicated out of critical temperature
676 * since Card State Notification will not provide any temperature reading
677 * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
678 * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
680 static void iwl_bg_ct_exit(struct work_struct *work)
682 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
683 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
685 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
688 if (!iwl_is_ready(priv))
691 /* stop ct_kill_exit_tm timer */
692 del_timer_sync(&priv->power_data.ct_kill_exit_tm);
694 if (tt->state == IWL_TI_CT_KILL) {
696 "Device temperature below critical"
698 if (!priv->power_data.adv_tt)
699 iwl_legacy_tt_handler(priv,
700 IWL_REDUCED_PERFORMANCE_THRESHOLD_2);
702 iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD);
706 void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
708 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
711 IWL_DEBUG_POWER(priv, "Queueing critical temperature enter.\n");
712 queue_work(priv->workqueue, &priv->ct_enter);
714 EXPORT_SYMBOL(iwl_tt_enter_ct_kill);
716 void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
718 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
721 IWL_DEBUG_POWER(priv, "Queueing critical temperature exit.\n");
722 queue_work(priv->workqueue, &priv->ct_exit);
724 EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
726 static void iwl_bg_tt_work(struct work_struct *work)
728 struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
729 s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
731 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
734 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
735 temp = KELVIN_TO_CELSIUS(priv->temperature);
737 if (!priv->power_data.adv_tt)
738 iwl_legacy_tt_handler(priv, temp);
740 iwl_advance_tt_handler(priv, temp);
743 void iwl_tt_handler(struct iwl_priv *priv)
745 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
748 IWL_DEBUG_POWER(priv, "Queueing thermal throttling work.\n");
749 queue_work(priv->workqueue, &priv->tt_work);
751 EXPORT_SYMBOL(iwl_tt_handler);
753 /* Thermal throttling initialization
754 * For advance thermal throttling:
755 * Initialize Thermal Index and temperature threshold table
756 * Initialize thermal throttling restriction table
758 void iwl_tt_initialize(struct iwl_priv *priv)
760 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
761 struct iwl_power_mgr *setting = &priv->power_data;
762 int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
763 struct iwl_tt_trans *transaction;
765 IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");
767 memset(tt, 0, sizeof(struct iwl_tt_mgmt));
769 tt->state = IWL_TI_0;
770 tt->sys_power_mode = setting->power_mode;
771 tt->tt_power_mode = tt->sys_power_mode;
772 init_timer(&priv->power_data.ct_kill_exit_tm);
773 priv->power_data.ct_kill_exit_tm.data = (unsigned long)priv;
774 priv->power_data.ct_kill_exit_tm.function = iwl_tt_check_exit_ct_kill;
776 /* setup deferred ct kill work */
777 INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
778 INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
779 INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
781 switch (priv->hw_rev & CSR_HW_REV_TYPE_MSK) {
782 case CSR_HW_REV_TYPE_6x00:
783 case CSR_HW_REV_TYPE_6x50:
784 IWL_DEBUG_POWER(priv, "Advanced Thermal Throttling\n");
785 tt->restriction = kzalloc(sizeof(struct iwl_tt_restriction) *
786 IWL_TI_STATE_MAX, GFP_KERNEL);
787 tt->transaction = kzalloc(sizeof(struct iwl_tt_trans) *
788 IWL_TI_STATE_MAX * (IWL_TI_STATE_MAX - 1),
790 if (!tt->restriction || !tt->transaction) {
791 IWL_ERR(priv, "Fallback to Legacy Throttling\n");
792 priv->power_data.adv_tt = false;
793 kfree(tt->restriction);
794 tt->restriction = NULL;
795 kfree(tt->transaction);
796 tt->transaction = NULL;
798 transaction = tt->transaction +
799 (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
800 memcpy(transaction, &tt_range_0[0], size);
801 transaction = tt->transaction +
802 (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
803 memcpy(transaction, &tt_range_1[0], size);
804 transaction = tt->transaction +
805 (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
806 memcpy(transaction, &tt_range_2[0], size);
807 transaction = tt->transaction +
808 (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
809 memcpy(transaction, &tt_range_3[0], size);
810 size = sizeof(struct iwl_tt_restriction) *
812 memcpy(tt->restriction,
813 &restriction_range[0], size);
814 priv->power_data.adv_tt = true;
818 IWL_DEBUG_POWER(priv, "Legacy Thermal Throttling\n");
819 priv->power_data.adv_tt = false;
823 EXPORT_SYMBOL(iwl_tt_initialize);
825 /* cleanup thermal throttling management related memory and timer */
826 void iwl_tt_exit(struct iwl_priv *priv)
828 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
830 /* stop ct_kill_exit_tm timer if activated */
831 del_timer_sync(&priv->power_data.ct_kill_exit_tm);
832 cancel_work_sync(&priv->tt_work);
833 cancel_work_sync(&priv->ct_enter);
834 cancel_work_sync(&priv->ct_exit);
836 if (priv->power_data.adv_tt) {
837 /* free advance thermal throttling memory */
838 kfree(tt->restriction);
839 tt->restriction = NULL;
840 kfree(tt->transaction);
841 tt->transaction = NULL;
844 EXPORT_SYMBOL(iwl_tt_exit);
846 /* initialize to default */
847 void iwl_power_initialize(struct iwl_priv *priv)
849 iwl_power_init_handle(priv);
850 priv->power_data.user_power_setting = IWL_POWER_INDEX_1;
851 /* default to disabled until mac80211 says otherwise */
852 priv->power_data.power_disabled = 1;
854 EXPORT_SYMBOL(iwl_power_initialize);