PageRenderTime 1676ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/iwlwifi/iwl-agn-tt.c

https://github.com/Mengqi/linux-2.6
C | 699 lines | 480 code | 70 blank | 149 comment | 91 complexity | 1788ed25a0c3889ca79c6ac296f4cabf MD5 | raw file
  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->cfg->base_params->temperature_kelvin)
  104. temp = KELVIN_TO_CELSIUS(priv->temperature);
  105. if (!priv->thermal_throttle.advanced_tt)
  106. within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
  107. CT_KILL_THRESHOLD_LEGACY) ? true : false;
  108. else
  109. within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
  110. CT_KILL_THRESHOLD) ? true : false;
  111. return within_margin;
  112. }
  113. bool iwl_check_for_ct_kill(struct iwl_priv *priv)
  114. {
  115. bool is_ct_kill = false;
  116. if (iwl_within_ct_kill_margin(priv)) {
  117. iwl_tt_enter_ct_kill(priv);
  118. is_ct_kill = true;
  119. }
  120. return is_ct_kill;
  121. }
  122. enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
  123. {
  124. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  125. struct iwl_tt_restriction *restriction;
  126. if (!priv->thermal_throttle.advanced_tt)
  127. return IWL_ANT_OK_MULTI;
  128. restriction = tt->restriction + tt->state;
  129. return restriction->tx_stream;
  130. }
  131. enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
  132. {
  133. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  134. struct iwl_tt_restriction *restriction;
  135. if (!priv->thermal_throttle.advanced_tt)
  136. return IWL_ANT_OK_MULTI;
  137. restriction = tt->restriction + tt->state;
  138. return restriction->rx_stream;
  139. }
  140. #define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
  141. #define CT_KILL_WAITING_DURATION (300) /* 300ms duration */
  142. /*
  143. * toggle the bit to wake up uCode and check the temperature
  144. * if the temperature is below CT, uCode will stay awake and send card
  145. * state notification with CT_KILL bit clear to inform Thermal Throttling
  146. * Management to change state. Otherwise, uCode will go back to sleep
  147. * without doing anything, driver should continue the 5 seconds timer
  148. * to wake up uCode for temperature check until temperature drop below CT
  149. */
  150. static void iwl_tt_check_exit_ct_kill(unsigned long data)
  151. {
  152. struct iwl_priv *priv = (struct iwl_priv *)data;
  153. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  154. unsigned long flags;
  155. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  156. return;
  157. if (tt->state == IWL_TI_CT_KILL) {
  158. if (priv->thermal_throttle.ct_kill_toggle) {
  159. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  160. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  161. priv->thermal_throttle.ct_kill_toggle = false;
  162. } else {
  163. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  164. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  165. priv->thermal_throttle.ct_kill_toggle = true;
  166. }
  167. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  168. spin_lock_irqsave(&priv->reg_lock, flags);
  169. if (!iwl_grab_nic_access(priv))
  170. iwl_release_nic_access(priv);
  171. spin_unlock_irqrestore(&priv->reg_lock, flags);
  172. /* Reschedule the ct_kill timer to occur in
  173. * CT_KILL_EXIT_DURATION seconds to ensure we get a
  174. * thermal update */
  175. IWL_DEBUG_TEMP(priv, "schedule ct_kill exit timer\n");
  176. mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
  177. jiffies + CT_KILL_EXIT_DURATION * HZ);
  178. }
  179. }
  180. static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
  181. bool stop)
  182. {
  183. if (stop) {
  184. IWL_DEBUG_TEMP(priv, "Stop all queues\n");
  185. if (priv->mac80211_registered)
  186. ieee80211_stop_queues(priv->hw);
  187. IWL_DEBUG_TEMP(priv,
  188. "Schedule 5 seconds CT_KILL Timer\n");
  189. mod_timer(&priv->thermal_throttle.ct_kill_exit_tm,
  190. jiffies + CT_KILL_EXIT_DURATION * HZ);
  191. } else {
  192. IWL_DEBUG_TEMP(priv, "Wake all queues\n");
  193. if (priv->mac80211_registered)
  194. ieee80211_wake_queues(priv->hw);
  195. }
  196. }
  197. static void iwl_tt_ready_for_ct_kill(unsigned long data)
  198. {
  199. struct iwl_priv *priv = (struct iwl_priv *)data;
  200. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  201. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  202. return;
  203. /* temperature timer expired, ready to go into CT_KILL state */
  204. if (tt->state != IWL_TI_CT_KILL) {
  205. IWL_DEBUG_TEMP(priv, "entering CT_KILL state when "
  206. "temperature timer expired\n");
  207. tt->state = IWL_TI_CT_KILL;
  208. set_bit(STATUS_CT_KILL, &priv->status);
  209. iwl_perform_ct_kill_task(priv, true);
  210. }
  211. }
  212. static void iwl_prepare_ct_kill_task(struct iwl_priv *priv)
  213. {
  214. IWL_DEBUG_TEMP(priv, "Prepare to enter IWL_TI_CT_KILL\n");
  215. /* make request to retrieve statistics information */
  216. iwl_send_statistics_request(priv, CMD_SYNC, false);
  217. /* Reschedule the ct_kill wait timer */
  218. mod_timer(&priv->thermal_throttle.ct_kill_waiting_tm,
  219. jiffies + msecs_to_jiffies(CT_KILL_WAITING_DURATION));
  220. }
  221. #define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
  222. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
  223. #define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
  224. /*
  225. * Legacy thermal throttling
  226. * 1) Avoid NIC destruction due to high temperatures
  227. * Chip will identify dangerously high temperatures that can
  228. * harm the device and will power down
  229. * 2) Avoid the NIC power down due to high temperature
  230. * Throttle early enough to lower the power consumption before
  231. * drastic steps are needed
  232. */
  233. static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
  234. {
  235. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  236. enum iwl_tt_state old_state;
  237. #ifdef CONFIG_IWLWIFI_DEBUG
  238. if ((tt->tt_previous_temp) &&
  239. (temp > tt->tt_previous_temp) &&
  240. ((temp - tt->tt_previous_temp) >
  241. IWL_TT_INCREASE_MARGIN)) {
  242. IWL_DEBUG_TEMP(priv,
  243. "Temperature increase %d degree Celsius\n",
  244. (temp - tt->tt_previous_temp));
  245. }
  246. #endif
  247. old_state = tt->state;
  248. /* in Celsius */
  249. if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
  250. tt->state = IWL_TI_CT_KILL;
  251. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
  252. tt->state = IWL_TI_2;
  253. else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
  254. tt->state = IWL_TI_1;
  255. else
  256. tt->state = IWL_TI_0;
  257. #ifdef CONFIG_IWLWIFI_DEBUG
  258. tt->tt_previous_temp = temp;
  259. #endif
  260. /* stop ct_kill_waiting_tm timer */
  261. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  262. if (tt->state != old_state) {
  263. switch (tt->state) {
  264. case IWL_TI_0:
  265. /*
  266. * When the system is ready to go back to IWL_TI_0
  267. * we only have to call iwl_power_update_mode() to
  268. * do so.
  269. */
  270. break;
  271. case IWL_TI_1:
  272. tt->tt_power_mode = IWL_POWER_INDEX_3;
  273. break;
  274. case IWL_TI_2:
  275. tt->tt_power_mode = IWL_POWER_INDEX_4;
  276. break;
  277. default:
  278. tt->tt_power_mode = IWL_POWER_INDEX_5;
  279. break;
  280. }
  281. mutex_lock(&priv->mutex);
  282. if (old_state == IWL_TI_CT_KILL)
  283. clear_bit(STATUS_CT_KILL, &priv->status);
  284. if (tt->state != IWL_TI_CT_KILL &&
  285. iwl_power_update_mode(priv, true)) {
  286. /* TT state not updated
  287. * try again during next temperature read
  288. */
  289. if (old_state == IWL_TI_CT_KILL)
  290. set_bit(STATUS_CT_KILL, &priv->status);
  291. tt->state = old_state;
  292. IWL_ERR(priv, "Cannot update power mode, "
  293. "TT state not updated\n");
  294. } else {
  295. if (tt->state == IWL_TI_CT_KILL) {
  296. if (force) {
  297. set_bit(STATUS_CT_KILL, &priv->status);
  298. iwl_perform_ct_kill_task(priv, true);
  299. } else {
  300. iwl_prepare_ct_kill_task(priv);
  301. tt->state = old_state;
  302. }
  303. } else if (old_state == IWL_TI_CT_KILL &&
  304. tt->state != IWL_TI_CT_KILL)
  305. iwl_perform_ct_kill_task(priv, false);
  306. IWL_DEBUG_TEMP(priv, "Temperature state changed %u\n",
  307. tt->state);
  308. IWL_DEBUG_TEMP(priv, "Power Index change to %u\n",
  309. tt->tt_power_mode);
  310. }
  311. mutex_unlock(&priv->mutex);
  312. }
  313. }
  314. /*
  315. * Advance thermal throttling
  316. * 1) Avoid NIC destruction due to high temperatures
  317. * Chip will identify dangerously high temperatures that can
  318. * harm the device and will power down
  319. * 2) Avoid the NIC power down due to high temperature
  320. * Throttle early enough to lower the power consumption before
  321. * drastic steps are needed
  322. * Actions include relaxing the power down sleep thresholds and
  323. * decreasing the number of TX streams
  324. * 3) Avoid throughput performance impact as much as possible
  325. *
  326. *=============================================================================
  327. * Condition Nxt State Condition Nxt State Condition Nxt State
  328. *-----------------------------------------------------------------------------
  329. * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
  330. * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
  331. * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
  332. * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
  333. *=============================================================================
  334. */
  335. static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
  336. {
  337. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  338. int i;
  339. bool changed = false;
  340. enum iwl_tt_state old_state;
  341. struct iwl_tt_trans *transaction;
  342. old_state = tt->state;
  343. for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
  344. /* based on the current TT state,
  345. * find the curresponding transaction table
  346. * each table has (IWL_TI_STATE_MAX - 1) entries
  347. * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
  348. * will advance to the correct table.
  349. * then based on the current temperature
  350. * find the next state need to transaction to
  351. * go through all the possible (IWL_TI_STATE_MAX - 1) entries
  352. * in the current table to see if transaction is needed
  353. */
  354. transaction = tt->transaction +
  355. ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
  356. if (temp >= transaction->tt_low &&
  357. temp <= transaction->tt_high) {
  358. #ifdef CONFIG_IWLWIFI_DEBUG
  359. if ((tt->tt_previous_temp) &&
  360. (temp > tt->tt_previous_temp) &&
  361. ((temp - tt->tt_previous_temp) >
  362. IWL_TT_INCREASE_MARGIN)) {
  363. IWL_DEBUG_TEMP(priv,
  364. "Temperature increase %d "
  365. "degree Celsius\n",
  366. (temp - tt->tt_previous_temp));
  367. }
  368. tt->tt_previous_temp = temp;
  369. #endif
  370. if (old_state !=
  371. transaction->next_state) {
  372. changed = true;
  373. tt->state =
  374. transaction->next_state;
  375. }
  376. break;
  377. }
  378. }
  379. /* stop ct_kill_waiting_tm timer */
  380. del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
  381. if (changed) {
  382. if (tt->state >= IWL_TI_1) {
  383. /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
  384. tt->tt_power_mode = IWL_POWER_INDEX_5;
  385. if (!iwl_ht_enabled(priv)) {
  386. struct iwl_rxon_context *ctx;
  387. for_each_context(priv, ctx) {
  388. struct iwl_rxon_cmd *rxon;
  389. rxon = &ctx->staging;
  390. /* disable HT */
  391. rxon->flags &= ~(
  392. RXON_FLG_CHANNEL_MODE_MSK |
  393. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  394. RXON_FLG_HT40_PROT_MSK |
  395. RXON_FLG_HT_PROT_MSK);
  396. }
  397. } else {
  398. /* check HT capability and set
  399. * according to the system HT capability
  400. * in case get disabled before */
  401. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  402. }
  403. } else {
  404. /*
  405. * restore system power setting -- it will be
  406. * recalculated automatically.
  407. */
  408. /* check HT capability and set
  409. * according to the system HT capability
  410. * in case get disabled before */
  411. iwl_set_rxon_ht(priv, &priv->current_ht_config);
  412. }
  413. mutex_lock(&priv->mutex);
  414. if (old_state == IWL_TI_CT_KILL)
  415. clear_bit(STATUS_CT_KILL, &priv->status);
  416. if (tt->state != IWL_TI_CT_KILL &&
  417. iwl_power_update_mode(priv, true)) {
  418. /* TT state not updated
  419. * try again during next temperature read
  420. */
  421. IWL_ERR(priv, "Cannot update power mode, "
  422. "TT state not updated\n");
  423. if (old_state == IWL_TI_CT_KILL)
  424. set_bit(STATUS_CT_KILL, &priv->status);
  425. tt->state = old_state;
  426. } else {
  427. IWL_DEBUG_TEMP(priv,
  428. "Thermal Throttling to new state: %u\n",
  429. tt->state);
  430. if (old_state != IWL_TI_CT_KILL &&
  431. tt->state == IWL_TI_CT_KILL) {
  432. if (force) {
  433. IWL_DEBUG_TEMP(priv,
  434. "Enter IWL_TI_CT_KILL\n");
  435. set_bit(STATUS_CT_KILL, &priv->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->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->status))
  465. return;
  466. if (!iwl_is_ready(priv))
  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->status))
  490. return;
  491. if (!iwl_is_ready(priv))
  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->status))
  516. return;
  517. IWL_DEBUG_TEMP(priv, "Queueing critical temperature enter.\n");
  518. queue_work(priv->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->status))
  523. return;
  524. IWL_DEBUG_TEMP(priv, "Queueing critical temperature exit.\n");
  525. queue_work(priv->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->status))
  532. return;
  533. if (priv->cfg->base_params->temperature_kelvin)
  534. temp = KELVIN_TO_CELSIUS(priv->temperature);
  535. if (!priv->thermal_throttle.advanced_tt)
  536. iwl_legacy_tt_handler(priv, temp, false);
  537. else
  538. iwl_advance_tt_handler(priv, temp, false);
  539. }
  540. void iwl_tt_handler(struct iwl_priv *priv)
  541. {
  542. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  543. return;
  544. IWL_DEBUG_TEMP(priv, "Queueing thermal throttling work.\n");
  545. queue_work(priv->workqueue, &priv->tt_work);
  546. }
  547. /* Thermal throttling initialization
  548. * For advance thermal throttling:
  549. * Initialize Thermal Index and temperature threshold table
  550. * Initialize thermal throttling restriction table
  551. */
  552. void iwl_tt_initialize(struct iwl_priv *priv)
  553. {
  554. struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
  555. int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
  556. struct iwl_tt_trans *transaction;
  557. IWL_DEBUG_TEMP(priv, "Initialize Thermal Throttling\n");
  558. memset(tt, 0, sizeof(struct iwl_tt_mgmt));
  559. tt->state = IWL_TI_0;
  560. init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
  561. priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
  562. priv->thermal_throttle.ct_kill_exit_tm.function =
  563. iwl_tt_check_exit_ct_kill;
  564. init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
  565. priv->thermal_throttle.ct_kill_waiting_tm.data =
  566. (unsigned long)priv;
  567. priv->thermal_throttle.ct_kill_waiting_tm.function =
  568. iwl_tt_ready_for_ct_kill;
  569. /* setup deferred ct kill work */
  570. INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
  571. INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
  572. INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
  573. if (priv->cfg->base_params->adv_thermal_throttle) {
  574. IWL_DEBUG_TEMP(priv, "Advanced Thermal Throttling\n");
  575. tt->restriction = kzalloc(sizeof(struct iwl_tt_restriction) *
  576. IWL_TI_STATE_MAX, GFP_KERNEL);
  577. tt->transaction = kzalloc(sizeof(struct iwl_tt_trans) *
  578. IWL_TI_STATE_MAX * (IWL_TI_STATE_MAX - 1),
  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. }