iwl-power.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 >= 115 CT_KILL 115>T>=105 TI_1 N/A N/A
  146. * IWL_TI_1 T >= 115 CT_KILL 115>T>=110 TI_2 T<=95 TI_0
  147. * IWL_TI_2 T >= 115 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},
  154. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, 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},
  159. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, 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 + 1, IWL_ABSOLUTE_MAX},
  164. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, 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. /* TT power setting overwrites everything */
  253. if (tt->state >= IWL_TI_1)
  254. iwl_static_sleep_cmd(priv, &cmd, tt->tt_power_mode, dtimper);
  255. else if (!enabled)
  256. iwl_power_sleep_cam_cmd(priv, &cmd);
  257. else if (priv->power_data.debug_sleep_level_override >= 0)
  258. iwl_static_sleep_cmd(priv, &cmd,
  259. priv->power_data.debug_sleep_level_override,
  260. dtimper);
  261. else if (no_sleep_autoadjust)
  262. iwl_static_sleep_cmd(priv, &cmd, IWL_POWER_INDEX_1, dtimper);
  263. else
  264. iwl_power_fill_sleep_cmd(priv, &cmd,
  265. priv->hw->conf.dynamic_ps_timeout,
  266. priv->hw->conf.max_sleep_period);
  267. if (iwl_is_ready_rf(priv) &&
  268. (memcmp(&priv->power_data.sleep_cmd, &cmd, sizeof(cmd)) || force)) {
  269. if (cmd.flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
  270. set_bit(STATUS_POWER_PMI, &priv->status);
  271. ret = iwl_set_power(priv, &cmd);
  272. if (!ret) {
  273. if (!(cmd.flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
  274. clear_bit(STATUS_POWER_PMI, &priv->status);
  275. if (priv->cfg->ops->lib->update_chain_flags &&
  276. update_chains)
  277. priv->cfg->ops->lib->update_chain_flags(priv);
  278. else if (priv->cfg->ops->lib->update_chain_flags)
  279. IWL_DEBUG_POWER(priv,
  280. "Cannot update the power, chain noise "
  281. "calibration running: %d\n",
  282. priv->chain_noise_data.state);
  283. memcpy(&priv->power_data.sleep_cmd, &cmd, sizeof(cmd));
  284. } else
  285. IWL_ERR(priv, "set power fail, ret = %d", ret);
  286. }
  287. return ret;
  288. }
  289. EXPORT_SYMBOL(iwl_power_update_mode);
  290. bool iwl_ht_enabled(struct iwl_priv *priv)
  291. {
  292. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  293. struct iwl_tt_restriction *restriction;
  294. if (!priv->thermal_throttle.advanced_tt)
  295. return true;
  296. restriction = tt->restriction + tt->state;
  297. return restriction->is_ht;
  298. }
  299. EXPORT_SYMBOL(iwl_ht_enabled);
  300. enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
  301. {
  302. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  303. struct iwl_tt_restriction *restriction;
  304. if (!priv->thermal_throttle.advanced_tt)
  305. return IWL_ANT_OK_MULTI;
  306. restriction = tt->restriction + tt->state;
  307. return restriction->tx_stream;
  308. }
  309. EXPORT_SYMBOL(iwl_tx_ant_restriction);
  310. enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
  311. {
  312. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  313. struct iwl_tt_restriction *restriction;
  314. if (!priv->thermal_throttle.advanced_tt)
  315. return IWL_ANT_OK_MULTI;
  316. restriction = tt->restriction + tt->state;
  317. return restriction->rx_stream;
  318. }
  319. #define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
  320. /*
  321. * toggle the bit to wake up uCode and check the temperature
  322. * if the temperature is below CT, uCode will stay awake and send card
  323. * state notification with CT_KILL bit clear to inform Thermal Throttling
  324. * Management to change state. Otherwise, uCode will go back to sleep
  325. * without doing anything, driver should continue the 5 seconds timer
  326. * to wake up uCode for temperature check until temperature drop below CT
  327. */
  328. static void iwl_tt_check_exit_ct_kill(unsigned long data)
  329. {
  330. struct iwl_priv *priv = (struct iwl_priv *)data;
  331. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  332. unsigned long flags;
  333. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  334. return;
  335. if (tt->state == IWL_TI_CT_KILL) {
  336. if (priv->thermal_throttle.ct_kill_toggle) {
  337. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  338. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  339. priv->thermal_throttle.ct_kill_toggle = false;
  340. } else {
  341. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  342. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  343. priv->thermal_throttle.ct_kill_toggle = true;
  344. }
  345. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  346. spin_lock_irqsave(&priv->reg_lock, flags);
  347. if (!iwl_grab_nic_access(priv))
  348. iwl_release_nic_access(priv);
  349. spin_unlock_irqrestore(&priv->reg_lock, flags);
  350. /* Reschedule the ct_kill timer to occur in
  351. * CT_KILL_EXIT_DURATION seconds to ensure we get a
  352. * thermal update */
  353. mod_timer(&priv->thermal_throttle.ct_kill_exit_tm, jiffies +
  354. CT_KILL_EXIT_DURATION * HZ);
  355. }
  356. }
  357. static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
  358. bool stop)
  359. {
  360. if (stop) {
  361. IWL_DEBUG_POWER(priv, "Stop all queues\n");
  362. if (priv->mac80211_registered)
  363. ieee80211_stop_queues(priv->hw);
  364. IWL_DEBUG_POWER(priv,
  365. "Schedule 5 seconds CT_KILL Timer\n");
  366. mod_timer(&priv->thermal_throttle.ct_kill_exit_tm, jiffies +
  367. CT_KILL_EXIT_DURATION * HZ);
  368. } else {
  369. IWL_DEBUG_POWER(priv, "Wake all queues\n");
  370. if (priv->mac80211_registered)
  371. ieee80211_wake_queues(priv->hw);
  372. }
  373. }
  374. #define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
  375. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
  376. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
  377. /*
  378. * Legacy thermal throttling
  379. * 1) Avoid NIC destruction due to high temperatures
  380. * Chip will identify dangerously high temperatures that can
  381. * harm the device and will power down
  382. * 2) Avoid the NIC power down due to high temperature
  383. * Throttle early enough to lower the power consumption before
  384. * drastic steps are needed
  385. */
  386. static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp)
  387. {
  388. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  389. enum iwl_tt_state old_state;
  390. #ifdef CONFIG_IWLWIFI_DEBUG
  391. if ((tt->tt_previous_temp) &&
  392. (temp > tt->tt_previous_temp) &&
  393. ((temp - tt->tt_previous_temp) >
  394. IWL_TT_INCREASE_MARGIN)) {
  395. IWL_DEBUG_POWER(priv,
  396. "Temperature increase %d degree Celsius\n",
  397. (temp - tt->tt_previous_temp));
  398. }
  399. #endif
  400. old_state = tt->state;
  401. /* in Celsius */
  402. if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
  403. tt->state = IWL_TI_CT_KILL;
  404. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
  405. tt->state = IWL_TI_2;
  406. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
  407. tt->state = IWL_TI_1;
  408. else
  409. tt->state = IWL_TI_0;
  410. #ifdef CONFIG_IWLWIFI_DEBUG
  411. tt->tt_previous_temp = temp;
  412. #endif
  413. if (tt->state != old_state) {
  414. switch (tt->state) {
  415. case IWL_TI_0:
  416. /*
  417. * When the system is ready to go back to IWL_TI_0
  418. * we only have to call iwl_power_update_mode() to
  419. * do so.
  420. */
  421. break;
  422. case IWL_TI_1:
  423. tt->tt_power_mode = IWL_POWER_INDEX_3;
  424. break;
  425. case IWL_TI_2:
  426. tt->tt_power_mode = IWL_POWER_INDEX_4;
  427. break;
  428. default:
  429. tt->tt_power_mode = IWL_POWER_INDEX_5;
  430. break;
  431. }
  432. mutex_lock(&priv->mutex);
  433. if (iwl_power_update_mode(priv, true)) {
  434. /* TT state not updated
  435. * try again during next temperature read
  436. */
  437. tt->state = old_state;
  438. IWL_ERR(priv, "Cannot update power mode, "
  439. "TT state not updated\n");
  440. } else {
  441. if (tt->state == IWL_TI_CT_KILL)
  442. iwl_perform_ct_kill_task(priv, true);
  443. else if (old_state == IWL_TI_CT_KILL &&
  444. tt->state != IWL_TI_CT_KILL)
  445. iwl_perform_ct_kill_task(priv, false);
  446. IWL_DEBUG_POWER(priv, "Temperature state changed %u\n",
  447. tt->state);
  448. IWL_DEBUG_POWER(priv, "Power Index change to %u\n",
  449. tt->tt_power_mode);
  450. }
  451. mutex_unlock(&priv->mutex);
  452. }
  453. }
  454. /*
  455. * Advance thermal throttling
  456. * 1) Avoid NIC destruction due to high temperatures
  457. * Chip will identify dangerously high temperatures that can
  458. * harm the device and will power down
  459. * 2) Avoid the NIC power down due to high temperature
  460. * Throttle early enough to lower the power consumption before
  461. * drastic steps are needed
  462. * Actions include relaxing the power down sleep thresholds and
  463. * decreasing the number of TX streams
  464. * 3) Avoid throughput performance impact as much as possible
  465. *
  466. *=============================================================================
  467. * Condition Nxt State Condition Nxt State Condition Nxt State
  468. *-----------------------------------------------------------------------------
  469. * IWL_TI_0 T >= 115 CT_KILL 115>T>=105 TI_1 N/A N/A
  470. * IWL_TI_1 T >= 115 CT_KILL 115>T>=110 TI_2 T<=95 TI_0
  471. * IWL_TI_2 T >= 115 CT_KILL T<=100 TI_1
  472. * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
  473. *=============================================================================
  474. */
  475. static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp)
  476. {
  477. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  478. int i;
  479. bool changed = false;
  480. enum iwl_tt_state old_state;
  481. struct iwl_tt_trans *transaction;
  482. old_state = tt->state;
  483. for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
  484. /* based on the current TT state,
  485. * find the curresponding transaction table
  486. * each table has (IWL_TI_STATE_MAX - 1) entries
  487. * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
  488. * will advance to the correct table.
  489. * then based on the current temperature
  490. * find the next state need to transaction to
  491. * go through all the possible (IWL_TI_STATE_MAX - 1) entries
  492. * in the current table to see if transaction is needed
  493. */
  494. transaction = tt->transaction +
  495. ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
  496. if (temp >= transaction->tt_low &&
  497. temp <= transaction->tt_high) {
  498. #ifdef CONFIG_IWLWIFI_DEBUG
  499. if ((tt->tt_previous_temp) &&
  500. (temp > tt->tt_previous_temp) &&
  501. ((temp - tt->tt_previous_temp) >
  502. IWL_TT_INCREASE_MARGIN)) {
  503. IWL_DEBUG_POWER(priv,
  504. "Temperature increase %d "
  505. "degree Celsius\n",
  506. (temp - tt->tt_previous_temp));
  507. }
  508. tt->tt_previous_temp = temp;
  509. #endif
  510. if (old_state !=
  511. transaction->next_state) {
  512. changed = true;
  513. tt->state =
  514. transaction->next_state;
  515. }
  516. break;
  517. }
  518. }
  519. if (changed) {
  520. struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
  521. if (tt->state >= IWL_TI_1) {
  522. /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
  523. tt->tt_power_mode = IWL_POWER_INDEX_5;
  524. if (!iwl_ht_enabled(priv))
  525. /* disable HT */
  526. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  527. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  528. RXON_FLG_HT40_PROT_MSK |
  529. RXON_FLG_HT_PROT_MSK);
  530. else {
  531. /* check HT capability and set
  532. * according to the system HT capability
  533. * in case get disabled before */
  534. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  535. }
  536. } else {
  537. /*
  538. * restore system power setting -- it will be
  539. * recalculated automatically.
  540. */
  541. /* check HT capability and set
  542. * according to the system HT capability
  543. * in case get disabled before */
  544. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  545. }
  546. mutex_lock(&priv->mutex);
  547. if (iwl_power_update_mode(priv, true)) {
  548. /* TT state not updated
  549. * try again during next temperature read
  550. */
  551. IWL_ERR(priv, "Cannot update power mode, "
  552. "TT state not updated\n");
  553. tt->state = old_state;
  554. } else {
  555. IWL_DEBUG_POWER(priv,
  556. "Thermal Throttling to new state: %u\n",
  557. tt->state);
  558. if (old_state != IWL_TI_CT_KILL &&
  559. tt->state == IWL_TI_CT_KILL) {
  560. IWL_DEBUG_POWER(priv, "Enter IWL_TI_CT_KILL\n");
  561. iwl_perform_ct_kill_task(priv, true);
  562. } else if (old_state == IWL_TI_CT_KILL &&
  563. tt->state != IWL_TI_CT_KILL) {
  564. IWL_DEBUG_POWER(priv, "Exit IWL_TI_CT_KILL\n");
  565. iwl_perform_ct_kill_task(priv, false);
  566. }
  567. }
  568. mutex_unlock(&priv->mutex);
  569. }
  570. }
  571. /* Card State Notification indicated reach critical temperature
  572. * if PSP not enable, no Thermal Throttling function will be performed
  573. * just set the GP1 bit to acknowledge the event
  574. * otherwise, go into IWL_TI_CT_KILL state
  575. * since Card State Notification will not provide any temperature reading
  576. * for Legacy mode
  577. * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
  578. * for advance mode
  579. * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
  580. */
  581. static void iwl_bg_ct_enter(struct work_struct *work)
  582. {
  583. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
  584. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  585. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  586. return;
  587. if (!iwl_is_ready(priv))
  588. return;
  589. if (tt->state != IWL_TI_CT_KILL) {
  590. IWL_ERR(priv, "Device reached critical temperature "
  591. "- ucode going to sleep!\n");
  592. if (!priv->thermal_throttle.advanced_tt)
  593. iwl_legacy_tt_handler(priv,
  594. IWL_MINIMAL_POWER_THRESHOLD);
  595. else
  596. iwl_advance_tt_handler(priv,
  597. CT_KILL_THRESHOLD + 1);
  598. }
  599. }
  600. /* Card State Notification indicated out of critical temperature
  601. * since Card State Notification will not provide any temperature reading
  602. * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
  603. * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
  604. */
  605. static void iwl_bg_ct_exit(struct work_struct *work)
  606. {
  607. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
  608. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  609. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  610. return;
  611. if (!iwl_is_ready(priv))
  612. return;
  613. /* stop ct_kill_exit_tm timer */
  614. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  615. if (tt->state == IWL_TI_CT_KILL) {
  616. IWL_ERR(priv,
  617. "Device temperature below critical"
  618. "- ucode awake!\n");
  619. if (!priv->thermal_throttle.advanced_tt)
  620. iwl_legacy_tt_handler(priv,
  621. IWL_REDUCED_PERFORMANCE_THRESHOLD_2);
  622. else
  623. iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD);
  624. }
  625. }
  626. void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
  627. {
  628. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  629. return;
  630. IWL_DEBUG_POWER(priv, "Queueing critical temperature enter.\n");
  631. queue_work(priv->workqueue, &priv->ct_enter);
  632. }
  633. EXPORT_SYMBOL(iwl_tt_enter_ct_kill);
  634. void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
  635. {
  636. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  637. return;
  638. IWL_DEBUG_POWER(priv, "Queueing critical temperature exit.\n");
  639. queue_work(priv->workqueue, &priv->ct_exit);
  640. }
  641. EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
  642. static void iwl_bg_tt_work(struct work_struct *work)
  643. {
  644. struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
  645. s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
  646. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  647. return;
  648. if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
  649. temp = KELVIN_TO_CELSIUS(priv->temperature);
  650. if (!priv->thermal_throttle.advanced_tt)
  651. iwl_legacy_tt_handler(priv, temp);
  652. else
  653. iwl_advance_tt_handler(priv, temp);
  654. }
  655. void iwl_tt_handler(struct iwl_priv *priv)
  656. {
  657. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  658. return;
  659. IWL_DEBUG_POWER(priv, "Queueing thermal throttling work.\n");
  660. queue_work(priv->workqueue, &priv->tt_work);
  661. }
  662. EXPORT_SYMBOL(iwl_tt_handler);
  663. /* Thermal throttling initialization
  664. * For advance thermal throttling:
  665. * Initialize Thermal Index and temperature threshold table
  666. * Initialize thermal throttling restriction table
  667. */
  668. void iwl_tt_initialize(struct iwl_priv *priv)
  669. {
  670. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  671. int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
  672. struct iwl_tt_trans *transaction;
  673. IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");
  674. memset(tt, 0, sizeof(struct iwl_tt_mgmt));
  675. tt->state = IWL_TI_0;
  676. init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
  677. priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
  678. priv->thermal_throttle.ct_kill_exit_tm.function = iwl_tt_check_exit_ct_kill;
  679. /* setup deferred ct kill work */
  680. INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
  681. INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
  682. INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
  683. switch (priv->hw_rev & CSR_HW_REV_TYPE_MSK) {
  684. case CSR_HW_REV_TYPE_6x00:
  685. case CSR_HW_REV_TYPE_6x50:
  686. IWL_DEBUG_POWER(priv, "Advanced Thermal Throttling\n");
  687. tt->restriction = kzalloc(sizeof(struct iwl_tt_restriction) *
  688. IWL_TI_STATE_MAX, GFP_KERNEL);
  689. tt->transaction = kzalloc(sizeof(struct iwl_tt_trans) *
  690. IWL_TI_STATE_MAX * (IWL_TI_STATE_MAX - 1),
  691. GFP_KERNEL);
  692. if (!tt->restriction || !tt->transaction) {
  693. IWL_ERR(priv, "Fallback to Legacy Throttling\n");
  694. priv->thermal_throttle.advanced_tt = false;
  695. kfree(tt->restriction);
  696. tt->restriction = NULL;
  697. kfree(tt->transaction);
  698. tt->transaction = NULL;
  699. } else {
  700. transaction = tt->transaction +
  701. (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
  702. memcpy(transaction, &tt_range_0[0], size);
  703. transaction = tt->transaction +
  704. (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
  705. memcpy(transaction, &tt_range_1[0], size);
  706. transaction = tt->transaction +
  707. (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
  708. memcpy(transaction, &tt_range_2[0], size);
  709. transaction = tt->transaction +
  710. (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
  711. memcpy(transaction, &tt_range_3[0], size);
  712. size = sizeof(struct iwl_tt_restriction) *
  713. IWL_TI_STATE_MAX;
  714. memcpy(tt->restriction,
  715. &restriction_range[0], size);
  716. priv->thermal_throttle.advanced_tt = true;
  717. }
  718. break;
  719. default:
  720. IWL_DEBUG_POWER(priv, "Legacy Thermal Throttling\n");
  721. priv->thermal_throttle.advanced_tt = false;
  722. break;
  723. }
  724. }
  725. EXPORT_SYMBOL(iwl_tt_initialize);
  726. /* cleanup thermal throttling management related memory and timer */
  727. void iwl_tt_exit(struct iwl_priv *priv)
  728. {
  729. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  730. /* stop ct_kill_exit_tm timer if activated */
  731. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  732. cancel_work_sync(&priv->tt_work);
  733. cancel_work_sync(&priv->ct_enter);
  734. cancel_work_sync(&priv->ct_exit);
  735. if (priv->thermal_throttle.advanced_tt) {
  736. /* free advance thermal throttling memory */
  737. kfree(tt->restriction);
  738. tt->restriction = NULL;
  739. kfree(tt->transaction);
  740. tt->transaction = NULL;
  741. }
  742. }
  743. EXPORT_SYMBOL(iwl_tt_exit);
  744. /* initialize to default */
  745. void iwl_power_initialize(struct iwl_priv *priv)
  746. {
  747. u16 lctl = iwl_pcie_link_ctl(priv);
  748. priv->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
  749. priv->power_data.debug_sleep_level_override = -1;
  750. memset(&priv->power_data.sleep_cmd, 0,
  751. sizeof(priv->power_data.sleep_cmd));
  752. }
  753. EXPORT_SYMBOL(iwl_power_initialize);