iwl-agn-tt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2011 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-eeprom.h"
  34. #include "iwl-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-commands.h"
  38. #include "iwl-debug.h"
  39. #include "iwl-agn-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->shrd->status))
  154. return;
  155. if (tt->state == IWL_TI_CT_KILL) {
  156. if (priv->thermal_throttle.ct_kill_toggle) {
  157. iwl_write32(bus(priv), 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(bus(priv), 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(bus(priv), CSR_UCODE_DRV_GP1);
  166. spin_lock_irqsave(&bus(priv)->reg_lock, flags);
  167. if (!iwl_grab_nic_access(bus(priv)))
  168. iwl_release_nic_access(bus(priv));
  169. spin_unlock_irqrestore(&bus(priv)->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->shrd->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->shrd->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->shrd->mutex);
  280. if (old_state == IWL_TI_CT_KILL)
  281. clear_bit(STATUS_CT_KILL, &priv->shrd->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->shrd->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,
  296. &priv->shrd->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->shrd->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->shrd->mutex);
  413. if (old_state == IWL_TI_CT_KILL)
  414. clear_bit(STATUS_CT_KILL, &priv->shrd->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->shrd->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,
  435. &priv->shrd->status);
  436. iwl_perform_ct_kill_task(priv, true);
  437. } else {
  438. iwl_prepare_ct_kill_task(priv);
  439. tt->state = old_state;
  440. }
  441. } else if (old_state == IWL_TI_CT_KILL &&
  442. tt->state != IWL_TI_CT_KILL) {
  443. IWL_DEBUG_TEMP(priv, "Exit IWL_TI_CT_KILL\n");
  444. iwl_perform_ct_kill_task(priv, false);
  445. }
  446. }
  447. mutex_unlock(&priv->shrd->mutex);
  448. }
  449. }
  450. /* Card State Notification indicated reach critical temperature
  451. * if PSP not enable, no Thermal Throttling function will be performed
  452. * just set the GP1 bit to acknowledge the event
  453. * otherwise, go into IWL_TI_CT_KILL state
  454. * since Card State Notification will not provide any temperature reading
  455. * for Legacy mode
  456. * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
  457. * for advance mode
  458. * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
  459. */
  460. static void iwl_bg_ct_enter(struct work_struct *work)
  461. {
  462. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
  463. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  464. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  465. return;
  466. if (!iwl_is_ready(priv->shrd))
  467. return;
  468. if (tt->state != IWL_TI_CT_KILL) {
  469. IWL_ERR(priv, "Device reached critical temperature "
  470. "- ucode going to sleep!\n");
  471. if (!priv->thermal_throttle.advanced_tt)
  472. iwl_legacy_tt_handler(priv,
  473. IWL_MINIMAL_POWER_THRESHOLD,
  474. true);
  475. else
  476. iwl_advance_tt_handler(priv,
  477. CT_KILL_THRESHOLD + 1, true);
  478. }
  479. }
  480. /* Card State Notification indicated out of critical temperature
  481. * since Card State Notification will not provide any temperature reading
  482. * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
  483. * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
  484. */
  485. static void iwl_bg_ct_exit(struct work_struct *work)
  486. {
  487. struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
  488. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  489. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  490. return;
  491. if (!iwl_is_ready(priv->shrd))
  492. return;
  493. /* stop ct_kill_exit_tm timer */
  494. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  495. if (tt->state == IWL_TI_CT_KILL) {
  496. IWL_ERR(priv,
  497. "Device temperature below critical"
  498. "- ucode awake!\n");
  499. /*
  500. * exit from CT_KILL state
  501. * reset the current temperature reading
  502. */
  503. priv->temperature = 0;
  504. if (!priv->thermal_throttle.advanced_tt)
  505. iwl_legacy_tt_handler(priv,
  506. IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
  507. true);
  508. else
  509. iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
  510. true);
  511. }
  512. }
  513. void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
  514. {
  515. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  516. return;
  517. IWL_DEBUG_TEMP(priv, "Queueing critical temperature enter.\n");
  518. queue_work(priv->shrd->workqueue, &priv->ct_enter);
  519. }
  520. void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
  521. {
  522. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  523. return;
  524. IWL_DEBUG_TEMP(priv, "Queueing critical temperature exit.\n");
  525. queue_work(priv->shrd->workqueue, &priv->ct_exit);
  526. }
  527. static void iwl_bg_tt_work(struct work_struct *work)
  528. {
  529. struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
  530. s32 temp = priv->temperature; /* degrees CELSIUS except specified */
  531. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  532. return;
  533. if (!priv->thermal_throttle.advanced_tt)
  534. iwl_legacy_tt_handler(priv, temp, false);
  535. else
  536. iwl_advance_tt_handler(priv, temp, false);
  537. }
  538. void iwl_tt_handler(struct iwl_priv *priv)
  539. {
  540. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  541. return;
  542. IWL_DEBUG_TEMP(priv, "Queueing thermal throttling work.\n");
  543. queue_work(priv->shrd->workqueue, &priv->tt_work);
  544. }
  545. /* Thermal throttling initialization
  546. * For advance thermal throttling:
  547. * Initialize Thermal Index and temperature threshold table
  548. * Initialize thermal throttling restriction table
  549. */
  550. void iwl_tt_initialize(struct iwl_priv *priv)
  551. {
  552. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  553. int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
  554. struct iwl_tt_trans *transaction;
  555. IWL_DEBUG_TEMP(priv, "Initialize Thermal Throttling\n");
  556. memset(tt, 0, sizeof(struct iwl_tt_mgmt));
  557. tt->state = IWL_TI_0;
  558. init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
  559. priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
  560. priv->thermal_throttle.ct_kill_exit_tm.function =
  561. iwl_tt_check_exit_ct_kill;
  562. init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
  563. priv->thermal_throttle.ct_kill_waiting_tm.data =
  564. (unsigned long)priv;
  565. priv->thermal_throttle.ct_kill_waiting_tm.function =
  566. iwl_tt_ready_for_ct_kill;
  567. /* setup deferred ct kill work */
  568. INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
  569. INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
  570. INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
  571. if (priv->cfg->base_params->adv_thermal_throttle) {
  572. IWL_DEBUG_TEMP(priv, "Advanced Thermal Throttling\n");
  573. tt->restriction = kcalloc(IWL_TI_STATE_MAX,
  574. sizeof(struct iwl_tt_restriction),
  575. GFP_KERNEL);
  576. tt->transaction = kcalloc(IWL_TI_STATE_MAX *
  577. (IWL_TI_STATE_MAX - 1),
  578. sizeof(struct iwl_tt_trans),
  579. GFP_KERNEL);
  580. if (!tt->restriction || !tt->transaction) {
  581. IWL_ERR(priv, "Fallback to Legacy Throttling\n");
  582. priv->thermal_throttle.advanced_tt = false;
  583. kfree(tt->restriction);
  584. tt->restriction = NULL;
  585. kfree(tt->transaction);
  586. tt->transaction = NULL;
  587. } else {
  588. transaction = tt->transaction +
  589. (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
  590. memcpy(transaction, &tt_range_0[0], size);
  591. transaction = tt->transaction +
  592. (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
  593. memcpy(transaction, &tt_range_1[0], size);
  594. transaction = tt->transaction +
  595. (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
  596. memcpy(transaction, &tt_range_2[0], size);
  597. transaction = tt->transaction +
  598. (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
  599. memcpy(transaction, &tt_range_3[0], size);
  600. size = sizeof(struct iwl_tt_restriction) *
  601. IWL_TI_STATE_MAX;
  602. memcpy(tt->restriction,
  603. &restriction_range[0], size);
  604. priv->thermal_throttle.advanced_tt = true;
  605. }
  606. } else {
  607. IWL_DEBUG_TEMP(priv, "Legacy Thermal Throttling\n");
  608. priv->thermal_throttle.advanced_tt = false;
  609. }
  610. }
  611. /* cleanup thermal throttling management related memory and timer */
  612. void iwl_tt_exit(struct iwl_priv *priv)
  613. {
  614. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  615. /* stop ct_kill_exit_tm timer if activated */
  616. del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
  617. /* stop ct_kill_waiting_tm timer if activated */
  618. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  619. cancel_work_sync(&priv->tt_work);
  620. cancel_work_sync(&priv->ct_enter);
  621. cancel_work_sync(&priv->ct_exit);
  622. if (priv->thermal_throttle.advanced_tt) {
  623. /* free advance thermal throttling memory */
  624. kfree(tt->restriction);
  625. tt->restriction = NULL;
  626. kfree(tt->transaction);
  627. tt->transaction = NULL;
  628. }
  629. }