iwl-agn-tt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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->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 (likely(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->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->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->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->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, &priv->status);
  297. iwl_perform_ct_kill_task(priv, true);
  298. } else {
  299. iwl_prepare_ct_kill_task(priv);
  300. tt->state = old_state;
  301. }
  302. } else if (old_state == IWL_TI_CT_KILL &&
  303. tt->state != IWL_TI_CT_KILL)
  304. iwl_perform_ct_kill_task(priv, false);
  305. IWL_DEBUG_TEMP(priv, "Temperature state changed %u\n",
  306. tt->state);
  307. IWL_DEBUG_TEMP(priv, "Power Index change to %u\n",
  308. tt->tt_power_mode);
  309. }
  310. mutex_unlock(&priv->mutex);
  311. }
  312. }
  313. /*
  314. * Advance thermal throttling
  315. * 1) Avoid NIC destruction due to high temperatures
  316. * Chip will identify dangerously high temperatures that can
  317. * harm the device and will power down
  318. * 2) Avoid the NIC power down due to high temperature
  319. * Throttle early enough to lower the power consumption before
  320. * drastic steps are needed
  321. * Actions include relaxing the power down sleep thresholds and
  322. * decreasing the number of TX streams
  323. * 3) Avoid throughput performance impact as much as possible
  324. *
  325. *=============================================================================
  326. * Condition Nxt State Condition Nxt State Condition Nxt State
  327. *-----------------------------------------------------------------------------
  328. * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
  329. * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
  330. * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
  331. * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
  332. *=============================================================================
  333. */
  334. static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
  335. {
  336. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  337. int i;
  338. bool changed = false;
  339. enum iwl_tt_state old_state;
  340. struct iwl_tt_trans *transaction;
  341. old_state = tt->state;
  342. for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
  343. /* based on the current TT state,
  344. * find the curresponding transaction table
  345. * each table has (IWL_TI_STATE_MAX - 1) entries
  346. * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
  347. * will advance to the correct table.
  348. * then based on the current temperature
  349. * find the next state need to transaction to
  350. * go through all the possible (IWL_TI_STATE_MAX - 1) entries
  351. * in the current table to see if transaction is needed
  352. */
  353. transaction = tt->transaction +
  354. ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
  355. if (temp >= transaction->tt_low &&
  356. temp <= transaction->tt_high) {
  357. #ifdef CONFIG_IWLWIFI_DEBUG
  358. if ((tt->tt_previous_temp) &&
  359. (temp > tt->tt_previous_temp) &&
  360. ((temp - tt->tt_previous_temp) >
  361. IWL_TT_INCREASE_MARGIN)) {
  362. IWL_DEBUG_TEMP(priv,
  363. "Temperature increase %d "
  364. "degree Celsius\n",
  365. (temp - tt->tt_previous_temp));
  366. }
  367. tt->tt_previous_temp = temp;
  368. #endif
  369. if (old_state !=
  370. transaction->next_state) {
  371. changed = true;
  372. tt->state =
  373. transaction->next_state;
  374. }
  375. break;
  376. }
  377. }
  378. /* stop ct_kill_waiting_tm timer */
  379. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  380. if (changed) {
  381. if (tt->state >= IWL_TI_1) {
  382. /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
  383. tt->tt_power_mode = IWL_POWER_INDEX_5;
  384. if (!iwl_ht_enabled(priv)) {
  385. struct iwl_rxon_context *ctx;
  386. for_each_context(priv, ctx) {
  387. struct iwl_rxon_cmd *rxon;
  388. rxon = &ctx->staging;
  389. /* disable HT */
  390. rxon->flags &= ~(
  391. RXON_FLG_CHANNEL_MODE_MSK |
  392. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  393. RXON_FLG_HT40_PROT_MSK |
  394. RXON_FLG_HT_PROT_MSK);
  395. }
  396. } else {
  397. /* check HT capability and set
  398. * according to the system HT capability
  399. * in case get disabled before */
  400. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  401. }
  402. } else {
  403. /*
  404. * restore system power setting -- it will be
  405. * recalculated automatically.
  406. */
  407. /* check HT capability and set
  408. * according to the system HT capability
  409. * in case get disabled before */
  410. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  411. }
  412. mutex_lock(&priv->mutex);
  413. if (old_state == IWL_TI_CT_KILL)
  414. clear_bit(STATUS_CT_KILL, &priv->status);
  415. if (tt->state != IWL_TI_CT_KILL &&
  416. iwl_power_update_mode(priv, true)) {
  417. /* TT state not updated
  418. * try again during next temperature read
  419. */
  420. IWL_ERR(priv, "Cannot update power mode, "
  421. "TT state not updated\n");
  422. if (old_state == IWL_TI_CT_KILL)
  423. set_bit(STATUS_CT_KILL, &priv->status);
  424. tt->state = old_state;
  425. } else {
  426. IWL_DEBUG_TEMP(priv,
  427. "Thermal Throttling to new state: %u\n",
  428. tt->state);
  429. if (old_state != IWL_TI_CT_KILL &&
  430. tt->state == IWL_TI_CT_KILL) {
  431. if (force) {
  432. IWL_DEBUG_TEMP(priv,
  433. "Enter IWL_TI_CT_KILL\n");
  434. set_bit(STATUS_CT_KILL, &priv->status);
  435. iwl_perform_ct_kill_task(priv, true);
  436. } else {
  437. iwl_prepare_ct_kill_task(priv);
  438. tt->state = old_state;
  439. }
  440. } else if (old_state == IWL_TI_CT_KILL &&
  441. tt->state != IWL_TI_CT_KILL) {
  442. IWL_DEBUG_TEMP(priv, "Exit IWL_TI_CT_KILL\n");
  443. iwl_perform_ct_kill_task(priv, false);
  444. }
  445. }
  446. mutex_unlock(&priv->mutex);
  447. }
  448. }
  449. /* Card State Notification indicated reach critical temperature
  450. * if PSP not enable, no Thermal Throttling function will be performed
  451. * just set the GP1 bit to acknowledge the event
  452. * otherwise, go into IWL_TI_CT_KILL state
  453. * since Card State Notification will not provide any temperature reading
  454. * for Legacy mode
  455. * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
  456. * for advance mode
  457. * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
  458. */
  459. static void iwl_bg_ct_enter(struct work_struct *work)
  460. {
  461. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
  462. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  463. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  464. return;
  465. if (!iwl_is_ready(priv))
  466. return;
  467. if (tt->state != IWL_TI_CT_KILL) {
  468. IWL_ERR(priv, "Device reached critical temperature "
  469. "- ucode going to sleep!\n");
  470. if (!priv->thermal_throttle.advanced_tt)
  471. iwl_legacy_tt_handler(priv,
  472. IWL_MINIMAL_POWER_THRESHOLD,
  473. true);
  474. else
  475. iwl_advance_tt_handler(priv,
  476. CT_KILL_THRESHOLD + 1, true);
  477. }
  478. }
  479. /* Card State Notification indicated out of critical temperature
  480. * since Card State Notification will not provide any temperature reading
  481. * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
  482. * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
  483. */
  484. static void iwl_bg_ct_exit(struct work_struct *work)
  485. {
  486. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
  487. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  488. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  489. return;
  490. if (!iwl_is_ready(priv))
  491. return;
  492. /* stop ct_kill_exit_tm timer */
  493. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  494. if (tt->state == IWL_TI_CT_KILL) {
  495. IWL_ERR(priv,
  496. "Device temperature below critical"
  497. "- ucode awake!\n");
  498. /*
  499. * exit from CT_KILL state
  500. * reset the current temperature reading
  501. */
  502. priv->temperature = 0;
  503. if (!priv->thermal_throttle.advanced_tt)
  504. iwl_legacy_tt_handler(priv,
  505. IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
  506. true);
  507. else
  508. iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
  509. true);
  510. }
  511. }
  512. void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
  513. {
  514. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  515. return;
  516. IWL_DEBUG_TEMP(priv, "Queueing critical temperature enter.\n");
  517. queue_work(priv->workqueue, &priv->ct_enter);
  518. }
  519. void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
  520. {
  521. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  522. return;
  523. IWL_DEBUG_TEMP(priv, "Queueing critical temperature exit.\n");
  524. queue_work(priv->workqueue, &priv->ct_exit);
  525. }
  526. static void iwl_bg_tt_work(struct work_struct *work)
  527. {
  528. struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
  529. s32 temp = priv->temperature; /* degrees CELSIUS except specified */
  530. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  531. return;
  532. if (!priv->thermal_throttle.advanced_tt)
  533. iwl_legacy_tt_handler(priv, temp, false);
  534. else
  535. iwl_advance_tt_handler(priv, temp, false);
  536. }
  537. void iwl_tt_handler(struct iwl_priv *priv)
  538. {
  539. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  540. return;
  541. IWL_DEBUG_TEMP(priv, "Queueing thermal throttling work.\n");
  542. queue_work(priv->workqueue, &priv->tt_work);
  543. }
  544. /* Thermal throttling initialization
  545. * For advance thermal throttling:
  546. * Initialize Thermal Index and temperature threshold table
  547. * Initialize thermal throttling restriction table
  548. */
  549. void iwl_tt_initialize(struct iwl_priv *priv)
  550. {
  551. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  552. int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
  553. struct iwl_tt_trans *transaction;
  554. IWL_DEBUG_TEMP(priv, "Initialize Thermal Throttling\n");
  555. memset(tt, 0, sizeof(struct iwl_tt_mgmt));
  556. tt->state = IWL_TI_0;
  557. init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
  558. priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
  559. priv->thermal_throttle.ct_kill_exit_tm.function =
  560. iwl_tt_check_exit_ct_kill;
  561. init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
  562. priv->thermal_throttle.ct_kill_waiting_tm.data =
  563. (unsigned long)priv;
  564. priv->thermal_throttle.ct_kill_waiting_tm.function =
  565. iwl_tt_ready_for_ct_kill;
  566. /* setup deferred ct kill work */
  567. INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
  568. INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
  569. INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
  570. if (cfg(priv)->base_params->adv_thermal_throttle) {
  571. IWL_DEBUG_TEMP(priv, "Advanced Thermal Throttling\n");
  572. tt->restriction = kcalloc(IWL_TI_STATE_MAX,
  573. sizeof(struct iwl_tt_restriction),
  574. GFP_KERNEL);
  575. tt->transaction = kcalloc(IWL_TI_STATE_MAX *
  576. (IWL_TI_STATE_MAX - 1),
  577. sizeof(struct iwl_tt_trans),
  578. GFP_KERNEL);
  579. if (!tt->restriction || !tt->transaction) {
  580. IWL_ERR(priv, "Fallback to Legacy Throttling\n");
  581. priv->thermal_throttle.advanced_tt = false;
  582. kfree(tt->restriction);
  583. tt->restriction = NULL;
  584. kfree(tt->transaction);
  585. tt->transaction = NULL;
  586. } else {
  587. transaction = tt->transaction +
  588. (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
  589. memcpy(transaction, &tt_range_0[0], size);
  590. transaction = tt->transaction +
  591. (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
  592. memcpy(transaction, &tt_range_1[0], size);
  593. transaction = tt->transaction +
  594. (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
  595. memcpy(transaction, &tt_range_2[0], size);
  596. transaction = tt->transaction +
  597. (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
  598. memcpy(transaction, &tt_range_3[0], size);
  599. size = sizeof(struct iwl_tt_restriction) *
  600. IWL_TI_STATE_MAX;
  601. memcpy(tt->restriction,
  602. &restriction_range[0], size);
  603. priv->thermal_throttle.advanced_tt = true;
  604. }
  605. } else {
  606. IWL_DEBUG_TEMP(priv, "Legacy Thermal Throttling\n");
  607. priv->thermal_throttle.advanced_tt = false;
  608. }
  609. }
  610. /* cleanup thermal throttling management related memory and timer */
  611. void iwl_tt_exit(struct iwl_priv *priv)
  612. {
  613. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  614. /* stop ct_kill_exit_tm timer if activated */
  615. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  616. /* stop ct_kill_waiting_tm timer if activated */
  617. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  618. cancel_work_sync(&priv->tt_work);
  619. cancel_work_sync(&priv->ct_enter);
  620. cancel_work_sync(&priv->ct_exit);
  621. if (priv->thermal_throttle.advanced_tt) {
  622. /* free advance thermal throttling memory */
  623. kfree(tt->restriction);
  624. tt->restriction = NULL;
  625. kfree(tt->transaction);
  626. tt->transaction = NULL;
  627. }
  628. }