tt.c 12 KB

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