iwl-power.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <net/mac80211.h>
  32. #include "iwl-eeprom.h"
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-io.h"
  36. #include "iwl-commands.h"
  37. #include "iwl-debug.h"
  38. #include "iwl-power.h"
  39. /*
  40. * Setting power level allows the card to go to sleep when not busy.
  41. *
  42. * We calculate a sleep command based on the required latency, which
  43. * we get from mac80211. In order to handle thermal throttling, we can
  44. * also use pre-defined power levels.
  45. */
  46. /*
  47. * For now, keep using power level 1 instead of automatically
  48. * adjusting ...
  49. */
  50. bool no_sleep_autoadjust = true;
  51. module_param(no_sleep_autoadjust, bool, S_IRUGO);
  52. MODULE_PARM_DESC(no_sleep_autoadjust,
  53. "don't automatically adjust sleep level "
  54. "according to maximum network latency");
  55. /*
  56. * This defines the old power levels. They are still used by default
  57. * (level 1) and for thermal throttle (levels 3 through 5)
  58. */
  59. struct iwl_power_vec_entry {
  60. struct iwl_powertable_cmd cmd;
  61. u8 no_dtim;
  62. };
  63. #define IWL_DTIM_RANGE_0_MAX 2
  64. #define IWL_DTIM_RANGE_1_MAX 10
  65. #define NOSLP cpu_to_le16(0), 0, 0
  66. #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
  67. #define TU_TO_USEC 1024
  68. #define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
  69. #define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
  70. cpu_to_le32(X1), \
  71. cpu_to_le32(X2), \
  72. cpu_to_le32(X3), \
  73. cpu_to_le32(X4)}
  74. /* default power management (not Tx power) table values */
  75. /* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
  76. static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
  77. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
  78. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
  79. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
  80. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
  81. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
  82. };
  83. /* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
  84. static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
  85. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
  86. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
  87. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
  88. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
  89. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
  90. };
  91. /* for DTIM period > IWL_DTIM_RANGE_1_MAX */
  92. static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
  93. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
  94. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
  95. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  96. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  97. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
  98. };
  99. static void iwl_static_sleep_cmd(struct iwl_priv *priv,
  100. struct iwl_powertable_cmd *cmd,
  101. enum iwl_power_level lvl, int period)
  102. {
  103. const struct iwl_power_vec_entry *table;
  104. int max_sleep, i;
  105. bool skip;
  106. table = range_2;
  107. if (period < IWL_DTIM_RANGE_1_MAX)
  108. table = range_1;
  109. if (period < IWL_DTIM_RANGE_0_MAX)
  110. table = range_0;
  111. BUG_ON(lvl < 0 || lvl >= IWL_POWER_NUM);
  112. *cmd = table[lvl].cmd;
  113. if (period == 0) {
  114. skip = false;
  115. period = 1;
  116. } else {
  117. skip = !!table[lvl].no_dtim;
  118. }
  119. if (skip) {
  120. __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
  121. max_sleep = le32_to_cpu(slp_itrvl);
  122. if (max_sleep == 0xFF)
  123. max_sleep = period * (skip + 1);
  124. else if (max_sleep > period)
  125. max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
  126. cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
  127. } else {
  128. max_sleep = period;
  129. cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
  130. }
  131. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  132. if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
  133. cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
  134. if (priv->power_data.pci_pm)
  135. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  136. else
  137. cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
  138. IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
  139. }
  140. /* default Thermal Throttling transaction table
  141. * Current state | Throttling Down | Throttling Up
  142. *=============================================================================
  143. * Condition Nxt State Condition Nxt State Condition Nxt State
  144. *-----------------------------------------------------------------------------
  145. * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
  146. * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
  147. * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
  148. * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
  149. *=============================================================================
  150. */
  151. static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
  152. {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
  153. {IWL_TI_1, 105, CT_KILL_THRESHOLD - 1},
  154. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
  155. };
  156. static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
  157. {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
  158. {IWL_TI_2, 110, CT_KILL_THRESHOLD - 1},
  159. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
  160. };
  161. static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
  162. {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
  163. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX},
  164. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
  165. };
  166. static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
  167. {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
  168. {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
  169. {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
  170. };
  171. /* Advance Thermal Throttling default restriction table */
  172. static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
  173. {IWL_ANT_OK_MULTI, IWL_ANT_OK_MULTI, true },
  174. {IWL_ANT_OK_SINGLE, IWL_ANT_OK_MULTI, true },
  175. {IWL_ANT_OK_SINGLE, IWL_ANT_OK_SINGLE, false },
  176. {IWL_ANT_OK_NONE, IWL_ANT_OK_NONE, false }
  177. };
  178. static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
  179. struct iwl_powertable_cmd *cmd)
  180. {
  181. memset(cmd, 0, sizeof(*cmd));
  182. if (priv->power_data.pci_pm)
  183. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  184. IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
  185. }
  186. static void iwl_power_fill_sleep_cmd(struct iwl_priv *priv,
  187. struct iwl_powertable_cmd *cmd,
  188. int dynps_ms, int wakeup_period)
  189. {
  190. /*
  191. * These are the original power level 3 sleep successions. The
  192. * device may behave better with such succession and was also
  193. * only tested with that. Just like the original sleep commands,
  194. * also adjust the succession here to the wakeup_period below.
  195. * The ranges are the same as for the sleep commands, 0-2, 3-9
  196. * and >10, which is selected based on the DTIM interval for
  197. * the sleep index but here we use the wakeup period since that
  198. * is what we need to do for the latency requirements.
  199. */
  200. static const u8 slp_succ_r0[IWL_POWER_VEC_SIZE] = { 2, 2, 2, 2, 2 };
  201. static const u8 slp_succ_r1[IWL_POWER_VEC_SIZE] = { 2, 4, 6, 7, 9 };
  202. static const u8 slp_succ_r2[IWL_POWER_VEC_SIZE] = { 2, 7, 9, 9, 0xFF };
  203. const u8 *slp_succ = slp_succ_r0;
  204. int i;
  205. if (wakeup_period > IWL_DTIM_RANGE_0_MAX)
  206. slp_succ = slp_succ_r1;
  207. if (wakeup_period > IWL_DTIM_RANGE_1_MAX)
  208. slp_succ = slp_succ_r2;
  209. memset(cmd, 0, sizeof(*cmd));
  210. cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK |
  211. IWL_POWER_FAST_PD; /* no use seeing frames for others */
  212. if (priv->power_data.pci_pm)
  213. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  214. cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms);
  215. cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms);
  216. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  217. cmd->sleep_interval[i] =
  218. cpu_to_le32(min_t(int, slp_succ[i], wakeup_period));
  219. IWL_DEBUG_POWER(priv, "Automatic sleep command\n");
  220. }
  221. static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
  222. {
  223. IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
  224. IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
  225. IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  226. IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  227. IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  228. le32_to_cpu(cmd->sleep_interval[0]),
  229. le32_to_cpu(cmd->sleep_interval[1]),
  230. le32_to_cpu(cmd->sleep_interval[2]),
  231. le32_to_cpu(cmd->sleep_interval[3]),
  232. le32_to_cpu(cmd->sleep_interval[4]));
  233. return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
  234. sizeof(struct iwl_powertable_cmd), cmd);
  235. }
  236. int iwl_power_update_mode(struct iwl_priv *priv, bool force)
  237. {
  238. int ret = 0;
  239. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  240. bool enabled = (priv->iw_mode == NL80211_IFTYPE_STATION) &&
  241. (priv->hw->conf.flags & IEEE80211_CONF_PS);
  242. bool update_chains;
  243. struct iwl_powertable_cmd cmd;
  244. int dtimper;
  245. /* Don't update the RX chain when chain noise calibration is running */
  246. update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
  247. priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
  248. if (priv->vif)
  249. dtimper = priv->vif->bss_conf.dtim_period;
  250. else
  251. dtimper = 1;
  252. if (priv->cfg->broken_powersave)
  253. iwl_power_sleep_cam_cmd(priv, &cmd);
  254. else if (priv->cfg->supports_idle &&
  255. priv->hw->conf.flags & IEEE80211_CONF_IDLE)
  256. iwl_static_sleep_cmd(priv, &cmd, IWL_POWER_INDEX_5, 20);
  257. else if (tt->state >= IWL_TI_1)
  258. iwl_static_sleep_cmd(priv, &cmd, tt->tt_power_mode, dtimper);
  259. else if (!enabled)
  260. iwl_power_sleep_cam_cmd(priv, &cmd);
  261. else if (priv->power_data.debug_sleep_level_override >= 0)
  262. iwl_static_sleep_cmd(priv, &cmd,
  263. priv->power_data.debug_sleep_level_override,
  264. dtimper);
  265. else if (no_sleep_autoadjust)
  266. iwl_static_sleep_cmd(priv, &cmd, IWL_POWER_INDEX_1, dtimper);
  267. else
  268. iwl_power_fill_sleep_cmd(priv, &cmd,
  269. priv->hw->conf.dynamic_ps_timeout,
  270. priv->hw->conf.max_sleep_period);
  271. if (iwl_is_ready_rf(priv) &&
  272. (memcmp(&priv->power_data.sleep_cmd, &cmd, sizeof(cmd)) || force)) {
  273. if (cmd.flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
  274. set_bit(STATUS_POWER_PMI, &priv->status);
  275. ret = iwl_set_power(priv, &cmd);
  276. if (!ret) {
  277. if (!(cmd.flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
  278. clear_bit(STATUS_POWER_PMI, &priv->status);
  279. if (priv->cfg->ops->lib->update_chain_flags &&
  280. update_chains)
  281. priv->cfg->ops->lib->update_chain_flags(priv);
  282. else if (priv->cfg->ops->lib->update_chain_flags)
  283. IWL_DEBUG_POWER(priv,
  284. "Cannot update the power, chain noise "
  285. "calibration running: %d\n",
  286. priv->chain_noise_data.state);
  287. memcpy(&priv->power_data.sleep_cmd, &cmd, sizeof(cmd));
  288. } else
  289. IWL_ERR(priv, "set power fail, ret = %d", ret);
  290. }
  291. return ret;
  292. }
  293. EXPORT_SYMBOL(iwl_power_update_mode);
  294. bool iwl_ht_enabled(struct iwl_priv *priv)
  295. {
  296. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  297. struct iwl_tt_restriction *restriction;
  298. if (!priv->thermal_throttle.advanced_tt)
  299. return true;
  300. restriction = tt->restriction + tt->state;
  301. return restriction->is_ht;
  302. }
  303. EXPORT_SYMBOL(iwl_ht_enabled);
  304. bool iwl_within_ct_kill_margin(struct iwl_priv *priv)
  305. {
  306. s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
  307. bool within_margin = false;
  308. if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
  309. temp = KELVIN_TO_CELSIUS(priv->temperature);
  310. if (!priv->thermal_throttle.advanced_tt)
  311. within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
  312. CT_KILL_THRESHOLD_LEGACY) ? true : false;
  313. else
  314. within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
  315. CT_KILL_THRESHOLD) ? true : false;
  316. return within_margin;
  317. }
  318. enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
  319. {
  320. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  321. struct iwl_tt_restriction *restriction;
  322. if (!priv->thermal_throttle.advanced_tt)
  323. return IWL_ANT_OK_MULTI;
  324. restriction = tt->restriction + tt->state;
  325. return restriction->tx_stream;
  326. }
  327. EXPORT_SYMBOL(iwl_tx_ant_restriction);
  328. enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
  329. {
  330. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  331. struct iwl_tt_restriction *restriction;
  332. if (!priv->thermal_throttle.advanced_tt)
  333. return IWL_ANT_OK_MULTI;
  334. restriction = tt->restriction + tt->state;
  335. return restriction->rx_stream;
  336. }
  337. #define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
  338. #define CT_KILL_WAITING_DURATION (300) /* 300ms duration */
  339. /*
  340. * toggle the bit to wake up uCode and check the temperature
  341. * if the temperature is below CT, uCode will stay awake and send card
  342. * state notification with CT_KILL bit clear to inform Thermal Throttling
  343. * Management to change state. Otherwise, uCode will go back to sleep
  344. * without doing anything, driver should continue the 5 seconds timer
  345. * to wake up uCode for temperature check until temperature drop below CT
  346. */
  347. static void iwl_tt_check_exit_ct_kill(unsigned long data)
  348. {
  349. struct iwl_priv *priv = (struct iwl_priv *)data;
  350. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  351. unsigned long flags;
  352. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  353. return;
  354. if (tt->state == IWL_TI_CT_KILL) {
  355. if (priv->thermal_throttle.ct_kill_toggle) {
  356. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  357. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  358. priv->thermal_throttle.ct_kill_toggle = false;
  359. } else {
  360. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  361. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  362. priv->thermal_throttle.ct_kill_toggle = true;
  363. }
  364. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  365. spin_lock_irqsave(&priv->reg_lock, flags);
  366. if (!iwl_grab_nic_access(priv))
  367. iwl_release_nic_access(priv);
  368. spin_unlock_irqrestore(&priv->reg_lock, flags);
  369. /* Reschedule the ct_kill timer to occur in
  370. * CT_KILL_EXIT_DURATION seconds to ensure we get a
  371. * thermal update */
  372. IWL_DEBUG_POWER(priv, "schedule ct_kill exit timer\n");
  373. mod_timer(&priv->thermal_throttle.ct_kill_exit_tm, jiffies +
  374. CT_KILL_EXIT_DURATION * HZ);
  375. }
  376. }
  377. static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
  378. bool stop)
  379. {
  380. if (stop) {
  381. IWL_DEBUG_POWER(priv, "Stop all queues\n");
  382. if (priv->mac80211_registered)
  383. ieee80211_stop_queues(priv->hw);
  384. IWL_DEBUG_POWER(priv,
  385. "Schedule 5 seconds CT_KILL Timer\n");
  386. mod_timer(&priv->thermal_throttle.ct_kill_exit_tm, jiffies +
  387. CT_KILL_EXIT_DURATION * HZ);
  388. } else {
  389. IWL_DEBUG_POWER(priv, "Wake all queues\n");
  390. if (priv->mac80211_registered)
  391. ieee80211_wake_queues(priv->hw);
  392. }
  393. }
  394. static void iwl_tt_ready_for_ct_kill(unsigned long data)
  395. {
  396. struct iwl_priv *priv = (struct iwl_priv *)data;
  397. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  398. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  399. return;
  400. /* temperature timer expired, ready to go into CT_KILL state */
  401. if (tt->state != IWL_TI_CT_KILL) {
  402. IWL_DEBUG_POWER(priv, "entering CT_KILL state when temperature timer expired\n");
  403. tt->state = IWL_TI_CT_KILL;
  404. set_bit(STATUS_CT_KILL, &priv->status);
  405. iwl_perform_ct_kill_task(priv, true);
  406. }
  407. }
  408. static void iwl_prepare_ct_kill_task(struct iwl_priv *priv)
  409. {
  410. IWL_DEBUG_POWER(priv, "Prepare to enter IWL_TI_CT_KILL\n");
  411. /* make request to retrieve statistics information */
  412. iwl_send_statistics_request(priv, 0);
  413. /* Reschedule the ct_kill wait timer */
  414. mod_timer(&priv->thermal_throttle.ct_kill_waiting_tm,
  415. jiffies + msecs_to_jiffies(CT_KILL_WAITING_DURATION));
  416. }
  417. #define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
  418. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
  419. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
  420. /*
  421. * Legacy thermal throttling
  422. * 1) Avoid NIC destruction due to high temperatures
  423. * Chip will identify dangerously high temperatures that can
  424. * harm the device and will power down
  425. * 2) Avoid the NIC power down due to high temperature
  426. * Throttle early enough to lower the power consumption before
  427. * drastic steps are needed
  428. */
  429. static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
  430. {
  431. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  432. enum iwl_tt_state old_state;
  433. #ifdef CONFIG_IWLWIFI_DEBUG
  434. if ((tt->tt_previous_temp) &&
  435. (temp > tt->tt_previous_temp) &&
  436. ((temp - tt->tt_previous_temp) >
  437. IWL_TT_INCREASE_MARGIN)) {
  438. IWL_DEBUG_POWER(priv,
  439. "Temperature increase %d degree Celsius\n",
  440. (temp - tt->tt_previous_temp));
  441. }
  442. #endif
  443. old_state = tt->state;
  444. /* in Celsius */
  445. if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
  446. tt->state = IWL_TI_CT_KILL;
  447. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
  448. tt->state = IWL_TI_2;
  449. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
  450. tt->state = IWL_TI_1;
  451. else
  452. tt->state = IWL_TI_0;
  453. #ifdef CONFIG_IWLWIFI_DEBUG
  454. tt->tt_previous_temp = temp;
  455. #endif
  456. /* stop ct_kill_waiting_tm timer */
  457. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  458. if (tt->state != old_state) {
  459. switch (tt->state) {
  460. case IWL_TI_0:
  461. /*
  462. * When the system is ready to go back to IWL_TI_0
  463. * we only have to call iwl_power_update_mode() to
  464. * do so.
  465. */
  466. break;
  467. case IWL_TI_1:
  468. tt->tt_power_mode = IWL_POWER_INDEX_3;
  469. break;
  470. case IWL_TI_2:
  471. tt->tt_power_mode = IWL_POWER_INDEX_4;
  472. break;
  473. default:
  474. tt->tt_power_mode = IWL_POWER_INDEX_5;
  475. break;
  476. }
  477. mutex_lock(&priv->mutex);
  478. if (old_state == IWL_TI_CT_KILL)
  479. clear_bit(STATUS_CT_KILL, &priv->status);
  480. if (tt->state != IWL_TI_CT_KILL &&
  481. iwl_power_update_mode(priv, true)) {
  482. /* TT state not updated
  483. * try again during next temperature read
  484. */
  485. if (old_state == IWL_TI_CT_KILL)
  486. set_bit(STATUS_CT_KILL, &priv->status);
  487. tt->state = old_state;
  488. IWL_ERR(priv, "Cannot update power mode, "
  489. "TT state not updated\n");
  490. } else {
  491. if (tt->state == IWL_TI_CT_KILL) {
  492. if (force) {
  493. set_bit(STATUS_CT_KILL, &priv->status);
  494. iwl_perform_ct_kill_task(priv, true);
  495. } else {
  496. iwl_prepare_ct_kill_task(priv);
  497. tt->state = old_state;
  498. }
  499. } else if (old_state == IWL_TI_CT_KILL &&
  500. tt->state != IWL_TI_CT_KILL)
  501. iwl_perform_ct_kill_task(priv, false);
  502. IWL_DEBUG_POWER(priv, "Temperature state changed %u\n",
  503. tt->state);
  504. IWL_DEBUG_POWER(priv, "Power Index change to %u\n",
  505. tt->tt_power_mode);
  506. }
  507. mutex_unlock(&priv->mutex);
  508. }
  509. }
  510. /*
  511. * Advance thermal throttling
  512. * 1) Avoid NIC destruction due to high temperatures
  513. * Chip will identify dangerously high temperatures that can
  514. * harm the device and will power down
  515. * 2) Avoid the NIC power down due to high temperature
  516. * Throttle early enough to lower the power consumption before
  517. * drastic steps are needed
  518. * Actions include relaxing the power down sleep thresholds and
  519. * decreasing the number of TX streams
  520. * 3) Avoid throughput performance impact as much as possible
  521. *
  522. *=============================================================================
  523. * Condition Nxt State Condition Nxt State Condition Nxt State
  524. *-----------------------------------------------------------------------------
  525. * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
  526. * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
  527. * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
  528. * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
  529. *=============================================================================
  530. */
  531. static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
  532. {
  533. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  534. int i;
  535. bool changed = false;
  536. enum iwl_tt_state old_state;
  537. struct iwl_tt_trans *transaction;
  538. old_state = tt->state;
  539. for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
  540. /* based on the current TT state,
  541. * find the curresponding transaction table
  542. * each table has (IWL_TI_STATE_MAX - 1) entries
  543. * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
  544. * will advance to the correct table.
  545. * then based on the current temperature
  546. * find the next state need to transaction to
  547. * go through all the possible (IWL_TI_STATE_MAX - 1) entries
  548. * in the current table to see if transaction is needed
  549. */
  550. transaction = tt->transaction +
  551. ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
  552. if (temp >= transaction->tt_low &&
  553. temp <= transaction->tt_high) {
  554. #ifdef CONFIG_IWLWIFI_DEBUG
  555. if ((tt->tt_previous_temp) &&
  556. (temp > tt->tt_previous_temp) &&
  557. ((temp - tt->tt_previous_temp) >
  558. IWL_TT_INCREASE_MARGIN)) {
  559. IWL_DEBUG_POWER(priv,
  560. "Temperature increase %d "
  561. "degree Celsius\n",
  562. (temp - tt->tt_previous_temp));
  563. }
  564. tt->tt_previous_temp = temp;
  565. #endif
  566. if (old_state !=
  567. transaction->next_state) {
  568. changed = true;
  569. tt->state =
  570. transaction->next_state;
  571. }
  572. break;
  573. }
  574. }
  575. /* stop ct_kill_waiting_tm timer */
  576. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  577. if (changed) {
  578. struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
  579. if (tt->state >= IWL_TI_1) {
  580. /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
  581. tt->tt_power_mode = IWL_POWER_INDEX_5;
  582. if (!iwl_ht_enabled(priv))
  583. /* disable HT */
  584. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  585. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  586. RXON_FLG_HT40_PROT_MSK |
  587. RXON_FLG_HT_PROT_MSK);
  588. else {
  589. /* check HT capability and set
  590. * according to the system HT capability
  591. * in case get disabled before */
  592. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  593. }
  594. } else {
  595. /*
  596. * restore system power setting -- it will be
  597. * recalculated automatically.
  598. */
  599. /* check HT capability and set
  600. * according to the system HT capability
  601. * in case get disabled before */
  602. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  603. }
  604. mutex_lock(&priv->mutex);
  605. if (old_state == IWL_TI_CT_KILL)
  606. clear_bit(STATUS_CT_KILL, &priv->status);
  607. if (tt->state != IWL_TI_CT_KILL &&
  608. iwl_power_update_mode(priv, true)) {
  609. /* TT state not updated
  610. * try again during next temperature read
  611. */
  612. IWL_ERR(priv, "Cannot update power mode, "
  613. "TT state not updated\n");
  614. if (old_state == IWL_TI_CT_KILL)
  615. set_bit(STATUS_CT_KILL, &priv->status);
  616. tt->state = old_state;
  617. } else {
  618. IWL_DEBUG_POWER(priv,
  619. "Thermal Throttling to new state: %u\n",
  620. tt->state);
  621. if (old_state != IWL_TI_CT_KILL &&
  622. tt->state == IWL_TI_CT_KILL) {
  623. if (force) {
  624. IWL_DEBUG_POWER(priv,
  625. "Enter IWL_TI_CT_KILL\n");
  626. set_bit(STATUS_CT_KILL, &priv->status);
  627. iwl_perform_ct_kill_task(priv, true);
  628. } else {
  629. iwl_prepare_ct_kill_task(priv);
  630. tt->state = old_state;
  631. }
  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);
  636. }
  637. }
  638. mutex_unlock(&priv->mutex);
  639. }
  640. }
  641. /* Card State Notification indicated reach critical temperature
  642. * if PSP not enable, no Thermal Throttling function will be performed
  643. * just set the GP1 bit to acknowledge the event
  644. * otherwise, go into IWL_TI_CT_KILL state
  645. * since Card State Notification will not provide any temperature reading
  646. * for Legacy mode
  647. * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
  648. * for advance mode
  649. * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
  650. */
  651. static void iwl_bg_ct_enter(struct work_struct *work)
  652. {
  653. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
  654. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  655. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  656. return;
  657. if (!iwl_is_ready(priv))
  658. return;
  659. if (tt->state != IWL_TI_CT_KILL) {
  660. IWL_ERR(priv, "Device reached critical temperature "
  661. "- ucode going to sleep!\n");
  662. if (!priv->thermal_throttle.advanced_tt)
  663. iwl_legacy_tt_handler(priv,
  664. IWL_MINIMAL_POWER_THRESHOLD,
  665. true);
  666. else
  667. iwl_advance_tt_handler(priv,
  668. CT_KILL_THRESHOLD + 1, true);
  669. }
  670. }
  671. /* Card State Notification indicated out of critical temperature
  672. * since Card State Notification will not provide any temperature reading
  673. * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
  674. * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
  675. */
  676. static void iwl_bg_ct_exit(struct work_struct *work)
  677. {
  678. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
  679. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  680. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  681. return;
  682. if (!iwl_is_ready(priv))
  683. return;
  684. /* stop ct_kill_exit_tm timer */
  685. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  686. if (tt->state == IWL_TI_CT_KILL) {
  687. IWL_ERR(priv,
  688. "Device temperature below critical"
  689. "- ucode awake!\n");
  690. /*
  691. * exit from CT_KILL state
  692. * reset the current temperature reading
  693. */
  694. priv->temperature = 0;
  695. if (!priv->thermal_throttle.advanced_tt)
  696. iwl_legacy_tt_handler(priv,
  697. IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
  698. true);
  699. else
  700. iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
  701. true);
  702. }
  703. }
  704. void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
  705. {
  706. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  707. return;
  708. IWL_DEBUG_POWER(priv, "Queueing critical temperature enter.\n");
  709. queue_work(priv->workqueue, &priv->ct_enter);
  710. }
  711. EXPORT_SYMBOL(iwl_tt_enter_ct_kill);
  712. void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
  713. {
  714. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  715. return;
  716. IWL_DEBUG_POWER(priv, "Queueing critical temperature exit.\n");
  717. queue_work(priv->workqueue, &priv->ct_exit);
  718. }
  719. EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
  720. static void iwl_bg_tt_work(struct work_struct *work)
  721. {
  722. struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
  723. s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
  724. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  725. return;
  726. if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
  727. temp = KELVIN_TO_CELSIUS(priv->temperature);
  728. if (!priv->thermal_throttle.advanced_tt)
  729. iwl_legacy_tt_handler(priv, temp, false);
  730. else
  731. iwl_advance_tt_handler(priv, temp, false);
  732. }
  733. void iwl_tt_handler(struct iwl_priv *priv)
  734. {
  735. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  736. return;
  737. IWL_DEBUG_POWER(priv, "Queueing thermal throttling work.\n");
  738. queue_work(priv->workqueue, &priv->tt_work);
  739. }
  740. EXPORT_SYMBOL(iwl_tt_handler);
  741. /* Thermal throttling initialization
  742. * For advance thermal throttling:
  743. * Initialize Thermal Index and temperature threshold table
  744. * Initialize thermal throttling restriction table
  745. */
  746. void iwl_tt_initialize(struct iwl_priv *priv)
  747. {
  748. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  749. int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
  750. struct iwl_tt_trans *transaction;
  751. IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");
  752. memset(tt, 0, sizeof(struct iwl_tt_mgmt));
  753. tt->state = IWL_TI_0;
  754. init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
  755. priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
  756. priv->thermal_throttle.ct_kill_exit_tm.function =
  757. iwl_tt_check_exit_ct_kill;
  758. init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
  759. priv->thermal_throttle.ct_kill_waiting_tm.data = (unsigned long)priv;
  760. priv->thermal_throttle.ct_kill_waiting_tm.function =
  761. iwl_tt_ready_for_ct_kill;
  762. /* setup deferred ct kill work */
  763. INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
  764. INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
  765. INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
  766. switch (priv->hw_rev & CSR_HW_REV_TYPE_MSK) {
  767. case CSR_HW_REV_TYPE_6x00:
  768. case CSR_HW_REV_TYPE_6x50:
  769. IWL_DEBUG_POWER(priv, "Advanced Thermal Throttling\n");
  770. tt->restriction = kzalloc(sizeof(struct iwl_tt_restriction) *
  771. IWL_TI_STATE_MAX, GFP_KERNEL);
  772. tt->transaction = kzalloc(sizeof(struct iwl_tt_trans) *
  773. IWL_TI_STATE_MAX * (IWL_TI_STATE_MAX - 1),
  774. GFP_KERNEL);
  775. if (!tt->restriction || !tt->transaction) {
  776. IWL_ERR(priv, "Fallback to Legacy Throttling\n");
  777. priv->thermal_throttle.advanced_tt = false;
  778. kfree(tt->restriction);
  779. tt->restriction = NULL;
  780. kfree(tt->transaction);
  781. tt->transaction = NULL;
  782. } else {
  783. transaction = tt->transaction +
  784. (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
  785. memcpy(transaction, &tt_range_0[0], size);
  786. transaction = tt->transaction +
  787. (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
  788. memcpy(transaction, &tt_range_1[0], size);
  789. transaction = tt->transaction +
  790. (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
  791. memcpy(transaction, &tt_range_2[0], size);
  792. transaction = tt->transaction +
  793. (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
  794. memcpy(transaction, &tt_range_3[0], size);
  795. size = sizeof(struct iwl_tt_restriction) *
  796. IWL_TI_STATE_MAX;
  797. memcpy(tt->restriction,
  798. &restriction_range[0], size);
  799. priv->thermal_throttle.advanced_tt = true;
  800. }
  801. break;
  802. default:
  803. IWL_DEBUG_POWER(priv, "Legacy Thermal Throttling\n");
  804. priv->thermal_throttle.advanced_tt = false;
  805. break;
  806. }
  807. }
  808. EXPORT_SYMBOL(iwl_tt_initialize);
  809. /* cleanup thermal throttling management related memory and timer */
  810. void iwl_tt_exit(struct iwl_priv *priv)
  811. {
  812. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  813. /* stop ct_kill_exit_tm timer if activated */
  814. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  815. /* stop ct_kill_waiting_tm timer if activated */
  816. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  817. cancel_work_sync(&priv->tt_work);
  818. cancel_work_sync(&priv->ct_enter);
  819. cancel_work_sync(&priv->ct_exit);
  820. if (priv->thermal_throttle.advanced_tt) {
  821. /* free advance thermal throttling memory */
  822. kfree(tt->restriction);
  823. tt->restriction = NULL;
  824. kfree(tt->transaction);
  825. tt->transaction = NULL;
  826. }
  827. }
  828. EXPORT_SYMBOL(iwl_tt_exit);
  829. /* initialize to default */
  830. void iwl_power_initialize(struct iwl_priv *priv)
  831. {
  832. u16 lctl = iwl_pcie_link_ctl(priv);
  833. priv->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
  834. priv->power_data.debug_sleep_level_override = -1;
  835. memset(&priv->power_data.sleep_cmd, 0,
  836. sizeof(priv->power_data.sleep_cmd));
  837. }
  838. EXPORT_SYMBOL(iwl_power_initialize);