tt.c 22 KB

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