tt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. * Copyright(c) 2015 Intel Deutschland GmbH
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. *
  43. * * Redistributions of source code must retain the above copyright
  44. * notice, this list of conditions and the following disclaimer.
  45. * * Redistributions in binary form must reproduce the above copyright
  46. * notice, this list of conditions and the following disclaimer in
  47. * the documentation and/or other materials provided with the
  48. * distribution.
  49. * * Neither the name Intel Corporation nor the names of its
  50. * contributors may be used to endorse or promote products derived
  51. * from this software without specific prior written permission.
  52. *
  53. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  54. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  55. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  56. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  57. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  58. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  59. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  60. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  61. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  62. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  63. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  64. *
  65. *****************************************************************************/
  66. #include "mvm.h"
  67. #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
  68. static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
  69. {
  70. struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
  71. u32 duration = tt->params.ct_kill_duration;
  72. if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
  73. return;
  74. IWL_ERR(mvm, "Enter CT Kill\n");
  75. iwl_mvm_set_hw_ctkill_state(mvm, true);
  76. tt->throttle = false;
  77. tt->dynamic_smps = false;
  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(&tt->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. void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp)
  94. {
  95. /* ignore the notification if we are in test mode */
  96. if (mvm->temperature_test)
  97. return;
  98. if (mvm->temperature == temp)
  99. return;
  100. mvm->temperature = temp;
  101. iwl_mvm_tt_handler(mvm);
  102. }
  103. static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm,
  104. struct iwl_rx_packet *pkt)
  105. {
  106. struct iwl_dts_measurement_notif *notif;
  107. int len = iwl_rx_packet_payload_len(pkt);
  108. int temp;
  109. if (WARN_ON_ONCE(len != sizeof(*notif))) {
  110. IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
  111. return -EINVAL;
  112. }
  113. notif = (void *)pkt->data;
  114. temp = le32_to_cpu(notif->temp);
  115. /* shouldn't be negative, but since it's s32, make sure it isn't */
  116. if (WARN_ON_ONCE(temp < 0))
  117. temp = 0;
  118. IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp);
  119. return temp;
  120. }
  121. static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait,
  122. struct iwl_rx_packet *pkt, void *data)
  123. {
  124. struct iwl_mvm *mvm =
  125. container_of(notif_wait, struct iwl_mvm, notif_wait);
  126. int *temp = data;
  127. int ret;
  128. ret = iwl_mvm_temp_notif_parse(mvm, pkt);
  129. if (ret < 0)
  130. return true;
  131. *temp = ret;
  132. return true;
  133. }
  134. void iwl_mvm_temp_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  135. {
  136. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  137. int temp;
  138. /* the notification is handled synchronously in ctkill, so skip here */
  139. if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
  140. return;
  141. temp = iwl_mvm_temp_notif_parse(mvm, pkt);
  142. if (temp < 0)
  143. return;
  144. iwl_mvm_tt_temp_changed(mvm, temp);
  145. }
  146. static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
  147. {
  148. struct iwl_dts_measurement_cmd cmd = {
  149. .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
  150. };
  151. return iwl_mvm_send_cmd_pdu(mvm, CMD_DTS_MEASUREMENT_TRIGGER, 0,
  152. sizeof(cmd), &cmd);
  153. }
  154. int iwl_mvm_get_temp(struct iwl_mvm *mvm)
  155. {
  156. struct iwl_notification_wait wait_temp_notif;
  157. static const u16 temp_notif[] = { DTS_MEASUREMENT_NOTIFICATION };
  158. int ret, temp;
  159. lockdep_assert_held(&mvm->mutex);
  160. iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
  161. temp_notif, ARRAY_SIZE(temp_notif),
  162. iwl_mvm_temp_notif_wait, &temp);
  163. ret = iwl_mvm_get_temp_cmd(mvm);
  164. if (ret) {
  165. IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
  166. iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
  167. return ret;
  168. }
  169. ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
  170. IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
  171. if (ret) {
  172. IWL_ERR(mvm, "Getting the temperature timed out\n");
  173. return ret;
  174. }
  175. return temp;
  176. }
  177. static void check_exit_ctkill(struct work_struct *work)
  178. {
  179. struct iwl_mvm_tt_mgmt *tt;
  180. struct iwl_mvm *mvm;
  181. u32 duration;
  182. s32 temp;
  183. tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
  184. mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
  185. duration = tt->params.ct_kill_duration;
  186. mutex_lock(&mvm->mutex);
  187. if (__iwl_mvm_mac_start(mvm))
  188. goto reschedule;
  189. /* make sure the device is available for direct read/writes */
  190. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
  191. __iwl_mvm_mac_stop(mvm);
  192. goto reschedule;
  193. }
  194. temp = iwl_mvm_get_temp(mvm);
  195. iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
  196. __iwl_mvm_mac_stop(mvm);
  197. if (temp < 0)
  198. goto reschedule;
  199. IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
  200. if (temp <= tt->params.ct_kill_exit) {
  201. mutex_unlock(&mvm->mutex);
  202. iwl_mvm_exit_ctkill(mvm);
  203. return;
  204. }
  205. reschedule:
  206. mutex_unlock(&mvm->mutex);
  207. schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
  208. round_jiffies(duration * HZ));
  209. }
  210. static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
  211. struct ieee80211_vif *vif)
  212. {
  213. struct iwl_mvm *mvm = _data;
  214. enum ieee80211_smps_mode smps_mode;
  215. lockdep_assert_held(&mvm->mutex);
  216. if (mvm->thermal_throttle.dynamic_smps)
  217. smps_mode = IEEE80211_SMPS_DYNAMIC;
  218. else
  219. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  220. if (vif->type != NL80211_IFTYPE_STATION)
  221. return;
  222. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
  223. }
  224. static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
  225. {
  226. struct ieee80211_sta *sta;
  227. struct iwl_mvm_sta *mvmsta;
  228. int i, err;
  229. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  230. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  231. lockdep_is_held(&mvm->mutex));
  232. if (IS_ERR_OR_NULL(sta))
  233. continue;
  234. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  235. if (enable == mvmsta->tt_tx_protection)
  236. continue;
  237. err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
  238. if (err) {
  239. IWL_ERR(mvm, "Failed to %s Tx protection\n",
  240. enable ? "enable" : "disable");
  241. } else {
  242. IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
  243. enable ? "Enable" : "Disable");
  244. mvmsta->tt_tx_protection = enable;
  245. }
  246. }
  247. }
  248. void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
  249. {
  250. struct iwl_host_cmd cmd = {
  251. .id = REPLY_THERMAL_MNG_BACKOFF,
  252. .len = { sizeof(u32), },
  253. .data = { &backoff, },
  254. };
  255. backoff = max(backoff, mvm->thermal_throttle.min_backoff);
  256. if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
  257. IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
  258. backoff);
  259. mvm->thermal_throttle.tx_backoff = backoff;
  260. } else {
  261. IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
  262. }
  263. }
  264. void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
  265. {
  266. struct iwl_tt_params *params = &mvm->thermal_throttle.params;
  267. struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
  268. s32 temperature = mvm->temperature;
  269. bool throttle_enable = false;
  270. int i;
  271. u32 tx_backoff;
  272. IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
  273. if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
  274. iwl_mvm_enter_ctkill(mvm);
  275. return;
  276. }
  277. if (params->support_ct_kill &&
  278. temperature <= params->ct_kill_exit) {
  279. iwl_mvm_exit_ctkill(mvm);
  280. return;
  281. }
  282. if (params->support_dynamic_smps) {
  283. if (!tt->dynamic_smps &&
  284. temperature >= params->dynamic_smps_entry) {
  285. IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
  286. tt->dynamic_smps = true;
  287. ieee80211_iterate_active_interfaces_atomic(
  288. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  289. iwl_mvm_tt_smps_iterator, mvm);
  290. throttle_enable = true;
  291. } else if (tt->dynamic_smps &&
  292. temperature <= params->dynamic_smps_exit) {
  293. IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
  294. tt->dynamic_smps = false;
  295. ieee80211_iterate_active_interfaces_atomic(
  296. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  297. iwl_mvm_tt_smps_iterator, mvm);
  298. }
  299. }
  300. if (params->support_tx_protection) {
  301. if (temperature >= params->tx_protection_entry) {
  302. iwl_mvm_tt_tx_protection(mvm, true);
  303. throttle_enable = true;
  304. } else if (temperature <= params->tx_protection_exit) {
  305. iwl_mvm_tt_tx_protection(mvm, false);
  306. }
  307. }
  308. if (params->support_tx_backoff) {
  309. tx_backoff = tt->min_backoff;
  310. for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
  311. if (temperature < params->tx_backoff[i].temperature)
  312. break;
  313. tx_backoff = max(tt->min_backoff,
  314. params->tx_backoff[i].backoff);
  315. }
  316. if (tx_backoff != tt->min_backoff)
  317. throttle_enable = true;
  318. if (tt->tx_backoff != tx_backoff)
  319. iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
  320. }
  321. if (!tt->throttle && throttle_enable) {
  322. IWL_WARN(mvm,
  323. "Due to high temperature thermal throttling initiated\n");
  324. tt->throttle = true;
  325. } else if (tt->throttle && !tt->dynamic_smps &&
  326. tt->tx_backoff == tt->min_backoff &&
  327. temperature <= params->tx_protection_exit) {
  328. IWL_WARN(mvm,
  329. "Temperature is back to normal thermal throttling stopped\n");
  330. tt->throttle = false;
  331. }
  332. }
  333. static const struct iwl_tt_params iwl_mvm_default_tt_params = {
  334. .ct_kill_entry = 118,
  335. .ct_kill_exit = 96,
  336. .ct_kill_duration = 5,
  337. .dynamic_smps_entry = 114,
  338. .dynamic_smps_exit = 110,
  339. .tx_protection_entry = 114,
  340. .tx_protection_exit = 108,
  341. .tx_backoff = {
  342. {.temperature = 112, .backoff = 200},
  343. {.temperature = 113, .backoff = 600},
  344. {.temperature = 114, .backoff = 1200},
  345. {.temperature = 115, .backoff = 2000},
  346. {.temperature = 116, .backoff = 4000},
  347. {.temperature = 117, .backoff = 10000},
  348. },
  349. .support_ct_kill = true,
  350. .support_dynamic_smps = true,
  351. .support_tx_protection = true,
  352. .support_tx_backoff = true,
  353. };
  354. void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff)
  355. {
  356. struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
  357. IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
  358. if (mvm->cfg->thermal_params)
  359. tt->params = *mvm->cfg->thermal_params;
  360. else
  361. tt->params = iwl_mvm_default_tt_params;
  362. tt->throttle = false;
  363. tt->dynamic_smps = false;
  364. tt->min_backoff = min_backoff;
  365. INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
  366. }
  367. void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
  368. {
  369. cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
  370. IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
  371. }