iwl-power.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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 allow the card to go to sleep when not busy.
  41. *
  42. * The power level is set to INDEX_1 (the least deep state) by
  43. * default, and will, in the future, be the deepest state unless
  44. * otherwise required by pm_qos network latency requirements.
  45. *
  46. * Using INDEX_1 without pm_qos is ok because mac80211 will disable
  47. * PS when even checking every beacon for the TIM bit would exceed
  48. * the required latency.
  49. */
  50. #define IWL_POWER_RANGE_0_MAX (2)
  51. #define IWL_POWER_RANGE_1_MAX (10)
  52. #define NOSLP cpu_to_le16(0), 0, 0
  53. #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
  54. #define TU_TO_USEC 1024
  55. #define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
  56. #define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
  57. cpu_to_le32(X1), \
  58. cpu_to_le32(X2), \
  59. cpu_to_le32(X3), \
  60. cpu_to_le32(X4)}
  61. /* default power management (not Tx power) table values */
  62. /* for DTIM period 0 through IWL_POWER_RANGE_0_MAX */
  63. static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
  64. {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  65. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
  66. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
  67. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
  68. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
  69. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
  70. };
  71. /* for DTIM period IWL_POWER_RANGE_0_MAX + 1 through IWL_POWER_RANGE_1_MAX */
  72. static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
  73. {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  74. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
  75. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
  76. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
  77. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
  78. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
  79. };
  80. /* for DTIM period > IWL_POWER_RANGE_1_MAX */
  81. static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
  82. {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  83. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
  84. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
  85. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  86. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  87. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
  88. };
  89. /* default Thermal Throttling transaction table
  90. * Current state | Throttling Down | Throttling Up
  91. *=============================================================================
  92. * Condition Nxt State Condition Nxt State Condition Nxt State
  93. *-----------------------------------------------------------------------------
  94. * IWL_TI_0 T >= 115 CT_KILL 115>T>=105 TI_1 N/A N/A
  95. * IWL_TI_1 T >= 115 CT_KILL 115>T>=110 TI_2 T<=95 TI_0
  96. * IWL_TI_2 T >= 115 CT_KILL T<=100 TI_1
  97. * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
  98. *=============================================================================
  99. */
  100. static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
  101. {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
  102. {IWL_TI_1, 105, CT_KILL_THRESHOLD},
  103. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
  104. };
  105. static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
  106. {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
  107. {IWL_TI_2, 110, CT_KILL_THRESHOLD},
  108. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
  109. };
  110. static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
  111. {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
  112. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
  113. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
  114. };
  115. static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
  116. {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
  117. {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
  118. {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
  119. };
  120. /* Advance Thermal Throttling default restriction table */
  121. static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
  122. {IWL_TX_MULTI, true, IWL_RX_MULTI},
  123. {IWL_TX_SINGLE, true, IWL_RX_MULTI},
  124. {IWL_TX_SINGLE, false, IWL_RX_SINGLE},
  125. {IWL_TX_NONE, false, IWL_RX_NONE}
  126. };
  127. /* set card power command */
  128. static int iwl_set_power(struct iwl_priv *priv, void *cmd)
  129. {
  130. return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
  131. sizeof(struct iwl_powertable_cmd), cmd);
  132. }
  133. /* initialize to default */
  134. static void iwl_power_init_handle(struct iwl_priv *priv)
  135. {
  136. struct iwl_power_mgr *pow_data;
  137. int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_NUM;
  138. struct iwl_powertable_cmd *cmd;
  139. int i;
  140. u16 lctl;
  141. IWL_DEBUG_POWER(priv, "Initialize power \n");
  142. pow_data = &priv->power_data;
  143. memset(pow_data, 0, sizeof(*pow_data));
  144. memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
  145. memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
  146. memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
  147. lctl = iwl_pcie_link_ctl(priv);
  148. IWL_DEBUG_POWER(priv, "adjust power command flags\n");
  149. for (i = 0; i < IWL_POWER_NUM; i++) {
  150. cmd = &pow_data->pwr_range_0[i].cmd;
  151. if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
  152. cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
  153. else
  154. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  155. }
  156. }
  157. /* adjust power command according to DTIM period and power level*/
  158. static int iwl_update_power_cmd(struct iwl_priv *priv,
  159. struct iwl_powertable_cmd *cmd, u16 mode)
  160. {
  161. struct iwl_power_vec_entry *range;
  162. struct iwl_power_mgr *pow_data;
  163. int i;
  164. u32 max_sleep = 0;
  165. u8 period;
  166. bool skip;
  167. if (mode > IWL_POWER_INDEX_5) {
  168. IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
  169. return -EINVAL;
  170. }
  171. pow_data = &priv->power_data;
  172. if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
  173. range = &pow_data->pwr_range_0[0];
  174. else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
  175. range = &pow_data->pwr_range_1[0];
  176. else
  177. range = &pow_data->pwr_range_2[0];
  178. period = pow_data->dtim_period;
  179. memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
  180. if (period == 0) {
  181. period = 1;
  182. skip = false;
  183. } else {
  184. skip = !!range[mode].no_dtim;
  185. }
  186. if (skip) {
  187. __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
  188. max_sleep = le32_to_cpu(slp_itrvl);
  189. if (max_sleep == 0xFF)
  190. max_sleep = period * (skip + 1);
  191. else if (max_sleep > period)
  192. max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
  193. cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
  194. } else {
  195. max_sleep = period;
  196. cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
  197. }
  198. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  199. if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
  200. cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
  201. IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
  202. IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  203. IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  204. IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  205. le32_to_cpu(cmd->sleep_interval[0]),
  206. le32_to_cpu(cmd->sleep_interval[1]),
  207. le32_to_cpu(cmd->sleep_interval[2]),
  208. le32_to_cpu(cmd->sleep_interval[3]),
  209. le32_to_cpu(cmd->sleep_interval[4]));
  210. return 0;
  211. }
  212. /*
  213. * compute the final power mode index
  214. */
  215. int iwl_power_update_mode(struct iwl_priv *priv, bool force)
  216. {
  217. struct iwl_power_mgr *setting = &(priv->power_data);
  218. int ret = 0;
  219. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  220. u16 uninitialized_var(final_mode);
  221. bool update_chains;
  222. /* Don't update the RX chain when chain noise calibration is running */
  223. update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
  224. priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
  225. final_mode = priv->power_data.user_power_setting;
  226. if (setting->power_disabled)
  227. final_mode = IWL_POWER_MODE_CAM;
  228. if (tt->state >= IWL_TI_1) {
  229. /* TT power setting overwrite user & system power setting */
  230. final_mode = tt->tt_power_mode;
  231. }
  232. if (iwl_is_ready_rf(priv) &&
  233. ((setting->power_mode != final_mode) || force)) {
  234. struct iwl_powertable_cmd cmd;
  235. if (final_mode != IWL_POWER_MODE_CAM)
  236. set_bit(STATUS_POWER_PMI, &priv->status);
  237. iwl_update_power_cmd(priv, &cmd, final_mode);
  238. cmd.keep_alive_beacons = 0;
  239. if (final_mode == IWL_POWER_INDEX_5)
  240. cmd.flags |= IWL_POWER_FAST_PD;
  241. ret = iwl_set_power(priv, &cmd);
  242. if (final_mode == IWL_POWER_MODE_CAM)
  243. clear_bit(STATUS_POWER_PMI, &priv->status);
  244. if (priv->cfg->ops->lib->update_chain_flags && update_chains)
  245. priv->cfg->ops->lib->update_chain_flags(priv);
  246. else
  247. IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
  248. "calibration running: %d\n",
  249. priv->chain_noise_data.state);
  250. if (!ret)
  251. setting->power_mode = final_mode;
  252. }
  253. return ret;
  254. }
  255. EXPORT_SYMBOL(iwl_power_update_mode);
  256. /* set user_power_setting */
  257. int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
  258. {
  259. if (mode >= IWL_POWER_NUM)
  260. return -EINVAL;
  261. priv->power_data.user_power_setting = mode;
  262. return iwl_power_update_mode(priv, 0);
  263. }
  264. EXPORT_SYMBOL(iwl_power_set_user_mode);
  265. bool iwl_ht_enabled(struct iwl_priv *priv)
  266. {
  267. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  268. struct iwl_tt_restriction *restriction;
  269. if (!priv->power_data.adv_tt)
  270. return true;
  271. restriction = tt->restriction + tt->state;
  272. return restriction->is_ht;
  273. }
  274. EXPORT_SYMBOL(iwl_ht_enabled);
  275. u8 iwl_tx_ant_restriction(struct iwl_priv *priv)
  276. {
  277. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  278. struct iwl_tt_restriction *restriction;
  279. if (!priv->power_data.adv_tt)
  280. return IWL_TX_MULTI;
  281. restriction = tt->restriction + tt->state;
  282. return restriction->tx_stream;
  283. }
  284. EXPORT_SYMBOL(iwl_tx_ant_restriction);
  285. u8 iwl_rx_ant_restriction(struct iwl_priv *priv)
  286. {
  287. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  288. struct iwl_tt_restriction *restriction;
  289. if (!priv->power_data.adv_tt)
  290. return IWL_RX_MULTI;
  291. restriction = tt->restriction + tt->state;
  292. return restriction->rx_stream;
  293. }
  294. EXPORT_SYMBOL(iwl_rx_ant_restriction);
  295. #define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
  296. /*
  297. * toggle the bit to wake up uCode and check the temperature
  298. * if the temperature is below CT, uCode will stay awake and send card
  299. * state notification with CT_KILL bit clear to inform Thermal Throttling
  300. * Management to change state. Otherwise, uCode will go back to sleep
  301. * without doing anything, driver should continue the 5 seconds timer
  302. * to wake up uCode for temperature check until temperature drop below CT
  303. */
  304. static void iwl_tt_check_exit_ct_kill(unsigned long data)
  305. {
  306. struct iwl_priv *priv = (struct iwl_priv *)data;
  307. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  308. unsigned long flags;
  309. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  310. return;
  311. if (tt->state == IWL_TI_CT_KILL) {
  312. if (priv->power_data.ct_kill_toggle) {
  313. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  314. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  315. priv->power_data.ct_kill_toggle = false;
  316. } else {
  317. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  318. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  319. priv->power_data.ct_kill_toggle = true;
  320. }
  321. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  322. spin_lock_irqsave(&priv->reg_lock, flags);
  323. if (!iwl_grab_nic_access(priv))
  324. iwl_release_nic_access(priv);
  325. spin_unlock_irqrestore(&priv->reg_lock, flags);
  326. /* Reschedule the ct_kill timer to occur in
  327. * CT_KILL_EXIT_DURATION seconds to ensure we get a
  328. * thermal update */
  329. mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
  330. CT_KILL_EXIT_DURATION * HZ);
  331. }
  332. }
  333. static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
  334. bool stop)
  335. {
  336. if (stop) {
  337. IWL_DEBUG_POWER(priv, "Stop all queues\n");
  338. if (priv->mac80211_registered)
  339. ieee80211_stop_queues(priv->hw);
  340. IWL_DEBUG_POWER(priv,
  341. "Schedule 5 seconds CT_KILL Timer\n");
  342. mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
  343. CT_KILL_EXIT_DURATION * HZ);
  344. } else {
  345. IWL_DEBUG_POWER(priv, "Wake all queues\n");
  346. if (priv->mac80211_registered)
  347. ieee80211_wake_queues(priv->hw);
  348. }
  349. }
  350. #define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
  351. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
  352. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
  353. /*
  354. * Legacy thermal throttling
  355. * 1) Avoid NIC destruction due to high temperatures
  356. * Chip will identify dangerously high temperatures that can
  357. * harm the device and will power down
  358. * 2) Avoid the NIC power down due to high temperature
  359. * Throttle early enough to lower the power consumption before
  360. * drastic steps are needed
  361. */
  362. static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp)
  363. {
  364. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  365. enum iwl_tt_state new_state;
  366. struct iwl_power_mgr *setting = &priv->power_data;
  367. #ifdef CONFIG_IWLWIFI_DEBUG
  368. if ((tt->tt_previous_temp) &&
  369. (temp > tt->tt_previous_temp) &&
  370. ((temp - tt->tt_previous_temp) >
  371. IWL_TT_INCREASE_MARGIN)) {
  372. IWL_DEBUG_POWER(priv,
  373. "Temperature increase %d degree Celsius\n",
  374. (temp - tt->tt_previous_temp));
  375. }
  376. #endif
  377. /* in Celsius */
  378. if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
  379. new_state = IWL_TI_CT_KILL;
  380. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
  381. new_state = IWL_TI_2;
  382. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
  383. new_state = IWL_TI_1;
  384. else
  385. new_state = IWL_TI_0;
  386. #ifdef CONFIG_IWLWIFI_DEBUG
  387. tt->tt_previous_temp = temp;
  388. #endif
  389. if (tt->state != new_state) {
  390. if (tt->state == IWL_TI_0) {
  391. tt->sys_power_mode = setting->power_mode;
  392. IWL_DEBUG_POWER(priv, "current power mode: %u\n",
  393. setting->power_mode);
  394. }
  395. switch (new_state) {
  396. case IWL_TI_0:
  397. /* when system ready to go back to IWL_TI_0 state
  398. * using system power mode instead of TT power mode
  399. * revert back to the orginal power mode which was saved
  400. * before enter Thermal Throttling state
  401. * update priv->power_data.user_power_setting to the
  402. * required power mode to make sure
  403. * iwl_power_update_mode() will update power correctly.
  404. */
  405. priv->power_data.user_power_setting =
  406. tt->sys_power_mode;
  407. tt->tt_power_mode = tt->sys_power_mode;
  408. break;
  409. case IWL_TI_1:
  410. tt->tt_power_mode = IWL_POWER_INDEX_3;
  411. break;
  412. case IWL_TI_2:
  413. tt->tt_power_mode = IWL_POWER_INDEX_4;
  414. break;
  415. default:
  416. tt->tt_power_mode = IWL_POWER_INDEX_5;
  417. break;
  418. }
  419. if (iwl_power_update_mode(priv, true)) {
  420. /* TT state not updated
  421. * try again during next temperature read
  422. */
  423. IWL_ERR(priv, "Cannot update power mode, "
  424. "TT state not updated\n");
  425. } else {
  426. if (new_state == IWL_TI_CT_KILL)
  427. iwl_perform_ct_kill_task(priv, true);
  428. else if (tt->state == IWL_TI_CT_KILL &&
  429. new_state != IWL_TI_CT_KILL)
  430. iwl_perform_ct_kill_task(priv, false);
  431. tt->state = new_state;
  432. IWL_DEBUG_POWER(priv, "Temperature state changed %u\n",
  433. tt->state);
  434. IWL_DEBUG_POWER(priv, "Power Index change to %u\n",
  435. tt->tt_power_mode);
  436. }
  437. }
  438. }
  439. /*
  440. * Advance thermal throttling
  441. * 1) Avoid NIC destruction due to high temperatures
  442. * Chip will identify dangerously high temperatures that can
  443. * harm the device and will power down
  444. * 2) Avoid the NIC power down due to high temperature
  445. * Throttle early enough to lower the power consumption before
  446. * drastic steps are needed
  447. * Actions include relaxing the power down sleep thresholds and
  448. * decreasing the number of TX streams
  449. * 3) Avoid throughput performance impact as much as possible
  450. *
  451. *=============================================================================
  452. * Condition Nxt State Condition Nxt State Condition Nxt State
  453. *-----------------------------------------------------------------------------
  454. * IWL_TI_0 T >= 115 CT_KILL 115>T>=105 TI_1 N/A N/A
  455. * IWL_TI_1 T >= 115 CT_KILL 115>T>=110 TI_2 T<=95 TI_0
  456. * IWL_TI_2 T >= 115 CT_KILL T<=100 TI_1
  457. * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
  458. *=============================================================================
  459. */
  460. static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp)
  461. {
  462. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  463. int i;
  464. bool changed = false;
  465. enum iwl_tt_state old_state;
  466. struct iwl_tt_trans *transaction;
  467. old_state = tt->state;
  468. for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
  469. /* based on the current TT state,
  470. * find the curresponding transaction table
  471. * each table has (IWL_TI_STATE_MAX - 1) entries
  472. * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
  473. * will advance to the correct table.
  474. * then based on the current temperature
  475. * find the next state need to transaction to
  476. * go through all the possible (IWL_TI_STATE_MAX - 1) entries
  477. * in the current table to see if transaction is needed
  478. */
  479. transaction = tt->transaction +
  480. ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
  481. if (temp >= transaction->tt_low &&
  482. temp <= transaction->tt_high) {
  483. #ifdef CONFIG_IWLWIFI_DEBUG
  484. if ((tt->tt_previous_temp) &&
  485. (temp > tt->tt_previous_temp) &&
  486. ((temp - tt->tt_previous_temp) >
  487. IWL_TT_INCREASE_MARGIN)) {
  488. IWL_DEBUG_POWER(priv,
  489. "Temperature increase %d "
  490. "degree Celsius\n",
  491. (temp - tt->tt_previous_temp));
  492. }
  493. tt->tt_previous_temp = temp;
  494. #endif
  495. if (old_state !=
  496. transaction->next_state) {
  497. changed = true;
  498. tt->state =
  499. transaction->next_state;
  500. }
  501. break;
  502. }
  503. }
  504. if (changed) {
  505. struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
  506. struct iwl_power_mgr *setting = &priv->power_data;
  507. if (tt->state >= IWL_TI_1) {
  508. /* if switching from IWL_TI_0 to other TT state
  509. * save previous power setting in tt->sys_power_mode */
  510. if (old_state == IWL_TI_0)
  511. tt->sys_power_mode = setting->power_mode;
  512. /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
  513. tt->tt_power_mode = IWL_POWER_INDEX_5;
  514. if (!iwl_ht_enabled(priv))
  515. /* disable HT */
  516. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  517. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  518. RXON_FLG_HT40_PROT_MSK |
  519. RXON_FLG_HT_PROT_MSK);
  520. else {
  521. /* check HT capability and set
  522. * according to the system HT capability
  523. * in case get disabled before */
  524. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  525. }
  526. } else {
  527. /* restore system power setting */
  528. /* the previous power mode was saved in
  529. * tt->sys_power_mode when system move into
  530. * Thermal Throttling state
  531. * set power_data.user_power_setting to the previous
  532. * system power mode to make sure power will get
  533. * updated correctly
  534. */
  535. priv->power_data.user_power_setting =
  536. tt->sys_power_mode;
  537. tt->tt_power_mode = tt->sys_power_mode;
  538. /* check HT capability and set
  539. * according to the system HT capability
  540. * in case get disabled before */
  541. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  542. }
  543. if (iwl_power_update_mode(priv, true)) {
  544. /* TT state not updated
  545. * try again during next temperature read
  546. */
  547. IWL_ERR(priv, "Cannot update power mode, "
  548. "TT state not updated\n");
  549. tt->state = old_state;
  550. } else {
  551. IWL_DEBUG_POWER(priv,
  552. "Thermal Throttling to new state: %u\n",
  553. tt->state);
  554. if (old_state != IWL_TI_CT_KILL &&
  555. tt->state == IWL_TI_CT_KILL) {
  556. IWL_DEBUG_POWER(priv, "Enter IWL_TI_CT_KILL\n");
  557. iwl_perform_ct_kill_task(priv, true);
  558. } else if (old_state == IWL_TI_CT_KILL &&
  559. tt->state != IWL_TI_CT_KILL) {
  560. IWL_DEBUG_POWER(priv, "Exit IWL_TI_CT_KILL\n");
  561. iwl_perform_ct_kill_task(priv, false);
  562. }
  563. }
  564. }
  565. }
  566. /* Card State Notification indicated reach critical temperature
  567. * if PSP not enable, no Thermal Throttling function will be performed
  568. * just set the GP1 bit to acknowledge the event
  569. * otherwise, go into IWL_TI_CT_KILL state
  570. * since Card State Notification will not provide any temperature reading
  571. * for Legacy mode
  572. * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
  573. * for advance mode
  574. * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
  575. */
  576. void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
  577. {
  578. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  579. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  580. return;
  581. if (tt->state != IWL_TI_CT_KILL) {
  582. IWL_ERR(priv, "Device reached critical temperature "
  583. "- ucode going to sleep!\n");
  584. if (!priv->power_data.adv_tt)
  585. iwl_legacy_tt_handler(priv,
  586. IWL_MINIMAL_POWER_THRESHOLD);
  587. else
  588. iwl_advance_tt_handler(priv,
  589. CT_KILL_THRESHOLD + 1);
  590. }
  591. }
  592. EXPORT_SYMBOL(iwl_tt_enter_ct_kill);
  593. /* Card State Notification indicated out of critical temperature
  594. * since Card State Notification will not provide any temperature reading
  595. * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
  596. * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
  597. */
  598. void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
  599. {
  600. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  601. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  602. return;
  603. /* stop ct_kill_exit_tm timer */
  604. del_timer_sync(&priv->power_data.ct_kill_exit_tm);
  605. if (tt->state == IWL_TI_CT_KILL) {
  606. IWL_ERR(priv,
  607. "Device temperature below critical"
  608. "- ucode awake!\n");
  609. if (!priv->power_data.adv_tt)
  610. iwl_legacy_tt_handler(priv,
  611. IWL_REDUCED_PERFORMANCE_THRESHOLD_2);
  612. else
  613. iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD);
  614. }
  615. }
  616. EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
  617. void iwl_tt_handler(struct iwl_priv *priv)
  618. {
  619. s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
  620. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  621. return;
  622. if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
  623. temp = KELVIN_TO_CELSIUS(priv->temperature);
  624. if (!priv->power_data.adv_tt)
  625. iwl_legacy_tt_handler(priv, temp);
  626. else
  627. iwl_advance_tt_handler(priv, temp);
  628. }
  629. EXPORT_SYMBOL(iwl_tt_handler);
  630. /* Thermal throttling initialization
  631. * For advance thermal throttling:
  632. * Initialize Thermal Index and temperature threshold table
  633. * Initialize thermal throttling restriction table
  634. */
  635. void iwl_tt_initialize(struct iwl_priv *priv)
  636. {
  637. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  638. struct iwl_power_mgr *setting = &priv->power_data;
  639. int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
  640. struct iwl_tt_trans *transaction;
  641. IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");
  642. memset(tt, 0, sizeof(struct iwl_tt_mgmt));
  643. tt->state = IWL_TI_0;
  644. tt->sys_power_mode = setting->power_mode;
  645. tt->tt_power_mode = tt->sys_power_mode;
  646. init_timer(&priv->power_data.ct_kill_exit_tm);
  647. priv->power_data.ct_kill_exit_tm.data = (unsigned long)priv;
  648. priv->power_data.ct_kill_exit_tm.function = iwl_tt_check_exit_ct_kill;
  649. switch (priv->hw_rev & CSR_HW_REV_TYPE_MSK) {
  650. case CSR_HW_REV_TYPE_6x00:
  651. case CSR_HW_REV_TYPE_6x50:
  652. IWL_DEBUG_POWER(priv, "Advanced Thermal Throttling\n");
  653. tt->restriction = kzalloc(sizeof(struct iwl_tt_restriction) *
  654. IWL_TI_STATE_MAX, GFP_KERNEL);
  655. tt->transaction = kzalloc(sizeof(struct iwl_tt_trans) *
  656. IWL_TI_STATE_MAX * (IWL_TI_STATE_MAX - 1),
  657. GFP_KERNEL);
  658. if (!tt->restriction || !tt->transaction) {
  659. IWL_ERR(priv, "Fallback to Legacy Throttling\n");
  660. priv->power_data.adv_tt = false;
  661. kfree(tt->restriction);
  662. tt->restriction = NULL;
  663. kfree(tt->transaction);
  664. tt->transaction = NULL;
  665. } else {
  666. transaction = tt->transaction +
  667. (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
  668. memcpy(transaction, &tt_range_0[0], size);
  669. transaction = tt->transaction +
  670. (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
  671. memcpy(transaction, &tt_range_1[0], size);
  672. transaction = tt->transaction +
  673. (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
  674. memcpy(transaction, &tt_range_2[0], size);
  675. transaction = tt->transaction +
  676. (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
  677. memcpy(transaction, &tt_range_3[0], size);
  678. size = sizeof(struct iwl_tt_restriction) *
  679. IWL_TI_STATE_MAX;
  680. memcpy(tt->restriction,
  681. &restriction_range[0], size);
  682. priv->power_data.adv_tt = true;
  683. }
  684. break;
  685. default:
  686. IWL_DEBUG_POWER(priv, "Legacy Thermal Throttling\n");
  687. priv->power_data.adv_tt = false;
  688. break;
  689. }
  690. }
  691. EXPORT_SYMBOL(iwl_tt_initialize);
  692. /* cleanup thermal throttling management related memory and timer */
  693. void iwl_tt_exit(struct iwl_priv *priv)
  694. {
  695. struct iwl_tt_mgmt *tt = &priv->power_data.tt;
  696. /* stop ct_kill_exit_tm timer if activated */
  697. del_timer_sync(&priv->power_data.ct_kill_exit_tm);
  698. if (priv->power_data.adv_tt) {
  699. /* free advance thermal throttling memory */
  700. kfree(tt->restriction);
  701. tt->restriction = NULL;
  702. kfree(tt->transaction);
  703. tt->transaction = NULL;
  704. }
  705. }
  706. EXPORT_SYMBOL(iwl_tt_exit);
  707. /* initialize to default */
  708. void iwl_power_initialize(struct iwl_priv *priv)
  709. {
  710. iwl_power_init_handle(priv);
  711. priv->power_data.user_power_setting = IWL_POWER_INDEX_1;
  712. /* default to disabled until mac80211 says otherwise */
  713. priv->power_data.power_disabled = 1;
  714. }
  715. EXPORT_SYMBOL(iwl_power_initialize);