blob: d7fdb5825450917c669f15d684aaad9515f32a24 [file] [log] [blame]
Mohamed Abbas5da4b552008-04-21 15:41:51 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
Mohamed Abbas5da4b552008-04-21 15:41:51 -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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Mohamed Abbas5da4b552008-04-21 15:41:51 -070026 * 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>
Mohamed Abbas5da4b552008-04-21 15:41:51 -070032#include <linux/init.h>
33
34#include <net/mac80211.h>
35
36#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070037#include "iwl-dev.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070038#include "iwl-core.h"
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -070039#include "iwl-io.h"
Tomas Winkler5a36ba02008-04-24 11:55:37 -070040#include "iwl-commands.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070041#include "iwl-debug.h"
42#include "iwl-power.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070043
44/*
Johannes Berg7af2c462009-05-08 13:44:38 -070045 * Setting power level allow the card to go to sleep when not busy.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070046 *
Johannes Berg7af2c462009-05-08 13:44:38 -070047 * 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.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070050 *
Johannes Berg7af2c462009-05-08 13:44:38 -070051 * 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.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070054 */
55
Mohamed Abbas5da4b552008-04-21 15:41:51 -070056#define IWL_POWER_RANGE_0_MAX (2)
57#define IWL_POWER_RANGE_1_MAX (10)
58
59
Johannes Berg7af2c462009-05-08 13:44:38 -070060#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)}
Mohamed Abbas5da4b552008-04-21 15:41:51 -070069/* default power management (not Tx power) table values */
Johannes Berg7af2c462009-05-08 13:44:38 -070070/* for DTIM period 0 through IWL_POWER_RANGE_0_MAX */
71static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -070072 {{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
Johannes Berg7af2c462009-05-08 13:44:38 -070081/* for DTIM period IWL_POWER_RANGE_0_MAX + 1 through IWL_POWER_RANGE_1_MAX */
82static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -070083 {{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
Johannes Berg7af2c462009-05-08 13:44:38 -070091/* for DTIM period > IWL_POWER_RANGE_1_MAX */
92static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -070093 {{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
Winkler, Tomasd25aabb2009-01-27 14:27:58 -0800101
Mohamed Abbasca579612008-07-18 13:52:57 +0800102/* set card power command */
103static int iwl_set_power(struct iwl_priv *priv, void *cmd)
104{
Johannes Bergf0f74a02009-05-08 13:44:37 -0700105 return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
106 sizeof(struct iwl_powertable_cmd), cmd);
Mohamed Abbasca579612008-07-18 13:52:57 +0800107}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700108
109/* initialize to default */
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800110static void iwl_power_init_handle(struct iwl_priv *priv)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700111{
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700112 struct iwl_power_mgr *pow_data;
Johannes Berg7af2c462009-05-08 13:44:38 -0700113 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_NUM;
Tomas Winklere7b63582008-09-03 11:26:49 +0800114 struct iwl_powertable_cmd *cmd;
115 int i;
Tomas Winkler3fdb68d2009-02-10 15:19:02 -0800116 u16 lctl;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700117
Tomas Winklere1623442009-01-27 14:27:56 -0800118 IWL_DEBUG_POWER(priv, "Initialize power \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700119
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800120 pow_data = &priv->power_data;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700121
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
Tomas Winkler3fdb68d2009-02-10 15:19:02 -0800128 lctl = iwl_pcie_link_ctl(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700129
Tomas Winklere1623442009-01-27 14:27:56 -0800130 IWL_DEBUG_POWER(priv, "adjust power command flags\n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700131
Johannes Berg7af2c462009-05-08 13:44:38 -0700132 for (i = 0; i < IWL_POWER_NUM; i++) {
Tomas Winklere7b63582008-09-03 11:26:49 +0800133 cmd = &pow_data->pwr_range_0[i].cmd;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700134
Tomas Winkler3fdb68d2009-02-10 15:19:02 -0800135 if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
Tomas Winklere7b63582008-09-03 11:26:49 +0800136 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
137 else
138 cmd->flags |= IWL_POWER_PCI_PM_MSK;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700139 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700140}
141
Tomas Winklera96a27f2008-10-23 23:48:56 -0700142/* adjust power command according to DTIM period and power level*/
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800143static int iwl_update_power_cmd(struct iwl_priv *priv,
144 struct iwl_powertable_cmd *cmd, u16 mode)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700145{
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700146 struct iwl_power_vec_entry *range;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700147 struct iwl_power_mgr *pow_data;
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800148 int i;
149 u32 max_sleep = 0;
150 u8 period;
151 bool skip;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700152
153 if (mode > IWL_POWER_INDEX_5) {
Tomas Winklere1623442009-01-27 14:27:56 -0800154 IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800155 return -EINVAL;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700156 }
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800157
158 pow_data = &priv->power_data;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700159
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;
Mohamed Abbasca579612008-07-18 13:52:57 +0800168 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700169
170 if (period == 0) {
171 period = 1;
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800172 skip = false;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700173 } else {
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800174 skip = !!range[mode].no_dtim;
175 }
176
177 if (skip) {
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700178 __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;
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800185 } else {
186 max_sleep = period;
187 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700188 }
189
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800190 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700191 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
192 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700193
Tomas Winklere1623442009-01-27 14:27:56 -0800194 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",
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700198 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
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800204 return 0;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700205}
206
207
208/*
Emmanuel Grumbacha33c2f42008-09-03 11:26:56 +0800209 * compute the final power mode index
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700210 */
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800211int iwl_power_update_mode(struct iwl_priv *priv, bool force)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700212{
213 struct iwl_power_mgr *setting = &(priv->power_data);
214 int ret = 0;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700215 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700216 u16 uninitialized_var(final_mode);
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800217 bool update_chains;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700218
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800219 /* Don't update the RX chain when chain noise calibration is running */
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800220 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
221 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800222
Johannes Berg7af2c462009-05-08 13:44:38 -0700223 final_mode = priv->power_data.user_power_setting;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700224
Johannes Berg7af2c462009-05-08 13:44:38 -0700225 if (setting->power_disabled)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700226 final_mode = IWL_POWER_MODE_CAM;
227
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700228 if (tt->state >= IWL_TI_1) {
229 /* TT power setting overwrite user & system power setting */
230 final_mode = tt->tt_power_mode;
231 }
Johannes Berg7af2c462009-05-08 13:44:38 -0700232 if (iwl_is_ready_rf(priv) &&
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800233 ((setting->power_mode != final_mode) || force)) {
Mohamed Abbasca579612008-07-18 13:52:57 +0800234 struct iwl_powertable_cmd cmd;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700235
236 if (final_mode != IWL_POWER_MODE_CAM)
237 set_bit(STATUS_POWER_PMI, &priv->status);
238
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800239 iwl_update_power_cmd(priv, &cmd, final_mode);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700240 cmd.keep_alive_beacons = 0;
241
242 if (final_mode == IWL_POWER_INDEX_5)
243 cmd.flags |= IWL_POWER_FAST_PD;
244
Mohamed Abbasca579612008-07-18 13:52:57 +0800245 ret = iwl_set_power(priv, &cmd);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700246
247 if (final_mode == IWL_POWER_MODE_CAM)
248 clear_bit(STATUS_POWER_PMI, &priv->status);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700249
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800250 if (priv->cfg->ops->lib->update_chain_flags && update_chains)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700251 priv->cfg->ops->lib->update_chain_flags(priv);
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800252 else
Tomas Winklere1623442009-01-27 14:27:56 -0800253 IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800254 "calibration running: %d\n",
255 priv->chain_noise_data.state);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700256 if (!ret)
257 setting->power_mode = final_mode;
258 }
259
260 return ret;
261}
262EXPORT_SYMBOL(iwl_power_update_mode);
263
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700264/* set user_power_setting */
265int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
266{
Johannes Berg7af2c462009-05-08 13:44:38 -0700267 if (mode >= IWL_POWER_NUM)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700268 return -EINVAL;
269
270 priv->power_data.user_power_setting = mode;
271
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800272 return iwl_power_update_mode(priv, 0);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700273}
274EXPORT_SYMBOL(iwl_power_set_user_mode);
275
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700276#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 */
286static 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
319static 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 */
350static 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 */
437void 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}
450EXPORT_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 */
457void 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}
475EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
476
477void 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}
489EXPORT_SYMBOL(iwl_tt_handler);
490
491/* Thermal throttling initialization
492 */
493void 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}
509EXPORT_SYMBOL(iwl_tt_initialize);
510
511/* cleanup thermal throttling management related memory and timer */
512void 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}
517EXPORT_SYMBOL(iwl_tt_exit);
518
Tomas Winklera96a27f2008-10-23 23:48:56 -0700519/* initialize to default */
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700520void iwl_power_initialize(struct iwl_priv *priv)
521{
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700522 iwl_power_init_handle(priv);
Johannes Berg7af2c462009-05-08 13:44:38 -0700523 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;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700526}
527EXPORT_SYMBOL(iwl_power_initialize);