iwl-power.c 28 KB

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