tt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 <linuxwifi@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. struct iwl_ext_dts_measurement_cmd extcmd = {
  152. .control_mode = cpu_to_le32(DTS_AUTOMATIC),
  153. };
  154. u32 cmdid;
  155. if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR))
  156. cmdid = iwl_cmd_id(CMD_DTS_MEASUREMENT_TRIGGER_WIDE,
  157. PHY_OPS_GROUP, 0);
  158. else
  159. cmdid = CMD_DTS_MEASUREMENT_TRIGGER;
  160. if (!fw_has_capa(&mvm->fw->ucode_capa,
  161. IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE))
  162. return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(cmd), &cmd);
  163. return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(extcmd), &extcmd);
  164. }
  165. int iwl_mvm_get_temp(struct iwl_mvm *mvm)
  166. {
  167. struct iwl_notification_wait wait_temp_notif;
  168. static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP,
  169. DTS_MEASUREMENT_NOTIF_WIDE) };
  170. int ret, temp;
  171. if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR))
  172. temp_notif[0] = DTS_MEASUREMENT_NOTIFICATION;
  173. lockdep_assert_held(&mvm->mutex);
  174. iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
  175. temp_notif, ARRAY_SIZE(temp_notif),
  176. iwl_mvm_temp_notif_wait, &temp);
  177. ret = iwl_mvm_get_temp_cmd(mvm);
  178. if (ret) {
  179. IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
  180. iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
  181. return ret;
  182. }
  183. ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
  184. IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
  185. if (ret) {
  186. IWL_ERR(mvm, "Getting the temperature timed out\n");
  187. return ret;
  188. }
  189. return temp;
  190. }
  191. static void check_exit_ctkill(struct work_struct *work)
  192. {
  193. struct iwl_mvm_tt_mgmt *tt;
  194. struct iwl_mvm *mvm;
  195. u32 duration;
  196. s32 temp;
  197. tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
  198. mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
  199. duration = tt->params.ct_kill_duration;
  200. mutex_lock(&mvm->mutex);
  201. if (__iwl_mvm_mac_start(mvm))
  202. goto reschedule;
  203. /* make sure the device is available for direct read/writes */
  204. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
  205. __iwl_mvm_mac_stop(mvm);
  206. goto reschedule;
  207. }
  208. temp = iwl_mvm_get_temp(mvm);
  209. iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
  210. __iwl_mvm_mac_stop(mvm);
  211. if (temp < 0)
  212. goto reschedule;
  213. IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
  214. if (temp <= tt->params.ct_kill_exit) {
  215. mutex_unlock(&mvm->mutex);
  216. iwl_mvm_exit_ctkill(mvm);
  217. return;
  218. }
  219. reschedule:
  220. mutex_unlock(&mvm->mutex);
  221. schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
  222. round_jiffies(duration * HZ));
  223. }
  224. static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
  225. struct ieee80211_vif *vif)
  226. {
  227. struct iwl_mvm *mvm = _data;
  228. enum ieee80211_smps_mode smps_mode;
  229. lockdep_assert_held(&mvm->mutex);
  230. if (mvm->thermal_throttle.dynamic_smps)
  231. smps_mode = IEEE80211_SMPS_DYNAMIC;
  232. else
  233. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  234. if (vif->type != NL80211_IFTYPE_STATION)
  235. return;
  236. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
  237. }
  238. static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
  239. {
  240. struct ieee80211_sta *sta;
  241. struct iwl_mvm_sta *mvmsta;
  242. int i, err;
  243. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  244. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  245. lockdep_is_held(&mvm->mutex));
  246. if (IS_ERR_OR_NULL(sta))
  247. continue;
  248. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  249. if (enable == mvmsta->tt_tx_protection)
  250. continue;
  251. err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
  252. if (err) {
  253. IWL_ERR(mvm, "Failed to %s Tx protection\n",
  254. enable ? "enable" : "disable");
  255. } else {
  256. IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
  257. enable ? "Enable" : "Disable");
  258. mvmsta->tt_tx_protection = enable;
  259. }
  260. }
  261. }
  262. void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
  263. {
  264. struct iwl_host_cmd cmd = {
  265. .id = REPLY_THERMAL_MNG_BACKOFF,
  266. .len = { sizeof(u32), },
  267. .data = { &backoff, },
  268. };
  269. backoff = max(backoff, mvm->thermal_throttle.min_backoff);
  270. if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
  271. IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
  272. backoff);
  273. mvm->thermal_throttle.tx_backoff = backoff;
  274. } else {
  275. IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
  276. }
  277. }
  278. void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
  279. {
  280. struct iwl_tt_params *params = &mvm->thermal_throttle.params;
  281. struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
  282. s32 temperature = mvm->temperature;
  283. bool throttle_enable = false;
  284. int i;
  285. u32 tx_backoff;
  286. IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
  287. if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
  288. iwl_mvm_enter_ctkill(mvm);
  289. return;
  290. }
  291. if (params->support_ct_kill &&
  292. temperature <= params->ct_kill_exit) {
  293. iwl_mvm_exit_ctkill(mvm);
  294. return;
  295. }
  296. if (params->support_dynamic_smps) {
  297. if (!tt->dynamic_smps &&
  298. temperature >= params->dynamic_smps_entry) {
  299. IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
  300. tt->dynamic_smps = true;
  301. ieee80211_iterate_active_interfaces_atomic(
  302. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  303. iwl_mvm_tt_smps_iterator, mvm);
  304. throttle_enable = true;
  305. } else if (tt->dynamic_smps &&
  306. temperature <= params->dynamic_smps_exit) {
  307. IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
  308. tt->dynamic_smps = false;
  309. ieee80211_iterate_active_interfaces_atomic(
  310. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  311. iwl_mvm_tt_smps_iterator, mvm);
  312. }
  313. }
  314. if (params->support_tx_protection) {
  315. if (temperature >= params->tx_protection_entry) {
  316. iwl_mvm_tt_tx_protection(mvm, true);
  317. throttle_enable = true;
  318. } else if (temperature <= params->tx_protection_exit) {
  319. iwl_mvm_tt_tx_protection(mvm, false);
  320. }
  321. }
  322. if (params->support_tx_backoff) {
  323. tx_backoff = tt->min_backoff;
  324. for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
  325. if (temperature < params->tx_backoff[i].temperature)
  326. break;
  327. tx_backoff = max(tt->min_backoff,
  328. params->tx_backoff[i].backoff);
  329. }
  330. if (tx_backoff != tt->min_backoff)
  331. throttle_enable = true;
  332. if (tt->tx_backoff != tx_backoff)
  333. iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
  334. }
  335. if (!tt->throttle && throttle_enable) {
  336. IWL_WARN(mvm,
  337. "Due to high temperature thermal throttling initiated\n");
  338. tt->throttle = true;
  339. } else if (tt->throttle && !tt->dynamic_smps &&
  340. tt->tx_backoff == tt->min_backoff &&
  341. temperature <= params->tx_protection_exit) {
  342. IWL_WARN(mvm,
  343. "Temperature is back to normal thermal throttling stopped\n");
  344. tt->throttle = false;
  345. }
  346. }
  347. static const struct iwl_tt_params iwl_mvm_default_tt_params = {
  348. .ct_kill_entry = 118,
  349. .ct_kill_exit = 96,
  350. .ct_kill_duration = 5,
  351. .dynamic_smps_entry = 114,
  352. .dynamic_smps_exit = 110,
  353. .tx_protection_entry = 114,
  354. .tx_protection_exit = 108,
  355. .tx_backoff = {
  356. {.temperature = 112, .backoff = 200},
  357. {.temperature = 113, .backoff = 600},
  358. {.temperature = 114, .backoff = 1200},
  359. {.temperature = 115, .backoff = 2000},
  360. {.temperature = 116, .backoff = 4000},
  361. {.temperature = 117, .backoff = 10000},
  362. },
  363. .support_ct_kill = true,
  364. .support_dynamic_smps = true,
  365. .support_tx_protection = true,
  366. .support_tx_backoff = true,
  367. };
  368. void iwl_mvm_tt_initialize(struct iwl_mvm *mvm, u32 min_backoff)
  369. {
  370. struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
  371. IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
  372. if (mvm->cfg->thermal_params)
  373. tt->params = *mvm->cfg->thermal_params;
  374. else
  375. tt->params = iwl_mvm_default_tt_params;
  376. tt->throttle = false;
  377. tt->dynamic_smps = false;
  378. tt->min_backoff = min_backoff;
  379. INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
  380. }
  381. void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
  382. {
  383. cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
  384. IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
  385. }