tt.c 12 KB

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