• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
iwl_mvm_enter_ctkill(struct iwl_mvm * mvm)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 
iwl_mvm_exit_ctkill(struct iwl_mvm * mvm)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 
iwl_mvm_tt_temp_changed(struct iwl_mvm * mvm,u32 temp)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 
iwl_mvm_temp_notif_parse(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)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 
iwl_mvm_temp_notif_wait(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)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 
iwl_mvm_temp_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)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 
iwl_mvm_ct_kill_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)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  */
iwl_mvm_send_temp_cmd(struct iwl_mvm * mvm,bool response,s32 * temp)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 
iwl_mvm_get_temp(struct iwl_mvm * mvm,s32 * temp)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 
check_exit_ctkill(struct work_struct * work)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 	flush_work(&mvm->roc_done_wk);
353 
354 	mutex_lock(&mvm->mutex);
355 
356 	if (__iwl_mvm_mac_start(mvm))
357 		goto reschedule;
358 
359 	ret = iwl_mvm_get_temp(mvm, &temp);
360 
361 	__iwl_mvm_mac_stop(mvm);
362 
363 	if (ret)
364 		goto reschedule;
365 
366 	IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);
367 
368 	if (temp <= tt->params.ct_kill_exit) {
369 		mutex_unlock(&mvm->mutex);
370 		iwl_mvm_exit_ctkill(mvm);
371 		return;
372 	}
373 
374 reschedule:
375 	mutex_unlock(&mvm->mutex);
376 	schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
377 			      round_jiffies(duration * HZ));
378 }
379 
iwl_mvm_tt_smps_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)380 static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
381 				     struct ieee80211_vif *vif)
382 {
383 	struct iwl_mvm *mvm = _data;
384 	enum ieee80211_smps_mode smps_mode;
385 
386 	lockdep_assert_held(&mvm->mutex);
387 
388 	if (mvm->thermal_throttle.dynamic_smps)
389 		smps_mode = IEEE80211_SMPS_DYNAMIC;
390 	else
391 		smps_mode = IEEE80211_SMPS_AUTOMATIC;
392 
393 	if (vif->type != NL80211_IFTYPE_STATION)
394 		return;
395 
396 	iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
397 }
398 
iwl_mvm_tt_tx_protection(struct iwl_mvm * mvm,bool enable)399 static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
400 {
401 	struct iwl_mvm_sta *mvmsta;
402 	int i, err;
403 
404 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
405 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, i);
406 		if (!mvmsta)
407 			continue;
408 
409 		if (enable == mvmsta->tt_tx_protection)
410 			continue;
411 		err = iwl_mvm_tx_protection(mvm, mvmsta, enable);
412 		if (err) {
413 			IWL_ERR(mvm, "Failed to %s Tx protection\n",
414 				enable ? "enable" : "disable");
415 		} else {
416 			IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
417 				       enable ? "Enable" : "Disable");
418 			mvmsta->tt_tx_protection = enable;
419 		}
420 	}
421 }
422 
iwl_mvm_tt_tx_backoff(struct iwl_mvm * mvm,u32 backoff)423 void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
424 {
425 	struct iwl_host_cmd cmd = {
426 		.id = REPLY_THERMAL_MNG_BACKOFF,
427 		.len = { sizeof(u32), },
428 		.data = { &backoff, },
429 	};
430 
431 	backoff = max(backoff, mvm->thermal_throttle.min_backoff);
432 
433 	if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
434 		IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
435 			       backoff);
436 		mvm->thermal_throttle.tx_backoff = backoff;
437 	} else {
438 		IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
439 	}
440 }
441 
iwl_mvm_tt_handler(struct iwl_mvm * mvm)442 void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
443 {
444 	struct iwl_tt_params *params = &mvm->thermal_throttle.params;
445 	struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
446 	s32 temperature = mvm->temperature;
447 	bool throttle_enable = false;
448 	int i;
449 	u32 tx_backoff;
450 
451 	IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);
452 
453 	if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
454 		iwl_mvm_enter_ctkill(mvm);
455 		return;
456 	}
457 
458 	if (params->support_ct_kill &&
459 	    temperature <= params->ct_kill_exit) {
460 		iwl_mvm_exit_ctkill(mvm);
461 		return;
462 	}
463 
464 	if (params->support_dynamic_smps) {
465 		if (!tt->dynamic_smps &&
466 		    temperature >= params->dynamic_smps_entry) {
467 			IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
468 			tt->dynamic_smps = true;
469 			ieee80211_iterate_active_interfaces_atomic(
470 					mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
471 					iwl_mvm_tt_smps_iterator, mvm);
472 			throttle_enable = true;
473 		} else if (tt->dynamic_smps &&
474 			   temperature <= params->dynamic_smps_exit) {
475 			IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
476 			tt->dynamic_smps = false;
477 			ieee80211_iterate_active_interfaces_atomic(
478 					mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
479 					iwl_mvm_tt_smps_iterator, mvm);
480 		}
481 	}
482 
483 	if (params->support_tx_protection) {
484 		if (temperature >= params->tx_protection_entry) {
485 			iwl_mvm_tt_tx_protection(mvm, true);
486 			throttle_enable = true;
487 		} else if (temperature <= params->tx_protection_exit) {
488 			iwl_mvm_tt_tx_protection(mvm, false);
489 		}
490 	}
491 
492 	if (params->support_tx_backoff) {
493 		tx_backoff = tt->min_backoff;
494 		for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
495 			if (temperature < params->tx_backoff[i].temperature)
496 				break;
497 			tx_backoff = max(tt->min_backoff,
498 					 params->tx_backoff[i].backoff);
499 		}
500 		if (tx_backoff != tt->min_backoff)
501 			throttle_enable = true;
502 		if (tt->tx_backoff != tx_backoff)
503 			iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
504 	}
505 
506 	if (!tt->throttle && throttle_enable) {
507 		IWL_WARN(mvm,
508 			 "Due to high temperature thermal throttling initiated\n");
509 		tt->throttle = true;
510 	} else if (tt->throttle && !tt->dynamic_smps &&
511 		   tt->tx_backoff == tt->min_backoff &&
512 		   temperature <= params->tx_protection_exit) {
513 		IWL_WARN(mvm,
514 			 "Temperature is back to normal thermal throttling stopped\n");
515 		tt->throttle = false;
516 	}
517 }
518 
519 static const struct iwl_tt_params iwl_mvm_default_tt_params = {
520 	.ct_kill_entry = 118,
521 	.ct_kill_exit = 96,
522 	.ct_kill_duration = 5,
523 	.dynamic_smps_entry = 114,
524 	.dynamic_smps_exit = 110,
525 	.tx_protection_entry = 114,
526 	.tx_protection_exit = 108,
527 	.tx_backoff = {
528 		{.temperature = 112, .backoff = 200},
529 		{.temperature = 113, .backoff = 600},
530 		{.temperature = 114, .backoff = 1200},
531 		{.temperature = 115, .backoff = 2000},
532 		{.temperature = 116, .backoff = 4000},
533 		{.temperature = 117, .backoff = 10000},
534 	},
535 	.support_ct_kill = true,
536 	.support_dynamic_smps = true,
537 	.support_tx_protection = true,
538 	.support_tx_backoff = true,
539 };
540 
541 /* budget in mWatt */
542 static const u32 iwl_mvm_cdev_budgets[] = {
543 	2400,	/* cooling state 0 */
544 	2000,	/* cooling state 1 */
545 	1800,	/* cooling state 2 */
546 	1600,	/* cooling state 3 */
547 	1400,	/* cooling state 4 */
548 	1200,	/* cooling state 5 */
549 	1000,	/* cooling state 6 */
550 	900,	/* cooling state 7 */
551 	800,	/* cooling state 8 */
552 	700,	/* cooling state 9 */
553 	650,	/* cooling state 10 */
554 	600,	/* cooling state 11 */
555 	550,	/* cooling state 12 */
556 	500,	/* cooling state 13 */
557 	450,	/* cooling state 14 */
558 	400,	/* cooling state 15 */
559 	350,	/* cooling state 16 */
560 	300,	/* cooling state 17 */
561 	250,	/* cooling state 18 */
562 	200,	/* cooling state 19 */
563 	150,	/* cooling state 20 */
564 };
565 
iwl_mvm_ctdp_command(struct iwl_mvm * mvm,u32 op,u32 state)566 int iwl_mvm_ctdp_command(struct iwl_mvm *mvm, u32 op, u32 state)
567 {
568 	struct iwl_mvm_ctdp_cmd cmd = {
569 		.operation = cpu_to_le32(op),
570 		.budget = cpu_to_le32(iwl_mvm_cdev_budgets[state]),
571 		.window_size = 0,
572 	};
573 	int ret;
574 	u32 status;
575 
576 	lockdep_assert_held(&mvm->mutex);
577 
578 	status = 0;
579 	ret = iwl_mvm_send_cmd_pdu_status(mvm, WIDE_ID(PHY_OPS_GROUP,
580 						       CTDP_CONFIG_CMD),
581 					  sizeof(cmd), &cmd, &status);
582 
583 	if (ret) {
584 		IWL_ERR(mvm, "cTDP command failed (err=%d)\n", ret);
585 		return ret;
586 	}
587 
588 	switch (op) {
589 	case CTDP_CMD_OPERATION_START:
590 #ifdef CONFIG_THERMAL
591 		mvm->cooling_dev.cur_state = state;
592 #endif /* CONFIG_THERMAL */
593 		break;
594 	case CTDP_CMD_OPERATION_REPORT:
595 		IWL_DEBUG_TEMP(mvm, "cTDP avg energy in mWatt = %d\n", status);
596 		/* when the function is called with CTDP_CMD_OPERATION_REPORT
597 		 * option the function should return the average budget value
598 		 * that is received from the FW.
599 		 * The budget can't be less or equal to 0, so it's possible
600 		 * to distinguish between error values and budgets.
601 		 */
602 		return status;
603 	case CTDP_CMD_OPERATION_STOP:
604 		IWL_DEBUG_TEMP(mvm, "cTDP stopped successfully\n");
605 		break;
606 	}
607 
608 	return 0;
609 }
610 
611 #ifdef CONFIG_THERMAL
compare_temps(const void * a,const void * b)612 static int compare_temps(const void *a, const void *b)
613 {
614 	return ((s16)le16_to_cpu(*(__le16 *)a) -
615 		(s16)le16_to_cpu(*(__le16 *)b));
616 }
617 #endif
618 
iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm * mvm)619 int iwl_mvm_send_temp_report_ths_cmd(struct iwl_mvm *mvm)
620 {
621 	struct temp_report_ths_cmd cmd = {0};
622 	int ret;
623 #ifdef CONFIG_THERMAL
624 	int i, j, idx = 0;
625 
626 	lockdep_assert_held(&mvm->mutex);
627 
628 	if (!mvm->tz_device.tzone)
629 		goto send;
630 
631 	/* The driver holds array of temperature trips that are unsorted
632 	 * and uncompressed, the FW should get it compressed and sorted
633 	 */
634 
635 	/* compress temp_trips to cmd array, remove uninitialized values*/
636 	for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
637 		if (mvm->tz_device.temp_trips[i] != S16_MIN) {
638 			cmd.thresholds[idx++] =
639 				cpu_to_le16(mvm->tz_device.temp_trips[i]);
640 		}
641 	}
642 	cmd.num_temps = cpu_to_le32(idx);
643 
644 	if (!idx)
645 		goto send;
646 
647 	/*sort cmd array*/
648 	sort(cmd.thresholds, idx, sizeof(s16), compare_temps, NULL);
649 
650 	/* we should save the indexes of trips because we sort
651 	 * and compress the orginal array
652 	 */
653 	for (i = 0; i < idx; i++) {
654 		for (j = 0; j < IWL_MAX_DTS_TRIPS; j++) {
655 			if (le16_to_cpu(cmd.thresholds[i]) ==
656 				mvm->tz_device.temp_trips[j])
657 				mvm->tz_device.fw_trips_index[i] = j;
658 		}
659 	}
660 
661 send:
662 #endif
663 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(PHY_OPS_GROUP,
664 						TEMP_REPORTING_THRESHOLDS_CMD),
665 				   0, sizeof(cmd), &cmd);
666 	if (ret)
667 		IWL_ERR(mvm, "TEMP_REPORT_THS_CMD command failed (err=%d)\n",
668 			ret);
669 
670 	return ret;
671 }
672 
673 #ifdef CONFIG_THERMAL
iwl_mvm_tzone_get_temp(struct thermal_zone_device * device,int * temperature)674 static int iwl_mvm_tzone_get_temp(struct thermal_zone_device *device,
675 				  int *temperature)
676 {
677 	struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
678 	int ret;
679 	int temp;
680 
681 	mutex_lock(&mvm->mutex);
682 
683 	if (!iwl_mvm_firmware_running(mvm) ||
684 	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) {
685 		ret = -ENODATA;
686 		goto out;
687 	}
688 
689 	ret = iwl_mvm_get_temp(mvm, &temp);
690 	if (ret)
691 		goto out;
692 
693 	*temperature = temp * 1000;
694 
695 out:
696 	mutex_unlock(&mvm->mutex);
697 	return ret;
698 }
699 
iwl_mvm_tzone_get_trip_temp(struct thermal_zone_device * device,int trip,int * temp)700 static int iwl_mvm_tzone_get_trip_temp(struct thermal_zone_device *device,
701 				       int trip, int *temp)
702 {
703 	struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
704 
705 	if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
706 		return -EINVAL;
707 
708 	*temp = mvm->tz_device.temp_trips[trip] * 1000;
709 
710 	return 0;
711 }
712 
iwl_mvm_tzone_get_trip_type(struct thermal_zone_device * device,int trip,enum thermal_trip_type * type)713 static int iwl_mvm_tzone_get_trip_type(struct thermal_zone_device *device,
714 				       int trip, enum thermal_trip_type *type)
715 {
716 	if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS)
717 		return -EINVAL;
718 
719 	*type = THERMAL_TRIP_PASSIVE;
720 
721 	return 0;
722 }
723 
iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device * device,int trip,int temp)724 static int iwl_mvm_tzone_set_trip_temp(struct thermal_zone_device *device,
725 				       int trip, int temp)
726 {
727 	struct iwl_mvm *mvm = (struct iwl_mvm *)device->devdata;
728 	struct iwl_mvm_thermal_device *tzone;
729 	int i, ret;
730 	s16 temperature;
731 
732 	mutex_lock(&mvm->mutex);
733 
734 	if (!iwl_mvm_firmware_running(mvm) ||
735 	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) {
736 		ret = -EIO;
737 		goto out;
738 	}
739 
740 	if (trip < 0 || trip >= IWL_MAX_DTS_TRIPS) {
741 		ret = -EINVAL;
742 		goto out;
743 	}
744 
745 	if ((temp / 1000) > S16_MAX) {
746 		ret = -EINVAL;
747 		goto out;
748 	}
749 
750 	temperature = (s16)(temp / 1000);
751 	tzone = &mvm->tz_device;
752 
753 	if (!tzone) {
754 		ret = -EIO;
755 		goto out;
756 	}
757 
758 	/* no updates*/
759 	if (tzone->temp_trips[trip] == temperature) {
760 		ret = 0;
761 		goto out;
762 	}
763 
764 	/* already existing temperature */
765 	for (i = 0; i < IWL_MAX_DTS_TRIPS; i++) {
766 		if (tzone->temp_trips[i] == temperature) {
767 			ret = -EINVAL;
768 			goto out;
769 		}
770 	}
771 
772 	tzone->temp_trips[trip] = temperature;
773 
774 	ret = iwl_mvm_send_temp_report_ths_cmd(mvm);
775 out:
776 	mutex_unlock(&mvm->mutex);
777 	return ret;
778 }
779 
780 static  struct thermal_zone_device_ops tzone_ops = {
781 	.get_temp = iwl_mvm_tzone_get_temp,
782 	.get_trip_temp = iwl_mvm_tzone_get_trip_temp,
783 	.get_trip_type = iwl_mvm_tzone_get_trip_type,
784 	.set_trip_temp = iwl_mvm_tzone_set_trip_temp,
785 };
786 
787 /* make all trips writable */
788 #define IWL_WRITABLE_TRIPS_MSK (BIT(IWL_MAX_DTS_TRIPS) - 1)
789 
iwl_mvm_thermal_zone_register(struct iwl_mvm * mvm)790 static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm)
791 {
792 	int i, ret;
793 	char name[16];
794 	static atomic_t counter = ATOMIC_INIT(0);
795 
796 	if (!iwl_mvm_is_tt_in_fw(mvm)) {
797 		mvm->tz_device.tzone = NULL;
798 
799 		return;
800 	}
801 
802 	BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
803 
804 	sprintf(name, "iwlwifi_%u", atomic_inc_return(&counter) & 0xFF);
805 	mvm->tz_device.tzone = thermal_zone_device_register(name,
806 							IWL_MAX_DTS_TRIPS,
807 							IWL_WRITABLE_TRIPS_MSK,
808 							mvm, &tzone_ops,
809 							NULL, 0, 0);
810 	if (IS_ERR(mvm->tz_device.tzone)) {
811 		IWL_DEBUG_TEMP(mvm,
812 			       "Failed to register to thermal zone (err = %ld)\n",
813 			       PTR_ERR(mvm->tz_device.tzone));
814 		mvm->tz_device.tzone = NULL;
815 		return;
816 	}
817 
818 	ret = thermal_zone_device_enable(mvm->tz_device.tzone);
819 	if (ret) {
820 		IWL_DEBUG_TEMP(mvm, "Failed to enable thermal zone\n");
821 		thermal_zone_device_unregister(mvm->tz_device.tzone);
822 		return;
823 	}
824 
825 	/* 0 is a valid temperature,
826 	 * so initialize the array with S16_MIN which invalid temperature
827 	 */
828 	for (i = 0 ; i < IWL_MAX_DTS_TRIPS; i++)
829 		mvm->tz_device.temp_trips[i] = S16_MIN;
830 }
831 
iwl_mvm_tcool_get_max_state(struct thermal_cooling_device * cdev,unsigned long * state)832 static int iwl_mvm_tcool_get_max_state(struct thermal_cooling_device *cdev,
833 				       unsigned long *state)
834 {
835 	*state = ARRAY_SIZE(iwl_mvm_cdev_budgets) - 1;
836 
837 	return 0;
838 }
839 
iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device * cdev,unsigned long * state)840 static int iwl_mvm_tcool_get_cur_state(struct thermal_cooling_device *cdev,
841 				       unsigned long *state)
842 {
843 	struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
844 
845 	*state = mvm->cooling_dev.cur_state;
846 
847 	return 0;
848 }
849 
iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device * cdev,unsigned long new_state)850 static int iwl_mvm_tcool_set_cur_state(struct thermal_cooling_device *cdev,
851 				       unsigned long new_state)
852 {
853 	struct iwl_mvm *mvm = (struct iwl_mvm *)(cdev->devdata);
854 	int ret;
855 
856 	mutex_lock(&mvm->mutex);
857 
858 	if (!iwl_mvm_firmware_running(mvm) ||
859 	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR) {
860 		ret = -EIO;
861 		goto unlock;
862 	}
863 
864 	if (new_state >= ARRAY_SIZE(iwl_mvm_cdev_budgets)) {
865 		ret = -EINVAL;
866 		goto unlock;
867 	}
868 
869 	ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_START,
870 				   new_state);
871 
872 unlock:
873 	mutex_unlock(&mvm->mutex);
874 	return ret;
875 }
876 
877 static const struct thermal_cooling_device_ops tcooling_ops = {
878 	.get_max_state = iwl_mvm_tcool_get_max_state,
879 	.get_cur_state = iwl_mvm_tcool_get_cur_state,
880 	.set_cur_state = iwl_mvm_tcool_set_cur_state,
881 };
882 
iwl_mvm_cooling_device_register(struct iwl_mvm * mvm)883 static void iwl_mvm_cooling_device_register(struct iwl_mvm *mvm)
884 {
885 	char name[] = "iwlwifi";
886 
887 	if (!iwl_mvm_is_ctdp_supported(mvm))
888 		return;
889 
890 	BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
891 
892 	mvm->cooling_dev.cdev =
893 		thermal_cooling_device_register(name,
894 						mvm,
895 						&tcooling_ops);
896 
897 	if (IS_ERR(mvm->cooling_dev.cdev)) {
898 		IWL_DEBUG_TEMP(mvm,
899 			       "Failed to register to cooling device (err = %ld)\n",
900 			       PTR_ERR(mvm->cooling_dev.cdev));
901 		mvm->cooling_dev.cdev = NULL;
902 		return;
903 	}
904 }
905 
iwl_mvm_thermal_zone_unregister(struct iwl_mvm * mvm)906 static void iwl_mvm_thermal_zone_unregister(struct iwl_mvm *mvm)
907 {
908 	if (!iwl_mvm_is_tt_in_fw(mvm) || !mvm->tz_device.tzone)
909 		return;
910 
911 	IWL_DEBUG_TEMP(mvm, "Thermal zone device unregister\n");
912 	if (mvm->tz_device.tzone) {
913 		thermal_zone_device_unregister(mvm->tz_device.tzone);
914 		mvm->tz_device.tzone = NULL;
915 	}
916 }
917 
iwl_mvm_cooling_device_unregister(struct iwl_mvm * mvm)918 static void iwl_mvm_cooling_device_unregister(struct iwl_mvm *mvm)
919 {
920 	if (!iwl_mvm_is_ctdp_supported(mvm) || !mvm->cooling_dev.cdev)
921 		return;
922 
923 	IWL_DEBUG_TEMP(mvm, "Cooling device unregister\n");
924 	if (mvm->cooling_dev.cdev) {
925 		thermal_cooling_device_unregister(mvm->cooling_dev.cdev);
926 		mvm->cooling_dev.cdev = NULL;
927 	}
928 }
929 #endif /* CONFIG_THERMAL */
930 
iwl_mvm_thermal_initialize(struct iwl_mvm * mvm,u32 min_backoff)931 void iwl_mvm_thermal_initialize(struct iwl_mvm *mvm, u32 min_backoff)
932 {
933 	struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
934 
935 	IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
936 
937 	if (mvm->cfg->thermal_params)
938 		tt->params = *mvm->cfg->thermal_params;
939 	else
940 		tt->params = iwl_mvm_default_tt_params;
941 
942 	tt->throttle = false;
943 	tt->dynamic_smps = false;
944 	tt->min_backoff = min_backoff;
945 	INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
946 
947 #ifdef CONFIG_THERMAL
948 	iwl_mvm_cooling_device_register(mvm);
949 	iwl_mvm_thermal_zone_register(mvm);
950 #endif
951 	mvm->init_status |= IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE;
952 }
953 
iwl_mvm_thermal_exit(struct iwl_mvm * mvm)954 void iwl_mvm_thermal_exit(struct iwl_mvm *mvm)
955 {
956 	if (!(mvm->init_status & IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE))
957 		return;
958 
959 	cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
960 	IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
961 
962 #ifdef CONFIG_THERMAL
963 	iwl_mvm_cooling_device_unregister(mvm);
964 	iwl_mvm_thermal_zone_unregister(mvm);
965 #endif
966 	mvm->init_status &= ~IWL_MVM_INIT_STATUS_THERMAL_INIT_COMPLETE;
967 }
968