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 * The full GNU General Public License is included in this distribution
22 * in the file called COPYING.
24 * Contact Information:
25 * Intel Linux Wireless <linuxwifi@intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
31 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
32 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
39 * * Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * * Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in
43 * the documentation and/or other materials provided with the
45 * * Neither the name Intel Corporation nor the names of its
46 * contributors may be used to endorse or promote products derived
47 * from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
63 #include <linux/sort.h>
67 #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
69 void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
71 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
72 u32 duration = tt->params.ct_kill_duration;
74 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
77 IWL_ERR(mvm, "Enter CT Kill\n");
78 iwl_mvm_set_hw_ctkill_state(mvm, true);
80 if (!iwl_mvm_is_tt_in_fw(mvm)) {
82 tt->dynamic_smps = false;
85 /* Don't schedule an exit work if we're in test mode, since
86 * the temperature will not change unless we manually set it
87 * again (or disable testing).
89 if (!mvm->temperature_test)
90 schedule_delayed_work(&tt->ct_kill_exit,
91 round_jiffies_relative(duration * HZ));
94 static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
96 if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
99 IWL_ERR(mvm, "Exit CT Kill\n");
100 iwl_mvm_set_hw_ctkill_state(mvm, false);
103 void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp)
105 /* ignore the notification if we are in test mode */
106 if (mvm->temperature_test)
109 if (mvm->temperature == temp)
112 mvm->temperature = temp;
113 iwl_mvm_tt_handler(mvm);
116 static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm,
117 struct iwl_rx_packet *pkt)
119 struct iwl_dts_measurement_notif_v1 *notif_v1;
120 int len = iwl_rx_packet_payload_len(pkt);
123 /* we can use notif_v1 only, because v2 only adds an additional
124 * parameter, which is not used in this function.
126 if (WARN_ON_ONCE(len < sizeof(*notif_v1))) {
127 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
131 notif_v1 = (void *)pkt->data;
133 temp = le32_to_cpu(notif_v1->temp);
135 /* shouldn't be negative, but since it's s32, make sure it isn't */
136 if (WARN_ON_ONCE(temp < 0))
139 IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp);
144 static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait,
145 struct iwl_rx_packet *pkt, void *data)
147 struct iwl_mvm *mvm =
148 container_of(notif_wait, struct iwl_mvm, notif_wait);
152 ret = iwl_mvm_temp_notif_parse(mvm, pkt);
161 void iwl_mvm_temp_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
163 struct iwl_rx_packet *pkt = rxb_addr(rxb);
164 struct iwl_dts_measurement_notif_v2 *notif_v2;
165 int len = iwl_rx_packet_payload_len(pkt);
169 /* the notification is handled synchronously in ctkill, so skip here */
170 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
173 temp = iwl_mvm_temp_notif_parse(mvm, pkt);
175 if (!iwl_mvm_is_tt_in_fw(mvm)) {
177 iwl_mvm_tt_temp_changed(mvm, temp);
181 if (WARN_ON_ONCE(len < sizeof(*notif_v2))) {
182 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
186 notif_v2 = (void *)pkt->data;
187 ths_crossed = le32_to_cpu(notif_v2->threshold_idx);
189 /* 0xFF in ths_crossed means the notification is not related
190 * to a trip, so we can ignore it here.
192 if (ths_crossed == 0xFF)
195 IWL_DEBUG_TEMP(mvm, "Temp = %d Threshold crossed = %d\n",
198 #ifdef CONFIG_THERMAL
199 if (WARN_ON(ths_crossed >= IWL_MAX_DTS_TRIPS))
202 if (mvm->tz_device.tzone) {
203 struct iwl_mvm_thermal_device *tz_dev = &mvm->tz_device;
205 thermal_notify_framework(tz_dev->tzone,
206 tz_dev->fw_trips_index[ths_crossed]);
208 #endif /* CONFIG_THERMAL */
211 void iwl_mvm_ct_kill_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
213 struct iwl_rx_packet *pkt = rxb_addr(rxb);
214 struct ct_kill_notif *notif;
215 int len = iwl_rx_packet_payload_len(pkt);
217 if (WARN_ON_ONCE(len != sizeof(*notif))) {
218 IWL_ERR(mvm, "Invalid CT_KILL_NOTIFICATION\n");
222 notif = (struct ct_kill_notif *)pkt->data;
223 IWL_DEBUG_TEMP(mvm, "CT Kill notification temperature = %d\n",
226 iwl_mvm_enter_ctkill(mvm);
229 static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm)
231 struct iwl_dts_measurement_cmd cmd = {
232 .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
234 struct iwl_ext_dts_measurement_cmd extcmd = {
235 .control_mode = cpu_to_le32(DTS_AUTOMATIC),
239 cmdid = iwl_cmd_id(CMD_DTS_MEASUREMENT_TRIGGER_WIDE,
242 if (!fw_has_capa(&mvm->fw->ucode_capa,
243 IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE))
244 return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(cmd), &cmd);
246 return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(extcmd), &extcmd);
249 int iwl_mvm_get_temp(struct iwl_mvm *mvm, s32 *temp)
251 struct iwl_notification_wait wait_temp_notif;
252 static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP,
253 DTS_MEASUREMENT_NOTIF_WIDE) };
256 lockdep_assert_held(&mvm->mutex);
258 iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
259 temp_notif, ARRAY_SIZE(temp_notif),
260 iwl_mvm_temp_notif_wait, temp);
262 ret = iwl_mvm_get_temp_cmd(mvm);
264 IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret);
265 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
269 ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
270 IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
272 IWL_ERR(mvm, "Getting the temperature timed out\n");
277 static void check_exit_ctkill(struct work_struct *work)
279 struct iwl_mvm_tt_mgmt *tt;
285 tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
286 mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
288 if (iwl_mvm_is_tt_in_fw(mvm)) {
289 iwl_mvm_exit_ctkill(mvm);
294 duration = tt->params.ct_kill_duration;
296 mutex_lock(&mvm->mutex);
298 if (__iwl_mvm_mac_start(mvm))
301 /* make sure the device is available for direct read/writes */
302 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) {
303 __iwl_mvm_mac_stop(mvm);
307 ret = iwl_mvm_get_temp(mvm, &temp);
309 iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL);
311 __iwl_mvm_mac_stop(mvm);
316 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
318 if (temp <= tt->params.ct_kill_exit) {
319 mutex_unlock(&mvm->mutex);
320 iwl_mvm_exit_ctkill(mvm);
325 mutex_unlock(&mvm->mutex);
326 schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
327 round_jiffies(duration * HZ));
330 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
331 struct ieee80211_vif *vif)
333 struct iwl_mvm *mvm = _data;
334 enum ieee80211_smps_mode smps_mode;
336 lockdep_assert_held(&mvm->mutex);
338 if (mvm->thermal_throttle.dynamic_smps)
339 smps_mode = IEEE80211_SMPS_DYNAMIC;
341 smps_mode = IEEE80211_SMPS_AUTOMATIC;
343 if (vif->type != NL80211_IFTYPE_STATION)
346 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
349 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
351 struct iwl_mvm_sta *mvmsta;
354 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
355 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, i);
359 if (enable == mvmsta->tt_tx_protection)
361 err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
363 IWL_ERR(mvm, "Failed to %s Tx protection\n",
364 enable ? "enable" : "disable");
366 IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
367 enable ? "Enable" : "Disable");
368 mvmsta->tt_tx_protection = enable;
373 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
375 struct iwl_host_cmd cmd = {
376 .id = REPLY_THERMAL_MNG_BACKOFF,
377 .len = { sizeof(u32), },
378 .data = { &backoff, },
381 backoff = max(backoff, mvm->thermal_throttle.min_backoff);
383 if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
384 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
386 mvm->thermal_throttle.tx_backoff = backoff;
388 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
392 void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
394 struct iwl_tt_params *params = &mvm->thermal_throttle.params;
395 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
396 s32 temperature = mvm->temperature;
397 bool throttle_enable = false;
401 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
403 if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
404 iwl_mvm_enter_ctkill(mvm);
408 if (params->support_ct_kill &&
409 temperature <= params->ct_kill_exit) {
410 iwl_mvm_exit_ctkill(mvm);
414 if (params->support_dynamic_smps) {
415 if (!tt->dynamic_smps &&
416 temperature >= params->dynamic_smps_entry) {
417 IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
418 tt->dynamic_smps = true;
419 ieee80211_iterate_active_interfaces_atomic(
420 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
421 iwl_mvm_tt_smps_iterator, mvm);
422 throttle_enable = true;
423 } else if (tt->dynamic_smps &&
424 temperature <= params->dynamic_smps_exit) {
425 IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
426 tt->dynamic_smps = false;
427 ieee80211_iterate_active_interfaces_atomic(
428 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
429 iwl_mvm_tt_smps_iterator, mvm);
433 if (params->support_tx_protection) {
434 if (temperature >= params->tx_protection_entry) {
435 iwl_mvm_tt_tx_protection(mvm, true);
436 throttle_enable = true;
437 } else if (temperature <= params->tx_protection_exit) {
438 iwl_mvm_tt_tx_protection(mvm, false);
442 if (params->support_tx_backoff) {
443 tx_backoff = tt->min_backoff;
444 for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
445 if (temperature < params->tx_backoff[i].temperature)
447 tx_backoff = max(tt->min_backoff,
448 params->tx_backoff[i].backoff);
450 if (tx_backoff != tt->min_backoff)
451 throttle_enable = true;
452 if (tt->tx_backoff != tx_backoff)
453 iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
456 if (!tt->throttle && throttle_enable) {
458 "Due to high temperature thermal throttling initiated\n");
460 } else if (tt->throttle && !tt->dynamic_smps &&
461 tt->tx_backoff == tt->min_backoff &&
462 temperature <= params->tx_protection_exit) {
464 "Temperature is back to normal thermal throttling stopped\n");
465 tt->throttle = false;
469 static const struct iwl_tt_params iwl_mvm_default_tt_params = {
470 .ct_kill_entry = 118,
472 .ct_kill_duration = 5,
473 .dynamic_smps_entry = 114,
474 .dynamic_smps_exit = 110,
475 .tx_protection_entry = 114,
476 .tx_protection_exit = 108,
478 {.temperature = 112, .backoff = 200},
479 {.temperature = 113, .backoff = 600},
480 {.temperature = 114, .backoff = 1200},
481 {.temperature = 115, .backoff = 2000},
482 {.temperature = 116, .backoff = 4000},
483 {.temperature = 117, .backoff = 10000},
485 .support_ct_kill = true,
486 .support_dynamic_smps = true,
487 .support_tx_protection = true,
488 .support_tx_backoff = true,
491 /* budget in mWatt */
492 static const u32 iwl_mvm_cdev_budgets[] = {
493 2000, /* cooling state 0 */
494 1800, /* cooling state 1 */
495 1600, /* cooling state 2 */
496 1400, /* cooling state 3 */
497 1200, /* cooling state 4 */
498 1000, /* cooling state 5 */
499 900, /* cooling state 6 */
500 800, /* cooling state 7 */
501 700, /* cooling state 8 */
502 650, /* cooling state 9 */
503 600, /* cooling state 10 */
504 550, /* cooling state 11 */
505 500, /* cooling state 12 */
506 450, /* cooling state 13 */
507 400, /* cooling state 14 */
508 350, /* cooling state 15 */
509 300, /* cooling state 16 */
510 250, /* cooling state 17 */
511 200, /* cooling state 18 */
512 150, /* cooling state 19 */
515 int iwl_mvm_ctdp_command(struct iwl_mvm *mvm, u32 op, u32 state)
517 struct iwl_mvm_ctdp_cmd cmd = {
518 .operation = cpu_to_le32(op),
519 .budget = cpu_to_le32(iwl_mvm_cdev_budgets[state]),
525 lockdep_assert_held(&mvm->mutex);
528 ret = iwl_mvm_send_cmd_pdu_status(mvm, WIDE_ID(PHY_OPS_GROUP,
530 sizeof(cmd), &cmd, &status);
533 IWL_ERR(mvm, "cTDP command failed (err=%d)\n", ret);
538 case CTDP_CMD_OPERATION_START:
539 #ifdef CONFIG_THERMAL
540 mvm->cooling_dev.cur_state = state;
541 #endif /* CONFIG_THERMAL */
543 case CTDP_CMD_OPERATION_REPORT:
544 IWL_DEBUG_TEMP(mvm, "cTDP avg energy in mWatt = %d\n", status);
545 /* when the function is called with CTDP_CMD_OPERATION_REPORT
546 * option the function should return the average budget value
547 * that is received from the FW.
548 * The budget can't be less or equal to 0, so it's possible
549 * to distinguish between error values and budgets.
552 case CTDP_CMD_OPERATION_STOP:
553 IWL_DEBUG_TEMP(mvm, "cTDP stopped successfully\n");
560 #ifdef CONFIG_THERMAL
561 static int compare_temps(const void *a, const void *b)
563 return ((s16)le16_to_cpu(*(__le16 *)a) -
564 (s16)le16_to_cpu(*(__le16 *)b));
567 int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm *mvm)
569 struct temp_report_ths_cmd cmd = {0};
570 int ret, i, j, idx = 0;
572 lockdep_assert_held(&mvm->mutex);
574 if (!mvm->tz_device.tzone)
577 /* The driver holds array of temperature trips that are unsorted
578 * and uncompressed, the FW should get it compressed and sorted
581 /* compress temp_trips to cmd array, remove uninitialized values*/
582 for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
583 if (mvm->tz_device.temp_trips[i] != S16_MIN) {
584 cmd.thresholds[idx++] =
585 cpu_to_le16(mvm->tz_device.temp_trips[i]);
588 cmd.num_temps = cpu_to_le32(idx);
594 sort(cmd.thresholds, idx, sizeof(s16), compare_temps, NULL);
596 /* we should save the indexes of trips because we sort
597 * and compress the orginal array
599 for (i = 0; i < idx; i++) {
600 for (j = 0; j < IWL_MAX_DTS_TRIPS; j++) {
601 if (le16_to_cpu(cmd.thresholds[i]) ==
602 mvm->tz_device.temp_trips[j])
603 mvm->tz_device.fw_trips_index[i] = j;
608 ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP,
609 TEMP_REPORTING_THRESHOLDS_CMD),
610 0, sizeof(cmd), &cmd);
612 IWL_ERR(mvm, "TEMP_REPORT_THS_CMD command failed (err=%d)\n",
618 static int iwl_mvm_tzone_get_temp(struct thermal_zone_device *device,
621 struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
625 mutex_lock(&mvm->mutex);
627 if (!iwl_mvm_firmware_running(mvm) ||
628 mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) {
633 ret = iwl_mvm_get_temp(mvm, &temp);
637 *temperature = temp * 1000;
640 mutex_unlock(&mvm->mutex);
644 static int iwl_mvm_tzone_get_trip_temp(struct thermal_zone_device *device,
647 struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
649 if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
652 *temp = mvm->tz_device.temp_trips[trip] * 1000;
657 static int iwl_mvm_tzone_get_trip_type(struct thermal_zone_device *device,
658 int trip, enum thermal_trip_type *type)
660 if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
663 *type = THERMAL_TRIP_PASSIVE;
668 static int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device *device,
671 struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
672 struct iwl_mvm_thermal_device *tzone;
676 mutex_lock(&mvm->mutex);
678 if (!iwl_mvm_firmware_running(mvm) ||
679 mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) {
684 if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) {
689 if ((temp / 1000) > S16_MAX) {
694 temperature = (s16)(temp / 1000);
695 tzone = &mvm->tz_device;
703 if (tzone->temp_trips[trip] == temperature) {
708 /* already existing temperature */
709 for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
710 if (tzone->temp_trips[i] == temperature) {
716 tzone->temp_trips[trip] = temperature;
718 ret = iwl_mvm_send_temp_report_ths_cmd(mvm);
720 mutex_unlock(&mvm->mutex);
724 static struct thermal_zone_device_ops tzone_ops = {
725 .get_temp = iwl_mvm_tzone_get_temp,
726 .get_trip_temp = iwl_mvm_tzone_get_trip_temp,
727 .get_trip_type = iwl_mvm_tzone_get_trip_type,
728 .set_trip_temp = iwl_mvm_tzone_set_trip_temp,
731 /* make all trips writable */
732 #define IWL_WRITABLE_TRIPS_MSK (BIT(IWL_MAX_DTS_TRIPS) - 1)
734 static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm)
737 char name[] = "iwlwifi";
739 if (!iwl_mvm_is_tt_in_fw(mvm)) {
740 mvm->tz_device.tzone = NULL;
745 BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
747 mvm->tz_device.tzone = thermal_zone_device_register(name,
749 IWL_WRITABLE_TRIPS_MSK,
752 if (IS_ERR(mvm->tz_device.tzone)) {
754 "Failed to register to thermal zone (err = %ld)\n",
755 PTR_ERR(mvm->tz_device.tzone));
756 mvm->tz_device.tzone = NULL;
760 /* 0 is a valid temperature,
761 * so initialize the array with S16_MIN which invalid temperature
763 for (i = 0 ; i < IWL_MAX_DTS_TRIPS; i++)
764 mvm->tz_device.temp_trips[i] = S16_MIN;
767 static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device *cdev,
768 unsigned long *state)
770 *state = ARRAY_SIZE(iwl_mvm_cdev_budgets) - 1;
775 static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device *cdev,
776 unsigned long *state)
778 struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
780 *state = mvm->cooling_dev.cur_state;
785 static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device *cdev,
786 unsigned long new_state)
788 struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
791 mutex_lock(&mvm->mutex);
793 if (!iwl_mvm_firmware_running(mvm) ||
794 mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) {
799 if (new_state >= ARRAY_SIZE(iwl_mvm_cdev_budgets)) {
804 ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
808 mutex_unlock(&mvm->mutex);
812 static const struct thermal_cooling_device_ops tcooling_ops = {
813 .get_max_state = iwl_mvm_tcool_get_max_state,
814 .get_cur_state = iwl_mvm_tcool_get_cur_state,
815 .set_cur_state = iwl_mvm_tcool_set_cur_state,
818 static void iwl_mvm_cooling_device_register(struct iwl_mvm *mvm)
820 char name[] = "iwlwifi";
822 if (!iwl_mvm_is_ctdp_supported(mvm))
825 BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
827 mvm->cooling_dev.cdev =
828 thermal_cooling_device_register(name,
832 if (IS_ERR(mvm->cooling_dev.cdev)) {
834 "Failed to register to cooling device (err = %ld)\n",
835 PTR_ERR(mvm->cooling_dev.cdev));
836 mvm->cooling_dev.cdev = NULL;
841 static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm *mvm)
843 if (!iwl_mvm_is_tt_in_fw(mvm) || !mvm->tz_device.tzone)
846 IWL_DEBUG_TEMP(mvm, "Thermal zone device unregister\n");
847 if (mvm->tz_device.tzone) {
848 thermal_zone_device_unregister(mvm->tz_device.tzone);
849 mvm->tz_device.tzone = NULL;
853 static void iwl_mvm_cooling_device_unregister(struct iwl_mvm *mvm)
855 if (!iwl_mvm_is_ctdp_supported(mvm) || !mvm->cooling_dev.cdev)
858 IWL_DEBUG_TEMP(mvm, "Cooling device unregister\n");
859 if (mvm->cooling_dev.cdev) {
860 thermal_cooling_device_unregister(mvm->cooling_dev.cdev);
861 mvm->cooling_dev.cdev = NULL;
864 #endif /* CONFIG_THERMAL */
866 void iwl_mvm_thermal_initialize(struct iwl_mvm *mvm, u32 min_backoff)
868 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
870 IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
872 if (mvm->cfg->thermal_params)
873 tt->params = *mvm->cfg->thermal_params;
875 tt->params = iwl_mvm_default_tt_params;
877 tt->throttle = false;
878 tt->dynamic_smps = false;
879 tt->min_backoff = min_backoff;
880 INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
882 #ifdef CONFIG_THERMAL
883 iwl_mvm_cooling_device_register(mvm);
884 iwl_mvm_thermal_zone_register(mvm);
886 mvm->init_status |= IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE;
889 void iwl_mvm_thermal_exit(struct iwl_mvm *mvm)
891 if (!(mvm->init_status & IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE))
894 cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
895 IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
897 #ifdef CONFIG_THERMAL
898 iwl_mvm_cooling_device_unregister(mvm);
899 iwl_mvm_thermal_zone_unregister(mvm);
901 mvm->init_status &= ~IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE;