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}
102 /* set card power command */
103 static int iwl_set_power(struct iwl_priv *priv, void *cmd)
105 return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
106 sizeof(struct iwl_powertable_cmd), cmd);
109 /* initialize to default */
110 static void iwl_power_init_handle(struct iwl_priv *priv)
112 struct iwl_power_mgr *pow_data;
113 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_NUM;
114 struct iwl_powertable_cmd *cmd;
118 IWL_DEBUG_POWER(priv, "Initialize power \n");
120 pow_data = &priv->power_data;
122 memset(pow_data, 0, sizeof(*pow_data));
124 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
125 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
126 memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
128 lctl = iwl_pcie_link_ctl(priv);
130 IWL_DEBUG_POWER(priv, "adjust power command flags\n");
132 for (i = 0; i < IWL_POWER_NUM; i++) {
133 cmd = &pow_data->pwr_range_0[i].cmd;
135 if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
136 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
138 cmd->flags |= IWL_POWER_PCI_PM_MSK;
142 /* adjust power command according to DTIM period and power level*/
143 static int iwl_update_power_cmd(struct iwl_priv *priv,
144 struct iwl_powertable_cmd *cmd, u16 mode)
146 struct iwl_power_vec_entry *range;
147 struct iwl_power_mgr *pow_data;
153 if (mode > IWL_POWER_INDEX_5) {
154 IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
158 pow_data = &priv->power_data;
160 if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
161 range = &pow_data->pwr_range_0[0];
162 else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
163 range = &pow_data->pwr_range_1[0];
165 range = &pow_data->pwr_range_2[0];
167 period = pow_data->dtim_period;
168 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
174 skip = !!range[mode].no_dtim;
178 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
179 max_sleep = le32_to_cpu(slp_itrvl);
180 if (max_sleep == 0xFF)
181 max_sleep = period * (skip + 1);
182 else if (max_sleep > period)
183 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
184 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
187 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
190 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
191 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
192 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
194 IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
195 IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
196 IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
197 IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
198 le32_to_cpu(cmd->sleep_interval[0]),
199 le32_to_cpu(cmd->sleep_interval[1]),
200 le32_to_cpu(cmd->sleep_interval[2]),
201 le32_to_cpu(cmd->sleep_interval[3]),
202 le32_to_cpu(cmd->sleep_interval[4]));
209 * compute the final power mode index
211 int iwl_power_update_mode(struct iwl_priv *priv, bool force)
213 struct iwl_power_mgr *setting = &(priv->power_data);
215 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
216 u16 uninitialized_var(final_mode);
219 /* Don't update the RX chain when chain noise calibration is running */
220 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
221 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
223 final_mode = priv->power_data.user_power_setting;
225 if (setting->power_disabled)
226 final_mode = IWL_POWER_MODE_CAM;
228 if (tt->state >= IWL_TI_1) {
229 /* TT power setting overwrite user & system power setting */
230 final_mode = tt->tt_power_mode;
232 if (iwl_is_ready_rf(priv) &&
233 ((setting->power_mode != final_mode) || force)) {
234 struct iwl_powertable_cmd cmd;
236 if (final_mode != IWL_POWER_MODE_CAM)
237 set_bit(STATUS_POWER_PMI, &priv->status);
239 iwl_update_power_cmd(priv, &cmd, final_mode);
240 cmd.keep_alive_beacons = 0;
242 if (final_mode == IWL_POWER_INDEX_5)
243 cmd.flags |= IWL_POWER_FAST_PD;
245 ret = iwl_set_power(priv, &cmd);
247 if (final_mode == IWL_POWER_MODE_CAM)
248 clear_bit(STATUS_POWER_PMI, &priv->status);
250 if (priv->cfg->ops->lib->update_chain_flags && update_chains)
251 priv->cfg->ops->lib->update_chain_flags(priv);
253 IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
254 "calibration running: %d\n",
255 priv->chain_noise_data.state);
257 setting->power_mode = final_mode;
262 EXPORT_SYMBOL(iwl_power_update_mode);
264 /* set user_power_setting */
265 int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
267 if (mode >= IWL_POWER_NUM)
270 priv->power_data.user_power_setting = mode;
272 return iwl_power_update_mode(priv, 0);
274 EXPORT_SYMBOL(iwl_power_set_user_mode);
276 #define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
279 * toggle the bit to wake up uCode and check the temperature
280 * if the temperature is below CT, uCode will stay awake and send card
281 * state notification with CT_KILL bit clear to inform Thermal Throttling
282 * Management to change state. Otherwise, uCode will go back to sleep
283 * without doing anything, driver should continue the 5 seconds timer
284 * to wake up uCode for temperature check until temperature drop below CT
286 static void iwl_tt_check_exit_ct_kill(unsigned long data)
288 struct iwl_priv *priv = (struct iwl_priv *)data;
289 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
292 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
295 if (tt->state == IWL_TI_CT_KILL) {
296 if (priv->power_data.ct_kill_toggle) {
297 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
298 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
299 priv->power_data.ct_kill_toggle = false;
301 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
302 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
303 priv->power_data.ct_kill_toggle = true;
305 iwl_read32(priv, CSR_UCODE_DRV_GP1);
306 spin_lock_irqsave(&priv->reg_lock, flags);
307 if (!iwl_grab_nic_access(priv))
308 iwl_release_nic_access(priv);
309 spin_unlock_irqrestore(&priv->reg_lock, flags);
311 /* Reschedule the ct_kill timer to occur in
312 * CT_KILL_EXIT_DURATION seconds to ensure we get a
314 mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
315 CT_KILL_EXIT_DURATION * HZ);
319 static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
323 IWL_DEBUG_POWER(priv, "Stop all queues\n");
324 if (priv->mac80211_registered)
325 ieee80211_stop_queues(priv->hw);
326 IWL_DEBUG_POWER(priv,
327 "Schedule 5 seconds CT_KILL Timer\n");
328 mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
329 CT_KILL_EXIT_DURATION * HZ);
331 IWL_DEBUG_POWER(priv, "Wake all queues\n");
332 if (priv->mac80211_registered)
333 ieee80211_wake_queues(priv->hw);
337 #define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
338 #define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
339 #define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
342 * Legacy thermal throttling
343 * 1) Avoid NIC destruction due to high temperatures
344 * Chip will identify dangerously high temperatures that can
345 * harm the device and will power down
346 * 2) Avoid the NIC power down due to high temperature
347 * Throttle early enough to lower the power consumption before
348 * drastic steps are needed
350 static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp)
352 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
353 enum iwl_tt_state new_state;
354 struct iwl_power_mgr *setting = &priv->power_data;
356 #ifdef CONFIG_IWLWIFI_DEBUG
357 if ((tt->tt_previous_temp) &&
358 (temp > tt->tt_previous_temp) &&
359 ((temp - tt->tt_previous_temp) >
360 IWL_TT_INCREASE_MARGIN)) {
361 IWL_DEBUG_POWER(priv,
362 "Temperature increase %d degree Celsius\n",
363 (temp - tt->tt_previous_temp));
367 if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
368 new_state = IWL_TI_CT_KILL;
369 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
370 new_state = IWL_TI_2;
371 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
372 new_state = IWL_TI_1;
374 new_state = IWL_TI_0;
376 #ifdef CONFIG_IWLWIFI_DEBUG
377 tt->tt_previous_temp = temp;
379 if (tt->state != new_state) {
380 if (tt->state == IWL_TI_0) {
381 tt->sys_power_mode = setting->power_mode;
382 IWL_DEBUG_POWER(priv, "current power mode: %u\n",
383 setting->power_mode);
387 /* when system ready to go back to IWL_TI_0 state
388 * using system power mode instead of TT power mode
389 * revert back to the orginal power mode which was saved
390 * before enter Thermal Throttling state
391 * update priv->power_data.user_power_setting to the
392 * required power mode to make sure
393 * iwl_power_update_mode() will update power correctly.
395 priv->power_data.user_power_setting =
397 tt->tt_power_mode = tt->sys_power_mode;
400 tt->tt_power_mode = IWL_POWER_INDEX_3;
403 tt->tt_power_mode = IWL_POWER_INDEX_4;
406 tt->tt_power_mode = IWL_POWER_INDEX_5;
409 if (iwl_power_update_mode(priv, true)) {
410 /* TT state not updated
411 * try again during next temperature read
413 IWL_ERR(priv, "Cannot update power mode, "
414 "TT state not updated\n");
416 if (new_state == IWL_TI_CT_KILL)
417 iwl_perform_ct_kill_task(priv, true);
418 else if (tt->state == IWL_TI_CT_KILL &&
419 new_state != IWL_TI_CT_KILL)
420 iwl_perform_ct_kill_task(priv, false);
421 tt->state = new_state;
422 IWL_DEBUG_POWER(priv, "Temperature state changed %u\n",
424 IWL_DEBUG_POWER(priv, "Power Index change to %u\n",
430 /* Card State Notification indicated reach critical temperature
431 * if PSP not enable, no Thermal Throttling function will be performed
432 * just set the GP1 bit to acknowledge the event
433 * otherwise, go into IWL_TI_CT_KILL state
434 * since Card State Notification will not provide any temperature reading
435 * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
437 void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
439 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
441 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
444 if (tt->state != IWL_TI_CT_KILL) {
445 IWL_ERR(priv, "Device reached critical temperature "
446 "- ucode going to sleep!\n");
447 iwl_legacy_tt_handler(priv, IWL_MINIMAL_POWER_THRESHOLD);
450 EXPORT_SYMBOL(iwl_tt_enter_ct_kill);
452 /* Card State Notification indicated out of critical temperature
453 * since Card State Notification will not provide any temperature reading
454 * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
455 * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
457 void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
459 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
461 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
464 /* stop ct_kill_exit_tm timer */
465 del_timer_sync(&priv->power_data.ct_kill_exit_tm);
467 if (tt->state == IWL_TI_CT_KILL) {
469 "Device temperature below critical"
471 iwl_legacy_tt_handler(priv,
472 IWL_REDUCED_PERFORMANCE_THRESHOLD_2);
475 EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
477 void iwl_tt_handler(struct iwl_priv *priv)
479 s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
481 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
484 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
485 temp = KELVIN_TO_CELSIUS(priv->temperature);
487 iwl_legacy_tt_handler(priv, temp);
489 EXPORT_SYMBOL(iwl_tt_handler);
491 /* Thermal throttling initialization
493 void iwl_tt_initialize(struct iwl_priv *priv)
495 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
496 struct iwl_power_mgr *setting = &priv->power_data;
498 IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");
500 memset(tt, 0, sizeof(struct iwl_tt_mgmt));
502 tt->state = IWL_TI_0;
503 tt->sys_power_mode = setting->power_mode;
504 tt->tt_power_mode = tt->sys_power_mode;
505 init_timer(&priv->power_data.ct_kill_exit_tm);
506 priv->power_data.ct_kill_exit_tm.data = (unsigned long)priv;
507 priv->power_data.ct_kill_exit_tm.function = iwl_tt_check_exit_ct_kill;
509 EXPORT_SYMBOL(iwl_tt_initialize);
511 /* cleanup thermal throttling management related memory and timer */
512 void iwl_tt_exit(struct iwl_priv *priv)
514 /* stop ct_kill_exit_tm timer if activated */
515 del_timer_sync(&priv->power_data.ct_kill_exit_tm);
517 EXPORT_SYMBOL(iwl_tt_exit);
519 /* initialize to default */
520 void iwl_power_initialize(struct iwl_priv *priv)
522 iwl_power_init_handle(priv);
523 priv->power_data.user_power_setting = IWL_POWER_INDEX_1;
524 /* default to disabled until mac80211 says otherwise */
525 priv->power_data.power_disabled = 1;
527 EXPORT_SYMBOL(iwl_power_initialize);