iwlwifi: Thermal Throttling Management - Part 1
[linux-2.6.git] / drivers / net / wireless / iwlwifi / iwl-power.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
4  *
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/init.h>
33
34 #include <net/mac80211.h>
35
36 #include "iwl-eeprom.h"
37 #include "iwl-dev.h"
38 #include "iwl-core.h"
39 #include "iwl-io.h"
40 #include "iwl-commands.h"
41 #include "iwl-debug.h"
42 #include "iwl-power.h"
43
44 /*
45  * Setting power level allow the card to go to sleep when not busy.
46  *
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.
50  *
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.
54  */
55
56 #define IWL_POWER_RANGE_0_MAX  (2)
57 #define IWL_POWER_RANGE_1_MAX  (10)
58
59
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), \
65                                      cpu_to_le32(X1), \
66                                      cpu_to_le32(X2), \
67                                      cpu_to_le32(X3), \
68                                      cpu_to_le32(X4)}
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}
78 };
79
80
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}
89 };
90
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}
99 };
100
101
102 /* set card power command */
103 static int iwl_set_power(struct iwl_priv *priv, void *cmd)
104 {
105         return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
106                                 sizeof(struct iwl_powertable_cmd), cmd);
107 }
108
109 /* initialize to default */
110 static void iwl_power_init_handle(struct iwl_priv *priv)
111 {
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;
115         int i;
116         u16 lctl;
117
118         IWL_DEBUG_POWER(priv, "Initialize power \n");
119
120         pow_data = &priv->power_data;
121
122         memset(pow_data, 0, sizeof(*pow_data));
123
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);
127
128         lctl = iwl_pcie_link_ctl(priv);
129
130         IWL_DEBUG_POWER(priv, "adjust power command flags\n");
131
132         for (i = 0; i < IWL_POWER_NUM; i++) {
133                 cmd = &pow_data->pwr_range_0[i].cmd;
134
135                 if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
136                         cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
137                 else
138                         cmd->flags |= IWL_POWER_PCI_PM_MSK;
139         }
140 }
141
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)
145 {
146         struct iwl_power_vec_entry *range;
147         struct iwl_power_mgr *pow_data;
148         int i;
149         u32 max_sleep = 0;
150         u8 period;
151         bool skip;
152
153         if (mode > IWL_POWER_INDEX_5) {
154                 IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
155                 return -EINVAL;
156         }
157
158         pow_data = &priv->power_data;
159
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];
164         else
165                 range = &pow_data->pwr_range_2[0];
166
167         period = pow_data->dtim_period;
168         memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
169
170         if (period == 0) {
171                 period = 1;
172                 skip = false;
173         } else {
174                 skip = !!range[mode].no_dtim;
175         }
176
177         if (skip) {
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;
185         } else {
186                 max_sleep = period;
187                 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
188         }
189
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);
193
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]));
203
204         return 0;
205 }
206
207
208 /*
209  * compute the final power mode index
210  */
211 int iwl_power_update_mode(struct iwl_priv *priv, bool force)
212 {
213         struct iwl_power_mgr *setting = &(priv->power_data);
214         int ret = 0;
215         struct iwl_tt_mgmt *tt = &priv->power_data.tt;
216         u16 uninitialized_var(final_mode);
217         bool update_chains;
218
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;
222
223         final_mode = priv->power_data.user_power_setting;
224
225         if (setting->power_disabled)
226                 final_mode = IWL_POWER_MODE_CAM;
227
228         if (tt->state >= IWL_TI_1) {
229                 /* TT power setting overwrite user & system power setting */
230                 final_mode = tt->tt_power_mode;
231         }
232         if (iwl_is_ready_rf(priv) &&
233             ((setting->power_mode != final_mode) || force)) {
234                 struct iwl_powertable_cmd cmd;
235
236                 if (final_mode != IWL_POWER_MODE_CAM)
237                         set_bit(STATUS_POWER_PMI, &priv->status);
238
239                 iwl_update_power_cmd(priv, &cmd, final_mode);
240                 cmd.keep_alive_beacons = 0;
241
242                 if (final_mode == IWL_POWER_INDEX_5)
243                         cmd.flags |= IWL_POWER_FAST_PD;
244
245                 ret = iwl_set_power(priv, &cmd);
246
247                 if (final_mode == IWL_POWER_MODE_CAM)
248                         clear_bit(STATUS_POWER_PMI, &priv->status);
249
250                 if (priv->cfg->ops->lib->update_chain_flags && update_chains)
251                         priv->cfg->ops->lib->update_chain_flags(priv);
252                 else
253                         IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
254                                         "calibration running: %d\n",
255                                         priv->chain_noise_data.state);
256                 if (!ret)
257                         setting->power_mode = final_mode;
258         }
259
260         return ret;
261 }
262 EXPORT_SYMBOL(iwl_power_update_mode);
263
264 /* set user_power_setting */
265 int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
266 {
267         if (mode >= IWL_POWER_NUM)
268                 return -EINVAL;
269
270         priv->power_data.user_power_setting = mode;
271
272         return iwl_power_update_mode(priv, 0);
273 }
274 EXPORT_SYMBOL(iwl_power_set_user_mode);
275
276 #define CT_KILL_EXIT_DURATION (5)       /* 5 seconds duration */
277
278 /*
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
285  */
286 static void iwl_tt_check_exit_ct_kill(unsigned long data)
287 {
288         struct iwl_priv *priv = (struct iwl_priv *)data;
289         struct iwl_tt_mgmt *tt = &priv->power_data.tt;
290         unsigned long flags;
291
292         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
293                 return;
294
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;
300                 } else {
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;
304                 }
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);
310
311                 /* Reschedule the ct_kill timer to occur in
312                  * CT_KILL_EXIT_DURATION seconds to ensure we get a
313                  * thermal update */
314                 mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
315                           CT_KILL_EXIT_DURATION * HZ);
316         }
317 }
318
319 static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
320                            bool stop)
321 {
322         if (stop) {
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);
330         } else {
331                 IWL_DEBUG_POWER(priv, "Wake all queues\n");
332                 if (priv->mac80211_registered)
333                         ieee80211_wake_queues(priv->hw);
334         }
335 }
336
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)
340
341 /*
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
349  */
350 static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp)
351 {
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;
355
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));
364         }
365 #endif
366         /* in Celsius */
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;
373         else
374                 new_state = IWL_TI_0;
375
376 #ifdef CONFIG_IWLWIFI_DEBUG
377         tt->tt_previous_temp = temp;
378 #endif
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);
384                 }
385                 switch (new_state) {
386                 case IWL_TI_0:
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.
394                          */
395                         priv->power_data.user_power_setting =
396                                 tt->sys_power_mode;
397                         tt->tt_power_mode = tt->sys_power_mode;
398                         break;
399                 case IWL_TI_1:
400                         tt->tt_power_mode = IWL_POWER_INDEX_3;
401                         break;
402                 case IWL_TI_2:
403                         tt->tt_power_mode = IWL_POWER_INDEX_4;
404                         break;
405                 default:
406                         tt->tt_power_mode = IWL_POWER_INDEX_5;
407                         break;
408                 }
409                 if (iwl_power_update_mode(priv, true)) {
410                         /* TT state not updated
411                          * try again during next temperature read
412                          */
413                         IWL_ERR(priv, "Cannot update power mode, "
414                                         "TT state not updated\n");
415                 } else {
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",
423                                         tt->state);
424                         IWL_DEBUG_POWER(priv, "Power Index change to %u\n",
425                                         tt->tt_power_mode);
426                 }
427         }
428 }
429
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()
436  */
437 void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
438 {
439         struct iwl_tt_mgmt *tt = &priv->power_data.tt;
440
441         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
442                 return;
443
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);
448         }
449 }
450 EXPORT_SYMBOL(iwl_tt_enter_ct_kill);
451
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
456  */
457 void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
458 {
459         struct iwl_tt_mgmt *tt = &priv->power_data.tt;
460
461         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
462                 return;
463
464         /* stop ct_kill_exit_tm timer */
465         del_timer_sync(&priv->power_data.ct_kill_exit_tm);
466
467         if (tt->state == IWL_TI_CT_KILL) {
468                 IWL_ERR(priv,
469                         "Device temperature below critical"
470                         "- ucode awake!\n");
471                 iwl_legacy_tt_handler(priv,
472                         IWL_REDUCED_PERFORMANCE_THRESHOLD_2);
473         }
474 }
475 EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
476
477 void iwl_tt_handler(struct iwl_priv *priv)
478 {
479         s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
480
481         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
482                 return;
483
484         if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
485                 temp = KELVIN_TO_CELSIUS(priv->temperature);
486
487         iwl_legacy_tt_handler(priv, temp);
488 }
489 EXPORT_SYMBOL(iwl_tt_handler);
490
491 /* Thermal throttling initialization
492  */
493 void iwl_tt_initialize(struct iwl_priv *priv)
494 {
495         struct iwl_tt_mgmt *tt = &priv->power_data.tt;
496         struct iwl_power_mgr *setting = &priv->power_data;
497
498         IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");
499
500         memset(tt, 0, sizeof(struct iwl_tt_mgmt));
501
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;
508 }
509 EXPORT_SYMBOL(iwl_tt_initialize);
510
511 /* cleanup thermal throttling management related memory and timer */
512 void iwl_tt_exit(struct iwl_priv *priv)
513 {
514         /* stop ct_kill_exit_tm timer if activated */
515         del_timer_sync(&priv->power_data.ct_kill_exit_tm);
516 }
517 EXPORT_SYMBOL(iwl_tt_exit);
518
519 /* initialize to default */
520 void iwl_power_initialize(struct iwl_priv *priv)
521 {
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;
526 }
527 EXPORT_SYMBOL(iwl_power_initialize);