18c2ecf20Sopenharmony_ci/******************************************************************************
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
48c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright(c) 2013 - 2014, 2019 Intel Corporation. All rights reserved.
98c2ecf20Sopenharmony_ci * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
108c2ecf20Sopenharmony_ci * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
118c2ecf20Sopenharmony_ci * Copyright(c) 2019 - 2020 Intel Corporation
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
148c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
158c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
188c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
198c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
208c2ecf20Sopenharmony_ci * General Public License for more details.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * The full GNU General Public License is included in this distribution
238c2ecf20Sopenharmony_ci * in the file called COPYING.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * Contact Information:
268c2ecf20Sopenharmony_ci *  Intel Linux Wireless <linuxwifi@intel.com>
278c2ecf20Sopenharmony_ci * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * BSD LICENSE
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * Copyright(c) 2012 - 2014, 2019 Intel Corporation. All rights reserved.
328c2ecf20Sopenharmony_ci * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
338c2ecf20Sopenharmony_ci * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
348c2ecf20Sopenharmony_ci * Copyright(c) 2019 - 2020 Intel Corporation
358c2ecf20Sopenharmony_ci * All rights reserved.
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
388c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
398c2ecf20Sopenharmony_ci * are met:
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci *  * Redistributions of source code must retain the above copyright
428c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
438c2ecf20Sopenharmony_ci *  * Redistributions in binary form must reproduce the above copyright
448c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
458c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
468c2ecf20Sopenharmony_ci *    distribution.
478c2ecf20Sopenharmony_ci *  * Neither the name Intel Corporation nor the names of its
488c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
498c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
528c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
538c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
548c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
558c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
568c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
578c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
588c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
598c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
608c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
618c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci *****************************************************************************/
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#include <linux/sort.h>
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#include "mvm.h"
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT	HZ
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_civoid iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
748c2ecf20Sopenharmony_ci	u32 duration = tt->params.ct_kill_duration;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
778c2ecf20Sopenharmony_ci		return;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "Enter CT Kill\n");
808c2ecf20Sopenharmony_ci	iwl_mvm_set_hw_ctkill_state(mvm, true);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (!iwl_mvm_is_tt_in_fw(mvm)) {
838c2ecf20Sopenharmony_ci		tt->throttle = false;
848c2ecf20Sopenharmony_ci		tt->dynamic_smps = false;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* Don't schedule an exit work if we're in test mode, since
888c2ecf20Sopenharmony_ci	 * the temperature will not change unless we manually set it
898c2ecf20Sopenharmony_ci	 * again (or disable testing).
908c2ecf20Sopenharmony_ci	 */
918c2ecf20Sopenharmony_ci	if (!mvm->temperature_test)
928c2ecf20Sopenharmony_ci		schedule_delayed_work(&tt->ct_kill_exit,
938c2ecf20Sopenharmony_ci				      round_jiffies_relative(duration * HZ));
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
998c2ecf20Sopenharmony_ci		return;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "Exit CT Kill\n");
1028c2ecf20Sopenharmony_ci	iwl_mvm_set_hw_ctkill_state(mvm, false);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_civoid iwl_mvm_tt_temp_changed(struct iwl_mvm *mvm, u32 temp)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	/* ignore the notification if we are in test mode */
1088c2ecf20Sopenharmony_ci	if (mvm->temperature_test)
1098c2ecf20Sopenharmony_ci		return;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (mvm->temperature == temp)
1128c2ecf20Sopenharmony_ci		return;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	mvm->temperature = temp;
1158c2ecf20Sopenharmony_ci	iwl_mvm_tt_handler(mvm);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic int iwl_mvm_temp_notif_parse(struct iwl_mvm *mvm,
1198c2ecf20Sopenharmony_ci				    struct iwl_rx_packet *pkt)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct iwl_dts_measurement_notif_v1 *notif_v1;
1228c2ecf20Sopenharmony_ci	int len = iwl_rx_packet_payload_len(pkt);
1238c2ecf20Sopenharmony_ci	int temp;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/* we can use notif_v1 only, because v2 only adds an additional
1268c2ecf20Sopenharmony_ci	 * parameter, which is not used in this function.
1278c2ecf20Sopenharmony_ci	*/
1288c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(len < sizeof(*notif_v1))) {
1298c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
1308c2ecf20Sopenharmony_ci		return -EINVAL;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	notif_v1 = (void *)pkt->data;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	temp = le32_to_cpu(notif_v1->temp);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	/* shouldn't be negative, but since it's s32, make sure it isn't */
1388c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(temp < 0))
1398c2ecf20Sopenharmony_ci		temp = 0;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm, "DTS_MEASUREMENT_NOTIFICATION - %d\n", temp);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return temp;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic bool iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data *notif_wait,
1478c2ecf20Sopenharmony_ci				    struct iwl_rx_packet *pkt, void *data)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm =
1508c2ecf20Sopenharmony_ci		container_of(notif_wait, struct iwl_mvm, notif_wait);
1518c2ecf20Sopenharmony_ci	int *temp = data;
1528c2ecf20Sopenharmony_ci	int ret;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	ret = iwl_mvm_temp_notif_parse(mvm, pkt);
1558c2ecf20Sopenharmony_ci	if (ret < 0)
1568c2ecf20Sopenharmony_ci		return true;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	*temp = ret;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	return true;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_civoid iwl_mvm_temp_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1668c2ecf20Sopenharmony_ci	struct iwl_dts_measurement_notif_v2 *notif_v2;
1678c2ecf20Sopenharmony_ci	int len = iwl_rx_packet_payload_len(pkt);
1688c2ecf20Sopenharmony_ci	int temp;
1698c2ecf20Sopenharmony_ci	u32 ths_crossed;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/* the notification is handled synchronously in ctkill, so skip here */
1728c2ecf20Sopenharmony_ci	if (test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status))
1738c2ecf20Sopenharmony_ci		return;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	temp = iwl_mvm_temp_notif_parse(mvm, pkt);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (!iwl_mvm_is_tt_in_fw(mvm)) {
1788c2ecf20Sopenharmony_ci		if (temp >= 0)
1798c2ecf20Sopenharmony_ci			iwl_mvm_tt_temp_changed(mvm, temp);
1808c2ecf20Sopenharmony_ci		return;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(len < sizeof(*notif_v2))) {
1848c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Invalid DTS_MEASUREMENT_NOTIFICATION\n");
1858c2ecf20Sopenharmony_ci		return;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	notif_v2 = (void *)pkt->data;
1898c2ecf20Sopenharmony_ci	ths_crossed = le32_to_cpu(notif_v2->threshold_idx);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/* 0xFF in ths_crossed means the notification is not related
1928c2ecf20Sopenharmony_ci	 * to a trip, so we can ignore it here.
1938c2ecf20Sopenharmony_ci	 */
1948c2ecf20Sopenharmony_ci	if (ths_crossed == 0xFF)
1958c2ecf20Sopenharmony_ci		return;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm, "Temp = %d Threshold crossed = %d\n",
1988c2ecf20Sopenharmony_ci		       temp, ths_crossed);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci#ifdef CONFIG_THERMAL
2018c2ecf20Sopenharmony_ci	if (WARN_ON(ths_crossed >= IWL_MAX_DTS_TRIPS))
2028c2ecf20Sopenharmony_ci		return;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (mvm->tz_device.tzone) {
2058c2ecf20Sopenharmony_ci		struct iwl_mvm_thermal_device *tz_dev = &mvm->tz_device;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci		thermal_notify_framework(tz_dev->tzone,
2088c2ecf20Sopenharmony_ci					 tz_dev->fw_trips_index[ths_crossed]);
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci#endif /* CONFIG_THERMAL */
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_civoid iwl_mvm_ct_kill_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2168c2ecf20Sopenharmony_ci	struct ct_kill_notif *notif;
2178c2ecf20Sopenharmony_ci	int len = iwl_rx_packet_payload_len(pkt);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(len != sizeof(*notif))) {
2208c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Invalid CT_KILL_NOTIFICATION\n");
2218c2ecf20Sopenharmony_ci		return;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	notif = (struct ct_kill_notif *)pkt->data;
2258c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm, "CT Kill notification temperature = %d\n",
2268c2ecf20Sopenharmony_ci		       notif->temperature);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	iwl_mvm_enter_ctkill(mvm);
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci/*
2328c2ecf20Sopenharmony_ci * send the DTS_MEASUREMENT_TRIGGER command with or without waiting for a
2338c2ecf20Sopenharmony_ci * response. If we get a response then the measurement is stored in 'temp'
2348c2ecf20Sopenharmony_ci */
2358c2ecf20Sopenharmony_cistatic int iwl_mvm_send_temp_cmd(struct iwl_mvm *mvm, bool response, s32 *temp)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {};
2388c2ecf20Sopenharmony_ci	struct iwl_dts_measurement_cmd dts_cmd = {
2398c2ecf20Sopenharmony_ci		.flags = cpu_to_le32(DTS_TRIGGER_CMD_FLAGS_TEMP),
2408c2ecf20Sopenharmony_ci	};
2418c2ecf20Sopenharmony_ci	struct iwl_ext_dts_measurement_cmd ext_cmd = {
2428c2ecf20Sopenharmony_ci		.control_mode = cpu_to_le32(DTS_DIRECT_WITHOUT_MEASURE),
2438c2ecf20Sopenharmony_ci	};
2448c2ecf20Sopenharmony_ci	struct iwl_dts_measurement_resp *resp;
2458c2ecf20Sopenharmony_ci	void *cmd_ptr;
2468c2ecf20Sopenharmony_ci	int ret;
2478c2ecf20Sopenharmony_ci	u32 cmd_flags = 0;
2488c2ecf20Sopenharmony_ci	u16 len;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* Check which command format is used (regular/extended) */
2518c2ecf20Sopenharmony_ci	if (fw_has_capa(&mvm->fw->ucode_capa,
2528c2ecf20Sopenharmony_ci			IWL_UCODE_TLV_CAPA_EXTENDED_DTS_MEASURE)) {
2538c2ecf20Sopenharmony_ci		len = sizeof(ext_cmd);
2548c2ecf20Sopenharmony_ci		cmd_ptr = &ext_cmd;
2558c2ecf20Sopenharmony_ci	} else {
2568c2ecf20Sopenharmony_ci		len = sizeof(dts_cmd);
2578c2ecf20Sopenharmony_ci		cmd_ptr = &dts_cmd;
2588c2ecf20Sopenharmony_ci	}
2598c2ecf20Sopenharmony_ci	/* The command version where we get a response is zero length */
2608c2ecf20Sopenharmony_ci	if (response) {
2618c2ecf20Sopenharmony_ci		cmd_flags = CMD_WANT_SKB;
2628c2ecf20Sopenharmony_ci		len = 0;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	cmd.id =  WIDE_ID(PHY_OPS_GROUP, CMD_DTS_MEASUREMENT_TRIGGER_WIDE);
2668c2ecf20Sopenharmony_ci	cmd.len[0] = len;
2678c2ecf20Sopenharmony_ci	cmd.flags = cmd_flags;
2688c2ecf20Sopenharmony_ci	cmd.data[0] = cmd_ptr;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm,
2718c2ecf20Sopenharmony_ci		       "Sending temperature measurement command - %s response\n",
2728c2ecf20Sopenharmony_ci		       response ? "with" : "without");
2738c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd(mvm, &cmd);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (ret) {
2768c2ecf20Sopenharmony_ci		IWL_ERR(mvm,
2778c2ecf20Sopenharmony_ci			"Failed to send the temperature measurement command (err=%d)\n",
2788c2ecf20Sopenharmony_ci			ret);
2798c2ecf20Sopenharmony_ci		return ret;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	if (response) {
2838c2ecf20Sopenharmony_ci		resp = (void *)cmd.resp_pkt->data;
2848c2ecf20Sopenharmony_ci		*temp = le32_to_cpu(resp->temp);
2858c2ecf20Sopenharmony_ci		IWL_DEBUG_TEMP(mvm,
2868c2ecf20Sopenharmony_ci			       "Got temperature measurement response: temp=%d\n",
2878c2ecf20Sopenharmony_ci			       *temp);
2888c2ecf20Sopenharmony_ci		iwl_free_resp(&cmd);
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	return ret;
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ciint iwl_mvm_get_temp(struct iwl_mvm *mvm, s32 *temp)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	struct iwl_notification_wait wait_temp_notif;
2978c2ecf20Sopenharmony_ci	static u16 temp_notif[] = { WIDE_ID(PHY_OPS_GROUP,
2988c2ecf20Sopenharmony_ci					    DTS_MEASUREMENT_NOTIF_WIDE) };
2998c2ecf20Sopenharmony_ci	int ret;
3008c2ecf20Sopenharmony_ci	u8 cmd_ver;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/*
3038c2ecf20Sopenharmony_ci	 * If command version is 1 we send the command and immediately get
3048c2ecf20Sopenharmony_ci	 * a response. For older versions we send the command and wait for a
3058c2ecf20Sopenharmony_ci	 * notification (no command TLV for previous versions).
3068c2ecf20Sopenharmony_ci	 */
3078c2ecf20Sopenharmony_ci	cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, PHY_OPS_GROUP,
3088c2ecf20Sopenharmony_ci					CMD_DTS_MEASUREMENT_TRIGGER_WIDE,
3098c2ecf20Sopenharmony_ci					IWL_FW_CMD_VER_UNKNOWN);
3108c2ecf20Sopenharmony_ci	if (cmd_ver == 1)
3118c2ecf20Sopenharmony_ci		return iwl_mvm_send_temp_cmd(mvm, true, temp);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	iwl_init_notification_wait(&mvm->notif_wait, &wait_temp_notif,
3168c2ecf20Sopenharmony_ci				   temp_notif, ARRAY_SIZE(temp_notif),
3178c2ecf20Sopenharmony_ci				   iwl_mvm_temp_notif_wait, temp);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_temp_cmd(mvm, false, temp);
3208c2ecf20Sopenharmony_ci	if (ret) {
3218c2ecf20Sopenharmony_ci		iwl_remove_notification(&mvm->notif_wait, &wait_temp_notif);
3228c2ecf20Sopenharmony_ci		return ret;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	ret = iwl_wait_notification(&mvm->notif_wait, &wait_temp_notif,
3268c2ecf20Sopenharmony_ci				    IWL_MVM_TEMP_NOTIF_WAIT_TIMEOUT);
3278c2ecf20Sopenharmony_ci	if (ret)
3288c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Getting the temperature timed out\n");
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	return ret;
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic void check_exit_ctkill(struct work_struct *work)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	struct iwl_mvm_tt_mgmt *tt;
3368c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm;
3378c2ecf20Sopenharmony_ci	u32 duration;
3388c2ecf20Sopenharmony_ci	s32 temp;
3398c2ecf20Sopenharmony_ci	int ret;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
3428c2ecf20Sopenharmony_ci	mvm = container_of(tt, struct iwl_mvm, thermal_throttle);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (iwl_mvm_is_tt_in_fw(mvm)) {
3458c2ecf20Sopenharmony_ci		iwl_mvm_exit_ctkill(mvm);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci		return;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	duration = tt->params.ct_kill_duration;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	flush_work(&mvm->roc_done_wk);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	if (__iwl_mvm_mac_start(mvm))
3578c2ecf20Sopenharmony_ci		goto reschedule;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	ret = iwl_mvm_get_temp(mvm, &temp);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	__iwl_mvm_mac_stop(mvm);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (ret)
3648c2ecf20Sopenharmony_ci		goto reschedule;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	if (temp <= tt->params.ct_kill_exit) {
3698c2ecf20Sopenharmony_ci		mutex_unlock(&mvm->mutex);
3708c2ecf20Sopenharmony_ci		iwl_mvm_exit_ctkill(mvm);
3718c2ecf20Sopenharmony_ci		return;
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cireschedule:
3758c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
3768c2ecf20Sopenharmony_ci	schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
3778c2ecf20Sopenharmony_ci			      round_jiffies(duration * HZ));
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
3818c2ecf20Sopenharmony_ci				     struct ieee80211_vif *vif)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = _data;
3848c2ecf20Sopenharmony_ci	enum ieee80211_smps_mode smps_mode;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	if (mvm->thermal_throttle.dynamic_smps)
3898c2ecf20Sopenharmony_ci		smps_mode = IEEE80211_SMPS_DYNAMIC;
3908c2ecf20Sopenharmony_ci	else
3918c2ecf20Sopenharmony_ci		smps_mode = IEEE80211_SMPS_AUTOMATIC;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	if (vif->type != NL80211_IFTYPE_STATION)
3948c2ecf20Sopenharmony_ci		return;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	struct iwl_mvm_sta *mvmsta;
4028c2ecf20Sopenharmony_ci	int i, err;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4058c2ecf20Sopenharmony_ci		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, i);
4068c2ecf20Sopenharmony_ci		if (!mvmsta)
4078c2ecf20Sopenharmony_ci			continue;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci		if (enable == mvmsta->tt_tx_protection)
4108c2ecf20Sopenharmony_ci			continue;
4118c2ecf20Sopenharmony_ci		err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
4128c2ecf20Sopenharmony_ci		if (err) {
4138c2ecf20Sopenharmony_ci			IWL_ERR(mvm, "Failed to %s Tx protection\n",
4148c2ecf20Sopenharmony_ci				enable ? "enable" : "disable");
4158c2ecf20Sopenharmony_ci		} else {
4168c2ecf20Sopenharmony_ci			IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
4178c2ecf20Sopenharmony_ci				       enable ? "Enable" : "Disable");
4188c2ecf20Sopenharmony_ci			mvmsta->tt_tx_protection = enable;
4198c2ecf20Sopenharmony_ci		}
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_civoid iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
4268c2ecf20Sopenharmony_ci		.id = REPLY_THERMAL_MNG_BACKOFF,
4278c2ecf20Sopenharmony_ci		.len = { sizeof(u32), },
4288c2ecf20Sopenharmony_ci		.data = { &backoff, },
4298c2ecf20Sopenharmony_ci	};
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	backoff = max(backoff, mvm->thermal_throttle.min_backoff);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
4348c2ecf20Sopenharmony_ci		IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
4358c2ecf20Sopenharmony_ci			       backoff);
4368c2ecf20Sopenharmony_ci		mvm->thermal_throttle.tx_backoff = backoff;
4378c2ecf20Sopenharmony_ci	} else {
4388c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
4398c2ecf20Sopenharmony_ci	}
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_civoid iwl_mvm_tt_handler(struct iwl_mvm *mvm)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	struct iwl_tt_params *params = &mvm->thermal_throttle.params;
4458c2ecf20Sopenharmony_ci	struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
4468c2ecf20Sopenharmony_ci	s32 temperature = mvm->temperature;
4478c2ecf20Sopenharmony_ci	bool throttle_enable = false;
4488c2ecf20Sopenharmony_ci	int i;
4498c2ecf20Sopenharmony_ci	u32 tx_backoff;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
4548c2ecf20Sopenharmony_ci		iwl_mvm_enter_ctkill(mvm);
4558c2ecf20Sopenharmony_ci		return;
4568c2ecf20Sopenharmony_ci	}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	if (params->support_ct_kill &&
4598c2ecf20Sopenharmony_ci	    temperature <= params->ct_kill_exit) {
4608c2ecf20Sopenharmony_ci		iwl_mvm_exit_ctkill(mvm);
4618c2ecf20Sopenharmony_ci		return;
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	if (params->support_dynamic_smps) {
4658c2ecf20Sopenharmony_ci		if (!tt->dynamic_smps &&
4668c2ecf20Sopenharmony_ci		    temperature >= params->dynamic_smps_entry) {
4678c2ecf20Sopenharmony_ci			IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
4688c2ecf20Sopenharmony_ci			tt->dynamic_smps = true;
4698c2ecf20Sopenharmony_ci			ieee80211_iterate_active_interfaces_atomic(
4708c2ecf20Sopenharmony_ci					mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
4718c2ecf20Sopenharmony_ci					iwl_mvm_tt_smps_iterator, mvm);
4728c2ecf20Sopenharmony_ci			throttle_enable = true;
4738c2ecf20Sopenharmony_ci		} else if (tt->dynamic_smps &&
4748c2ecf20Sopenharmony_ci			   temperature <= params->dynamic_smps_exit) {
4758c2ecf20Sopenharmony_ci			IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
4768c2ecf20Sopenharmony_ci			tt->dynamic_smps = false;
4778c2ecf20Sopenharmony_ci			ieee80211_iterate_active_interfaces_atomic(
4788c2ecf20Sopenharmony_ci					mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
4798c2ecf20Sopenharmony_ci					iwl_mvm_tt_smps_iterator, mvm);
4808c2ecf20Sopenharmony_ci		}
4818c2ecf20Sopenharmony_ci	}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	if (params->support_tx_protection) {
4848c2ecf20Sopenharmony_ci		if (temperature >= params->tx_protection_entry) {
4858c2ecf20Sopenharmony_ci			iwl_mvm_tt_tx_protection(mvm, true);
4868c2ecf20Sopenharmony_ci			throttle_enable = true;
4878c2ecf20Sopenharmony_ci		} else if (temperature <= params->tx_protection_exit) {
4888c2ecf20Sopenharmony_ci			iwl_mvm_tt_tx_protection(mvm, false);
4898c2ecf20Sopenharmony_ci		}
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	if (params->support_tx_backoff) {
4938c2ecf20Sopenharmony_ci		tx_backoff = tt->min_backoff;
4948c2ecf20Sopenharmony_ci		for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
4958c2ecf20Sopenharmony_ci			if (temperature < params->tx_backoff[i].temperature)
4968c2ecf20Sopenharmony_ci				break;
4978c2ecf20Sopenharmony_ci			tx_backoff = max(tt->min_backoff,
4988c2ecf20Sopenharmony_ci					 params->tx_backoff[i].backoff);
4998c2ecf20Sopenharmony_ci		}
5008c2ecf20Sopenharmony_ci		if (tx_backoff != tt->min_backoff)
5018c2ecf20Sopenharmony_ci			throttle_enable = true;
5028c2ecf20Sopenharmony_ci		if (tt->tx_backoff != tx_backoff)
5038c2ecf20Sopenharmony_ci			iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
5048c2ecf20Sopenharmony_ci	}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	if (!tt->throttle && throttle_enable) {
5078c2ecf20Sopenharmony_ci		IWL_WARN(mvm,
5088c2ecf20Sopenharmony_ci			 "Due to high temperature thermal throttling initiated\n");
5098c2ecf20Sopenharmony_ci		tt->throttle = true;
5108c2ecf20Sopenharmony_ci	} else if (tt->throttle && !tt->dynamic_smps &&
5118c2ecf20Sopenharmony_ci		   tt->tx_backoff == tt->min_backoff &&
5128c2ecf20Sopenharmony_ci		   temperature <= params->tx_protection_exit) {
5138c2ecf20Sopenharmony_ci		IWL_WARN(mvm,
5148c2ecf20Sopenharmony_ci			 "Temperature is back to normal thermal throttling stopped\n");
5158c2ecf20Sopenharmony_ci		tt->throttle = false;
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic const struct iwl_tt_params iwl_mvm_default_tt_params = {
5208c2ecf20Sopenharmony_ci	.ct_kill_entry = 118,
5218c2ecf20Sopenharmony_ci	.ct_kill_exit = 96,
5228c2ecf20Sopenharmony_ci	.ct_kill_duration = 5,
5238c2ecf20Sopenharmony_ci	.dynamic_smps_entry = 114,
5248c2ecf20Sopenharmony_ci	.dynamic_smps_exit = 110,
5258c2ecf20Sopenharmony_ci	.tx_protection_entry = 114,
5268c2ecf20Sopenharmony_ci	.tx_protection_exit = 108,
5278c2ecf20Sopenharmony_ci	.tx_backoff = {
5288c2ecf20Sopenharmony_ci		{.temperature = 112, .backoff = 200},
5298c2ecf20Sopenharmony_ci		{.temperature = 113, .backoff = 600},
5308c2ecf20Sopenharmony_ci		{.temperature = 114, .backoff = 1200},
5318c2ecf20Sopenharmony_ci		{.temperature = 115, .backoff = 2000},
5328c2ecf20Sopenharmony_ci		{.temperature = 116, .backoff = 4000},
5338c2ecf20Sopenharmony_ci		{.temperature = 117, .backoff = 10000},
5348c2ecf20Sopenharmony_ci	},
5358c2ecf20Sopenharmony_ci	.support_ct_kill = true,
5368c2ecf20Sopenharmony_ci	.support_dynamic_smps = true,
5378c2ecf20Sopenharmony_ci	.support_tx_protection = true,
5388c2ecf20Sopenharmony_ci	.support_tx_backoff = true,
5398c2ecf20Sopenharmony_ci};
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci/* budget in mWatt */
5428c2ecf20Sopenharmony_cistatic const u32 iwl_mvm_cdev_budgets[] = {
5438c2ecf20Sopenharmony_ci	2400,	/* cooling state 0 */
5448c2ecf20Sopenharmony_ci	2000,	/* cooling state 1 */
5458c2ecf20Sopenharmony_ci	1800,	/* cooling state 2 */
5468c2ecf20Sopenharmony_ci	1600,	/* cooling state 3 */
5478c2ecf20Sopenharmony_ci	1400,	/* cooling state 4 */
5488c2ecf20Sopenharmony_ci	1200,	/* cooling state 5 */
5498c2ecf20Sopenharmony_ci	1000,	/* cooling state 6 */
5508c2ecf20Sopenharmony_ci	900,	/* cooling state 7 */
5518c2ecf20Sopenharmony_ci	800,	/* cooling state 8 */
5528c2ecf20Sopenharmony_ci	700,	/* cooling state 9 */
5538c2ecf20Sopenharmony_ci	650,	/* cooling state 10 */
5548c2ecf20Sopenharmony_ci	600,	/* cooling state 11 */
5558c2ecf20Sopenharmony_ci	550,	/* cooling state 12 */
5568c2ecf20Sopenharmony_ci	500,	/* cooling state 13 */
5578c2ecf20Sopenharmony_ci	450,	/* cooling state 14 */
5588c2ecf20Sopenharmony_ci	400,	/* cooling state 15 */
5598c2ecf20Sopenharmony_ci	350,	/* cooling state 16 */
5608c2ecf20Sopenharmony_ci	300,	/* cooling state 17 */
5618c2ecf20Sopenharmony_ci	250,	/* cooling state 18 */
5628c2ecf20Sopenharmony_ci	200,	/* cooling state 19 */
5638c2ecf20Sopenharmony_ci	150,	/* cooling state 20 */
5648c2ecf20Sopenharmony_ci};
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ciint iwl_mvm_ctdp_command(struct iwl_mvm *mvm, u32 op, u32 state)
5678c2ecf20Sopenharmony_ci{
5688c2ecf20Sopenharmony_ci	struct iwl_mvm_ctdp_cmd cmd = {
5698c2ecf20Sopenharmony_ci		.operation = cpu_to_le32(op),
5708c2ecf20Sopenharmony_ci		.budget = cpu_to_le32(iwl_mvm_cdev_budgets[state]),
5718c2ecf20Sopenharmony_ci		.window_size = 0,
5728c2ecf20Sopenharmony_ci	};
5738c2ecf20Sopenharmony_ci	int ret;
5748c2ecf20Sopenharmony_ci	u32 status;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	status = 0;
5798c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu_status(mvm, WIDE_ID(PHY_OPS_GROUP,
5808c2ecf20Sopenharmony_ci						       CTDP_CONFIG_CMD),
5818c2ecf20Sopenharmony_ci					  sizeof(cmd), &cmd, &status);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	if (ret) {
5848c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "cTDP command failed (err=%d)\n", ret);
5858c2ecf20Sopenharmony_ci		return ret;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	switch (op) {
5898c2ecf20Sopenharmony_ci	case CTDP_CMD_OPERATION_START:
5908c2ecf20Sopenharmony_ci#ifdef CONFIG_THERMAL
5918c2ecf20Sopenharmony_ci		mvm->cooling_dev.cur_state = state;
5928c2ecf20Sopenharmony_ci#endif /* CONFIG_THERMAL */
5938c2ecf20Sopenharmony_ci		break;
5948c2ecf20Sopenharmony_ci	case CTDP_CMD_OPERATION_REPORT:
5958c2ecf20Sopenharmony_ci		IWL_DEBUG_TEMP(mvm, "cTDP avg energy in mWatt = %d\n", status);
5968c2ecf20Sopenharmony_ci		/* when the function is called with CTDP_CMD_OPERATION_REPORT
5978c2ecf20Sopenharmony_ci		 * option the function should return the average budget value
5988c2ecf20Sopenharmony_ci		 * that is received from the FW.
5998c2ecf20Sopenharmony_ci		 * The budget can't be less or equal to 0, so it's possible
6008c2ecf20Sopenharmony_ci		 * to distinguish between error values and budgets.
6018c2ecf20Sopenharmony_ci		 */
6028c2ecf20Sopenharmony_ci		return status;
6038c2ecf20Sopenharmony_ci	case CTDP_CMD_OPERATION_STOP:
6048c2ecf20Sopenharmony_ci		IWL_DEBUG_TEMP(mvm, "cTDP stopped successfully\n");
6058c2ecf20Sopenharmony_ci		break;
6068c2ecf20Sopenharmony_ci	}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	return 0;
6098c2ecf20Sopenharmony_ci}
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci#ifdef CONFIG_THERMAL
6128c2ecf20Sopenharmony_cistatic int compare_temps(const void *a, const void *b)
6138c2ecf20Sopenharmony_ci{
6148c2ecf20Sopenharmony_ci	return ((s16)le16_to_cpu(*(__le16 *)a) -
6158c2ecf20Sopenharmony_ci		(s16)le16_to_cpu(*(__le16 *)b));
6168c2ecf20Sopenharmony_ci}
6178c2ecf20Sopenharmony_ci#endif
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ciint iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm *mvm)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	struct temp_report_ths_cmd cmd = {0};
6228c2ecf20Sopenharmony_ci	int ret;
6238c2ecf20Sopenharmony_ci#ifdef CONFIG_THERMAL
6248c2ecf20Sopenharmony_ci	int i, j, idx = 0;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (!mvm->tz_device.tzone)
6298c2ecf20Sopenharmony_ci		goto send;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	/* The driver holds array of temperature trips that are unsorted
6328c2ecf20Sopenharmony_ci	 * and uncompressed, the FW should get it compressed and sorted
6338c2ecf20Sopenharmony_ci	 */
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/* compress temp_trips to cmd array, remove uninitialized values*/
6368c2ecf20Sopenharmony_ci	for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
6378c2ecf20Sopenharmony_ci		if (mvm->tz_device.temp_trips[i] != S16_MIN) {
6388c2ecf20Sopenharmony_ci			cmd.thresholds[idx++] =
6398c2ecf20Sopenharmony_ci				cpu_to_le16(mvm->tz_device.temp_trips[i]);
6408c2ecf20Sopenharmony_ci		}
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci	cmd.num_temps = cpu_to_le32(idx);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	if (!idx)
6458c2ecf20Sopenharmony_ci		goto send;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	/*sort cmd array*/
6488c2ecf20Sopenharmony_ci	sort(cmd.thresholds, idx, sizeof(s16), compare_temps, NULL);
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	/* we should save the indexes of trips because we sort
6518c2ecf20Sopenharmony_ci	 * and compress the orginal array
6528c2ecf20Sopenharmony_ci	 */
6538c2ecf20Sopenharmony_ci	for (i = 0; i < idx; i++) {
6548c2ecf20Sopenharmony_ci		for (j = 0; j < IWL_MAX_DTS_TRIPS; j++) {
6558c2ecf20Sopenharmony_ci			if (le16_to_cpu(cmd.thresholds[i]) ==
6568c2ecf20Sopenharmony_ci				mvm->tz_device.temp_trips[j])
6578c2ecf20Sopenharmony_ci				mvm->tz_device.fw_trips_index[i] = j;
6588c2ecf20Sopenharmony_ci		}
6598c2ecf20Sopenharmony_ci	}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cisend:
6628c2ecf20Sopenharmony_ci#endif
6638c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP,
6648c2ecf20Sopenharmony_ci						TEMP_REPORTING_THRESHOLDS_CMD),
6658c2ecf20Sopenharmony_ci				   0, sizeof(cmd), &cmd);
6668c2ecf20Sopenharmony_ci	if (ret)
6678c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "TEMP_REPORT_THS_CMD command failed (err=%d)\n",
6688c2ecf20Sopenharmony_ci			ret);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	return ret;
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci#ifdef CONFIG_THERMAL
6748c2ecf20Sopenharmony_cistatic int iwl_mvm_tzone_get_temp(struct thermal_zone_device *device,
6758c2ecf20Sopenharmony_ci				  int *temperature)
6768c2ecf20Sopenharmony_ci{
6778c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
6788c2ecf20Sopenharmony_ci	int ret;
6798c2ecf20Sopenharmony_ci	int temp;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	if (!iwl_mvm_firmware_running(mvm) ||
6848c2ecf20Sopenharmony_ci	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) {
6858c2ecf20Sopenharmony_ci		ret = -ENODATA;
6868c2ecf20Sopenharmony_ci		goto out;
6878c2ecf20Sopenharmony_ci	}
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	ret = iwl_mvm_get_temp(mvm, &temp);
6908c2ecf20Sopenharmony_ci	if (ret)
6918c2ecf20Sopenharmony_ci		goto out;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	*temperature = temp * 1000;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ciout:
6968c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
6978c2ecf20Sopenharmony_ci	return ret;
6988c2ecf20Sopenharmony_ci}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_cistatic int iwl_mvm_tzone_get_trip_temp(struct thermal_zone_device *device,
7018c2ecf20Sopenharmony_ci				       int trip, int *temp)
7028c2ecf20Sopenharmony_ci{
7038c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
7068c2ecf20Sopenharmony_ci		return -EINVAL;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	*temp = mvm->tz_device.temp_trips[trip] * 1000;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	return 0;
7118c2ecf20Sopenharmony_ci}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_cistatic int iwl_mvm_tzone_get_trip_type(struct thermal_zone_device *device,
7148c2ecf20Sopenharmony_ci				       int trip, enum thermal_trip_type *type)
7158c2ecf20Sopenharmony_ci{
7168c2ecf20Sopenharmony_ci	if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
7178c2ecf20Sopenharmony_ci		return -EINVAL;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	*type = THERMAL_TRIP_PASSIVE;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	return 0;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device *device,
7258c2ecf20Sopenharmony_ci				       int trip, int temp)
7268c2ecf20Sopenharmony_ci{
7278c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
7288c2ecf20Sopenharmony_ci	struct iwl_mvm_thermal_device *tzone;
7298c2ecf20Sopenharmony_ci	int i, ret;
7308c2ecf20Sopenharmony_ci	s16 temperature;
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	if (!iwl_mvm_firmware_running(mvm) ||
7358c2ecf20Sopenharmony_ci	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) {
7368c2ecf20Sopenharmony_ci		ret = -EIO;
7378c2ecf20Sopenharmony_ci		goto out;
7388c2ecf20Sopenharmony_ci	}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) {
7418c2ecf20Sopenharmony_ci		ret = -EINVAL;
7428c2ecf20Sopenharmony_ci		goto out;
7438c2ecf20Sopenharmony_ci	}
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	if ((temp / 1000) > S16_MAX) {
7468c2ecf20Sopenharmony_ci		ret = -EINVAL;
7478c2ecf20Sopenharmony_ci		goto out;
7488c2ecf20Sopenharmony_ci	}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	temperature = (s16)(temp / 1000);
7518c2ecf20Sopenharmony_ci	tzone = &mvm->tz_device;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	if (!tzone) {
7548c2ecf20Sopenharmony_ci		ret = -EIO;
7558c2ecf20Sopenharmony_ci		goto out;
7568c2ecf20Sopenharmony_ci	}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	/* no updates*/
7598c2ecf20Sopenharmony_ci	if (tzone->temp_trips[trip] == temperature) {
7608c2ecf20Sopenharmony_ci		ret = 0;
7618c2ecf20Sopenharmony_ci		goto out;
7628c2ecf20Sopenharmony_ci	}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	/* already existing temperature */
7658c2ecf20Sopenharmony_ci	for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
7668c2ecf20Sopenharmony_ci		if (tzone->temp_trips[i] == temperature) {
7678c2ecf20Sopenharmony_ci			ret = -EINVAL;
7688c2ecf20Sopenharmony_ci			goto out;
7698c2ecf20Sopenharmony_ci		}
7708c2ecf20Sopenharmony_ci	}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	tzone->temp_trips[trip] = temperature;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_temp_report_ths_cmd(mvm);
7758c2ecf20Sopenharmony_ciout:
7768c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
7778c2ecf20Sopenharmony_ci	return ret;
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_cistatic  struct thermal_zone_device_ops tzone_ops = {
7818c2ecf20Sopenharmony_ci	.get_temp = iwl_mvm_tzone_get_temp,
7828c2ecf20Sopenharmony_ci	.get_trip_temp = iwl_mvm_tzone_get_trip_temp,
7838c2ecf20Sopenharmony_ci	.get_trip_type = iwl_mvm_tzone_get_trip_type,
7848c2ecf20Sopenharmony_ci	.set_trip_temp = iwl_mvm_tzone_set_trip_temp,
7858c2ecf20Sopenharmony_ci};
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci/* make all trips writable */
7888c2ecf20Sopenharmony_ci#define IWL_WRITABLE_TRIPS_MSK (BIT(IWL_MAX_DTS_TRIPS) - 1)
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_cistatic void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm)
7918c2ecf20Sopenharmony_ci{
7928c2ecf20Sopenharmony_ci	int i, ret;
7938c2ecf20Sopenharmony_ci	char name[16];
7948c2ecf20Sopenharmony_ci	static atomic_t counter = ATOMIC_INIT(0);
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	if (!iwl_mvm_is_tt_in_fw(mvm)) {
7978c2ecf20Sopenharmony_ci		mvm->tz_device.tzone = NULL;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci		return;
8008c2ecf20Sopenharmony_ci	}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	sprintf(name, "iwlwifi_%u", atomic_inc_return(&counter) & 0xFF);
8058c2ecf20Sopenharmony_ci	mvm->tz_device.tzone = thermal_zone_device_register(name,
8068c2ecf20Sopenharmony_ci							IWL_MAX_DTS_TRIPS,
8078c2ecf20Sopenharmony_ci							IWL_WRITABLE_TRIPS_MSK,
8088c2ecf20Sopenharmony_ci							mvm, &tzone_ops,
8098c2ecf20Sopenharmony_ci							NULL, 0, 0);
8108c2ecf20Sopenharmony_ci	if (IS_ERR(mvm->tz_device.tzone)) {
8118c2ecf20Sopenharmony_ci		IWL_DEBUG_TEMP(mvm,
8128c2ecf20Sopenharmony_ci			       "Failed to register to thermal zone (err = %ld)\n",
8138c2ecf20Sopenharmony_ci			       PTR_ERR(mvm->tz_device.tzone));
8148c2ecf20Sopenharmony_ci		mvm->tz_device.tzone = NULL;
8158c2ecf20Sopenharmony_ci		return;
8168c2ecf20Sopenharmony_ci	}
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	ret = thermal_zone_device_enable(mvm->tz_device.tzone);
8198c2ecf20Sopenharmony_ci	if (ret) {
8208c2ecf20Sopenharmony_ci		IWL_DEBUG_TEMP(mvm, "Failed to enable thermal zone\n");
8218c2ecf20Sopenharmony_ci		thermal_zone_device_unregister(mvm->tz_device.tzone);
8228c2ecf20Sopenharmony_ci		return;
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	/* 0 is a valid temperature,
8268c2ecf20Sopenharmony_ci	 * so initialize the array with S16_MIN which invalid temperature
8278c2ecf20Sopenharmony_ci	 */
8288c2ecf20Sopenharmony_ci	for (i = 0 ; i < IWL_MAX_DTS_TRIPS; i++)
8298c2ecf20Sopenharmony_ci		mvm->tz_device.temp_trips[i] = S16_MIN;
8308c2ecf20Sopenharmony_ci}
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_cistatic int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device *cdev,
8338c2ecf20Sopenharmony_ci				       unsigned long *state)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	*state = ARRAY_SIZE(iwl_mvm_cdev_budgets) - 1;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	return 0;
8388c2ecf20Sopenharmony_ci}
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_cistatic int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device *cdev,
8418c2ecf20Sopenharmony_ci				       unsigned long *state)
8428c2ecf20Sopenharmony_ci{
8438c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	*state = mvm->cooling_dev.cur_state;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	return 0;
8488c2ecf20Sopenharmony_ci}
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_cistatic int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device *cdev,
8518c2ecf20Sopenharmony_ci				       unsigned long new_state)
8528c2ecf20Sopenharmony_ci{
8538c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
8548c2ecf20Sopenharmony_ci	int ret;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	if (!iwl_mvm_firmware_running(mvm) ||
8598c2ecf20Sopenharmony_ci	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) {
8608c2ecf20Sopenharmony_ci		ret = -EIO;
8618c2ecf20Sopenharmony_ci		goto unlock;
8628c2ecf20Sopenharmony_ci	}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	if (new_state >= ARRAY_SIZE(iwl_mvm_cdev_budgets)) {
8658c2ecf20Sopenharmony_ci		ret = -EINVAL;
8668c2ecf20Sopenharmony_ci		goto unlock;
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
8708c2ecf20Sopenharmony_ci				   new_state);
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ciunlock:
8738c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
8748c2ecf20Sopenharmony_ci	return ret;
8758c2ecf20Sopenharmony_ci}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_cistatic const struct thermal_cooling_device_ops tcooling_ops = {
8788c2ecf20Sopenharmony_ci	.get_max_state = iwl_mvm_tcool_get_max_state,
8798c2ecf20Sopenharmony_ci	.get_cur_state = iwl_mvm_tcool_get_cur_state,
8808c2ecf20Sopenharmony_ci	.set_cur_state = iwl_mvm_tcool_set_cur_state,
8818c2ecf20Sopenharmony_ci};
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_cistatic void iwl_mvm_cooling_device_register(struct iwl_mvm *mvm)
8848c2ecf20Sopenharmony_ci{
8858c2ecf20Sopenharmony_ci	char name[] = "iwlwifi";
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	if (!iwl_mvm_is_ctdp_supported(mvm))
8888c2ecf20Sopenharmony_ci		return;
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	mvm->cooling_dev.cdev =
8938c2ecf20Sopenharmony_ci		thermal_cooling_device_register(name,
8948c2ecf20Sopenharmony_ci						mvm,
8958c2ecf20Sopenharmony_ci						&tcooling_ops);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	if (IS_ERR(mvm->cooling_dev.cdev)) {
8988c2ecf20Sopenharmony_ci		IWL_DEBUG_TEMP(mvm,
8998c2ecf20Sopenharmony_ci			       "Failed to register to cooling device (err = %ld)\n",
9008c2ecf20Sopenharmony_ci			       PTR_ERR(mvm->cooling_dev.cdev));
9018c2ecf20Sopenharmony_ci		mvm->cooling_dev.cdev = NULL;
9028c2ecf20Sopenharmony_ci		return;
9038c2ecf20Sopenharmony_ci	}
9048c2ecf20Sopenharmony_ci}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_cistatic void iwl_mvm_thermal_zone_unregister(struct iwl_mvm *mvm)
9078c2ecf20Sopenharmony_ci{
9088c2ecf20Sopenharmony_ci	if (!iwl_mvm_is_tt_in_fw(mvm) || !mvm->tz_device.tzone)
9098c2ecf20Sopenharmony_ci		return;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm, "Thermal zone device unregister\n");
9128c2ecf20Sopenharmony_ci	if (mvm->tz_device.tzone) {
9138c2ecf20Sopenharmony_ci		thermal_zone_device_unregister(mvm->tz_device.tzone);
9148c2ecf20Sopenharmony_ci		mvm->tz_device.tzone = NULL;
9158c2ecf20Sopenharmony_ci	}
9168c2ecf20Sopenharmony_ci}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_cistatic void iwl_mvm_cooling_device_unregister(struct iwl_mvm *mvm)
9198c2ecf20Sopenharmony_ci{
9208c2ecf20Sopenharmony_ci	if (!iwl_mvm_is_ctdp_supported(mvm) || !mvm->cooling_dev.cdev)
9218c2ecf20Sopenharmony_ci		return;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm, "Cooling device unregister\n");
9248c2ecf20Sopenharmony_ci	if (mvm->cooling_dev.cdev) {
9258c2ecf20Sopenharmony_ci		thermal_cooling_device_unregister(mvm->cooling_dev.cdev);
9268c2ecf20Sopenharmony_ci		mvm->cooling_dev.cdev = NULL;
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci}
9298c2ecf20Sopenharmony_ci#endif /* CONFIG_THERMAL */
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_civoid iwl_mvm_thermal_initialize(struct iwl_mvm *mvm, u32 min_backoff)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	if (mvm->cfg->thermal_params)
9388c2ecf20Sopenharmony_ci		tt->params = *mvm->cfg->thermal_params;
9398c2ecf20Sopenharmony_ci	else
9408c2ecf20Sopenharmony_ci		tt->params = iwl_mvm_default_tt_params;
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	tt->throttle = false;
9438c2ecf20Sopenharmony_ci	tt->dynamic_smps = false;
9448c2ecf20Sopenharmony_ci	tt->min_backoff = min_backoff;
9458c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci#ifdef CONFIG_THERMAL
9488c2ecf20Sopenharmony_ci	iwl_mvm_cooling_device_register(mvm);
9498c2ecf20Sopenharmony_ci	iwl_mvm_thermal_zone_register(mvm);
9508c2ecf20Sopenharmony_ci#endif
9518c2ecf20Sopenharmony_ci	mvm->init_status |= IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE;
9528c2ecf20Sopenharmony_ci}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_civoid iwl_mvm_thermal_exit(struct iwl_mvm *mvm)
9558c2ecf20Sopenharmony_ci{
9568c2ecf20Sopenharmony_ci	if (!(mvm->init_status & IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE))
9578c2ecf20Sopenharmony_ci		return;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
9608c2ecf20Sopenharmony_ci	IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci#ifdef CONFIG_THERMAL
9638c2ecf20Sopenharmony_ci	iwl_mvm_cooling_device_unregister(mvm);
9648c2ecf20Sopenharmony_ci	iwl_mvm_thermal_zone_unregister(mvm);
9658c2ecf20Sopenharmony_ci#endif
9668c2ecf20Sopenharmony_ci	mvm->init_status &= ~IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE;
9678c2ecf20Sopenharmony_ci}
968