1 /****************************************************************************** 2 * 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. 5 * 6 * GPL LICENSE SUMMARY 7 * 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 11 * 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. 15 * 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. 20 * 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, 24 * USA 25 * 26 * The full GNU General Public License is included in this distribution 27 * in the file called COPYING. 28 * 29 * Contact Information: 30 * Intel Linux Wireless <linuxwifi@intel.com> 31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 32 * 33 * BSD LICENSE 34 * 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. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 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 49 * distribution. 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. 53 * 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. 65 * 66 *****************************************************************************/ 67 68 #include <linux/sort.h> 69 70 #include "mvm.h" 71 72 #define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT HZ 73 74 static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm) 75 { 76 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; 77 u32 duration = tt->params.ct_kill_duration; 78 79 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) 80 return; 81 82 IWL_ERR(mvm, "Enter CT Kill\n"); 83 iwl_mvm_set_hw_ctkill_state(mvm, true); 84 85 if (!iwl_mvm_is_tt_in_fw(mvm)) { 86 tt->throttle = false; 87 tt->dynamic_smps = false; 88 } 89 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). 93 */ 94 if (!mvm->temperature_test) 95 schedule_delayed_work(&tt->ct_kill_exit, 96 round_jiffies_relative(duration * HZ)); 97 } 98 99 static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm) 100 { 101 if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) 102 return; 103 104 IWL_ERR(mvm, "Exit CT Kill\n"); 105 iwl_mvm_set_hw_ctkill_state(mvm, false); 106 } 107 108 void iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp) 109 { 110 /* ignore the notification if we are in test mode */ 111 if (mvm->temperature_test) 112 return; 113 114 if (mvm->temperature == temp) 115 return; 116 117 mvm->temperature = temp; 118 iwl_mvm_tt_handler(mvm); 119 } 120 121 static int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm, 122 struct iwl_rx_packet *pkt) 123 { 124 struct iwl_dts_measurement_notif_v1 *notif_v1; 125 int len = iwl_rx_packet_payload_len(pkt); 126 int temp; 127 128 /* we can use notif_v1 only, because v2 only adds an additional 129 * parameter, which is not used in this function. 130 */ 131 if (WARN_ON_ONCE(len < sizeof(*notif_v1))) { 132 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n"); 133 return -EINVAL; 134 } 135 136 notif_v1 = (void *)pkt->data; 137 138 temp = le32_to_cpu(notif_v1->temp); 139 140 /* shouldn't be negative, but since it's s32, make sure it isn't */ 141 if (WARN_ON_ONCE(temp < 0)) 142 temp = 0; 143 144 IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp); 145 146 return temp; 147 } 148 149 static bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait, 150 struct iwl_rx_packet *pkt, void *data) 151 { 152 struct iwl_mvm *mvm = 153 container_of(notif_wait, struct iwl_mvm, notif_wait); 154 int *temp = data; 155 int ret; 156 157 ret = iwl_mvm_temp_notif_parse(mvm, pkt); 158 if (ret < 0) 159 return true; 160 161 *temp = ret; 162 163 return true; 164 } 165 166 void iwl_mvm_temp_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 167 { 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); 171 int temp; 172 u32 ths_crossed; 173 174 /* the notification is handled synchronously in ctkill, so skip here */ 175 if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) 176 return; 177 178 temp = iwl_mvm_temp_notif_parse(mvm, pkt); 179 180 if (!iwl_mvm_is_tt_in_fw(mvm)) { 181 if (temp >= 0) 182 iwl_mvm_tt_temp_changed(mvm, temp); 183 return; 184 } 185 186 if (WARN_ON_ONCE(len < sizeof(*notif_v2))) { 187 IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n"); 188 return; 189 } 190 191 notif_v2 = (void *)pkt->data; 192 ths_crossed = le32_to_cpu(notif_v2->threshold_idx); 193 194 /* 0xFF in ths_crossed means the notification is not related 195 * to a trip, so we can ignore it here. 196 */ 197 if (ths_crossed == 0xFF) 198 return; 199 200 IWL_DEBUG_TEMP(mvm, "Temp = %d Threshold crossed = %d\n", 201 temp, ths_crossed); 202 203 #ifdef CONFIG_THERMAL 204 if (WARN_ON(ths_crossed >= IWL_MAX_DTS_TRIPS)) 205 return; 206 207 if (mvm->tz_device.tzone) { 208 struct iwl_mvm_thermal_device *tz_dev = &mvm->tz_device; 209 210 thermal_notify_framework(tz_dev->tzone, 211 tz_dev->fw_trips_index[ths_crossed]); 212 } 213 #endif /* CONFIG_THERMAL */ 214 } 215 216 void iwl_mvm_ct_kill_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 217 { 218 struct iwl_rx_packet *pkt = rxb_addr(rxb); 219 struct ct_kill_notif *notif; 220 int len = iwl_rx_packet_payload_len(pkt); 221 222 if (WARN_ON_ONCE(len != sizeof(*notif))) { 223 IWL_ERR(mvm, "Invalid CT_KILL_NOTIFICATION\n"); 224 return; 225 } 226 227 notif = (struct ct_kill_notif *)pkt->data; 228 IWL_DEBUG_TEMP(mvm, "CT Kill notification temperature = %d\n", 229 notif->temperature); 230 231 iwl_mvm_enter_ctkill(mvm); 232 } 233 234 static int iwl_mvm_get_temp_cmd(struct iwl_mvm *mvm) 235 { 236 struct iwl_dts_measurement_cmd cmd = { 237 .flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP), 238 }; 239 struct iwl_ext_dts_measurement_cmd extcmd = { 240 .control_mode = cpu_to_le32(DTS_AUTOMATIC), 241 }; 242 u32 cmdid; 243 244 cmdid = iwl_cmd_id(CMD_DTS_MEASUREMENT_TRIGGER_WIDE, 245 PHY_OPS_GROUP, 0); 246 247 if (!fw_has_capa(&mvm->fw->ucode_capa, 248 IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE)) 249 return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(cmd), &cmd); 250 251 return iwl_mvm_send_cmd_pdu(mvm, cmdid, 0, sizeof(extcmd), &extcmd); 252 } 253 254 int iwl_mvm_get_temp(struct iwl_mvm *mvm, s32 *temp) 255 { 256 struct iwl_notification_wait wait_temp_notif; 257 static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP, 258 DTS_MEASUREMENT_NOTIF_WIDE) }; 259 int ret; 260 261 lockdep_assert_held(&mvm->mutex); 262 263 iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif, 264 temp_notif, ARRAY_SIZE(temp_notif), 265 iwl_mvm_temp_notif_wait, temp); 266 267 ret = iwl_mvm_get_temp_cmd(mvm); 268 if (ret) { 269 IWL_ERR(mvm, "Failed to get the temperature (err=%d)\n", ret); 270 iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif); 271 return ret; 272 } 273 274 ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif, 275 IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT); 276 if (ret) 277 IWL_ERR(mvm, "Getting the temperature timed out\n"); 278 279 return ret; 280 } 281 282 static void check_exit_ctkill(struct work_struct *work) 283 { 284 struct iwl_mvm_tt_mgmt *tt; 285 struct iwl_mvm *mvm; 286 u32 duration; 287 s32 temp; 288 int ret; 289 290 tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work); 291 mvm = container_of(tt, struct iwl_mvm, thermal_throttle); 292 293 if (iwl_mvm_is_tt_in_fw(mvm)) { 294 iwl_mvm_exit_ctkill(mvm); 295 296 return; 297 } 298 299 duration = tt->params.ct_kill_duration; 300 301 mutex_lock(&mvm->mutex); 302 303 if (__iwl_mvm_mac_start(mvm)) 304 goto reschedule; 305 306 /* make sure the device is available for direct read/writes */ 307 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_CHECK_CTKILL)) { 308 __iwl_mvm_mac_stop(mvm); 309 goto reschedule; 310 } 311 312 ret = iwl_mvm_get_temp(mvm, &temp); 313 314 iwl_mvm_unref(mvm, IWL_MVM_REF_CHECK_CTKILL); 315 316 __iwl_mvm_mac_stop(mvm); 317 318 if (ret) 319 goto reschedule; 320 321 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp); 322 323 if (temp <= tt->params.ct_kill_exit) { 324 mutex_unlock(&mvm->mutex); 325 iwl_mvm_exit_ctkill(mvm); 326 return; 327 } 328 329 reschedule: 330 mutex_unlock(&mvm->mutex); 331 schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit, 332 round_jiffies(duration * HZ)); 333 } 334 335 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac, 336 struct ieee80211_vif *vif) 337 { 338 struct iwl_mvm *mvm = _data; 339 enum ieee80211_smps_mode smps_mode; 340 341 lockdep_assert_held(&mvm->mutex); 342 343 if (mvm->thermal_throttle.dynamic_smps) 344 smps_mode = IEEE80211_SMPS_DYNAMIC; 345 else 346 smps_mode = IEEE80211_SMPS_AUTOMATIC; 347 348 if (vif->type != NL80211_IFTYPE_STATION) 349 return; 350 351 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode); 352 } 353 354 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable) 355 { 356 struct iwl_mvm_sta *mvmsta; 357 int i, err; 358 359 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) { 360 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, i); 361 if (!mvmsta) 362 continue; 363 364 if (enable == mvmsta->tt_tx_protection) 365 continue; 366 err = iwl_mvm_tx_protection(mvm, mvmsta, enable); 367 if (err) { 368 IWL_ERR(mvm, "Failed to %s Tx protection\n", 369 enable ? "enable" : "disable"); 370 } else { 371 IWL_DEBUG_TEMP(mvm, "%s Tx protection\n", 372 enable ? "Enable" : "Disable"); 373 mvmsta->tt_tx_protection = enable; 374 } 375 } 376 } 377 378 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff) 379 { 380 struct iwl_host_cmd cmd = { 381 .id = REPLY_THERMAL_MNG_BACKOFF, 382 .len = { sizeof(u32), }, 383 .data = { &backoff, }, 384 }; 385 386 backoff = max(backoff, mvm->thermal_throttle.min_backoff); 387 388 if (iwl_mvm_send_cmd(mvm, &cmd) == 0) { 389 IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n", 390 backoff); 391 mvm->thermal_throttle.tx_backoff = backoff; 392 } else { 393 IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n"); 394 } 395 } 396 397 void iwl_mvm_tt_handler(struct iwl_mvm *mvm) 398 { 399 struct iwl_tt_params *params = &mvm->thermal_throttle.params; 400 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; 401 s32 temperature = mvm->temperature; 402 bool throttle_enable = false; 403 int i; 404 u32 tx_backoff; 405 406 IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature); 407 408 if (params->support_ct_kill && temperature >= params->ct_kill_entry) { 409 iwl_mvm_enter_ctkill(mvm); 410 return; 411 } 412 413 if (params->support_ct_kill && 414 temperature <= params->ct_kill_exit) { 415 iwl_mvm_exit_ctkill(mvm); 416 return; 417 } 418 419 if (params->support_dynamic_smps) { 420 if (!tt->dynamic_smps && 421 temperature >= params->dynamic_smps_entry) { 422 IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n"); 423 tt->dynamic_smps = true; 424 ieee80211_iterate_active_interfaces_atomic( 425 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 426 iwl_mvm_tt_smps_iterator, mvm); 427 throttle_enable = true; 428 } else if (tt->dynamic_smps && 429 temperature <= params->dynamic_smps_exit) { 430 IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n"); 431 tt->dynamic_smps = false; 432 ieee80211_iterate_active_interfaces_atomic( 433 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 434 iwl_mvm_tt_smps_iterator, mvm); 435 } 436 } 437 438 if (params->support_tx_protection) { 439 if (temperature >= params->tx_protection_entry) { 440 iwl_mvm_tt_tx_protection(mvm, true); 441 throttle_enable = true; 442 } else if (temperature <= params->tx_protection_exit) { 443 iwl_mvm_tt_tx_protection(mvm, false); 444 } 445 } 446 447 if (params->support_tx_backoff) { 448 tx_backoff = tt->min_backoff; 449 for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) { 450 if (temperature < params->tx_backoff[i].temperature) 451 break; 452 tx_backoff = max(tt->min_backoff, 453 params->tx_backoff[i].backoff); 454 } 455 if (tx_backoff != tt->min_backoff) 456 throttle_enable = true; 457 if (tt->tx_backoff != tx_backoff) 458 iwl_mvm_tt_tx_backoff(mvm, tx_backoff); 459 } 460 461 if (!tt->throttle && throttle_enable) { 462 IWL_WARN(mvm, 463 "Due to high temperature thermal throttling initiated\n"); 464 tt->throttle = true; 465 } else if (tt->throttle && !tt->dynamic_smps && 466 tt->tx_backoff == tt->min_backoff && 467 temperature <= params->tx_protection_exit) { 468 IWL_WARN(mvm, 469 "Temperature is back to normal thermal throttling stopped\n"); 470 tt->throttle = false; 471 } 472 } 473 474 static const struct iwl_tt_params iwl_mvm_default_tt_params = { 475 .ct_kill_entry = 118, 476 .ct_kill_exit = 96, 477 .ct_kill_duration = 5, 478 .dynamic_smps_entry = 114, 479 .dynamic_smps_exit = 110, 480 .tx_protection_entry = 114, 481 .tx_protection_exit = 108, 482 .tx_backoff = { 483 {.temperature = 112, .backoff = 200}, 484 {.temperature = 113, .backoff = 600}, 485 {.temperature = 114, .backoff = 1200}, 486 {.temperature = 115, .backoff = 2000}, 487 {.temperature = 116, .backoff = 4000}, 488 {.temperature = 117, .backoff = 10000}, 489 }, 490 .support_ct_kill = true, 491 .support_dynamic_smps = true, 492 .support_tx_protection = true, 493 .support_tx_backoff = true, 494 }; 495 496 /* budget in mWatt */ 497 static const u32 iwl_mvm_cdev_budgets[] = { 498 2000, /* cooling state 0 */ 499 1800, /* cooling state 1 */ 500 1600, /* cooling state 2 */ 501 1400, /* cooling state 3 */ 502 1200, /* cooling state 4 */ 503 1000, /* cooling state 5 */ 504 900, /* cooling state 6 */ 505 800, /* cooling state 7 */ 506 700, /* cooling state 8 */ 507 650, /* cooling state 9 */ 508 600, /* cooling state 10 */ 509 550, /* cooling state 11 */ 510 500, /* cooling state 12 */ 511 450, /* cooling state 13 */ 512 400, /* cooling state 14 */ 513 350, /* cooling state 15 */ 514 300, /* cooling state 16 */ 515 250, /* cooling state 17 */ 516 200, /* cooling state 18 */ 517 150, /* cooling state 19 */ 518 }; 519 520 int iwl_mvm_ctdp_command(struct iwl_mvm *mvm, u32 op, u32 state) 521 { 522 struct iwl_mvm_ctdp_cmd cmd = { 523 .operation = cpu_to_le32(op), 524 .budget = cpu_to_le32(iwl_mvm_cdev_budgets[state]), 525 .window_size = 0, 526 }; 527 int ret; 528 u32 status; 529 530 lockdep_assert_held(&mvm->mutex); 531 532 ret = iwl_mvm_send_cmd_pdu_status(mvm, WIDE_ID(PHY_OPS_GROUP, 533 CTDP_CONFIG_CMD), 534 sizeof(cmd), &cmd, &status); 535 536 if (ret) { 537 IWL_ERR(mvm, "cTDP command failed (err=%d)\n", ret); 538 return ret; 539 } 540 541 switch (op) { 542 case CTDP_CMD_OPERATION_START: 543 #ifdef CONFIG_THERMAL 544 mvm->cooling_dev.cur_state = state; 545 #endif /* CONFIG_THERMAL */ 546 break; 547 case CTDP_CMD_OPERATION_REPORT: 548 IWL_DEBUG_TEMP(mvm, "cTDP avg energy in mWatt = %d\n", status); 549 /* when the function is called with CTDP_CMD_OPERATION_REPORT 550 * option the function should return the average budget value 551 * that is received from the FW. 552 * The budget can't be less or equal to 0, so it's possible 553 * to distinguish between error values and budgets. 554 */ 555 return status; 556 case CTDP_CMD_OPERATION_STOP: 557 IWL_DEBUG_TEMP(mvm, "cTDP stopped successfully\n"); 558 break; 559 } 560 561 return 0; 562 } 563 564 #ifdef CONFIG_THERMAL 565 static int compare_temps(const void *a, const void *b) 566 { 567 return ((s16)le16_to_cpu(*(__le16 *)a) - 568 (s16)le16_to_cpu(*(__le16 *)b)); 569 } 570 571 int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm *mvm) 572 { 573 struct temp_report_ths_cmd cmd = {0}; 574 int ret, i, j, idx = 0; 575 576 lockdep_assert_held(&mvm->mutex); 577 578 if (!mvm->tz_device.tzone) 579 return -EINVAL; 580 581 /* The driver holds array of temperature trips that are unsorted 582 * and uncompressed, the FW should get it compressed and sorted 583 */ 584 585 /* compress temp_trips to cmd array, remove uninitialized values*/ 586 for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) { 587 if (mvm->tz_device.temp_trips[i] != S16_MIN) { 588 cmd.thresholds[idx++] = 589 cpu_to_le16(mvm->tz_device.temp_trips[i]); 590 } 591 } 592 cmd.num_temps = cpu_to_le32(idx); 593 594 if (!idx) 595 goto send; 596 597 /*sort cmd array*/ 598 sort(cmd.thresholds, idx, sizeof(s16), compare_temps, NULL); 599 600 /* we should save the indexes of trips because we sort 601 * and compress the orginal array 602 */ 603 for (i = 0; i < idx; i++) { 604 for (j = 0; j < IWL_MAX_DTS_TRIPS; j++) { 605 if (le16_to_cpu(cmd.thresholds[i]) == 606 mvm->tz_device.temp_trips[j]) 607 mvm->tz_device.fw_trips_index[i] = j; 608 } 609 } 610 611 send: 612 ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP, 613 TEMP_REPORTING_THRESHOLDS_CMD), 614 0, sizeof(cmd), &cmd); 615 if (ret) 616 IWL_ERR(mvm, "TEMP_REPORT_THS_CMD command failed (err=%d)\n", 617 ret); 618 619 return ret; 620 } 621 622 static int iwl_mvm_tzone_get_temp(struct thermal_zone_device *device, 623 int *temperature) 624 { 625 struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata; 626 int ret; 627 int temp; 628 629 mutex_lock(&mvm->mutex); 630 631 if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) { 632 ret = -EIO; 633 goto out; 634 } 635 636 ret = iwl_mvm_get_temp(mvm, &temp); 637 if (ret) 638 goto out; 639 640 *temperature = temp * 1000; 641 642 out: 643 mutex_unlock(&mvm->mutex); 644 return ret; 645 } 646 647 static int iwl_mvm_tzone_get_trip_temp(struct thermal_zone_device *device, 648 int trip, int *temp) 649 { 650 struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata; 651 652 if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) 653 return -EINVAL; 654 655 *temp = mvm->tz_device.temp_trips[trip] * 1000; 656 657 return 0; 658 } 659 660 static int iwl_mvm_tzone_get_trip_type(struct thermal_zone_device *device, 661 int trip, enum thermal_trip_type *type) 662 { 663 if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) 664 return -EINVAL; 665 666 *type = THERMAL_TRIP_PASSIVE; 667 668 return 0; 669 } 670 671 static int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device *device, 672 int trip, int temp) 673 { 674 struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata; 675 struct iwl_mvm_thermal_device *tzone; 676 int i, ret; 677 s16 temperature; 678 679 mutex_lock(&mvm->mutex); 680 681 if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) { 682 ret = -EIO; 683 goto out; 684 } 685 686 if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) { 687 ret = -EINVAL; 688 goto out; 689 } 690 691 if ((temp / 1000) > S16_MAX) { 692 ret = -EINVAL; 693 goto out; 694 } 695 696 temperature = (s16)(temp / 1000); 697 tzone = &mvm->tz_device; 698 699 if (!tzone) { 700 ret = -EIO; 701 goto out; 702 } 703 704 /* no updates*/ 705 if (tzone->temp_trips[trip] == temperature) { 706 ret = 0; 707 goto out; 708 } 709 710 /* already existing temperature */ 711 for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) { 712 if (tzone->temp_trips[i] == temperature) { 713 ret = -EINVAL; 714 goto out; 715 } 716 } 717 718 tzone->temp_trips[trip] = temperature; 719 720 ret = iwl_mvm_send_temp_report_ths_cmd(mvm); 721 out: 722 mutex_unlock(&mvm->mutex); 723 return ret; 724 } 725 726 static struct thermal_zone_device_ops tzone_ops = { 727 .get_temp = iwl_mvm_tzone_get_temp, 728 .get_trip_temp = iwl_mvm_tzone_get_trip_temp, 729 .get_trip_type = iwl_mvm_tzone_get_trip_type, 730 .set_trip_temp = iwl_mvm_tzone_set_trip_temp, 731 }; 732 733 /* make all trips writable */ 734 #define IWL_WRITABLE_TRIPS_MSK (BIT(IWL_MAX_DTS_TRIPS) - 1) 735 736 static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm) 737 { 738 int i; 739 char name[] = "iwlwifi"; 740 741 if (!iwl_mvm_is_tt_in_fw(mvm)) { 742 mvm->tz_device.tzone = NULL; 743 744 return; 745 } 746 747 BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH); 748 749 mvm->tz_device.tzone = thermal_zone_device_register(name, 750 IWL_MAX_DTS_TRIPS, 751 IWL_WRITABLE_TRIPS_MSK, 752 mvm, &tzone_ops, 753 NULL, 0, 0); 754 if (IS_ERR(mvm->tz_device.tzone)) { 755 IWL_DEBUG_TEMP(mvm, 756 "Failed to register to thermal zone (err = %ld)\n", 757 PTR_ERR(mvm->tz_device.tzone)); 758 mvm->tz_device.tzone = NULL; 759 return; 760 } 761 762 /* 0 is a valid temperature, 763 * so initialize the array with S16_MIN which invalid temperature 764 */ 765 for (i = 0 ; i < IWL_MAX_DTS_TRIPS; i++) 766 mvm->tz_device.temp_trips[i] = S16_MIN; 767 } 768 769 static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device *cdev, 770 unsigned long *state) 771 { 772 *state = ARRAY_SIZE(iwl_mvm_cdev_budgets) - 1; 773 774 return 0; 775 } 776 777 static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device *cdev, 778 unsigned long *state) 779 { 780 struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata); 781 782 *state = mvm->cooling_dev.cur_state; 783 784 return 0; 785 } 786 787 static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device *cdev, 788 unsigned long new_state) 789 { 790 struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata); 791 int ret; 792 793 if (!mvm->ucode_loaded || !(mvm->cur_ucode == IWL_UCODE_REGULAR)) 794 return -EIO; 795 796 mutex_lock(&mvm->mutex); 797 798 if (new_state >= ARRAY_SIZE(iwl_mvm_cdev_budgets)) { 799 ret = -EINVAL; 800 goto unlock; 801 } 802 803 ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START, 804 new_state); 805 806 unlock: 807 mutex_unlock(&mvm->mutex); 808 return ret; 809 } 810 811 static struct thermal_cooling_device_ops tcooling_ops = { 812 .get_max_state = iwl_mvm_tcool_get_max_state, 813 .get_cur_state = iwl_mvm_tcool_get_cur_state, 814 .set_cur_state = iwl_mvm_tcool_set_cur_state, 815 }; 816 817 static void iwl_mvm_cooling_device_register(struct iwl_mvm *mvm) 818 { 819 char name[] = "iwlwifi"; 820 821 if (!iwl_mvm_is_ctdp_supported(mvm)) 822 return; 823 824 BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH); 825 826 mvm->cooling_dev.cdev = 827 thermal_cooling_device_register(name, 828 mvm, 829 &tcooling_ops); 830 831 if (IS_ERR(mvm->cooling_dev.cdev)) { 832 IWL_DEBUG_TEMP(mvm, 833 "Failed to register to cooling device (err = %ld)\n", 834 PTR_ERR(mvm->cooling_dev.cdev)); 835 mvm->cooling_dev.cdev = NULL; 836 return; 837 } 838 } 839 840 static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm *mvm) 841 { 842 if (!iwl_mvm_is_tt_in_fw(mvm) || !mvm->tz_device.tzone) 843 return; 844 845 IWL_DEBUG_TEMP(mvm, "Thermal zone device unregister\n"); 846 if (mvm->tz_device.tzone) { 847 thermal_zone_device_unregister(mvm->tz_device.tzone); 848 mvm->tz_device.tzone = NULL; 849 } 850 } 851 852 static void iwl_mvm_cooling_device_unregister(struct iwl_mvm *mvm) 853 { 854 if (!iwl_mvm_is_ctdp_supported(mvm) || !mvm->cooling_dev.cdev) 855 return; 856 857 IWL_DEBUG_TEMP(mvm, "Cooling device unregister\n"); 858 if (mvm->cooling_dev.cdev) { 859 thermal_cooling_device_unregister(mvm->cooling_dev.cdev); 860 mvm->cooling_dev.cdev = NULL; 861 } 862 } 863 #endif /* CONFIG_THERMAL */ 864 865 void iwl_mvm_thermal_initialize(struct iwl_mvm *mvm, u32 min_backoff) 866 { 867 struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle; 868 869 IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n"); 870 871 if (mvm->cfg->thermal_params) 872 tt->params = *mvm->cfg->thermal_params; 873 else 874 tt->params = iwl_mvm_default_tt_params; 875 876 tt->throttle = false; 877 tt->dynamic_smps = false; 878 tt->min_backoff = min_backoff; 879 INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill); 880 881 #ifdef CONFIG_THERMAL 882 iwl_mvm_cooling_device_register(mvm); 883 iwl_mvm_thermal_zone_register(mvm); 884 #endif 885 } 886 887 void iwl_mvm_thermal_exit(struct iwl_mvm *mvm) 888 { 889 cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit); 890 IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n"); 891 892 #ifdef CONFIG_THERMAL 893 iwl_mvm_cooling_device_unregister(mvm); 894 iwl_mvm_thermal_zone_unregister(mvm); 895 #endif 896 } 897