tt.c 22 KB

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