tt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include "mvm.h"
  66. #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
  67. static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
  68. {
  69. u32 duration = mvm->thermal_throttle.params->ct_kill_duration;
  70. if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
  71. return;
  72. IWL_ERR(mvm, "Enter CT Kill\n");
  73. iwl_mvm_set_hw_ctkill_state(mvm, true);
  74. /* Don't schedule an exit work if we're in test mode, since
  75. * the temperature will not change unless we manually set it
  76. * again (or disable testing).
  77. */
  78. if (!mvm->temperature_test)
  79. schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
  80. round_jiffies_relative(duration * HZ));
  81. }
  82. static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
  83. {
  84. if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
  85. return;
  86. IWL_ERR(mvm, "Exit CT Kill\n");
  87. iwl_mvm_set_hw_ctkill_state(mvm, false);
  88. }
  89. void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp)
  90. {
  91. /* ignore the notification if we are in test mode */
  92. if (mvm->temperature_test)
  93. return;
  94. if (mvm->temperature == temp)
  95. return;
  96. mvm->temperature = temp;
  97. iwl_mvm_tt_handler(mvm);
  98. }
  99. static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm,
  100. struct iwl_rx_packet *pkt)
  101. {
  102. struct iwl_dts_measurement_notif *notif;
  103. int len = iwl_rx_packet_payload_len(pkt);
  104. int temp;
  105. if (WARN_ON_ONCE(len != sizeof(*notif))) {
  106. IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
  107. return -EINVAL;
  108. }
  109. notif = (void *)pkt->data;
  110. temp = le32_to_cpu(notif->temp);
  111. /* shouldn't be negative, but since it's s32, make sure it isn't */
  112. if (WARN_ON_ONCE(temp < 0))
  113. temp = 0;
  114. IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp);
  115. return temp;
  116. }
  117. static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait,
  118. struct iwl_rx_packet *pkt, void *data)
  119. {
  120. struct iwl_mvm *mvm =
  121. container_of(notif_wait, struct iwl_mvm, notif_wait);
  122. int *temp = data;
  123. int ret;
  124. ret = iwl_mvm_temp_notif_parse(mvm, pkt);
  125. if (ret < 0)
  126. return true;
  127. *temp = ret;
  128. return true;
  129. }
  130. int iwl_mvm_temp_notif(struct iwl_mvm *mvm,
  131. struct iwl_rx_cmd_buffer *rxb,
  132. struct iwl_device_cmd *cmd)
  133. {
  134. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  135. int temp;
  136. /* the notification is handled synchronously in ctkill, so skip here */
  137. if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
  138. return 0;
  139. temp = iwl_mvm_temp_notif_parse(mvm, pkt);
  140. if (temp < 0)
  141. return 0;
  142. iwl_mvm_tt_temp_changed(mvm, temp);
  143. return 0;
  144. }
  145. static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
  146. {
  147. struct iwl_dts_measurement_cmd cmd = {
  148. .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
  149. };
  150. return iwl_mvm_send_cmd_pdu(mvm, CMD_DTS_MEASUREMENT_TRIGGER, 0,
  151. sizeof(cmd), &cmd);
  152. }
  153. int iwl_mvm_get_temp(struct iwl_mvm *mvm)
  154. {
  155. struct iwl_notification_wait wait_temp_notif;
  156. static const u8 temp_notif[] = { DTS_MEASUREMENT_NOTIFICATION };
  157. int ret, temp;
  158. lockdep_assert_held(&mvm->mutex);
  159. iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
  160. temp_notif, ARRAY_SIZE(temp_notif),
  161. iwl_mvm_temp_notif_wait, &temp);
  162. ret = iwl_mvm_get_temp_cmd(mvm);
  163. if (ret) {
  164. IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
  165. iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
  166. return ret;
  167. }
  168. ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
  169. IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
  170. if (ret) {
  171. IWL_ERR(mvm, "Getting the temperature timed out\n");
  172. return ret;
  173. }
  174. return temp;
  175. }
  176. static void check_exit_ctkill(struct work_struct *work)
  177. {
  178. struct iwl_mvm_tt_mgmt *tt;
  179. struct iwl_mvm *mvm;
  180. u32 duration;
  181. s32 temp;
  182. tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
  183. mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
  184. duration = tt->params->ct_kill_duration;
  185. mutex_lock(&mvm->mutex);
  186. if (__iwl_mvm_mac_start(mvm))
  187. goto reschedule;
  188. /* make sure the device is available for direct read/writes */
  189. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
  190. __iwl_mvm_mac_stop(mvm);
  191. goto reschedule;
  192. }
  193. temp = iwl_mvm_get_temp(mvm);
  194. iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
  195. __iwl_mvm_mac_stop(mvm);
  196. if (temp < 0)
  197. goto reschedule;
  198. IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
  199. if (temp <= tt->params->ct_kill_exit) {
  200. mutex_unlock(&mvm->mutex);
  201. iwl_mvm_exit_ctkill(mvm);
  202. return;
  203. }
  204. reschedule:
  205. mutex_unlock(&mvm->mutex);
  206. schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
  207. round_jiffies(duration * HZ));
  208. }
  209. static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
  210. struct ieee80211_vif *vif)
  211. {
  212. struct iwl_mvm *mvm = _data;
  213. enum ieee80211_smps_mode smps_mode;
  214. lockdep_assert_held(&mvm->mutex);
  215. if (mvm->thermal_throttle.dynamic_smps)
  216. smps_mode = IEEE80211_SMPS_DYNAMIC;
  217. else
  218. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  219. if (vif->type != NL80211_IFTYPE_STATION)
  220. return;
  221. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
  222. }
  223. static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
  224. {
  225. struct ieee80211_sta *sta;
  226. struct iwl_mvm_sta *mvmsta;
  227. int i, err;
  228. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  229. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  230. lockdep_is_held(&mvm->mutex));
  231. if (IS_ERR_OR_NULL(sta))
  232. continue;
  233. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  234. if (enable == mvmsta->tt_tx_protection)
  235. continue;
  236. err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
  237. if (err) {
  238. IWL_ERR(mvm, "Failed to %s Tx protection\n",
  239. enable ? "enable" : "disable");
  240. } else {
  241. IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
  242. enable ? "Enable" : "Disable");
  243. mvmsta->tt_tx_protection = enable;
  244. }
  245. }
  246. }
  247. void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
  248. {
  249. struct iwl_host_cmd cmd = {
  250. .id = REPLY_THERMAL_MNG_BACKOFF,
  251. .len = { sizeof(u32), },
  252. .data = { &backoff, },
  253. };
  254. backoff = max(backoff, mvm->thermal_throttle.min_backoff);
  255. if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
  256. IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
  257. backoff);
  258. mvm->thermal_throttle.tx_backoff = backoff;
  259. } else {
  260. IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
  261. }
  262. }
  263. void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
  264. {
  265. const struct iwl_tt_params *params = mvm->thermal_throttle.params;
  266. struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
  267. s32 temperature = mvm->temperature;
  268. bool throttle_enable = false;
  269. int i;
  270. u32 tx_backoff;
  271. IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
  272. if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
  273. iwl_mvm_enter_ctkill(mvm);
  274. return;
  275. }
  276. if (params->support_ct_kill &&
  277. temperature <= tt->params->ct_kill_exit) {
  278. iwl_mvm_exit_ctkill(mvm);
  279. return;
  280. }
  281. if (params->support_dynamic_smps) {
  282. if (!tt->dynamic_smps &&
  283. temperature >= params->dynamic_smps_entry) {
  284. IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
  285. tt->dynamic_smps = true;
  286. ieee80211_iterate_active_interfaces_atomic(
  287. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  288. iwl_mvm_tt_smps_iterator, mvm);
  289. throttle_enable = true;
  290. } else if (tt->dynamic_smps &&
  291. temperature <= params->dynamic_smps_exit) {
  292. IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
  293. tt->dynamic_smps = false;
  294. ieee80211_iterate_active_interfaces_atomic(
  295. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  296. iwl_mvm_tt_smps_iterator, mvm);
  297. }
  298. }
  299. if (params->support_tx_protection) {
  300. if (temperature >= params->tx_protection_entry) {
  301. iwl_mvm_tt_tx_protection(mvm, true);
  302. throttle_enable = true;
  303. } else if (temperature <= params->tx_protection_exit) {
  304. iwl_mvm_tt_tx_protection(mvm, false);
  305. }
  306. }
  307. if (params->support_tx_backoff) {
  308. tx_backoff = tt->min_backoff;
  309. for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
  310. if (temperature < params->tx_backoff[i].temperature)
  311. break;
  312. tx_backoff = max(tt->min_backoff,
  313. params->tx_backoff[i].backoff);
  314. }
  315. if (tx_backoff != tt->min_backoff)
  316. throttle_enable = true;
  317. if (tt->tx_backoff != tx_backoff)
  318. iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
  319. }
  320. if (!tt->throttle && throttle_enable) {
  321. IWL_WARN(mvm,
  322. "Due to high temperature thermal throttling initiated\n");
  323. tt->throttle = true;
  324. } else if (tt->throttle && !tt->dynamic_smps &&
  325. tt->tx_backoff == tt->min_backoff &&
  326. temperature <= params->tx_protection_exit) {
  327. IWL_WARN(mvm,
  328. "Temperature is back to normal thermal throttling stopped\n");
  329. tt->throttle = false;
  330. }
  331. }
  332. static const struct iwl_tt_params iwl7000_tt_params = {
  333. .ct_kill_entry = 118,
  334. .ct_kill_exit = 96,
  335. .ct_kill_duration = 5,
  336. .dynamic_smps_entry = 114,
  337. .dynamic_smps_exit = 110,
  338. .tx_protection_entry = 114,
  339. .tx_protection_exit = 108,
  340. .tx_backoff = {
  341. {.temperature = 112, .backoff = 200},
  342. {.temperature = 113, .backoff = 600},
  343. {.temperature = 114, .backoff = 1200},
  344. {.temperature = 115, .backoff = 2000},
  345. {.temperature = 116, .backoff = 4000},
  346. {.temperature = 117, .backoff = 10000},
  347. },
  348. .support_ct_kill = true,
  349. .support_dynamic_smps = true,
  350. .support_tx_protection = true,
  351. .support_tx_backoff = true,
  352. };
  353. static const struct iwl_tt_params iwl7000_high_temp_tt_params = {
  354. .ct_kill_entry = 118,
  355. .ct_kill_exit = 96,
  356. .ct_kill_duration = 5,
  357. .dynamic_smps_entry = 114,
  358. .dynamic_smps_exit = 110,
  359. .tx_protection_entry = 114,
  360. .tx_protection_exit = 108,
  361. .tx_backoff = {
  362. {.temperature = 112, .backoff = 300},
  363. {.temperature = 113, .backoff = 800},
  364. {.temperature = 114, .backoff = 1500},
  365. {.temperature = 115, .backoff = 3000},
  366. {.temperature = 116, .backoff = 5000},
  367. {.temperature = 117, .backoff = 10000},
  368. },
  369. .support_ct_kill = true,
  370. .support_dynamic_smps = true,
  371. .support_tx_protection = true,
  372. .support_tx_backoff = true,
  373. };
  374. void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff)
  375. {
  376. struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
  377. IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
  378. if (mvm->cfg->high_temp)
  379. tt->params = &iwl7000_high_temp_tt_params;
  380. else
  381. tt->params = &iwl7000_tt_params;
  382. tt->throttle = false;
  383. tt->min_backoff = min_backoff;
  384. INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
  385. }
  386. void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
  387. {
  388. cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
  389. IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
  390. }