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, 2019 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
11 * Copyright(c) 2019 - 2020 Intel Corporation
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of version 2 of the GNU General Public License as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * The full GNU General Public License is included in this distribution
23 * in the file called COPYING.
25 * Contact Information:
26 * Intel Linux Wireless <linuxwifi@intel.com>
27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 * Copyright(c) 2012 - 2014, 2019 Intel Corporation. All rights reserved.
32 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
33 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
34 * Copyright(c) 2019 - 2020 Intel Corporation
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
47 * * Neither the name Intel Corporation nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *****************************************************************************/
65 #include <linux/sort.h>
69 #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
71 void iwl_mvm_enter_ctkill(struct iwl_mvm
*mvm
)
73 struct iwl_mvm_tt_mgmt
*tt
= &mvm
->thermal_throttle
;
74 u32 duration
= tt
->params
.ct_kill_duration
;
76 if (test_bit(IWL_MVM_STATUS_HW_CTKILL
, &mvm
->status
))
79 IWL_ERR(mvm
, "Enter CT Kill\n");
80 iwl_mvm_set_hw_ctkill_state(mvm
, true);
82 if (!iwl_mvm_is_tt_in_fw(mvm
)) {
84 tt
->dynamic_smps
= false;
87 /* Don't schedule an exit work if we're in test mode, since
88 * the temperature will not change unless we manually set it
89 * again (or disable testing).
91 if (!mvm
->temperature_test
)
92 schedule_delayed_work(&tt
->ct_kill_exit
,
93 round_jiffies_relative(duration
* HZ
));
96 static void iwl_mvm_exit_ctkill(struct iwl_mvm
*mvm
)
98 if (!test_bit(IWL_MVM_STATUS_HW_CTKILL
, &mvm
->status
))
101 IWL_ERR(mvm
, "Exit CT Kill\n");
102 iwl_mvm_set_hw_ctkill_state(mvm
, false);
105 void iwl_mvm_tt_temp_changed(struct iwl_mvm
*mvm
, u32 temp
)
107 /* ignore the notification if we are in test mode */
108 if (mvm
->temperature_test
)
111 if (mvm
->temperature
== temp
)
114 mvm
->temperature
= temp
;
115 iwl_mvm_tt_handler(mvm
);
118 static int iwl_mvm_temp_notif_parse(struct iwl_mvm
*mvm
,
119 struct iwl_rx_packet
*pkt
)
121 struct iwl_dts_measurement_notif_v1
*notif_v1
;
122 int len
= iwl_rx_packet_payload_len(pkt
);
125 /* we can use notif_v1 only, because v2 only adds an additional
126 * parameter, which is not used in this function.
128 if (WARN_ON_ONCE(len
< sizeof(*notif_v1
))) {
129 IWL_ERR(mvm
, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
133 notif_v1
= (void *)pkt
->data
;
135 temp
= le32_to_cpu(notif_v1
->temp
);
137 /* shouldn't be negative, but since it's s32, make sure it isn't */
138 if (WARN_ON_ONCE(temp
< 0))
141 IWL_DEBUG_TEMP(mvm
, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp
);
146 static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data
*notif_wait
,
147 struct iwl_rx_packet
*pkt
, void *data
)
149 struct iwl_mvm
*mvm
=
150 container_of(notif_wait
, struct iwl_mvm
, notif_wait
);
154 ret
= iwl_mvm_temp_notif_parse(mvm
, pkt
);
163 void iwl_mvm_temp_notif(struct iwl_mvm
*mvm
, struct iwl_rx_cmd_buffer
*rxb
)
165 struct iwl_rx_packet
*pkt
= rxb_addr(rxb
);
166 struct iwl_dts_measurement_notif_v2
*notif_v2
;
167 int len
= iwl_rx_packet_payload_len(pkt
);
171 /* the notification is handled synchronously in ctkill, so skip here */
172 if (test_bit(IWL_MVM_STATUS_HW_CTKILL
, &mvm
->status
))
175 temp
= iwl_mvm_temp_notif_parse(mvm
, pkt
);
177 if (!iwl_mvm_is_tt_in_fw(mvm
)) {
179 iwl_mvm_tt_temp_changed(mvm
, temp
);
183 if (WARN_ON_ONCE(len
< sizeof(*notif_v2
))) {
184 IWL_ERR(mvm
, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
188 notif_v2
= (void *)pkt
->data
;
189 ths_crossed
= le32_to_cpu(notif_v2
->threshold_idx
);
191 /* 0xFF in ths_crossed means the notification is not related
192 * to a trip, so we can ignore it here.
194 if (ths_crossed
== 0xFF)
197 IWL_DEBUG_TEMP(mvm
, "Temp = %d Threshold crossed = %d\n",
200 #ifdef CONFIG_THERMAL
201 if (WARN_ON(ths_crossed
>= IWL_MAX_DTS_TRIPS
))
204 if (mvm
->tz_device
.tzone
) {
205 struct iwl_mvm_thermal_device
*tz_dev
= &mvm
->tz_device
;
207 thermal_notify_framework(tz_dev
->tzone
,
208 tz_dev
->fw_trips_index
[ths_crossed
]);
210 #endif /* CONFIG_THERMAL */
213 void iwl_mvm_ct_kill_notif(struct iwl_mvm
*mvm
, struct iwl_rx_cmd_buffer
*rxb
)
215 struct iwl_rx_packet
*pkt
= rxb_addr(rxb
);
216 struct ct_kill_notif
*notif
;
217 int len
= iwl_rx_packet_payload_len(pkt
);
219 if (WARN_ON_ONCE(len
!= sizeof(*notif
))) {
220 IWL_ERR(mvm
, "Invalid CT_KILL_NOTIFICATION\n");
224 notif
= (struct ct_kill_notif
*)pkt
->data
;
225 IWL_DEBUG_TEMP(mvm
, "CT Kill notification temperature = %d\n",
228 iwl_mvm_enter_ctkill(mvm
);
231 static int iwl_mvm_get_temp_cmd(struct iwl_mvm
*mvm
)
233 struct iwl_dts_measurement_cmd cmd
= {
234 .flags
= cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP
),
236 struct iwl_ext_dts_measurement_cmd extcmd
= {
237 .control_mode
= cpu_to_le32(DTS_DIRECT_WITHOUT_MEASURE
),
241 cmdid
= iwl_cmd_id(CMD_DTS_MEASUREMENT_TRIGGER_WIDE
,
244 if (!fw_has_capa(&mvm
->fw
->ucode_capa
,
245 IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE
))
246 return iwl_mvm_send_cmd_pdu(mvm
, cmdid
, 0, sizeof(cmd
), &cmd
);
248 return iwl_mvm_send_cmd_pdu(mvm
, cmdid
, 0, sizeof(extcmd
), &extcmd
);
251 int iwl_mvm_get_temp(struct iwl_mvm
*mvm
, s32
*temp
)
253 struct iwl_notification_wait wait_temp_notif
;
254 static u16 temp_notif
[] = { WIDE_ID(PHY_OPS_GROUP
,
255 DTS_MEASUREMENT_NOTIF_WIDE
) };
258 lockdep_assert_held(&mvm
->mutex
);
260 iwl_init_notification_wait(&mvm
->notif_wait
, &wait_temp_notif
,
261 temp_notif
, ARRAY_SIZE(temp_notif
),
262 iwl_mvm_temp_notif_wait
, temp
);
264 ret
= iwl_mvm_get_temp_cmd(mvm
);
266 IWL_ERR(mvm
, "Failed to get the temperature (err=%d)\n", ret
);
267 iwl_remove_notification(&mvm
->notif_wait
, &wait_temp_notif
);
271 ret
= iwl_wait_notification(&mvm
->notif_wait
, &wait_temp_notif
,
272 IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT
);
274 IWL_ERR(mvm
, "Getting the temperature timed out\n");
279 static void check_exit_ctkill(struct work_struct
*work
)
281 struct iwl_mvm_tt_mgmt
*tt
;
287 tt
= container_of(work
, struct iwl_mvm_tt_mgmt
, ct_kill_exit
.work
);
288 mvm
= container_of(tt
, struct iwl_mvm
, thermal_throttle
);
290 if (iwl_mvm_is_tt_in_fw(mvm
)) {
291 iwl_mvm_exit_ctkill(mvm
);
296 duration
= tt
->params
.ct_kill_duration
;
298 mutex_lock(&mvm
->mutex
);
300 if (__iwl_mvm_mac_start(mvm
))
303 ret
= iwl_mvm_get_temp(mvm
, &temp
);
305 __iwl_mvm_mac_stop(mvm
);
310 IWL_DEBUG_TEMP(mvm
, "NIC temperature: %d\n", temp
);
312 if (temp
<= tt
->params
.ct_kill_exit
) {
313 mutex_unlock(&mvm
->mutex
);
314 iwl_mvm_exit_ctkill(mvm
);
319 mutex_unlock(&mvm
->mutex
);
320 schedule_delayed_work(&mvm
->thermal_throttle
.ct_kill_exit
,
321 round_jiffies(duration
* HZ
));
324 static void iwl_mvm_tt_smps_iterator(void *_data
, u8
*mac
,
325 struct ieee80211_vif
*vif
)
327 struct iwl_mvm
*mvm
= _data
;
328 enum ieee80211_smps_mode smps_mode
;
330 lockdep_assert_held(&mvm
->mutex
);
332 if (mvm
->thermal_throttle
.dynamic_smps
)
333 smps_mode
= IEEE80211_SMPS_DYNAMIC
;
335 smps_mode
= IEEE80211_SMPS_AUTOMATIC
;
337 if (vif
->type
!= NL80211_IFTYPE_STATION
)
340 iwl_mvm_update_smps(mvm
, vif
, IWL_MVM_SMPS_REQ_TT
, smps_mode
);
343 static void iwl_mvm_tt_tx_protection(struct iwl_mvm
*mvm
, bool enable
)
345 struct iwl_mvm_sta
*mvmsta
;
348 for (i
= 0; i
< ARRAY_SIZE(mvm
->fw_id_to_mac_id
); i
++) {
349 mvmsta
= iwl_mvm_sta_from_staid_protected(mvm
, i
);
353 if (enable
== mvmsta
->tt_tx_protection
)
355 err
= iwl_mvm_tx_protection(mvm
, mvmsta
, enable
);
357 IWL_ERR(mvm
, "Failed to %s Tx protection\n",
358 enable
? "enable" : "disable");
360 IWL_DEBUG_TEMP(mvm
, "%s Tx protection\n",
361 enable
? "Enable" : "Disable");
362 mvmsta
->tt_tx_protection
= enable
;
367 void iwl_mvm_tt_tx_backoff(struct iwl_mvm
*mvm
, u32 backoff
)
369 struct iwl_host_cmd cmd
= {
370 .id
= REPLY_THERMAL_MNG_BACKOFF
,
371 .len
= { sizeof(u32
), },
372 .data
= { &backoff
, },
375 backoff
= max(backoff
, mvm
->thermal_throttle
.min_backoff
);
377 if (iwl_mvm_send_cmd(mvm
, &cmd
) == 0) {
378 IWL_DEBUG_TEMP(mvm
, "Set Thermal Tx backoff to: %u\n",
380 mvm
->thermal_throttle
.tx_backoff
= backoff
;
382 IWL_ERR(mvm
, "Failed to change Thermal Tx backoff\n");
386 void iwl_mvm_tt_handler(struct iwl_mvm
*mvm
)
388 struct iwl_tt_params
*params
= &mvm
->thermal_throttle
.params
;
389 struct iwl_mvm_tt_mgmt
*tt
= &mvm
->thermal_throttle
;
390 s32 temperature
= mvm
->temperature
;
391 bool throttle_enable
= false;
395 IWL_DEBUG_TEMP(mvm
, "NIC temperature: %d\n", mvm
->temperature
);
397 if (params
->support_ct_kill
&& temperature
>= params
->ct_kill_entry
) {
398 iwl_mvm_enter_ctkill(mvm
);
402 if (params
->support_ct_kill
&&
403 temperature
<= params
->ct_kill_exit
) {
404 iwl_mvm_exit_ctkill(mvm
);
408 if (params
->support_dynamic_smps
) {
409 if (!tt
->dynamic_smps
&&
410 temperature
>= params
->dynamic_smps_entry
) {
411 IWL_DEBUG_TEMP(mvm
, "Enable dynamic SMPS\n");
412 tt
->dynamic_smps
= true;
413 ieee80211_iterate_active_interfaces_atomic(
414 mvm
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
415 iwl_mvm_tt_smps_iterator
, mvm
);
416 throttle_enable
= true;
417 } else if (tt
->dynamic_smps
&&
418 temperature
<= params
->dynamic_smps_exit
) {
419 IWL_DEBUG_TEMP(mvm
, "Disable dynamic SMPS\n");
420 tt
->dynamic_smps
= false;
421 ieee80211_iterate_active_interfaces_atomic(
422 mvm
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
423 iwl_mvm_tt_smps_iterator
, mvm
);
427 if (params
->support_tx_protection
) {
428 if (temperature
>= params
->tx_protection_entry
) {
429 iwl_mvm_tt_tx_protection(mvm
, true);
430 throttle_enable
= true;
431 } else if (temperature
<= params
->tx_protection_exit
) {
432 iwl_mvm_tt_tx_protection(mvm
, false);
436 if (params
->support_tx_backoff
) {
437 tx_backoff
= tt
->min_backoff
;
438 for (i
= 0; i
< TT_TX_BACKOFF_SIZE
; i
++) {
439 if (temperature
< params
->tx_backoff
[i
].temperature
)
441 tx_backoff
= max(tt
->min_backoff
,
442 params
->tx_backoff
[i
].backoff
);
444 if (tx_backoff
!= tt
->min_backoff
)
445 throttle_enable
= true;
446 if (tt
->tx_backoff
!= tx_backoff
)
447 iwl_mvm_tt_tx_backoff(mvm
, tx_backoff
);
450 if (!tt
->throttle
&& throttle_enable
) {
452 "Due to high temperature thermal throttling initiated\n");
454 } else if (tt
->throttle
&& !tt
->dynamic_smps
&&
455 tt
->tx_backoff
== tt
->min_backoff
&&
456 temperature
<= params
->tx_protection_exit
) {
458 "Temperature is back to normal thermal throttling stopped\n");
459 tt
->throttle
= false;
463 static const struct iwl_tt_params iwl_mvm_default_tt_params
= {
464 .ct_kill_entry
= 118,
466 .ct_kill_duration
= 5,
467 .dynamic_smps_entry
= 114,
468 .dynamic_smps_exit
= 110,
469 .tx_protection_entry
= 114,
470 .tx_protection_exit
= 108,
472 {.temperature
= 112, .backoff
= 200},
473 {.temperature
= 113, .backoff
= 600},
474 {.temperature
= 114, .backoff
= 1200},
475 {.temperature
= 115, .backoff
= 2000},
476 {.temperature
= 116, .backoff
= 4000},
477 {.temperature
= 117, .backoff
= 10000},
479 .support_ct_kill
= true,
480 .support_dynamic_smps
= true,
481 .support_tx_protection
= true,
482 .support_tx_backoff
= true,
485 /* budget in mWatt */
486 static const u32 iwl_mvm_cdev_budgets
[] = {
487 2400, /* cooling state 0 */
488 2000, /* cooling state 1 */
489 1800, /* cooling state 2 */
490 1600, /* cooling state 3 */
491 1400, /* cooling state 4 */
492 1200, /* cooling state 5 */
493 1000, /* cooling state 6 */
494 900, /* cooling state 7 */
495 800, /* cooling state 8 */
496 700, /* cooling state 9 */
497 650, /* cooling state 10 */
498 600, /* cooling state 11 */
499 550, /* cooling state 12 */
500 500, /* cooling state 13 */
501 450, /* cooling state 14 */
502 400, /* cooling state 15 */
503 350, /* cooling state 16 */
504 300, /* cooling state 17 */
505 250, /* cooling state 18 */
506 200, /* cooling state 19 */
507 150, /* cooling state 20 */
510 int iwl_mvm_ctdp_command(struct iwl_mvm
*mvm
, u32 op
, u32 state
)
512 struct iwl_mvm_ctdp_cmd cmd
= {
513 .operation
= cpu_to_le32(op
),
514 .budget
= cpu_to_le32(iwl_mvm_cdev_budgets
[state
]),
520 lockdep_assert_held(&mvm
->mutex
);
523 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, WIDE_ID(PHY_OPS_GROUP
,
525 sizeof(cmd
), &cmd
, &status
);
528 IWL_ERR(mvm
, "cTDP command failed (err=%d)\n", ret
);
533 case CTDP_CMD_OPERATION_START
:
534 #ifdef CONFIG_THERMAL
535 mvm
->cooling_dev
.cur_state
= state
;
536 #endif /* CONFIG_THERMAL */
538 case CTDP_CMD_OPERATION_REPORT
:
539 IWL_DEBUG_TEMP(mvm
, "cTDP avg energy in mWatt = %d\n", status
);
540 /* when the function is called with CTDP_CMD_OPERATION_REPORT
541 * option the function should return the average budget value
542 * that is received from the FW.
543 * The budget can't be less or equal to 0, so it's possible
544 * to distinguish between error values and budgets.
547 case CTDP_CMD_OPERATION_STOP
:
548 IWL_DEBUG_TEMP(mvm
, "cTDP stopped successfully\n");
555 #ifdef CONFIG_THERMAL
556 static int compare_temps(const void *a
, const void *b
)
558 return ((s16
)le16_to_cpu(*(__le16
*)a
) -
559 (s16
)le16_to_cpu(*(__le16
*)b
));
563 int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm
*mvm
)
565 struct temp_report_ths_cmd cmd
= {0};
567 #ifdef CONFIG_THERMAL
570 lockdep_assert_held(&mvm
->mutex
);
572 if (!mvm
->tz_device
.tzone
)
575 /* The driver holds array of temperature trips that are unsorted
576 * and uncompressed, the FW should get it compressed and sorted
579 /* compress temp_trips to cmd array, remove uninitialized values*/
580 for (i
= 0; i
< IWL_MAX_DTS_TRIPS
; i
++) {
581 if (mvm
->tz_device
.temp_trips
[i
] != S16_MIN
) {
582 cmd
.thresholds
[idx
++] =
583 cpu_to_le16(mvm
->tz_device
.temp_trips
[i
]);
586 cmd
.num_temps
= cpu_to_le32(idx
);
592 sort(cmd
.thresholds
, idx
, sizeof(s16
), compare_temps
, NULL
);
594 /* we should save the indexes of trips because we sort
595 * and compress the orginal array
597 for (i
= 0; i
< idx
; i
++) {
598 for (j
= 0; j
< IWL_MAX_DTS_TRIPS
; j
++) {
599 if (le16_to_cpu(cmd
.thresholds
[i
]) ==
600 mvm
->tz_device
.temp_trips
[j
])
601 mvm
->tz_device
.fw_trips_index
[i
] = j
;
607 ret
= iwl_mvm_send_cmd_pdu(mvm
, WIDE_ID(PHY_OPS_GROUP
,
608 TEMP_REPORTING_THRESHOLDS_CMD
),
609 0, sizeof(cmd
), &cmd
);
611 IWL_ERR(mvm
, "TEMP_REPORT_THS_CMD command failed (err=%d)\n",
617 #ifdef CONFIG_THERMAL
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
)
738 static atomic_t counter
= ATOMIC_INIT(0);
740 if (!iwl_mvm_is_tt_in_fw(mvm
)) {
741 mvm
->tz_device
.tzone
= NULL
;
746 BUILD_BUG_ON(ARRAY_SIZE(name
) >= THERMAL_NAME_LENGTH
);
748 sprintf(name
, "iwlwifi_%u", atomic_inc_return(&counter
) & 0xFF);
749 mvm
->tz_device
.tzone
= thermal_zone_device_register(name
,
751 IWL_WRITABLE_TRIPS_MSK
,
754 if (IS_ERR(mvm
->tz_device
.tzone
)) {
756 "Failed to register to thermal zone (err = %ld)\n",
757 PTR_ERR(mvm
->tz_device
.tzone
));
758 mvm
->tz_device
.tzone
= NULL
;
762 /* 0 is a valid temperature,
763 * so initialize the array with S16_MIN which invalid temperature
765 for (i
= 0 ; i
< IWL_MAX_DTS_TRIPS
; i
++)
766 mvm
->tz_device
.temp_trips
[i
] = S16_MIN
;
769 static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device
*cdev
,
770 unsigned long *state
)
772 *state
= ARRAY_SIZE(iwl_mvm_cdev_budgets
) - 1;
777 static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device
*cdev
,
778 unsigned long *state
)
780 struct iwl_mvm
*mvm
= (struct iwl_mvm
*)(cdev
->devdata
);
782 *state
= mvm
->cooling_dev
.cur_state
;
787 static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device
*cdev
,
788 unsigned long new_state
)
790 struct iwl_mvm
*mvm
= (struct iwl_mvm
*)(cdev
->devdata
);
793 mutex_lock(&mvm
->mutex
);
795 if (!iwl_mvm_firmware_running(mvm
) ||
796 mvm
->fwrt
.cur_fw_img
!= IWL_UCODE_REGULAR
) {
801 if (new_state
>= ARRAY_SIZE(iwl_mvm_cdev_budgets
)) {
806 ret
= iwl_mvm_ctdp_command(mvm
, CTDP_CMD_OPERATION_START
,
810 mutex_unlock(&mvm
->mutex
);
814 static const struct thermal_cooling_device_ops tcooling_ops
= {
815 .get_max_state
= iwl_mvm_tcool_get_max_state
,
816 .get_cur_state
= iwl_mvm_tcool_get_cur_state
,
817 .set_cur_state
= iwl_mvm_tcool_set_cur_state
,
820 static void iwl_mvm_cooling_device_register(struct iwl_mvm
*mvm
)
822 char name
[] = "iwlwifi";
824 if (!iwl_mvm_is_ctdp_supported(mvm
))
827 BUILD_BUG_ON(ARRAY_SIZE(name
) >= THERMAL_NAME_LENGTH
);
829 mvm
->cooling_dev
.cdev
=
830 thermal_cooling_device_register(name
,
834 if (IS_ERR(mvm
->cooling_dev
.cdev
)) {
836 "Failed to register to cooling device (err = %ld)\n",
837 PTR_ERR(mvm
->cooling_dev
.cdev
));
838 mvm
->cooling_dev
.cdev
= NULL
;
843 static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm
*mvm
)
845 if (!iwl_mvm_is_tt_in_fw(mvm
) || !mvm
->tz_device
.tzone
)
848 IWL_DEBUG_TEMP(mvm
, "Thermal zone device unregister\n");
849 if (mvm
->tz_device
.tzone
) {
850 thermal_zone_device_unregister(mvm
->tz_device
.tzone
);
851 mvm
->tz_device
.tzone
= NULL
;
855 static void iwl_mvm_cooling_device_unregister(struct iwl_mvm
*mvm
)
857 if (!iwl_mvm_is_ctdp_supported(mvm
) || !mvm
->cooling_dev
.cdev
)
860 IWL_DEBUG_TEMP(mvm
, "Cooling device unregister\n");
861 if (mvm
->cooling_dev
.cdev
) {
862 thermal_cooling_device_unregister(mvm
->cooling_dev
.cdev
);
863 mvm
->cooling_dev
.cdev
= NULL
;
866 #endif /* CONFIG_THERMAL */
868 void iwl_mvm_thermal_initialize(struct iwl_mvm
*mvm
, u32 min_backoff
)
870 struct iwl_mvm_tt_mgmt
*tt
= &mvm
->thermal_throttle
;
872 IWL_DEBUG_TEMP(mvm
, "Initialize Thermal Throttling\n");
874 if (mvm
->cfg
->thermal_params
)
875 tt
->params
= *mvm
->cfg
->thermal_params
;
877 tt
->params
= iwl_mvm_default_tt_params
;
879 tt
->throttle
= false;
880 tt
->dynamic_smps
= false;
881 tt
->min_backoff
= min_backoff
;
882 INIT_DELAYED_WORK(&tt
->ct_kill_exit
, check_exit_ctkill
);
884 #ifdef CONFIG_THERMAL
885 iwl_mvm_cooling_device_register(mvm
);
886 iwl_mvm_thermal_zone_register(mvm
);
888 mvm
->init_status
|= IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE
;
891 void iwl_mvm_thermal_exit(struct iwl_mvm
*mvm
)
893 if (!(mvm
->init_status
& IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE
))
896 cancel_delayed_work_sync(&mvm
->thermal_throttle
.ct_kill_exit
);
897 IWL_DEBUG_TEMP(mvm
, "Exit Thermal Throttling\n");
899 #ifdef CONFIG_THERMAL
900 iwl_mvm_cooling_device_unregister(mvm
);
901 iwl_mvm_thermal_zone_unregister(mvm
);
903 mvm
->init_status
&= ~IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE
;