iwl-power.c 27 KB

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