iwl-power.c 31 KB

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