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