1 /******************************************************************************
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.
8 * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
26 * The full GNU General Public License is included in this distribution
27 * in the file called COPYING.
29 * Contact Information:
30 * Intel Linux Wireless <linuxwifi@intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
37 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *****************************************************************************/
68 #include <linux/sort.h>
72 #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
74 static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
76 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
77 u32 duration = tt->params.ct_kill_duration;
79 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
82 IWL_ERR(mvm, "Enter CT Kill\n");
83 iwl_mvm_set_hw_ctkill_state(mvm, true);
85 if (!iwl_mvm_is_tt_in_fw(mvm)) {
87 tt->dynamic_smps = false;
90 /* Don't schedule an exit work if we're in test mode, since
91 * the temperature will not change unless we manually set it
92 * again (or disable testing).
94 if (!mvm->temperature_test)
95 schedule_delayed_work(&tt->ct_kill_exit,
96 round_jiffies_relative(duration * HZ));
99 static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
101 if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
104 IWL_ERR(mvm, "Exit CT Kill\n");
105 iwl_mvm_set_hw_ctkill_state(mvm, false);
108 void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp)
110 /* ignore the notification if we are in test mode */
111 if (mvm->temperature_test)
114 if (mvm->temperature == temp)
117 mvm->temperature = temp;
118 iwl_mvm_tt_handler(mvm);
121 static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm,
122 struct iwl_rx_packet *pkt)
124 struct iwl_dts_measurement_notif_v1 *notif_v1;
125 int len = iwl_rx_packet_payload_len(pkt);
128 /* we can use notif_v1 only, because v2 only adds an additional
129 * parameter, which is not used in this function.
131 if (WARN_ON_ONCE(len < sizeof(*notif_v1))) {
132 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
136 notif_v1 = (void *)pkt->data;
138 temp = le32_to_cpu(notif_v1->temp);
140 /* shouldn't be negative, but since it's s32, make sure it isn't */
141 if (WARN_ON_ONCE(temp < 0))
144 IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp);
149 static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait,
150 struct iwl_rx_packet *pkt, void *data)
152 struct iwl_mvm *mvm =
153 container_of(notif_wait, struct iwl_mvm, notif_wait);
157 ret = iwl_mvm_temp_notif_parse(mvm, pkt);
166 void iwl_mvm_temp_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
168 struct iwl_rx_packet *pkt = rxb_addr(rxb);
169 struct iwl_dts_measurement_notif_v2 *notif_v2;
170 int len = iwl_rx_packet_payload_len(pkt);
174 /* the notification is handled synchronously in ctkill, so skip here */
175 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
178 temp = iwl_mvm_temp_notif_parse(mvm, pkt);
180 if (!iwl_mvm_is_tt_in_fw(mvm)) {
182 iwl_mvm_tt_temp_changed(mvm, temp);
186 if (WARN_ON_ONCE(len < sizeof(*notif_v2))) {
187 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
191 notif_v2 = (void *)pkt->data;
192 ths_crossed = le32_to_cpu(notif_v2->threshold_idx);
194 /* 0xFF in ths_crossed means the notification is not related
195 * to a trip, so we can ignore it here.
197 if (ths_crossed == 0xFF)
200 IWL_DEBUG_TEMP(mvm, "Temp = %d Threshold crossed = %d\n",
203 #ifdef CONFIG_THERMAL
204 if (WARN_ON(ths_crossed >= IWL_MAX_DTS_TRIPS))
208 * We are now handling a temperature notification from the firmware
209 * in ASYNC and hold the mutex. thermal_notify_framework will call
210 * us back through get_temp() which ought to send a SYNC command to
211 * the firmware and hence to take the mutex.
212 * Avoid the deadlock by unlocking the mutex here.
214 if (mvm->tz_device.tzone) {
215 struct iwl_mvm_thermal_device *tz_dev = &mvm->tz_device;
217 mutex_unlock(&mvm->mutex);
218 thermal_notify_framework(tz_dev->tzone,
219 tz_dev->fw_trips_index[ths_crossed]);
220 mutex_lock(&mvm->mutex);
222 #endif /* CONFIG_THERMAL */
225 void iwl_mvm_ct_kill_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
227 struct iwl_rx_packet *pkt = rxb_addr(rxb);
228 struct ct_kill_notif *notif;
229 int len = iwl_rx_packet_payload_len(pkt);
231 if (WARN_ON_ONCE(len != sizeof(*notif))) {
232 IWL_ERR(mvm, "Invalid CT_KILL_NOTIFICATION\n");
236 notif = (struct ct_kill_notif *)pkt->data;
237 IWL_DEBUG_TEMP(mvm, "CT Kill notification temperature = %d\n",
240 iwl_mvm_enter_ctkill(mvm);
243 static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
245 struct iwl_dts_measurement_cmd cmd = {
246 .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
248 struct iwl_ext_dts_measurement_cmd extcmd = {
249 .control_mode = cpu_to_le32(DTS_AUTOMATIC),
253 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR))
254 cmdid = iwl_cmd_id(CMD_DTS_MEASUREMENT_TRIGGER_WIDE,
257 cmdid = CMD_DTS_MEASUREMENT_TRIGGER;
259 if (!fw_has_capa(&mvm->fw->ucode_capa,
260 IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE))
261 return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(cmd), &cmd);
263 return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(extcmd), &extcmd);
266 int iwl_mvm_get_temp(struct iwl_mvm *mvm, s32 *temp)
268 struct iwl_notification_wait wait_temp_notif;
269 static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP,
270 DTS_MEASUREMENT_NOTIF_WIDE) };
273 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_WIDE_CMD_HDR))
274 temp_notif[0] = DTS_MEASUREMENT_NOTIFICATION;
276 lockdep_assert_held(&mvm->mutex);
278 iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
279 temp_notif, ARRAY_SIZE(temp_notif),
280 iwl_mvm_temp_notif_wait, temp);
282 ret = iwl_mvm_get_temp_cmd(mvm);
284 IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
285 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
289 ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
290 IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
292 IWL_ERR(mvm, "Getting the temperature timed out\n");
297 static void check_exit_ctkill(struct work_struct *work)
299 struct iwl_mvm_tt_mgmt *tt;
305 tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
306 mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
308 if (iwl_mvm_is_tt_in_fw(mvm)) {
309 iwl_mvm_exit_ctkill(mvm);
314 duration = tt->params.ct_kill_duration;
316 mutex_lock(&mvm->mutex);
318 if (__iwl_mvm_mac_start(mvm))
321 /* make sure the device is available for direct read/writes */
322 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
323 __iwl_mvm_mac_stop(mvm);
327 ret = iwl_mvm_get_temp(mvm, &temp);
329 iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
331 __iwl_mvm_mac_stop(mvm);
336 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
338 if (temp <= tt->params.ct_kill_exit) {
339 mutex_unlock(&mvm->mutex);
340 iwl_mvm_exit_ctkill(mvm);
345 mutex_unlock(&mvm->mutex);
346 schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
347 round_jiffies(duration * HZ));
350 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
351 struct ieee80211_vif *vif)
353 struct iwl_mvm *mvm = _data;
354 enum ieee80211_smps_mode smps_mode;
356 lockdep_assert_held(&mvm->mutex);
358 if (mvm->thermal_throttle.dynamic_smps)
359 smps_mode = IEEE80211_SMPS_DYNAMIC;
361 smps_mode = IEEE80211_SMPS_AUTOMATIC;
363 if (vif->type != NL80211_IFTYPE_STATION)
366 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
369 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
371 struct ieee80211_sta *sta;
372 struct iwl_mvm_sta *mvmsta;
375 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
376 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
377 lockdep_is_held(&mvm->mutex));
378 if (IS_ERR_OR_NULL(sta))
380 mvmsta = iwl_mvm_sta_from_mac80211(sta);
381 if (enable == mvmsta->tt_tx_protection)
383 err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
385 IWL_ERR(mvm, "Failed to %s Tx protection\n",
386 enable ? "enable" : "disable");
388 IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
389 enable ? "Enable" : "Disable");
390 mvmsta->tt_tx_protection = enable;
395 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
397 struct iwl_host_cmd cmd = {
398 .id = REPLY_THERMAL_MNG_BACKOFF,
399 .len = { sizeof(u32), },
400 .data = { &backoff, },
403 backoff = max(backoff, mvm->thermal_throttle.min_backoff);
405 if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
406 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
408 mvm->thermal_throttle.tx_backoff = backoff;
410 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
414 void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
416 struct iwl_tt_params *params = &mvm->thermal_throttle.params;
417 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
418 s32 temperature = mvm->temperature;
419 bool throttle_enable = false;
423 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
425 if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
426 iwl_mvm_enter_ctkill(mvm);
430 if (params->support_ct_kill &&
431 temperature <= params->ct_kill_exit) {
432 iwl_mvm_exit_ctkill(mvm);
436 if (params->support_dynamic_smps) {
437 if (!tt->dynamic_smps &&
438 temperature >= params->dynamic_smps_entry) {
439 IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
440 tt->dynamic_smps = true;
441 ieee80211_iterate_active_interfaces_atomic(
442 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
443 iwl_mvm_tt_smps_iterator, mvm);
444 throttle_enable = true;
445 } else if (tt->dynamic_smps &&
446 temperature <= params->dynamic_smps_exit) {
447 IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
448 tt->dynamic_smps = false;
449 ieee80211_iterate_active_interfaces_atomic(
450 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
451 iwl_mvm_tt_smps_iterator, mvm);
455 if (params->support_tx_protection) {
456 if (temperature >= params->tx_protection_entry) {
457 iwl_mvm_tt_tx_protection(mvm, true);
458 throttle_enable = true;
459 } else if (temperature <= params->tx_protection_exit) {
460 iwl_mvm_tt_tx_protection(mvm, false);
464 if (params->support_tx_backoff) {
465 tx_backoff = tt->min_backoff;
466 for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
467 if (temperature < params->tx_backoff[i].temperature)
469 tx_backoff = max(tt->min_backoff,
470 params->tx_backoff[i].backoff);
472 if (tx_backoff != tt->min_backoff)
473 throttle_enable = true;
474 if (tt->tx_backoff != tx_backoff)
475 iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
478 if (!tt->throttle && throttle_enable) {
480 "Due to high temperature thermal throttling initiated\n");
482 } else if (tt->throttle && !tt->dynamic_smps &&
483 tt->tx_backoff == tt->min_backoff &&
484 temperature <= params->tx_protection_exit) {
486 "Temperature is back to normal thermal throttling stopped\n");
487 tt->throttle = false;
491 static const struct iwl_tt_params iwl_mvm_default_tt_params = {
492 .ct_kill_entry = 118,
494 .ct_kill_duration = 5,
495 .dynamic_smps_entry = 114,
496 .dynamic_smps_exit = 110,
497 .tx_protection_entry = 114,
498 .tx_protection_exit = 108,
500 {.temperature = 112, .backoff = 200},
501 {.temperature = 113, .backoff = 600},
502 {.temperature = 114, .backoff = 1200},
503 {.temperature = 115, .backoff = 2000},
504 {.temperature = 116, .backoff = 4000},
505 {.temperature = 117, .backoff = 10000},
507 .support_ct_kill = true,
508 .support_dynamic_smps = true,
509 .support_tx_protection = true,
510 .support_tx_backoff = true,
513 /* budget in mWatt */
514 static const u32 iwl_mvm_cdev_budgets[] = {
515 2000, /* cooling state 0 */
516 1800, /* cooling state 1 */
517 1600, /* cooling state 2 */
518 1400, /* cooling state 3 */
519 1200, /* cooling state 4 */
520 1000, /* cooling state 5 */
521 900, /* cooling state 6 */
522 800, /* cooling state 7 */
523 700, /* cooling state 8 */
524 650, /* cooling state 9 */
525 600, /* cooling state 10 */
526 550, /* cooling state 11 */
527 500, /* cooling state 12 */
528 450, /* cooling state 13 */
529 400, /* cooling state 14 */
530 350, /* cooling state 15 */
531 300, /* cooling state 16 */
532 250, /* cooling state 17 */
533 200, /* cooling state 18 */
534 150, /* cooling state 19 */
537 int iwl_mvm_ctdp_command(struct iwl_mvm *mvm, u32 op, u32 state)
539 struct iwl_mvm_ctdp_cmd cmd = {
540 .operation = cpu_to_le32(op),
541 .budget = cpu_to_le32(iwl_mvm_cdev_budgets[state]),
547 lockdep_assert_held(&mvm->mutex);
549 ret = iwl_mvm_send_cmd_pdu_status(mvm, WIDE_ID(PHY_OPS_GROUP,
551 sizeof(cmd), &cmd, &status);
554 IWL_ERR(mvm, "cTDP command failed (err=%d)\n", ret);
559 case CTDP_CMD_OPERATION_START:
560 #ifdef CONFIG_THERMAL
561 mvm->cooling_dev.cur_state = state;
562 #endif /* CONFIG_THERMAL */
564 case CTDP_CMD_OPERATION_REPORT:
565 IWL_DEBUG_TEMP(mvm, "cTDP avg energy in mWatt = %d\n", status);
566 /* when the function is called with CTDP_CMD_OPERATION_REPORT
567 * option the function should return the average budget value
568 * that is received from the FW.
569 * The budget can't be less or equal to 0, so it's possible
570 * to distinguish between error values and budgets.
573 case CTDP_CMD_OPERATION_STOP:
574 IWL_DEBUG_TEMP(mvm, "cTDP stopped successfully\n");
581 #ifdef CONFIG_THERMAL
582 static int compare_temps(const void *a, const void *b)
584 return ((s16)le16_to_cpu(*(__le16 *)a) -
585 (s16)le16_to_cpu(*(__le16 *)b));
588 int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm *mvm)
590 struct temp_report_ths_cmd cmd = {0};
591 int ret, i, j, idx = 0;
593 lockdep_assert_held(&mvm->mutex);
595 if (!mvm->tz_device.tzone)
598 /* The driver holds array of temperature trips that are unsorted
599 * and uncompressed, the FW should get it compressed and sorted
602 /* compress temp_trips to cmd array, remove uninitialized values*/
603 for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
604 if (mvm->tz_device.temp_trips[i] != S16_MIN) {
605 cmd.thresholds[idx++] =
606 cpu_to_le16(mvm->tz_device.temp_trips[i]);
609 cmd.num_temps = cpu_to_le32(idx);
615 sort(cmd.thresholds, idx, sizeof(s16), compare_temps, NULL);
617 /* we should save the indexes of trips because we sort
618 * and compress the orginal array
620 for (i = 0; i < idx; i++) {
621 for (j = 0; j < IWL_MAX_DTS_TRIPS; j++) {
622 if (le16_to_cpu(cmd.thresholds[i]) ==
623 mvm->tz_device.temp_trips[j])
624 mvm->tz_device.fw_trips_index[i] = j;
629 ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP,
630 TEMP_REPORTING_THRESHOLDS_CMD),
631 0, sizeof(cmd), &cmd);
633 IWL_ERR(mvm, "TEMP_REPORT_THS_CMD command failed (err=%d)\n",
639 static int iwl_mvm_tzone_get_temp(struct thermal_zone_device *device,
642 struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
646 mutex_lock(&mvm->mutex);
648 if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) {
653 ret = iwl_mvm_get_temp(mvm, &temp);
657 *temperature = temp * 1000;
660 mutex_unlock(&mvm->mutex);
664 static int iwl_mvm_tzone_get_trip_temp(struct thermal_zone_device *device,
667 struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
669 if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
672 *temp = mvm->tz_device.temp_trips[trip] * 1000;
677 static int iwl_mvm_tzone_get_trip_type(struct thermal_zone_device *device,
678 int trip, enum thermal_trip_type *type)
680 if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
683 *type = THERMAL_TRIP_PASSIVE;
688 static int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device *device,
691 struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
692 struct iwl_mvm_thermal_device *tzone;
696 mutex_lock(&mvm->mutex);
698 if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) {
703 if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) {
708 if ((temp / 1000) > S16_MAX) {
713 temperature = (s16)(temp / 1000);
714 tzone = &mvm->tz_device;
722 if (tzone->temp_trips[trip] == temperature) {
727 /* already existing temperature */
728 for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
729 if (tzone->temp_trips[i] == temperature) {
735 tzone->temp_trips[trip] = temperature;
737 ret = iwl_mvm_send_temp_report_ths_cmd(mvm);
739 mutex_unlock(&mvm->mutex);
743 static struct thermal_zone_device_ops tzone_ops = {
744 .get_temp = iwl_mvm_tzone_get_temp,
745 .get_trip_temp = iwl_mvm_tzone_get_trip_temp,
746 .get_trip_type = iwl_mvm_tzone_get_trip_type,
747 .set_trip_temp = iwl_mvm_tzone_set_trip_temp,
750 /* make all trips writable */
751 #define IWL_WRITABLE_TRIPS_MSK (BIT(IWL_MAX_DTS_TRIPS) - 1)
753 static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm)
756 char name[] = "iwlwifi";
758 if (!iwl_mvm_is_tt_in_fw(mvm)) {
759 mvm->tz_device.tzone = NULL;
764 BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
766 mvm->tz_device.tzone = thermal_zone_device_register(name,
768 IWL_WRITABLE_TRIPS_MSK,
771 if (IS_ERR(mvm->tz_device.tzone)) {
773 "Failed to register to thermal zone (err = %ld)\n",
774 PTR_ERR(mvm->tz_device.tzone));
775 mvm->tz_device.tzone = NULL;
779 /* 0 is a valid temperature,
780 * so initialize the array with S16_MIN which invalid temperature
782 for (i = 0 ; i < IWL_MAX_DTS_TRIPS; i++)
783 mvm->tz_device.temp_trips[i] = S16_MIN;
786 static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device *cdev,
787 unsigned long *state)
789 *state = ARRAY_SIZE(iwl_mvm_cdev_budgets) - 1;
794 static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device *cdev,
795 unsigned long *state)
797 struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
799 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
802 *state = mvm->cooling_dev.cur_state;
807 static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device *cdev,
808 unsigned long new_state)
810 struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
813 if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR))
816 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
819 mutex_lock(&mvm->mutex);
821 if (new_state >= ARRAY_SIZE(iwl_mvm_cdev_budgets)) {
826 ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
830 mutex_unlock(&mvm->mutex);
834 static struct thermal_cooling_device_ops tcooling_ops = {
835 .get_max_state = iwl_mvm_tcool_get_max_state,
836 .get_cur_state = iwl_mvm_tcool_get_cur_state,
837 .set_cur_state = iwl_mvm_tcool_set_cur_state,
840 static void iwl_mvm_cooling_device_register(struct iwl_mvm *mvm)
842 char name[] = "iwlwifi";
844 if (!iwl_mvm_is_ctdp_supported(mvm))
847 BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
849 mvm->cooling_dev.cdev =
850 thermal_cooling_device_register(name,
854 if (IS_ERR(mvm->cooling_dev.cdev)) {
856 "Failed to register to cooling device (err = %ld)\n",
857 PTR_ERR(mvm->cooling_dev.cdev));
858 mvm->cooling_dev.cdev = NULL;
863 static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm *mvm)
865 if (!iwl_mvm_is_tt_in_fw(mvm) || !mvm->tz_device.tzone)
868 IWL_DEBUG_TEMP(mvm, "Thermal zone device unregister\n");
869 thermal_zone_device_unregister(mvm->tz_device.tzone);
870 mvm->tz_device.tzone = NULL;
873 static void iwl_mvm_cooling_device_unregister(struct iwl_mvm *mvm)
875 if (!iwl_mvm_is_ctdp_supported(mvm) || !mvm->cooling_dev.cdev)
878 IWL_DEBUG_TEMP(mvm, "Cooling device unregister\n");
879 thermal_cooling_device_unregister(mvm->cooling_dev.cdev);
880 mvm->cooling_dev.cdev = NULL;
882 #endif /* CONFIG_THERMAL */
884 void iwl_mvm_thermal_initialize(struct iwl_mvm *mvm, u32 min_backoff)
886 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
888 IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
890 if (mvm->cfg->thermal_params)
891 tt->params = *mvm->cfg->thermal_params;
893 tt->params = iwl_mvm_default_tt_params;
895 tt->throttle = false;
896 tt->dynamic_smps = false;
897 tt->min_backoff = min_backoff;
898 INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
900 #ifdef CONFIG_THERMAL
901 iwl_mvm_cooling_device_register(mvm);
902 iwl_mvm_thermal_zone_register(mvm);
906 void iwl_mvm_thermal_exit(struct iwl_mvm *mvm)
908 cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
909 IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
911 #ifdef CONFIG_THERMAL
912 iwl_mvm_cooling_device_unregister(mvm);
913 iwl_mvm_thermal_zone_unregister(mvm);