tt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. static bool iwl_mvm_temp_notif(struct iwl_notif_wait_data *notif_wait,
  90. struct iwl_rx_packet *pkt, void *data)
  91. {
  92. struct iwl_mvm *mvm =
  93. container_of(notif_wait, struct iwl_mvm, notif_wait);
  94. int *temp = data;
  95. struct iwl_dts_measurement_notif *notif;
  96. int len = iwl_rx_packet_payload_len(pkt);
  97. if (WARN_ON_ONCE(len != sizeof(*notif))) {
  98. IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
  99. return true;
  100. }
  101. notif = (void *)pkt->data;
  102. *temp = le32_to_cpu(notif->temp);
  103. /* shouldn't be negative, but since it's s32, make sure it isn't */
  104. if (WARN_ON_ONCE(*temp < 0))
  105. *temp = 0;
  106. IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", *temp);
  107. return true;
  108. }
  109. static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
  110. {
  111. struct iwl_dts_measurement_cmd cmd = {
  112. .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
  113. };
  114. return iwl_mvm_send_cmd_pdu(mvm, CMD_DTS_MEASUREMENT_TRIGGER, 0,
  115. sizeof(cmd), &cmd);
  116. }
  117. int iwl_mvm_get_temp(struct iwl_mvm *mvm)
  118. {
  119. struct iwl_notification_wait wait_temp_notif;
  120. static const u8 temp_notif[] = { DTS_MEASUREMENT_NOTIFICATION };
  121. int ret, temp;
  122. lockdep_assert_held(&mvm->mutex);
  123. iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
  124. temp_notif, ARRAY_SIZE(temp_notif),
  125. iwl_mvm_temp_notif, &temp);
  126. ret = iwl_mvm_get_temp_cmd(mvm);
  127. if (ret) {
  128. IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
  129. iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
  130. return ret;
  131. }
  132. ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
  133. IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
  134. if (ret) {
  135. IWL_ERR(mvm, "Getting the temperature timed out\n");
  136. return ret;
  137. }
  138. return temp;
  139. }
  140. static void check_exit_ctkill(struct work_struct *work)
  141. {
  142. struct iwl_mvm_tt_mgmt *tt;
  143. struct iwl_mvm *mvm;
  144. u32 duration;
  145. s32 temp;
  146. tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
  147. mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
  148. duration = tt->params->ct_kill_duration;
  149. mutex_lock(&mvm->mutex);
  150. if (__iwl_mvm_mac_start(mvm))
  151. goto reschedule;
  152. /* make sure the device is available for direct read/writes */
  153. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
  154. __iwl_mvm_mac_stop(mvm);
  155. goto reschedule;
  156. }
  157. temp = iwl_mvm_get_temp(mvm);
  158. iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
  159. __iwl_mvm_mac_stop(mvm);
  160. if (temp < 0)
  161. goto reschedule;
  162. IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
  163. if (temp <= tt->params->ct_kill_exit) {
  164. mutex_unlock(&mvm->mutex);
  165. iwl_mvm_exit_ctkill(mvm);
  166. return;
  167. }
  168. reschedule:
  169. mutex_unlock(&mvm->mutex);
  170. schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
  171. round_jiffies(duration * HZ));
  172. }
  173. static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
  174. struct ieee80211_vif *vif)
  175. {
  176. struct iwl_mvm *mvm = _data;
  177. enum ieee80211_smps_mode smps_mode;
  178. lockdep_assert_held(&mvm->mutex);
  179. if (mvm->thermal_throttle.dynamic_smps)
  180. smps_mode = IEEE80211_SMPS_DYNAMIC;
  181. else
  182. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  183. if (vif->type != NL80211_IFTYPE_STATION)
  184. return;
  185. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
  186. }
  187. static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
  188. {
  189. struct ieee80211_sta *sta;
  190. struct iwl_mvm_sta *mvmsta;
  191. int i, err;
  192. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  193. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  194. lockdep_is_held(&mvm->mutex));
  195. if (IS_ERR_OR_NULL(sta))
  196. continue;
  197. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  198. if (enable == mvmsta->tt_tx_protection)
  199. continue;
  200. err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
  201. if (err) {
  202. IWL_ERR(mvm, "Failed to %s Tx protection\n",
  203. enable ? "enable" : "disable");
  204. } else {
  205. IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
  206. enable ? "Enable" : "Disable");
  207. mvmsta->tt_tx_protection = enable;
  208. }
  209. }
  210. }
  211. void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
  212. {
  213. struct iwl_host_cmd cmd = {
  214. .id = REPLY_THERMAL_MNG_BACKOFF,
  215. .len = { sizeof(u32), },
  216. .data = { &backoff, },
  217. };
  218. backoff = max(backoff, mvm->thermal_throttle.min_backoff);
  219. if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
  220. IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
  221. backoff);
  222. mvm->thermal_throttle.tx_backoff = backoff;
  223. } else {
  224. IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
  225. }
  226. }
  227. void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
  228. {
  229. const struct iwl_tt_params *params = mvm->thermal_throttle.params;
  230. struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
  231. s32 temperature = mvm->temperature;
  232. bool throttle_enable = false;
  233. int i;
  234. u32 tx_backoff;
  235. IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
  236. if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
  237. iwl_mvm_enter_ctkill(mvm);
  238. return;
  239. }
  240. if (params->support_ct_kill &&
  241. temperature <= tt->params->ct_kill_exit) {
  242. iwl_mvm_exit_ctkill(mvm);
  243. return;
  244. }
  245. if (params->support_dynamic_smps) {
  246. if (!tt->dynamic_smps &&
  247. temperature >= params->dynamic_smps_entry) {
  248. IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
  249. tt->dynamic_smps = true;
  250. ieee80211_iterate_active_interfaces_atomic(
  251. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  252. iwl_mvm_tt_smps_iterator, mvm);
  253. throttle_enable = true;
  254. } else if (tt->dynamic_smps &&
  255. temperature <= params->dynamic_smps_exit) {
  256. IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
  257. tt->dynamic_smps = false;
  258. ieee80211_iterate_active_interfaces_atomic(
  259. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  260. iwl_mvm_tt_smps_iterator, mvm);
  261. }
  262. }
  263. if (params->support_tx_protection) {
  264. if (temperature >= params->tx_protection_entry) {
  265. iwl_mvm_tt_tx_protection(mvm, true);
  266. throttle_enable = true;
  267. } else if (temperature <= params->tx_protection_exit) {
  268. iwl_mvm_tt_tx_protection(mvm, false);
  269. }
  270. }
  271. if (params->support_tx_backoff) {
  272. tx_backoff = tt->min_backoff;
  273. for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
  274. if (temperature < params->tx_backoff[i].temperature)
  275. break;
  276. tx_backoff = max(tt->min_backoff,
  277. params->tx_backoff[i].backoff);
  278. }
  279. if (tx_backoff != tt->min_backoff)
  280. throttle_enable = true;
  281. if (tt->tx_backoff != tx_backoff)
  282. iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
  283. }
  284. if (!tt->throttle && throttle_enable) {
  285. IWL_WARN(mvm,
  286. "Due to high temperature thermal throttling initiated\n");
  287. tt->throttle = true;
  288. } else if (tt->throttle && !tt->dynamic_smps &&
  289. tt->tx_backoff == tt->min_backoff &&
  290. temperature <= params->tx_protection_exit) {
  291. IWL_WARN(mvm,
  292. "Temperature is back to normal thermal throttling stopped\n");
  293. tt->throttle = false;
  294. }
  295. }
  296. static const struct iwl_tt_params iwl7000_tt_params = {
  297. .ct_kill_entry = 118,
  298. .ct_kill_exit = 96,
  299. .ct_kill_duration = 5,
  300. .dynamic_smps_entry = 114,
  301. .dynamic_smps_exit = 110,
  302. .tx_protection_entry = 114,
  303. .tx_protection_exit = 108,
  304. .tx_backoff = {
  305. {.temperature = 112, .backoff = 200},
  306. {.temperature = 113, .backoff = 600},
  307. {.temperature = 114, .backoff = 1200},
  308. {.temperature = 115, .backoff = 2000},
  309. {.temperature = 116, .backoff = 4000},
  310. {.temperature = 117, .backoff = 10000},
  311. },
  312. .support_ct_kill = true,
  313. .support_dynamic_smps = true,
  314. .support_tx_protection = true,
  315. .support_tx_backoff = true,
  316. };
  317. static const struct iwl_tt_params iwl7000_high_temp_tt_params = {
  318. .ct_kill_entry = 118,
  319. .ct_kill_exit = 96,
  320. .ct_kill_duration = 5,
  321. .dynamic_smps_entry = 114,
  322. .dynamic_smps_exit = 110,
  323. .tx_protection_entry = 114,
  324. .tx_protection_exit = 108,
  325. .tx_backoff = {
  326. {.temperature = 112, .backoff = 300},
  327. {.temperature = 113, .backoff = 800},
  328. {.temperature = 114, .backoff = 1500},
  329. {.temperature = 115, .backoff = 3000},
  330. {.temperature = 116, .backoff = 5000},
  331. {.temperature = 117, .backoff = 10000},
  332. },
  333. .support_ct_kill = true,
  334. .support_dynamic_smps = true,
  335. .support_tx_protection = true,
  336. .support_tx_backoff = true,
  337. };
  338. void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff)
  339. {
  340. struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
  341. IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
  342. if (mvm->cfg->high_temp)
  343. tt->params = &iwl7000_high_temp_tt_params;
  344. else
  345. tt->params = &iwl7000_tt_params;
  346. tt->throttle = false;
  347. tt->min_backoff = min_backoff;
  348. INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
  349. }
  350. void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
  351. {
  352. cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
  353. IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
  354. }