iwl-agn-tt.c 22 KB

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