iwl-power.c 27 KB

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