tt.c 22 KB

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