iwl-power.c 31 KB

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