tt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2012 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-io.h"
  34. #include "iwl-modparams.h"
  35. #include "iwl-debug.h"
  36. #include "agn.h"
  37. #include "dev.h"
  38. #include "commands.h"
  39. #include "tt.h"
  40. /* default Thermal Throttling transaction table
  41. * Current state | Throttling Down | Throttling Up
  42. *=============================================================================
  43. * Condition Nxt State Condition Nxt State Condition Nxt State
  44. *-----------------------------------------------------------------------------
  45. * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
  46. * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
  47. * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
  48. * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
  49. *=============================================================================
  50. */
  51. static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
  52. {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
  53. {IWL_TI_1, 105, CT_KILL_THRESHOLD - 1},
  54. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
  55. };
  56. static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
  57. {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
  58. {IWL_TI_2, 110, CT_KILL_THRESHOLD - 1},
  59. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
  60. };
  61. static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
  62. {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
  63. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX},
  64. {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
  65. };
  66. static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
  67. {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
  68. {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
  69. {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
  70. };
  71. /* Advance Thermal Throttling default restriction table */
  72. static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
  73. {IWL_ANT_OK_MULTI, IWL_ANT_OK_MULTI, true },
  74. {IWL_ANT_OK_SINGLE, IWL_ANT_OK_MULTI, true },
  75. {IWL_ANT_OK_SINGLE, IWL_ANT_OK_SINGLE, false },
  76. {IWL_ANT_OK_NONE, IWL_ANT_OK_NONE, false }
  77. };
  78. bool iwl_tt_is_low_power_state(struct iwl_priv *priv)
  79. {
  80. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  81. if (tt->state >= IWL_TI_1)
  82. return true;
  83. return false;
  84. }
  85. u8 iwl_tt_current_power_mode(struct iwl_priv *priv)
  86. {
  87. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  88. return tt->tt_power_mode;
  89. }
  90. bool iwl_ht_enabled(struct iwl_priv *priv)
  91. {
  92. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  93. struct iwl_tt_restriction *restriction;
  94. if (!priv->thermal_throttle.advanced_tt)
  95. return true;
  96. restriction = tt->restriction + tt->state;
  97. return restriction->is_ht;
  98. }
  99. static bool iwl_within_ct_kill_margin(struct iwl_priv *priv)
  100. {
  101. s32 temp = priv->temperature; /* degrees CELSIUS except specified */
  102. bool within_margin = false;
  103. if (!priv->thermal_throttle.advanced_tt)
  104. within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
  105. CT_KILL_THRESHOLD_LEGACY) ? true : false;
  106. else
  107. within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
  108. CT_KILL_THRESHOLD) ? true : false;
  109. return within_margin;
  110. }
  111. bool iwl_check_for_ct_kill(struct iwl_priv *priv)
  112. {
  113. bool is_ct_kill = false;
  114. if (iwl_within_ct_kill_margin(priv)) {
  115. iwl_tt_enter_ct_kill(priv);
  116. is_ct_kill = true;
  117. }
  118. return is_ct_kill;
  119. }
  120. enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
  121. {
  122. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  123. struct iwl_tt_restriction *restriction;
  124. if (!priv->thermal_throttle.advanced_tt)
  125. return IWL_ANT_OK_MULTI;
  126. restriction = tt->restriction + tt->state;
  127. return restriction->tx_stream;
  128. }
  129. enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
  130. {
  131. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  132. struct iwl_tt_restriction *restriction;
  133. if (!priv->thermal_throttle.advanced_tt)
  134. return IWL_ANT_OK_MULTI;
  135. restriction = tt->restriction + tt->state;
  136. return restriction->rx_stream;
  137. }
  138. #define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
  139. #define CT_KILL_WAITING_DURATION (300) /* 300ms duration */
  140. /*
  141. * toggle the bit to wake up uCode and check the temperature
  142. * if the temperature is below CT, uCode will stay awake and send card
  143. * state notification with CT_KILL bit clear to inform Thermal Throttling
  144. * Management to change state. Otherwise, uCode will go back to sleep
  145. * without doing anything, driver should continue the 5 seconds timer
  146. * to wake up uCode for temperature check until temperature drop below CT
  147. */
  148. static void iwl_tt_check_exit_ct_kill(unsigned long data)
  149. {
  150. struct iwl_priv *priv = (struct iwl_priv *)data;
  151. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  152. unsigned long flags;
  153. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  154. return;
  155. if (tt->state == IWL_TI_CT_KILL) {
  156. if (priv->thermal_throttle.ct_kill_toggle) {
  157. iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
  158. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  159. priv->thermal_throttle.ct_kill_toggle = false;
  160. } else {
  161. iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
  162. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  163. priv->thermal_throttle.ct_kill_toggle = true;
  164. }
  165. iwl_read32(priv->trans, CSR_UCODE_DRV_GP1);
  166. spin_lock_irqsave(&priv->trans->reg_lock, flags);
  167. if (likely(iwl_grab_nic_access(priv->trans)))
  168. iwl_release_nic_access(priv->trans);
  169. spin_unlock_irqrestore(&priv->trans->reg_lock, flags);
  170. /* Reschedule the ct_kill timer to occur in
  171. * CT_KILL_EXIT_DURATION seconds to ensure we get a
  172. * thermal update */
  173. IWL_DEBUG_TEMP(priv, "schedule ct_kill exit timer\n");
  174. mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
  175. jiffies + CT_KILL_EXIT_DURATION * HZ);
  176. }
  177. }
  178. static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
  179. bool stop)
  180. {
  181. if (stop) {
  182. IWL_DEBUG_TEMP(priv, "Stop all queues\n");
  183. if (priv->mac80211_registered)
  184. ieee80211_stop_queues(priv->hw);
  185. IWL_DEBUG_TEMP(priv,
  186. "Schedule 5 seconds CT_KILL Timer\n");
  187. mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
  188. jiffies + CT_KILL_EXIT_DURATION * HZ);
  189. } else {
  190. IWL_DEBUG_TEMP(priv, "Wake all queues\n");
  191. if (priv->mac80211_registered)
  192. ieee80211_wake_queues(priv->hw);
  193. }
  194. }
  195. static void iwl_tt_ready_for_ct_kill(unsigned long data)
  196. {
  197. struct iwl_priv *priv = (struct iwl_priv *)data;
  198. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  199. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  200. return;
  201. /* temperature timer expired, ready to go into CT_KILL state */
  202. if (tt->state != IWL_TI_CT_KILL) {
  203. IWL_DEBUG_TEMP(priv, "entering CT_KILL state when "
  204. "temperature timer expired\n");
  205. tt->state = IWL_TI_CT_KILL;
  206. set_bit(STATUS_CT_KILL, &priv->status);
  207. iwl_perform_ct_kill_task(priv, true);
  208. }
  209. }
  210. static void iwl_prepare_ct_kill_task(struct iwl_priv *priv)
  211. {
  212. IWL_DEBUG_TEMP(priv, "Prepare to enter IWL_TI_CT_KILL\n");
  213. /* make request to retrieve statistics information */
  214. iwl_send_statistics_request(priv, CMD_SYNC, false);
  215. /* Reschedule the ct_kill wait timer */
  216. mod_timer(&priv->thermal_throttle.ct_kill_waiting_tm,
  217. jiffies + msecs_to_jiffies(CT_KILL_WAITING_DURATION));
  218. }
  219. #define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
  220. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
  221. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
  222. /*
  223. * Legacy thermal throttling
  224. * 1) Avoid NIC destruction due to high temperatures
  225. * Chip will identify dangerously high temperatures that can
  226. * harm the device and will power down
  227. * 2) Avoid the NIC power down due to high temperature
  228. * Throttle early enough to lower the power consumption before
  229. * drastic steps are needed
  230. */
  231. static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
  232. {
  233. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  234. enum iwl_tt_state old_state;
  235. #ifdef CONFIG_IWLWIFI_DEBUG
  236. if ((tt->tt_previous_temp) &&
  237. (temp > tt->tt_previous_temp) &&
  238. ((temp - tt->tt_previous_temp) >
  239. IWL_TT_INCREASE_MARGIN)) {
  240. IWL_DEBUG_TEMP(priv,
  241. "Temperature increase %d degree Celsius\n",
  242. (temp - tt->tt_previous_temp));
  243. }
  244. #endif
  245. old_state = tt->state;
  246. /* in Celsius */
  247. if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
  248. tt->state = IWL_TI_CT_KILL;
  249. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
  250. tt->state = IWL_TI_2;
  251. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
  252. tt->state = IWL_TI_1;
  253. else
  254. tt->state = IWL_TI_0;
  255. #ifdef CONFIG_IWLWIFI_DEBUG
  256. tt->tt_previous_temp = temp;
  257. #endif
  258. /* stop ct_kill_waiting_tm timer */
  259. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  260. if (tt->state != old_state) {
  261. switch (tt->state) {
  262. case IWL_TI_0:
  263. /*
  264. * When the system is ready to go back to IWL_TI_0
  265. * we only have to call iwl_power_update_mode() to
  266. * do so.
  267. */
  268. break;
  269. case IWL_TI_1:
  270. tt->tt_power_mode = IWL_POWER_INDEX_3;
  271. break;
  272. case IWL_TI_2:
  273. tt->tt_power_mode = IWL_POWER_INDEX_4;
  274. break;
  275. default:
  276. tt->tt_power_mode = IWL_POWER_INDEX_5;
  277. break;
  278. }
  279. mutex_lock(&priv->mutex);
  280. if (old_state == IWL_TI_CT_KILL)
  281. clear_bit(STATUS_CT_KILL, &priv->status);
  282. if (tt->state != IWL_TI_CT_KILL &&
  283. iwl_power_update_mode(priv, true)) {
  284. /* TT state not updated
  285. * try again during next temperature read
  286. */
  287. if (old_state == IWL_TI_CT_KILL)
  288. set_bit(STATUS_CT_KILL, &priv->status);
  289. tt->state = old_state;
  290. IWL_ERR(priv, "Cannot update power mode, "
  291. "TT state not updated\n");
  292. } else {
  293. if (tt->state == IWL_TI_CT_KILL) {
  294. if (force) {
  295. set_bit(STATUS_CT_KILL, &priv->status);
  296. iwl_perform_ct_kill_task(priv, true);
  297. } else {
  298. iwl_prepare_ct_kill_task(priv);
  299. tt->state = old_state;
  300. }
  301. } else if (old_state == IWL_TI_CT_KILL &&
  302. tt->state != IWL_TI_CT_KILL)
  303. iwl_perform_ct_kill_task(priv, false);
  304. IWL_DEBUG_TEMP(priv, "Temperature state changed %u\n",
  305. tt->state);
  306. IWL_DEBUG_TEMP(priv, "Power Index change to %u\n",
  307. tt->tt_power_mode);
  308. }
  309. mutex_unlock(&priv->mutex);
  310. }
  311. }
  312. /*
  313. * Advance thermal throttling
  314. * 1) Avoid NIC destruction due to high temperatures
  315. * Chip will identify dangerously high temperatures that can
  316. * harm the device and will power down
  317. * 2) Avoid the NIC power down due to high temperature
  318. * Throttle early enough to lower the power consumption before
  319. * drastic steps are needed
  320. * Actions include relaxing the power down sleep thresholds and
  321. * decreasing the number of TX streams
  322. * 3) Avoid throughput performance impact as much as possible
  323. *
  324. *=============================================================================
  325. * Condition Nxt State Condition Nxt State Condition Nxt State
  326. *-----------------------------------------------------------------------------
  327. * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
  328. * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
  329. * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
  330. * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
  331. *=============================================================================
  332. */
  333. static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
  334. {
  335. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  336. int i;
  337. bool changed = false;
  338. enum iwl_tt_state old_state;
  339. struct iwl_tt_trans *transaction;
  340. old_state = tt->state;
  341. for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
  342. /* based on the current TT state,
  343. * find the curresponding transaction table
  344. * each table has (IWL_TI_STATE_MAX - 1) entries
  345. * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
  346. * will advance to the correct table.
  347. * then based on the current temperature
  348. * find the next state need to transaction to
  349. * go through all the possible (IWL_TI_STATE_MAX - 1) entries
  350. * in the current table to see if transaction is needed
  351. */
  352. transaction = tt->transaction +
  353. ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
  354. if (temp >= transaction->tt_low &&
  355. temp <= transaction->tt_high) {
  356. #ifdef CONFIG_IWLWIFI_DEBUG
  357. if ((tt->tt_previous_temp) &&
  358. (temp > tt->tt_previous_temp) &&
  359. ((temp - tt->tt_previous_temp) >
  360. IWL_TT_INCREASE_MARGIN)) {
  361. IWL_DEBUG_TEMP(priv,
  362. "Temperature increase %d "
  363. "degree Celsius\n",
  364. (temp - tt->tt_previous_temp));
  365. }
  366. tt->tt_previous_temp = temp;
  367. #endif
  368. if (old_state !=
  369. transaction->next_state) {
  370. changed = true;
  371. tt->state =
  372. transaction->next_state;
  373. }
  374. break;
  375. }
  376. }
  377. /* stop ct_kill_waiting_tm timer */
  378. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  379. if (changed) {
  380. if (tt->state >= IWL_TI_1) {
  381. /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
  382. tt->tt_power_mode = IWL_POWER_INDEX_5;
  383. if (!iwl_ht_enabled(priv)) {
  384. struct iwl_rxon_context *ctx;
  385. for_each_context(priv, ctx) {
  386. struct iwl_rxon_cmd *rxon;
  387. rxon = &ctx->staging;
  388. /* disable HT */
  389. rxon->flags &= ~(
  390. RXON_FLG_CHANNEL_MODE_MSK |
  391. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  392. RXON_FLG_HT40_PROT_MSK |
  393. RXON_FLG_HT_PROT_MSK);
  394. }
  395. } else {
  396. /* check HT capability and set
  397. * according to the system HT capability
  398. * in case get disabled before */
  399. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  400. }
  401. } else {
  402. /*
  403. * restore system power setting -- it will be
  404. * recalculated automatically.
  405. */
  406. /* check HT capability and set
  407. * according to the system HT capability
  408. * in case get disabled before */
  409. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  410. }
  411. mutex_lock(&priv->mutex);
  412. if (old_state == IWL_TI_CT_KILL)
  413. clear_bit(STATUS_CT_KILL, &priv->status);
  414. if (tt->state != IWL_TI_CT_KILL &&
  415. iwl_power_update_mode(priv, true)) {
  416. /* TT state not updated
  417. * try again during next temperature read
  418. */
  419. IWL_ERR(priv, "Cannot update power mode, "
  420. "TT state not updated\n");
  421. if (old_state == IWL_TI_CT_KILL)
  422. set_bit(STATUS_CT_KILL, &priv->status);
  423. tt->state = old_state;
  424. } else {
  425. IWL_DEBUG_TEMP(priv,
  426. "Thermal Throttling to new state: %u\n",
  427. tt->state);
  428. if (old_state != IWL_TI_CT_KILL &&
  429. tt->state == IWL_TI_CT_KILL) {
  430. if (force) {
  431. IWL_DEBUG_TEMP(priv,
  432. "Enter IWL_TI_CT_KILL\n");
  433. set_bit(STATUS_CT_KILL, &priv->status);
  434. iwl_perform_ct_kill_task(priv, true);
  435. } else {
  436. iwl_prepare_ct_kill_task(priv);
  437. tt->state = old_state;
  438. }
  439. } else if (old_state == IWL_TI_CT_KILL &&
  440. tt->state != IWL_TI_CT_KILL) {
  441. IWL_DEBUG_TEMP(priv, "Exit IWL_TI_CT_KILL\n");
  442. iwl_perform_ct_kill_task(priv, false);
  443. }
  444. }
  445. mutex_unlock(&priv->mutex);
  446. }
  447. }
  448. /* Card State Notification indicated reach critical temperature
  449. * if PSP not enable, no Thermal Throttling function will be performed
  450. * just set the GP1 bit to acknowledge the event
  451. * otherwise, go into IWL_TI_CT_KILL state
  452. * since Card State Notification will not provide any temperature reading
  453. * for Legacy mode
  454. * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
  455. * for advance mode
  456. * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
  457. */
  458. static void iwl_bg_ct_enter(struct work_struct *work)
  459. {
  460. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
  461. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  462. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  463. return;
  464. if (!iwl_is_ready(priv))
  465. return;
  466. if (tt->state != IWL_TI_CT_KILL) {
  467. IWL_ERR(priv, "Device reached critical temperature "
  468. "- ucode going to sleep!\n");
  469. if (!priv->thermal_throttle.advanced_tt)
  470. iwl_legacy_tt_handler(priv,
  471. IWL_MINIMAL_POWER_THRESHOLD,
  472. true);
  473. else
  474. iwl_advance_tt_handler(priv,
  475. CT_KILL_THRESHOLD + 1, true);
  476. }
  477. }
  478. /* Card State Notification indicated out of critical temperature
  479. * since Card State Notification will not provide any temperature reading
  480. * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
  481. * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
  482. */
  483. static void iwl_bg_ct_exit(struct work_struct *work)
  484. {
  485. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
  486. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  487. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  488. return;
  489. if (!iwl_is_ready(priv))
  490. return;
  491. /* stop ct_kill_exit_tm timer */
  492. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  493. if (tt->state == IWL_TI_CT_KILL) {
  494. IWL_ERR(priv,
  495. "Device temperature below critical"
  496. "- ucode awake!\n");
  497. /*
  498. * exit from CT_KILL state
  499. * reset the current temperature reading
  500. */
  501. priv->temperature = 0;
  502. if (!priv->thermal_throttle.advanced_tt)
  503. iwl_legacy_tt_handler(priv,
  504. IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
  505. true);
  506. else
  507. iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
  508. true);
  509. }
  510. }
  511. void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
  512. {
  513. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  514. return;
  515. IWL_DEBUG_TEMP(priv, "Queueing critical temperature enter.\n");
  516. queue_work(priv->workqueue, &priv->ct_enter);
  517. }
  518. void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
  519. {
  520. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  521. return;
  522. IWL_DEBUG_TEMP(priv, "Queueing critical temperature exit.\n");
  523. queue_work(priv->workqueue, &priv->ct_exit);
  524. }
  525. static void iwl_bg_tt_work(struct work_struct *work)
  526. {
  527. struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
  528. s32 temp = priv->temperature; /* degrees CELSIUS except specified */
  529. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  530. return;
  531. if (!priv->thermal_throttle.advanced_tt)
  532. iwl_legacy_tt_handler(priv, temp, false);
  533. else
  534. iwl_advance_tt_handler(priv, temp, false);
  535. }
  536. void iwl_tt_handler(struct iwl_priv *priv)
  537. {
  538. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  539. return;
  540. IWL_DEBUG_TEMP(priv, "Queueing thermal throttling work.\n");
  541. queue_work(priv->workqueue, &priv->tt_work);
  542. }
  543. /* Thermal throttling initialization
  544. * For advance thermal throttling:
  545. * Initialize Thermal Index and temperature threshold table
  546. * Initialize thermal throttling restriction table
  547. */
  548. void iwl_tt_initialize(struct iwl_priv *priv)
  549. {
  550. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  551. int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
  552. struct iwl_tt_trans *transaction;
  553. IWL_DEBUG_TEMP(priv, "Initialize Thermal Throttling\n");
  554. memset(tt, 0, sizeof(struct iwl_tt_mgmt));
  555. tt->state = IWL_TI_0;
  556. init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
  557. priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
  558. priv->thermal_throttle.ct_kill_exit_tm.function =
  559. iwl_tt_check_exit_ct_kill;
  560. init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
  561. priv->thermal_throttle.ct_kill_waiting_tm.data =
  562. (unsigned long)priv;
  563. priv->thermal_throttle.ct_kill_waiting_tm.function =
  564. iwl_tt_ready_for_ct_kill;
  565. /* setup deferred ct kill work */
  566. INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
  567. INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
  568. INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
  569. if (priv->cfg->base_params->adv_thermal_throttle) {
  570. IWL_DEBUG_TEMP(priv, "Advanced Thermal Throttling\n");
  571. tt->restriction = kcalloc(IWL_TI_STATE_MAX,
  572. sizeof(struct iwl_tt_restriction),
  573. GFP_KERNEL);
  574. tt->transaction = kcalloc(IWL_TI_STATE_MAX *
  575. (IWL_TI_STATE_MAX - 1),
  576. sizeof(struct iwl_tt_trans),
  577. GFP_KERNEL);
  578. if (!tt->restriction || !tt->transaction) {
  579. IWL_ERR(priv, "Fallback to Legacy Throttling\n");
  580. priv->thermal_throttle.advanced_tt = false;
  581. kfree(tt->restriction);
  582. tt->restriction = NULL;
  583. kfree(tt->transaction);
  584. tt->transaction = NULL;
  585. } else {
  586. transaction = tt->transaction +
  587. (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
  588. memcpy(transaction, &tt_range_0[0], size);
  589. transaction = tt->transaction +
  590. (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
  591. memcpy(transaction, &tt_range_1[0], size);
  592. transaction = tt->transaction +
  593. (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
  594. memcpy(transaction, &tt_range_2[0], size);
  595. transaction = tt->transaction +
  596. (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
  597. memcpy(transaction, &tt_range_3[0], size);
  598. size = sizeof(struct iwl_tt_restriction) *
  599. IWL_TI_STATE_MAX;
  600. memcpy(tt->restriction,
  601. &restriction_range[0], size);
  602. priv->thermal_throttle.advanced_tt = true;
  603. }
  604. } else {
  605. IWL_DEBUG_TEMP(priv, "Legacy Thermal Throttling\n");
  606. priv->thermal_throttle.advanced_tt = false;
  607. }
  608. }
  609. /* cleanup thermal throttling management related memory and timer */
  610. void iwl_tt_exit(struct iwl_priv *priv)
  611. {
  612. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  613. /* stop ct_kill_exit_tm timer if activated */
  614. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  615. /* stop ct_kill_waiting_tm timer if activated */
  616. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  617. cancel_work_sync(&priv->tt_work);
  618. cancel_work_sync(&priv->ct_enter);
  619. cancel_work_sync(&priv->ct_exit);
  620. if (priv->thermal_throttle.advanced_tt) {
  621. /* free advance thermal throttling memory */
  622. kfree(tt->restriction);
  623. tt->restriction = NULL;
  624. kfree(tt->transaction);
  625. tt->transaction = NULL;
  626. }
  627. }