1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2012-2014, 2019-2022, 2024 Intel Corporation
4 * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5 * Copyright (C) 2015-2016 Intel Deutschland GmbH
7 #include <linux/sort.h>
11 #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ
13 void iwl_mvm_enter_ctkill(struct iwl_mvm
*mvm
)
15 struct iwl_mvm_tt_mgmt
*tt
= &mvm
->thermal_throttle
;
16 u32 duration
= tt
->params
.ct_kill_duration
;
18 if (test_bit(IWL_MVM_STATUS_HW_CTKILL
, &mvm
->status
))
21 IWL_ERR(mvm
, "Enter CT Kill\n");
22 iwl_mvm_set_hw_ctkill_state(mvm
, true);
24 if (!iwl_mvm_is_tt_in_fw(mvm
)) {
26 tt
->dynamic_smps
= false;
29 /* Don't schedule an exit work if we're in test mode, since
30 * the temperature will not change unless we manually set it
31 * again (or disable testing).
33 if (!mvm
->temperature_test
)
34 schedule_delayed_work(&tt
->ct_kill_exit
,
35 round_jiffies_relative(duration
* HZ
));
38 static void iwl_mvm_exit_ctkill(struct iwl_mvm
*mvm
)
40 if (!test_bit(IWL_MVM_STATUS_HW_CTKILL
, &mvm
->status
))
43 IWL_ERR(mvm
, "Exit CT Kill\n");
44 iwl_mvm_set_hw_ctkill_state(mvm
, false);
47 static void iwl_mvm_tt_temp_changed(struct iwl_mvm
*mvm
, u32 temp
)
49 /* ignore the notification if we are in test mode */
50 if (mvm
->temperature_test
)
53 if (mvm
->temperature
== temp
)
56 mvm
->temperature
= temp
;
57 iwl_mvm_tt_handler(mvm
);
60 static int iwl_mvm_temp_notif_parse(struct iwl_mvm
*mvm
,
61 struct iwl_rx_packet
*pkt
)
63 struct iwl_dts_measurement_notif_v1
*notif_v1
;
64 int len
= iwl_rx_packet_payload_len(pkt
);
67 /* we can use notif_v1 only, because v2 only adds an additional
68 * parameter, which is not used in this function.
70 if (WARN_ON_ONCE(len
< sizeof(*notif_v1
))) {
71 IWL_ERR(mvm
, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
75 notif_v1
= (void *)pkt
->data
;
77 temp
= le32_to_cpu(notif_v1
->temp
);
79 /* shouldn't be negative, but since it's s32, make sure it isn't */
80 if (WARN_ON_ONCE(temp
< 0))
83 IWL_DEBUG_TEMP(mvm
, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp
);
88 static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data
*notif_wait
,
89 struct iwl_rx_packet
*pkt
, void *data
)
92 container_of(notif_wait
, struct iwl_mvm
, notif_wait
);
96 ret
= iwl_mvm_temp_notif_parse(mvm
, pkt
);
105 void iwl_mvm_temp_notif(struct iwl_mvm
*mvm
, struct iwl_rx_cmd_buffer
*rxb
)
107 struct iwl_rx_packet
*pkt
= rxb_addr(rxb
);
108 struct iwl_dts_measurement_notif_v2
*notif_v2
;
109 int len
= iwl_rx_packet_payload_len(pkt
);
113 /* the notification is handled synchronously in ctkill, so skip here */
114 if (test_bit(IWL_MVM_STATUS_HW_CTKILL
, &mvm
->status
))
117 temp
= iwl_mvm_temp_notif_parse(mvm
, pkt
);
119 if (!iwl_mvm_is_tt_in_fw(mvm
)) {
121 iwl_mvm_tt_temp_changed(mvm
, temp
);
125 if (WARN_ON_ONCE(len
< sizeof(*notif_v2
))) {
126 IWL_ERR(mvm
, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
130 notif_v2
= (void *)pkt
->data
;
131 ths_crossed
= le32_to_cpu(notif_v2
->threshold_idx
);
133 /* 0xFF in ths_crossed means the notification is not related
134 * to a trip, so we can ignore it here.
136 if (ths_crossed
== 0xFF)
139 IWL_DEBUG_TEMP(mvm
, "Temp = %d Threshold crossed = %d\n",
142 #ifdef CONFIG_THERMAL
143 if (WARN_ON(ths_crossed
>= IWL_MAX_DTS_TRIPS
))
146 if (mvm
->tz_device
.tzone
) {
147 struct iwl_mvm_thermal_device
*tz_dev
= &mvm
->tz_device
;
149 thermal_zone_device_update(tz_dev
->tzone
,
150 THERMAL_TRIP_VIOLATED
);
152 #endif /* CONFIG_THERMAL */
155 void iwl_mvm_ct_kill_notif(struct iwl_mvm
*mvm
, struct iwl_rx_cmd_buffer
*rxb
)
157 struct iwl_rx_packet
*pkt
= rxb_addr(rxb
);
158 struct ct_kill_notif
*notif
;
160 notif
= (struct ct_kill_notif
*)pkt
->data
;
161 IWL_DEBUG_TEMP(mvm
, "CT Kill notification temperature = %d\n",
163 if (iwl_fw_lookup_notif_ver(mvm
->fw
, PHY_OPS_GROUP
,
164 CT_KILL_NOTIFICATION
, 0) > 1)
166 "CT kill notification DTS bitmap = 0x%x, Scheme = %d\n",
167 notif
->dts
, notif
->scheme
);
169 iwl_mvm_enter_ctkill(mvm
);
173 * send the DTS_MEASUREMENT_TRIGGER command with or without waiting for a
174 * response. If we get a response then the measurement is stored in 'temp'
176 static int iwl_mvm_send_temp_cmd(struct iwl_mvm
*mvm
, bool response
, s32
*temp
)
178 struct iwl_host_cmd cmd
= {};
179 struct iwl_dts_measurement_cmd dts_cmd
= {
180 .flags
= cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP
),
182 struct iwl_ext_dts_measurement_cmd ext_cmd
= {
183 .control_mode
= cpu_to_le32(DTS_DIRECT_WITHOUT_MEASURE
),
185 struct iwl_dts_measurement_resp
*resp
;
191 /* Check which command format is used (regular/extended) */
192 if (fw_has_capa(&mvm
->fw
->ucode_capa
,
193 IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE
)) {
194 len
= sizeof(ext_cmd
);
197 len
= sizeof(dts_cmd
);
200 /* The command version where we get a response is zero length */
202 cmd_flags
= CMD_WANT_SKB
;
206 cmd
.id
= WIDE_ID(PHY_OPS_GROUP
, CMD_DTS_MEASUREMENT_TRIGGER_WIDE
);
208 cmd
.flags
= cmd_flags
;
209 cmd
.data
[0] = cmd_ptr
;
212 "Sending temperature measurement command - %s response\n",
213 response
? "with" : "without");
214 ret
= iwl_mvm_send_cmd(mvm
, &cmd
);
218 "Failed to send the temperature measurement command (err=%d)\n",
224 resp
= (void *)cmd
.resp_pkt
->data
;
225 *temp
= le32_to_cpu(resp
->temp
);
227 "Got temperature measurement response: temp=%d\n",
235 int iwl_mvm_get_temp(struct iwl_mvm
*mvm
, s32
*temp
)
237 struct iwl_notification_wait wait_temp_notif
;
238 static u16 temp_notif
[] = { WIDE_ID(PHY_OPS_GROUP
,
239 DTS_MEASUREMENT_NOTIF_WIDE
) };
244 * If command version is 1 we send the command and immediately get
245 * a response. For older versions we send the command and wait for a
246 * notification (no command TLV for previous versions).
248 cmd_ver
= iwl_fw_lookup_cmd_ver(mvm
->fw
,
249 WIDE_ID(PHY_OPS_GROUP
, CMD_DTS_MEASUREMENT_TRIGGER_WIDE
),
250 IWL_FW_CMD_VER_UNKNOWN
);
252 return iwl_mvm_send_temp_cmd(mvm
, true, temp
);
254 lockdep_assert_held(&mvm
->mutex
);
256 iwl_init_notification_wait(&mvm
->notif_wait
, &wait_temp_notif
,
257 temp_notif
, ARRAY_SIZE(temp_notif
),
258 iwl_mvm_temp_notif_wait
, temp
);
260 ret
= iwl_mvm_send_temp_cmd(mvm
, false, temp
);
262 iwl_remove_notification(&mvm
->notif_wait
, &wait_temp_notif
);
266 ret
= iwl_wait_notification(&mvm
->notif_wait
, &wait_temp_notif
,
267 IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT
);
269 IWL_WARN(mvm
, "Getting the temperature timed out\n");
274 static void check_exit_ctkill(struct work_struct
*work
)
276 struct iwl_mvm_tt_mgmt
*tt
;
282 tt
= container_of(work
, struct iwl_mvm_tt_mgmt
, ct_kill_exit
.work
);
283 mvm
= container_of(tt
, struct iwl_mvm
, thermal_throttle
);
285 if (iwl_mvm_is_tt_in_fw(mvm
)) {
286 iwl_mvm_exit_ctkill(mvm
);
291 duration
= tt
->params
.ct_kill_duration
;
293 flush_work(&mvm
->roc_done_wk
);
295 mutex_lock(&mvm
->mutex
);
297 if (__iwl_mvm_mac_start(mvm
))
300 ret
= iwl_mvm_get_temp(mvm
, &temp
);
302 __iwl_mvm_mac_stop(mvm
, false);
307 IWL_DEBUG_TEMP(mvm
, "NIC temperature: %d\n", temp
);
309 if (temp
<= tt
->params
.ct_kill_exit
) {
310 mutex_unlock(&mvm
->mutex
);
311 iwl_mvm_exit_ctkill(mvm
);
316 mutex_unlock(&mvm
->mutex
);
317 schedule_delayed_work(&mvm
->thermal_throttle
.ct_kill_exit
,
318 round_jiffies(duration
* HZ
));
321 static void iwl_mvm_tt_smps_iterator(void *_data
, u8
*mac
,
322 struct ieee80211_vif
*vif
)
324 struct iwl_mvm
*mvm
= _data
;
325 enum ieee80211_smps_mode smps_mode
;
327 lockdep_assert_held(&mvm
->mutex
);
329 if (mvm
->thermal_throttle
.dynamic_smps
)
330 smps_mode
= IEEE80211_SMPS_DYNAMIC
;
332 smps_mode
= IEEE80211_SMPS_AUTOMATIC
;
334 if (vif
->type
!= NL80211_IFTYPE_STATION
)
337 iwl_mvm_update_smps(mvm
, vif
, IWL_MVM_SMPS_REQ_TT
, smps_mode
, 0);
340 static void iwl_mvm_tt_tx_protection(struct iwl_mvm
*mvm
, bool enable
)
342 struct iwl_mvm_sta
*mvmsta
;
345 for (i
= 0; i
< mvm
->fw
->ucode_capa
.num_stations
; i
++) {
346 mvmsta
= iwl_mvm_sta_from_staid_protected(mvm
, i
);
350 if (enable
== mvmsta
->tt_tx_protection
)
352 err
= iwl_mvm_tx_protection(mvm
, mvmsta
, enable
);
354 IWL_ERR(mvm
, "Failed to %s Tx protection\n",
355 enable
? "enable" : "disable");
357 IWL_DEBUG_TEMP(mvm
, "%s Tx protection\n",
358 enable
? "Enable" : "Disable");
359 mvmsta
->tt_tx_protection
= enable
;
364 void iwl_mvm_tt_tx_backoff(struct iwl_mvm
*mvm
, u32 backoff
)
366 struct iwl_host_cmd cmd
= {
367 .id
= REPLY_THERMAL_MNG_BACKOFF
,
368 .len
= { sizeof(u32
), },
369 .data
= { &backoff
, },
372 backoff
= max(backoff
, mvm
->thermal_throttle
.min_backoff
);
374 if (iwl_mvm_send_cmd(mvm
, &cmd
) == 0) {
375 IWL_DEBUG_TEMP(mvm
, "Set Thermal Tx backoff to: %u\n",
377 mvm
->thermal_throttle
.tx_backoff
= backoff
;
379 IWL_ERR(mvm
, "Failed to change Thermal Tx backoff\n");
383 void iwl_mvm_tt_handler(struct iwl_mvm
*mvm
)
385 struct iwl_tt_params
*params
= &mvm
->thermal_throttle
.params
;
386 struct iwl_mvm_tt_mgmt
*tt
= &mvm
->thermal_throttle
;
387 s32 temperature
= mvm
->temperature
;
388 bool throttle_enable
= false;
392 IWL_DEBUG_TEMP(mvm
, "NIC temperature: %d\n", mvm
->temperature
);
394 if (params
->support_ct_kill
&& temperature
>= params
->ct_kill_entry
) {
395 iwl_mvm_enter_ctkill(mvm
);
399 if (params
->support_ct_kill
&&
400 temperature
<= params
->ct_kill_exit
) {
401 iwl_mvm_exit_ctkill(mvm
);
405 if (params
->support_dynamic_smps
) {
406 if (!tt
->dynamic_smps
&&
407 temperature
>= params
->dynamic_smps_entry
) {
408 IWL_DEBUG_TEMP(mvm
, "Enable dynamic SMPS\n");
409 tt
->dynamic_smps
= true;
410 ieee80211_iterate_active_interfaces_atomic(
411 mvm
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
412 iwl_mvm_tt_smps_iterator
, mvm
);
413 throttle_enable
= true;
414 } else if (tt
->dynamic_smps
&&
415 temperature
<= params
->dynamic_smps_exit
) {
416 IWL_DEBUG_TEMP(mvm
, "Disable dynamic SMPS\n");
417 tt
->dynamic_smps
= false;
418 ieee80211_iterate_active_interfaces_atomic(
419 mvm
->hw
, IEEE80211_IFACE_ITER_NORMAL
,
420 iwl_mvm_tt_smps_iterator
, mvm
);
424 if (params
->support_tx_protection
) {
425 if (temperature
>= params
->tx_protection_entry
) {
426 iwl_mvm_tt_tx_protection(mvm
, true);
427 throttle_enable
= true;
428 } else if (temperature
<= params
->tx_protection_exit
) {
429 iwl_mvm_tt_tx_protection(mvm
, false);
433 if (params
->support_tx_backoff
) {
434 tx_backoff
= tt
->min_backoff
;
435 for (i
= 0; i
< TT_TX_BACKOFF_SIZE
; i
++) {
436 if (temperature
< params
->tx_backoff
[i
].temperature
)
438 tx_backoff
= max(tt
->min_backoff
,
439 params
->tx_backoff
[i
].backoff
);
441 if (tx_backoff
!= tt
->min_backoff
)
442 throttle_enable
= true;
443 if (tt
->tx_backoff
!= tx_backoff
)
444 iwl_mvm_tt_tx_backoff(mvm
, tx_backoff
);
447 if (!tt
->throttle
&& throttle_enable
) {
449 "Due to high temperature thermal throttling initiated\n");
451 } else if (tt
->throttle
&& !tt
->dynamic_smps
&&
452 tt
->tx_backoff
== tt
->min_backoff
&&
453 temperature
<= params
->tx_protection_exit
) {
455 "Temperature is back to normal thermal throttling stopped\n");
456 tt
->throttle
= false;
460 static const struct iwl_tt_params iwl_mvm_default_tt_params
= {
461 .ct_kill_entry
= 118,
463 .ct_kill_duration
= 5,
464 .dynamic_smps_entry
= 114,
465 .dynamic_smps_exit
= 110,
466 .tx_protection_entry
= 114,
467 .tx_protection_exit
= 108,
469 {.temperature
= 112, .backoff
= 200},
470 {.temperature
= 113, .backoff
= 600},
471 {.temperature
= 114, .backoff
= 1200},
472 {.temperature
= 115, .backoff
= 2000},
473 {.temperature
= 116, .backoff
= 4000},
474 {.temperature
= 117, .backoff
= 10000},
476 .support_ct_kill
= true,
477 .support_dynamic_smps
= true,
478 .support_tx_protection
= true,
479 .support_tx_backoff
= true,
482 /* budget in mWatt */
483 static const u32 iwl_mvm_cdev_budgets
[] = {
484 2400, /* cooling state 0 */
485 2000, /* cooling state 1 */
486 1800, /* cooling state 2 */
487 1600, /* cooling state 3 */
488 1400, /* cooling state 4 */
489 1200, /* cooling state 5 */
490 1000, /* cooling state 6 */
491 900, /* cooling state 7 */
492 800, /* cooling state 8 */
493 700, /* cooling state 9 */
494 650, /* cooling state 10 */
495 600, /* cooling state 11 */
496 550, /* cooling state 12 */
497 500, /* cooling state 13 */
498 450, /* cooling state 14 */
499 400, /* cooling state 15 */
500 350, /* cooling state 16 */
501 300, /* cooling state 17 */
502 250, /* cooling state 18 */
503 200, /* cooling state 19 */
504 150, /* cooling state 20 */
507 int iwl_mvm_ctdp_command(struct iwl_mvm
*mvm
, u32 op
, u32 state
)
509 struct iwl_mvm_ctdp_cmd cmd
= {
510 .operation
= cpu_to_le32(op
),
511 .budget
= cpu_to_le32(iwl_mvm_cdev_budgets
[state
]),
517 lockdep_assert_held(&mvm
->mutex
);
520 ret
= iwl_mvm_send_cmd_pdu_status(mvm
, WIDE_ID(PHY_OPS_GROUP
,
522 sizeof(cmd
), &cmd
, &status
);
525 IWL_ERR(mvm
, "cTDP command failed (err=%d)\n", ret
);
530 case CTDP_CMD_OPERATION_START
:
531 #ifdef CONFIG_THERMAL
532 mvm
->cooling_dev
.cur_state
= state
;
533 #endif /* CONFIG_THERMAL */
535 case CTDP_CMD_OPERATION_REPORT
:
536 IWL_DEBUG_TEMP(mvm
, "cTDP avg energy in mWatt = %d\n", status
);
537 /* when the function is called with CTDP_CMD_OPERATION_REPORT
538 * option the function should return the average budget value
539 * that is received from the FW.
540 * The budget can't be less or equal to 0, so it's possible
541 * to distinguish between error values and budgets.
544 case CTDP_CMD_OPERATION_STOP
:
545 IWL_DEBUG_TEMP(mvm
, "cTDP stopped successfully\n");
552 #ifdef CONFIG_THERMAL
553 static int compare_temps(const void *a
, const void *b
)
555 return ((s16
)le16_to_cpu(*(__le16
*)a
) -
556 (s16
)le16_to_cpu(*(__le16
*)b
));
559 struct iwl_trip_walk_data
{
564 static int iwl_trip_temp_cb(struct thermal_trip
*trip
, void *arg
)
566 struct iwl_trip_walk_data
*twd
= arg
;
568 if (trip
->temperature
== THERMAL_TEMP_INVALID
)
571 twd
->thresholds
[twd
->count
++] = cpu_to_le16((s16
)(trip
->temperature
/ 1000));
576 int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm
*mvm
)
578 struct temp_report_ths_cmd cmd
= {0};
580 #ifdef CONFIG_THERMAL
581 struct iwl_trip_walk_data twd
= { .thresholds
= cmd
.thresholds
, .count
= 0 };
583 lockdep_assert_held(&mvm
->mutex
);
585 if (!mvm
->tz_device
.tzone
)
589 * The thermal core holds an array of temperature trips that are
590 * unsorted and uncompressed, the FW should get it compressed and
594 /* compress trips to cmd array, remove uninitialized values*/
595 for_each_thermal_trip(mvm
->tz_device
.tzone
, iwl_trip_temp_cb
, &twd
);
597 cmd
.num_temps
= cpu_to_le32(twd
.count
);
599 sort(cmd
.thresholds
, twd
.count
, sizeof(s16
), compare_temps
, NULL
);
603 ret
= iwl_mvm_send_cmd_pdu(mvm
, WIDE_ID(PHY_OPS_GROUP
,
604 TEMP_REPORTING_THRESHOLDS_CMD
),
605 0, sizeof(cmd
), &cmd
);
607 IWL_ERR(mvm
, "TEMP_REPORT_THS_CMD command failed (err=%d)\n",
613 #ifdef CONFIG_THERMAL
614 static int iwl_mvm_tzone_get_temp(struct thermal_zone_device
*device
,
617 struct iwl_mvm
*mvm
= thermal_zone_device_priv(device
);
623 if (!iwl_mvm_firmware_running(mvm
) ||
624 mvm
->fwrt
.cur_fw_img
!= IWL_UCODE_REGULAR
) {
626 * Tell the core that there is no valid temperature value to
627 * return, but it need not worry about this.
629 *temperature
= THERMAL_TEMP_INVALID
;
633 ret
= iwl_mvm_get_temp(mvm
, &temp
);
637 *temperature
= temp
* 1000;
641 static int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device
*device
,
642 const struct thermal_trip
*trip
, int temp
)
644 struct iwl_mvm
*mvm
= thermal_zone_device_priv(device
);
648 if (!iwl_mvm_firmware_running(mvm
) ||
649 mvm
->fwrt
.cur_fw_img
!= IWL_UCODE_REGULAR
)
652 if ((temp
/ 1000) > S16_MAX
)
655 return iwl_mvm_send_temp_report_ths_cmd(mvm
);
658 static struct thermal_zone_device_ops tzone_ops
= {
659 .get_temp
= iwl_mvm_tzone_get_temp
,
660 .set_trip_temp
= iwl_mvm_tzone_set_trip_temp
,
663 static void iwl_mvm_thermal_zone_register(struct iwl_mvm
*mvm
)
667 static atomic_t counter
= ATOMIC_INIT(0);
669 if (!iwl_mvm_is_tt_in_fw(mvm
)) {
670 mvm
->tz_device
.tzone
= NULL
;
675 BUILD_BUG_ON(ARRAY_SIZE(name
) >= THERMAL_NAME_LENGTH
);
677 sprintf(name
, "iwlwifi_%u", atomic_inc_return(&counter
) & 0xFF);
679 * 0 is a valid temperature,
680 * so initialize the array with S16_MIN which invalid temperature
682 for (i
= 0 ; i
< IWL_MAX_DTS_TRIPS
; i
++) {
683 mvm
->tz_device
.trips
[i
].temperature
= THERMAL_TEMP_INVALID
;
684 mvm
->tz_device
.trips
[i
].type
= THERMAL_TRIP_PASSIVE
;
685 mvm
->tz_device
.trips
[i
].flags
= THERMAL_TRIP_FLAG_RW_TEMP
;
687 mvm
->tz_device
.tzone
= thermal_zone_device_register_with_trips(name
,
688 mvm
->tz_device
.trips
,
692 if (IS_ERR(mvm
->tz_device
.tzone
)) {
694 "Failed to register to thermal zone (err = %ld)\n",
695 PTR_ERR(mvm
->tz_device
.tzone
));
696 mvm
->tz_device
.tzone
= NULL
;
700 ret
= thermal_zone_device_enable(mvm
->tz_device
.tzone
);
702 IWL_DEBUG_TEMP(mvm
, "Failed to enable thermal zone\n");
703 thermal_zone_device_unregister(mvm
->tz_device
.tzone
);
707 static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device
*cdev
,
708 unsigned long *state
)
710 *state
= ARRAY_SIZE(iwl_mvm_cdev_budgets
) - 1;
715 static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device
*cdev
,
716 unsigned long *state
)
718 struct iwl_mvm
*mvm
= (struct iwl_mvm
*)(cdev
->devdata
);
720 *state
= mvm
->cooling_dev
.cur_state
;
725 static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device
*cdev
,
726 unsigned long new_state
)
728 struct iwl_mvm
*mvm
= (struct iwl_mvm
*)(cdev
->devdata
);
732 if (!iwl_mvm_firmware_running(mvm
) ||
733 mvm
->fwrt
.cur_fw_img
!= IWL_UCODE_REGULAR
)
736 if (new_state
>= ARRAY_SIZE(iwl_mvm_cdev_budgets
))
739 return iwl_mvm_ctdp_command(mvm
, CTDP_CMD_OPERATION_START
,
743 static const struct thermal_cooling_device_ops tcooling_ops
= {
744 .get_max_state
= iwl_mvm_tcool_get_max_state
,
745 .get_cur_state
= iwl_mvm_tcool_get_cur_state
,
746 .set_cur_state
= iwl_mvm_tcool_set_cur_state
,
749 static void iwl_mvm_cooling_device_register(struct iwl_mvm
*mvm
)
751 char name
[] = "iwlwifi";
753 if (!iwl_mvm_is_ctdp_supported(mvm
))
756 BUILD_BUG_ON(ARRAY_SIZE(name
) >= THERMAL_NAME_LENGTH
);
758 mvm
->cooling_dev
.cdev
=
759 thermal_cooling_device_register(name
,
763 if (IS_ERR(mvm
->cooling_dev
.cdev
)) {
765 "Failed to register to cooling device (err = %ld)\n",
766 PTR_ERR(mvm
->cooling_dev
.cdev
));
767 mvm
->cooling_dev
.cdev
= NULL
;
772 static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm
*mvm
)
774 if (!iwl_mvm_is_tt_in_fw(mvm
) || !mvm
->tz_device
.tzone
)
777 IWL_DEBUG_TEMP(mvm
, "Thermal zone device unregister\n");
778 if (mvm
->tz_device
.tzone
) {
779 thermal_zone_device_unregister(mvm
->tz_device
.tzone
);
780 mvm
->tz_device
.tzone
= NULL
;
784 static void iwl_mvm_cooling_device_unregister(struct iwl_mvm
*mvm
)
786 if (!iwl_mvm_is_ctdp_supported(mvm
) || !mvm
->cooling_dev
.cdev
)
789 IWL_DEBUG_TEMP(mvm
, "Cooling device unregister\n");
790 if (mvm
->cooling_dev
.cdev
) {
791 thermal_cooling_device_unregister(mvm
->cooling_dev
.cdev
);
792 mvm
->cooling_dev
.cdev
= NULL
;
795 #endif /* CONFIG_THERMAL */
797 void iwl_mvm_thermal_initialize(struct iwl_mvm
*mvm
, u32 min_backoff
)
799 struct iwl_mvm_tt_mgmt
*tt
= &mvm
->thermal_throttle
;
801 IWL_DEBUG_TEMP(mvm
, "Initialize Thermal Throttling\n");
803 if (mvm
->cfg
->thermal_params
)
804 tt
->params
= *mvm
->cfg
->thermal_params
;
806 tt
->params
= iwl_mvm_default_tt_params
;
808 tt
->throttle
= false;
809 tt
->dynamic_smps
= false;
810 tt
->min_backoff
= min_backoff
;
811 INIT_DELAYED_WORK(&tt
->ct_kill_exit
, check_exit_ctkill
);
813 #ifdef CONFIG_THERMAL
814 iwl_mvm_cooling_device_register(mvm
);
815 iwl_mvm_thermal_zone_register(mvm
);
817 mvm
->init_status
|= IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE
;
820 void iwl_mvm_thermal_exit(struct iwl_mvm
*mvm
)
822 if (!(mvm
->init_status
& IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE
))
825 cancel_delayed_work_sync(&mvm
->thermal_throttle
.ct_kill_exit
);
826 IWL_DEBUG_TEMP(mvm
, "Exit Thermal Throttling\n");
828 #ifdef CONFIG_THERMAL
829 iwl_mvm_cooling_device_unregister(mvm
);
830 iwl_mvm_thermal_zone_unregister(mvm
);
832 mvm
->init_status
&= ~IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE
;