iwl-power.c 31 KB

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