iwl-power.c 31 KB

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