1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2012-2014, 2019-2022, 2024-2025 Intel Corporation 4 * Copyright (C) 2013-2014 Intel Mobile Communications GmbH 5 * Copyright (C) 2015-2016 Intel Deutschland GmbH 6 */ 7 #include <linux/sort.h> 8 9 #include "mvm.h" 10 11 #define IWL_MVM_NUM_CTDP_STEPS 20 12 #define IWL_MVM_MIN_CTDP_BUDGET_MW 150 13 14 #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ 15 16 void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm) 17 { 18 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; 19 u32 duration = tt->params.ct_kill_duration; 20 21 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) 22 return; 23 24 IWL_ERR(mvm, "Enter CT Kill\n"); 25 iwl_mvm_set_hw_ctkill_state(mvm, true); 26 27 if (!iwl_mvm_is_tt_in_fw(mvm)) { 28 tt->throttle = false; 29 tt->dynamic_smps = false; 30 } 31 32 /* Don't schedule an exit work if we're in test mode, since 33 * the temperature will not change unless we manually set it 34 * again (or disable testing). 35 */ 36 if (!mvm->temperature_test) 37 schedule_delayed_work(&tt->ct_kill_exit, 38 round_jiffies_relative(duration * HZ)); 39 } 40 41 static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm) 42 { 43 if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) 44 return; 45 46 IWL_ERR(mvm, "Exit CT Kill\n"); 47 iwl_mvm_set_hw_ctkill_state(mvm, false); 48 } 49 50 static void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp) 51 { 52 /* ignore the notification if we are in test mode */ 53 if (mvm->temperature_test) 54 return; 55 56 if (mvm->temperature == temp) 57 return; 58 59 mvm->temperature = temp; 60 iwl_mvm_tt_handler(mvm); 61 } 62 63 static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm, 64 struct iwl_rx_packet *pkt) 65 { 66 struct iwl_dts_measurement_notif_v1 *notif_v1; 67 int len = iwl_rx_packet_payload_len(pkt); 68 int temp; 69 70 /* we can use notif_v1 only, because v2 only adds an additional 71 * parameter, which is not used in this function. 72 */ 73 if (WARN_ON_ONCE(len < sizeof(*notif_v1))) { 74 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n"); 75 return -EINVAL; 76 } 77 78 notif_v1 = (void *)pkt->data; 79 80 temp = le32_to_cpu(notif_v1->temp); 81 82 /* shouldn't be negative, but since it's s32, make sure it isn't */ 83 if (WARN_ON_ONCE(temp < 0)) 84 temp = 0; 85 86 IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp); 87 88 return temp; 89 } 90 91 static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait, 92 struct iwl_rx_packet *pkt, void *data) 93 { 94 struct iwl_mvm *mvm = 95 container_of(notif_wait, struct iwl_mvm, notif_wait); 96 int *temp = data; 97 int ret; 98 99 ret = iwl_mvm_temp_notif_parse(mvm, pkt); 100 if (ret < 0) 101 return true; 102 103 *temp = ret; 104 105 return true; 106 } 107 108 void iwl_mvm_temp_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 109 { 110 struct iwl_rx_packet *pkt = rxb_addr(rxb); 111 struct iwl_dts_measurement_notif *notif_v2; 112 int len = iwl_rx_packet_payload_len(pkt); 113 int temp; 114 u32 ths_crossed; 115 116 /* the notification is handled synchronously in ctkill, so skip here */ 117 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) 118 return; 119 120 temp = iwl_mvm_temp_notif_parse(mvm, pkt); 121 122 if (!iwl_mvm_is_tt_in_fw(mvm)) { 123 if (temp >= 0) 124 iwl_mvm_tt_temp_changed(mvm, temp); 125 return; 126 } 127 128 if (WARN_ON_ONCE(len < sizeof(*notif_v2))) { 129 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n"); 130 return; 131 } 132 133 notif_v2 = (void *)pkt->data; 134 ths_crossed = le32_to_cpu(notif_v2->threshold_idx); 135 136 /* 0xFF in ths_crossed means the notification is not related 137 * to a trip, so we can ignore it here. 138 */ 139 if (ths_crossed == 0xFF) 140 return; 141 142 IWL_DEBUG_TEMP(mvm, "Temp = %d Threshold crossed = %d\n", 143 temp, ths_crossed); 144 145 #ifdef CONFIG_THERMAL 146 if (WARN_ON(ths_crossed >= IWL_MAX_DTS_TRIPS)) 147 return; 148 149 if (mvm->tz_device.tzone) { 150 struct iwl_mvm_thermal_device *tz_dev = &mvm->tz_device; 151 152 thermal_zone_device_update(tz_dev->tzone, 153 THERMAL_TRIP_VIOLATED); 154 } 155 #endif /* CONFIG_THERMAL */ 156 } 157 158 void iwl_mvm_ct_kill_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 159 { 160 struct iwl_rx_packet *pkt = rxb_addr(rxb); 161 struct ct_kill_notif *notif; 162 163 notif = (struct ct_kill_notif *)pkt->data; 164 IWL_DEBUG_TEMP(mvm, "CT Kill notification temperature = %d\n", 165 notif->temperature); 166 if (iwl_fw_lookup_notif_ver(mvm->fw, PHY_OPS_GROUP, 167 CT_KILL_NOTIFICATION, 0) > 1) 168 IWL_DEBUG_TEMP(mvm, 169 "CT kill notification DTS bitmap = 0x%x, Scheme = %d\n", 170 notif->dts, notif->scheme); 171 172 iwl_mvm_enter_ctkill(mvm); 173 } 174 175 /* 176 * send the DTS_MEASUREMENT_TRIGGER command with or without waiting for a 177 * response. If we get a response then the measurement is stored in 'temp' 178 */ 179 static int iwl_mvm_send_temp_cmd(struct iwl_mvm *mvm, bool response, s32 *temp) 180 { 181 struct iwl_host_cmd cmd = {}; 182 struct iwl_dts_measurement_cmd dts_cmd = { 183 .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP), 184 }; 185 struct iwl_ext_dts_measurement_cmd ext_cmd = { 186 .control_mode = cpu_to_le32(DTS_DIRECT_WITHOUT_MEASURE), 187 }; 188 struct iwl_dts_measurement_resp *resp; 189 void *cmd_ptr; 190 int ret; 191 u32 cmd_flags = 0; 192 u16 len; 193 194 /* Check which command format is used (regular/extended) */ 195 if (fw_has_capa(&mvm->fw->ucode_capa, 196 IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE)) { 197 len = sizeof(ext_cmd); 198 cmd_ptr = &ext_cmd; 199 } else { 200 len = sizeof(dts_cmd); 201 cmd_ptr = &dts_cmd; 202 } 203 /* The command version where we get a response is zero length */ 204 if (response) { 205 cmd_flags = CMD_WANT_SKB; 206 len = 0; 207 } 208 209 cmd.id = WIDE_ID(PHY_OPS_GROUP, CMD_DTS_MEASUREMENT_TRIGGER_WIDE); 210 cmd.len[0] = len; 211 cmd.flags = cmd_flags; 212 cmd.data[0] = cmd_ptr; 213 214 IWL_DEBUG_TEMP(mvm, 215 "Sending temperature measurement command - %s response\n", 216 response ? "with" : "without"); 217 ret = iwl_mvm_send_cmd(mvm, &cmd); 218 219 if (ret) { 220 IWL_ERR(mvm, 221 "Failed to send the temperature measurement command (err=%d)\n", 222 ret); 223 return ret; 224 } 225 226 if (response) { 227 resp = (void *)cmd.resp_pkt->data; 228 *temp = le32_to_cpu(resp->temp); 229 IWL_DEBUG_TEMP(mvm, 230 "Got temperature measurement response: temp=%d\n", 231 *temp); 232 iwl_free_resp(&cmd); 233 } 234 235 return ret; 236 } 237 238 int iwl_mvm_get_temp(struct iwl_mvm *mvm, s32 *temp) 239 { 240 struct iwl_notification_wait wait_temp_notif; 241 static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP, 242 DTS_MEASUREMENT_NOTIF_WIDE) }; 243 int ret; 244 u8 cmd_ver; 245 246 /* 247 * If command version is 1 we send the command and immediately get 248 * a response. For older versions we send the command and wait for a 249 * notification (no command TLV for previous versions). 250 */ 251 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, 252 WIDE_ID(PHY_OPS_GROUP, CMD_DTS_MEASUREMENT_TRIGGER_WIDE), 253 IWL_FW_CMD_VER_UNKNOWN); 254 if (cmd_ver == 1) 255 return iwl_mvm_send_temp_cmd(mvm, true, temp); 256 257 lockdep_assert_held(&mvm->mutex); 258 259 iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif, 260 temp_notif, ARRAY_SIZE(temp_notif), 261 iwl_mvm_temp_notif_wait, temp); 262 263 ret = iwl_mvm_send_temp_cmd(mvm, false, temp); 264 if (ret) { 265 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif); 266 return ret; 267 } 268 269 ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif, 270 IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT); 271 if (ret) 272 IWL_WARN(mvm, "Getting the temperature timed out\n"); 273 274 return ret; 275 } 276 277 static void check_exit_ctkill(struct work_struct *work) 278 { 279 struct iwl_mvm_tt_mgmt *tt; 280 struct iwl_mvm *mvm; 281 u32 duration; 282 s32 temp; 283 int ret; 284 285 tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work); 286 mvm = container_of(tt, struct iwl_mvm, thermal_throttle); 287 288 if (iwl_mvm_is_tt_in_fw(mvm)) { 289 iwl_mvm_exit_ctkill(mvm); 290 291 return; 292 } 293 294 duration = tt->params.ct_kill_duration; 295 296 flush_work(&mvm->roc_done_wk); 297 298 mutex_lock(&mvm->mutex); 299 300 if (__iwl_mvm_mac_start(mvm)) 301 goto reschedule; 302 303 ret = iwl_mvm_get_temp(mvm, &temp); 304 305 __iwl_mvm_mac_stop(mvm, false); 306 307 if (ret) 308 goto reschedule; 309 310 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp); 311 312 if (temp <= tt->params.ct_kill_exit) { 313 mutex_unlock(&mvm->mutex); 314 iwl_mvm_exit_ctkill(mvm); 315 return; 316 } 317 318 reschedule: 319 mutex_unlock(&mvm->mutex); 320 schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit, 321 round_jiffies(duration * HZ)); 322 } 323 324 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac, 325 struct ieee80211_vif *vif) 326 { 327 struct iwl_mvm *mvm = _data; 328 enum ieee80211_smps_mode smps_mode; 329 330 lockdep_assert_held(&mvm->mutex); 331 332 if (mvm->thermal_throttle.dynamic_smps) 333 smps_mode = IEEE80211_SMPS_DYNAMIC; 334 else 335 smps_mode = IEEE80211_SMPS_AUTOMATIC; 336 337 if (vif->type != NL80211_IFTYPE_STATION) 338 return; 339 340 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode, 0); 341 } 342 343 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable) 344 { 345 struct iwl_mvm_sta *mvmsta; 346 int i, err; 347 348 for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) { 349 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, i); 350 if (!mvmsta) 351 continue; 352 353 if (enable == mvmsta->tt_tx_protection) 354 continue; 355 err = iwl_mvm_tx_protection(mvm, mvmsta, enable); 356 if (err) { 357 IWL_ERR(mvm, "Failed to %s Tx protection\n", 358 enable ? "enable" : "disable"); 359 } else { 360 IWL_DEBUG_TEMP(mvm, "%s Tx protection\n", 361 enable ? "Enable" : "Disable"); 362 mvmsta->tt_tx_protection = enable; 363 } 364 } 365 } 366 367 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff) 368 { 369 struct iwl_host_cmd cmd = { 370 .id = REPLY_THERMAL_MNG_BACKOFF, 371 .len = { sizeof(u32), }, 372 .data = { &backoff, }, 373 }; 374 375 backoff = max(backoff, mvm->thermal_throttle.min_backoff); 376 377 if (iwl_mvm_send_cmd(mvm, &cmd) == 0) { 378 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n", 379 backoff); 380 mvm->thermal_throttle.tx_backoff = backoff; 381 } else { 382 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n"); 383 } 384 } 385 386 void iwl_mvm_tt_handler(struct iwl_mvm *mvm) 387 { 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; 392 int i; 393 u32 tx_backoff; 394 395 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature); 396 397 if (params->support_ct_kill && temperature >= params->ct_kill_entry) { 398 iwl_mvm_enter_ctkill(mvm); 399 return; 400 } 401 402 if (params->support_ct_kill && 403 temperature <= params->ct_kill_exit) { 404 iwl_mvm_exit_ctkill(mvm); 405 return; 406 } 407 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); 424 } 425 } 426 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); 433 } 434 } 435 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) 440 break; 441 tx_backoff = max(tt->min_backoff, 442 params->tx_backoff[i].backoff); 443 } 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); 448 } 449 450 if (!tt->throttle && throttle_enable) { 451 IWL_WARN(mvm, 452 "Due to high temperature thermal throttling initiated\n"); 453 tt->throttle = true; 454 } else if (tt->throttle && !tt->dynamic_smps && 455 tt->tx_backoff == tt->min_backoff && 456 temperature <= params->tx_protection_exit) { 457 IWL_WARN(mvm, 458 "Temperature is back to normal thermal throttling stopped\n"); 459 tt->throttle = false; 460 } 461 } 462 463 static const struct iwl_tt_params iwl_mvm_default_tt_params = { 464 .ct_kill_entry = 118, 465 .ct_kill_exit = 96, 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, 471 .tx_backoff = { 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}, 478 }, 479 .support_ct_kill = true, 480 .support_dynamic_smps = true, 481 .support_tx_protection = true, 482 .support_tx_backoff = true, 483 }; 484 485 int iwl_mvm_ctdp_command(struct iwl_mvm *mvm, u32 op, u32 state) 486 { 487 struct iwl_ctdp_cmd cmd = { 488 .operation = cpu_to_le32(op), 489 .window_size = 0, 490 }; 491 u32 budget; 492 int ret; 493 u32 status; 494 495 lockdep_assert_held(&mvm->mutex); 496 497 /* Do a linear scale from IWL_MVM_MIN_CTDP_BUDGET_MW to the configured 498 * maximum in the predefined number of steps. 499 */ 500 budget = ((mvm->thermal_throttle.power_budget_mw - 501 IWL_MVM_MIN_CTDP_BUDGET_MW) * 502 (IWL_MVM_NUM_CTDP_STEPS - 1 - state)) / 503 (IWL_MVM_NUM_CTDP_STEPS - 1) + 504 IWL_MVM_MIN_CTDP_BUDGET_MW; 505 cmd.budget = cpu_to_le32(budget); 506 507 status = 0; 508 ret = iwl_mvm_send_cmd_pdu_status(mvm, WIDE_ID(PHY_OPS_GROUP, 509 CTDP_CONFIG_CMD), 510 sizeof(cmd), &cmd, &status); 511 512 if (ret) { 513 IWL_ERR(mvm, "cTDP command failed (err=%d)\n", ret); 514 return ret; 515 } 516 517 switch (op) { 518 case CTDP_CMD_OPERATION_START: 519 #ifdef CONFIG_THERMAL 520 mvm->cooling_dev.cur_state = state; 521 #endif /* CONFIG_THERMAL */ 522 break; 523 case CTDP_CMD_OPERATION_REPORT: 524 IWL_DEBUG_TEMP(mvm, "cTDP avg energy in mWatt = %d\n", status); 525 /* when the function is called with CTDP_CMD_OPERATION_REPORT 526 * option the function should return the average budget value 527 * that is received from the FW. 528 * The budget can't be less or equal to 0, so it's possible 529 * to distinguish between error values and budgets. 530 */ 531 return status; 532 case CTDP_CMD_OPERATION_STOP: 533 IWL_DEBUG_TEMP(mvm, "cTDP stopped successfully\n"); 534 break; 535 } 536 537 return 0; 538 } 539 540 #ifdef CONFIG_THERMAL 541 static int compare_temps(const void *a, const void *b) 542 { 543 return ((s16)le16_to_cpu(*(const __le16 *)a) - 544 (s16)le16_to_cpu(*(const __le16 *)b)); 545 } 546 547 struct iwl_trip_walk_data { 548 __le16 *thresholds; 549 int count; 550 }; 551 552 static int iwl_trip_temp_cb(struct thermal_trip *trip, void *arg) 553 { 554 struct iwl_trip_walk_data *twd = arg; 555 556 if (trip->temperature == THERMAL_TEMP_INVALID) 557 return 0; 558 559 twd->thresholds[twd->count++] = cpu_to_le16((s16)(trip->temperature / 1000)); 560 return 0; 561 } 562 #endif 563 564 int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm *mvm) 565 { 566 struct temp_report_ths_cmd cmd = {0}; 567 int ret; 568 #ifdef CONFIG_THERMAL 569 struct iwl_trip_walk_data twd = { .thresholds = cmd.thresholds, .count = 0 }; 570 571 lockdep_assert_held(&mvm->mutex); 572 573 if (!mvm->tz_device.tzone) 574 goto send; 575 576 /* 577 * The thermal core holds an array of temperature trips that are 578 * unsorted and uncompressed, the FW should get it compressed and 579 * sorted. 580 */ 581 582 /* compress trips to cmd array, remove uninitialized values*/ 583 for_each_thermal_trip(mvm->tz_device.tzone, iwl_trip_temp_cb, &twd); 584 585 cmd.num_temps = cpu_to_le32(twd.count); 586 if (twd.count) 587 sort(cmd.thresholds, twd.count, sizeof(s16), compare_temps, NULL); 588 589 send: 590 #endif 591 ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP, 592 TEMP_REPORTING_THRESHOLDS_CMD), 593 0, sizeof(cmd), &cmd); 594 if (ret) 595 IWL_ERR(mvm, "TEMP_REPORT_THS_CMD command failed (err=%d)\n", 596 ret); 597 598 return ret; 599 } 600 601 #ifdef CONFIG_THERMAL 602 static int iwl_mvm_tzone_get_temp(struct thermal_zone_device *device, 603 int *temperature) 604 { 605 struct iwl_mvm *mvm = thermal_zone_device_priv(device); 606 int ret; 607 int temp; 608 609 guard(mvm)(mvm); 610 611 if (!iwl_mvm_firmware_running(mvm) || 612 mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) { 613 /* 614 * Tell the core that there is no valid temperature value to 615 * return, but it need not worry about this. 616 */ 617 *temperature = THERMAL_TEMP_INVALID; 618 return 0; 619 } 620 621 ret = iwl_mvm_get_temp(mvm, &temp); 622 if (ret) 623 return ret; 624 625 *temperature = temp * 1000; 626 return 0; 627 } 628 629 static int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device *device, 630 const struct thermal_trip *trip, int temp) 631 { 632 struct iwl_mvm *mvm = thermal_zone_device_priv(device); 633 634 guard(mvm)(mvm); 635 636 if (!iwl_mvm_firmware_running(mvm) || 637 mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) 638 return -EIO; 639 640 if ((temp / 1000) > S16_MAX) 641 return -EINVAL; 642 643 return iwl_mvm_send_temp_report_ths_cmd(mvm); 644 } 645 646 static struct thermal_zone_device_ops tzone_ops = { 647 .get_temp = iwl_mvm_tzone_get_temp, 648 .set_trip_temp = iwl_mvm_tzone_set_trip_temp, 649 }; 650 651 static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm) 652 { 653 int i, ret; 654 char name[16]; 655 static atomic_t counter = ATOMIC_INIT(0); 656 657 if (!iwl_mvm_is_tt_in_fw(mvm)) { 658 mvm->tz_device.tzone = NULL; 659 660 return; 661 } 662 663 BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH); 664 665 sprintf(name, "iwlwifi_%u", atomic_inc_return(&counter) & 0xFF); 666 /* 667 * 0 is a valid temperature, 668 * so initialize the array with S16_MIN which invalid temperature 669 */ 670 for (i = 0 ; i < IWL_MAX_DTS_TRIPS; i++) { 671 mvm->tz_device.trips[i].temperature = THERMAL_TEMP_INVALID; 672 mvm->tz_device.trips[i].type = THERMAL_TRIP_PASSIVE; 673 mvm->tz_device.trips[i].flags = THERMAL_TRIP_FLAG_RW_TEMP; 674 } 675 mvm->tz_device.tzone = thermal_zone_device_register_with_trips(name, 676 mvm->tz_device.trips, 677 IWL_MAX_DTS_TRIPS, 678 mvm, &tzone_ops, 679 NULL, 0, 0); 680 if (IS_ERR(mvm->tz_device.tzone)) { 681 IWL_DEBUG_TEMP(mvm, 682 "Failed to register to thermal zone (err = %ld)\n", 683 PTR_ERR(mvm->tz_device.tzone)); 684 mvm->tz_device.tzone = NULL; 685 return; 686 } 687 688 ret = thermal_zone_device_enable(mvm->tz_device.tzone); 689 if (ret) { 690 IWL_DEBUG_TEMP(mvm, "Failed to enable thermal zone\n"); 691 thermal_zone_device_unregister(mvm->tz_device.tzone); 692 } 693 } 694 695 static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device *cdev, 696 unsigned long *state) 697 { 698 *state = IWL_MVM_NUM_CTDP_STEPS - 1; 699 700 return 0; 701 } 702 703 static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device *cdev, 704 unsigned long *state) 705 { 706 struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata); 707 708 *state = mvm->cooling_dev.cur_state; 709 710 return 0; 711 } 712 713 static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device *cdev, 714 unsigned long new_state) 715 { 716 struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata); 717 718 guard(mvm)(mvm); 719 720 if (!iwl_mvm_firmware_running(mvm) || 721 mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) 722 return -EIO; 723 724 if (new_state >= IWL_MVM_NUM_CTDP_STEPS) 725 return -EINVAL; 726 727 return iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START, 728 new_state); 729 } 730 731 static const struct thermal_cooling_device_ops tcooling_ops = { 732 .get_max_state = iwl_mvm_tcool_get_max_state, 733 .get_cur_state = iwl_mvm_tcool_get_cur_state, 734 .set_cur_state = iwl_mvm_tcool_set_cur_state, 735 }; 736 737 static void iwl_mvm_cooling_device_register(struct iwl_mvm *mvm) 738 { 739 char name[] = "iwlwifi"; 740 741 if (!iwl_mvm_is_ctdp_supported(mvm)) 742 return; 743 744 BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH); 745 746 mvm->cooling_dev.cdev = 747 thermal_cooling_device_register(name, 748 mvm, 749 &tcooling_ops); 750 751 if (IS_ERR(mvm->cooling_dev.cdev)) { 752 IWL_DEBUG_TEMP(mvm, 753 "Failed to register to cooling device (err = %ld)\n", 754 PTR_ERR(mvm->cooling_dev.cdev)); 755 mvm->cooling_dev.cdev = NULL; 756 return; 757 } 758 } 759 760 static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm *mvm) 761 { 762 if (!iwl_mvm_is_tt_in_fw(mvm) || !mvm->tz_device.tzone) 763 return; 764 765 IWL_DEBUG_TEMP(mvm, "Thermal zone device unregister\n"); 766 if (mvm->tz_device.tzone) { 767 thermal_zone_device_unregister(mvm->tz_device.tzone); 768 mvm->tz_device.tzone = NULL; 769 } 770 } 771 772 static void iwl_mvm_cooling_device_unregister(struct iwl_mvm *mvm) 773 { 774 if (!iwl_mvm_is_ctdp_supported(mvm) || !mvm->cooling_dev.cdev) 775 return; 776 777 IWL_DEBUG_TEMP(mvm, "Cooling device unregister\n"); 778 if (mvm->cooling_dev.cdev) { 779 thermal_cooling_device_unregister(mvm->cooling_dev.cdev); 780 mvm->cooling_dev.cdev = NULL; 781 } 782 } 783 #endif /* CONFIG_THERMAL */ 784 785 static u32 iwl_mvm_ctdp_get_max_budget(struct iwl_mvm *mvm) 786 { 787 u64 bios_power_budget = 0; 788 u32 default_power_budget; 789 790 switch (CSR_HW_RFID_TYPE(mvm->trans->info.hw_rf_id)) { 791 case IWL_CFG_RF_TYPE_JF2: 792 case IWL_CFG_RF_TYPE_JF1: 793 default_power_budget = 2000; 794 break; 795 case IWL_CFG_RF_TYPE_HR2: 796 case IWL_CFG_RF_TYPE_HR1: 797 default_power_budget = 2400; 798 break; 799 case IWL_CFG_RF_TYPE_GF: 800 /* dual-radio devices have a higher budget */ 801 if (CSR_HW_RFID_IS_CDB(mvm->trans->info.hw_rf_id)) 802 default_power_budget = 5200; 803 else 804 default_power_budget = 2880; 805 break; 806 case IWL_CFG_RF_TYPE_FM: 807 default_power_budget = 3450; 808 break; 809 default: 810 default_power_budget = 5550; 811 break; 812 } 813 814 iwl_bios_get_pwr_limit(&mvm->fwrt, &bios_power_budget); 815 816 /* 32bit in UEFI, 16bit in ACPI; use BIOS value if it is in range */ 817 if (bios_power_budget && 818 bios_power_budget != 0xffff && bios_power_budget != 0xffffffff && 819 bios_power_budget >= IWL_MVM_MIN_CTDP_BUDGET_MW && 820 bios_power_budget <= default_power_budget) 821 return (u32)bios_power_budget; 822 823 return default_power_budget; 824 } 825 826 void iwl_mvm_thermal_initialize(struct iwl_mvm *mvm, u32 min_backoff) 827 { 828 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; 829 830 IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n"); 831 832 if (mvm->cfg->thermal_params) 833 tt->params = *mvm->cfg->thermal_params; 834 else 835 tt->params = iwl_mvm_default_tt_params; 836 837 tt->power_budget_mw = iwl_mvm_ctdp_get_max_budget(mvm); 838 IWL_DEBUG_TEMP(mvm, "cTDP power budget: %d mW\n", tt->power_budget_mw); 839 tt->throttle = false; 840 tt->dynamic_smps = false; 841 tt->min_backoff = min_backoff; 842 INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill); 843 844 #ifdef CONFIG_THERMAL 845 iwl_mvm_cooling_device_register(mvm); 846 iwl_mvm_thermal_zone_register(mvm); 847 #endif 848 mvm->init_status |= IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE; 849 } 850 851 void iwl_mvm_thermal_exit(struct iwl_mvm *mvm) 852 { 853 if (!(mvm->init_status & IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE)) 854 return; 855 856 cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit); 857 IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n"); 858 859 #ifdef CONFIG_THERMAL 860 iwl_mvm_cooling_device_unregister(mvm); 861 iwl_mvm_thermal_zone_unregister(mvm); 862 #endif 863 mvm->init_status &= ~IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE; 864 } 865