tt.c 15.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
/******************************************************************************
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
 * Copyright(c) 2013 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
 * USA
 *
 * The full GNU General Public License is included in this distribution
 * in the file called COPYING.
 *
 * Contact Information:
 *  Intel Linux Wireless <ilw@linux.intel.com>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 * BSD LICENSE
 *
 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

#include "mvm.h"
#include "iwl-config.h"
#include "iwl-io.h"
#include "iwl-csr.h"
#include "iwl-prph.h"

#define OTP_DTS_DIODE_DEVIATION 96 /*in words*/
/* VBG - Voltage Band Gap error data (temperature offset) */
#define OTP_WP_DTS_VBG			(OTP_DTS_DIODE_DEVIATION + 2)
#define MEAS_VBG_MIN_VAL		2300
#define MEAS_VBG_MAX_VAL		3000
#define MEAS_VBG_DEFAULT_VAL		2700
#define DTS_DIODE_VALID(flags)		(flags & DTS_DIODE_REG_FLAGS_PASS_ONCE)
#define MIN_TEMPERATURE			0
#define MAX_TEMPERATURE			125
#define TEMPERATURE_ERROR		(MAX_TEMPERATURE + 1)
#define PTAT_DIGITAL_VALUE_MIN_VALUE	0
#define PTAT_DIGITAL_VALUE_MAX_VALUE	0xFF
#define DTS_VREFS_NUM			5
static inline u32 DTS_DIODE_GET_VREFS_ID(u32 flags)
{
	return (flags & DTS_DIODE_REG_FLAGS_VREFS_ID) >>
					DTS_DIODE_REG_FLAGS_VREFS_ID_POS;
}

#define CALC_VREFS_MIN_DIFF	43
#define CALC_VREFS_MAX_DIFF	51
#define CALC_LUT_SIZE		(1 + CALC_VREFS_MAX_DIFF - CALC_VREFS_MIN_DIFF)
#define CALC_LUT_INDEX_OFFSET	CALC_VREFS_MIN_DIFF
#define CALC_TEMPERATURE_RESULT_SHIFT_OFFSET	23

/*
 * @digital_value: The diode's digital-value sampled (temperature/voltage)
 * @vref_low: The lower voltage-reference (the vref just below the diode's
 *	sampled digital-value)
 * @vref_high: The higher voltage-reference (the vref just above the diode's
 *	sampled digital-value)
 * @flags: bits[1:0]: The ID of the Vrefs pair (lowVref,highVref)
 *	bits[6:2]: Reserved.
 *	bits[7:7]: Indicates completion of at least 1 successful sample
 *	since last DTS reset.
 */
struct iwl_mvm_dts_diode_bits {
	u8 digital_value;
	u8 vref_low;
	u8 vref_high;
	u8 flags;
} __packed;

union dts_diode_results {
	u32 reg_value;
	struct iwl_mvm_dts_diode_bits bits;
} __packed;

static s16 iwl_mvm_dts_get_volt_band_gap(struct iwl_mvm *mvm)
{
	struct iwl_nvm_section calib_sec;
	const __le16 *calib;
	u16 vbg;

	/* TODO: move parsing to NVM code */
	calib_sec = mvm->nvm_sections[NVM_SECTION_TYPE_CALIBRATION];
	calib = (__le16 *)calib_sec.data;

	vbg = le16_to_cpu(calib[OTP_WP_DTS_VBG]);

	if (vbg < MEAS_VBG_MIN_VAL || vbg > MEAS_VBG_MAX_VAL)
		vbg = MEAS_VBG_DEFAULT_VAL;

	return vbg;
}

static u16 iwl_mvm_dts_get_ptat_deviation_offset(struct iwl_mvm *mvm)
{
	const u8 *calib;
	u8 ptat, pa1, pa2, median;

	/* TODO: move parsing to NVM code */
	calib = mvm->nvm_sections[NVM_SECTION_TYPE_CALIBRATION].data;
	ptat = calib[OTP_DTS_DIODE_DEVIATION];
	pa1 = calib[OTP_DTS_DIODE_DEVIATION + 1];
	pa2 = calib[OTP_DTS_DIODE_DEVIATION + 2];

	/* get the median: */
	if (ptat > pa1) {
		if (ptat > pa2)
			median = (pa1 > pa2) ? pa1 : pa2;
		else
			median = ptat;
	} else {
		if (pa1 > pa2)
			median = (ptat > pa2) ? ptat : pa2;
		else
			median = pa1;
	}

	return ptat - median;
}

static u8 iwl_mvm_dts_calibrate_ptat_deviation(struct iwl_mvm *mvm, u8 value)
{
	/* Calibrate the PTAT digital value, based on PTAT deviation data: */
	s16 new_val = value - iwl_mvm_dts_get_ptat_deviation_offset(mvm);

	if (new_val > PTAT_DIGITAL_VALUE_MAX_VALUE)
		new_val = PTAT_DIGITAL_VALUE_MAX_VALUE;
	else if (new_val < PTAT_DIGITAL_VALUE_MIN_VALUE)
		new_val = PTAT_DIGITAL_VALUE_MIN_VALUE;

	return new_val;
}

static bool dts_get_adjacent_vrefs(struct iwl_mvm *mvm,
				   union dts_diode_results *avg_ptat)
{
	u8 vrefs_results[DTS_VREFS_NUM];
	u8 low_vref_index = 0, flags;
	u32 reg;

	reg = iwl_read_prph(mvm->trans, DTSC_VREF_AVG);
	memcpy(vrefs_results, &reg, sizeof(reg));
	reg = iwl_read_prph(mvm->trans, DTSC_VREF5_AVG);
	vrefs_results[4] = reg & 0xff;

	if (avg_ptat->bits.digital_value < vrefs_results[0] ||
	    avg_ptat->bits.digital_value > vrefs_results[4])
		return false;

	if (avg_ptat->bits.digital_value > vrefs_results[3])
		low_vref_index = 3;
	else if (avg_ptat->bits.digital_value > vrefs_results[2])
		low_vref_index = 2;
	else if (avg_ptat->bits.digital_value > vrefs_results[1])
		low_vref_index = 1;

	avg_ptat->bits.vref_low  = vrefs_results[low_vref_index];
	avg_ptat->bits.vref_high = vrefs_results[low_vref_index + 1];
	flags = avg_ptat->bits.flags;
	avg_ptat->bits.flags =
		(flags & ~DTS_DIODE_REG_FLAGS_VREFS_ID) |
		(low_vref_index & DTS_DIODE_REG_FLAGS_VREFS_ID);
	return true;
}

/*
 * return true it the results are valid, and false otherwise.
 */
static bool dts_read_ptat_avg_results(struct iwl_mvm *mvm,
				      union dts_diode_results *avg_ptat)
{
	u32 reg;
	u8 tmp;

	/* fill the diode value and pass_once with avg-reg results */
	reg = iwl_read_prph(mvm->trans, DTSC_PTAT_AVG);
	reg &= DTS_DIODE_REG_DIG_VAL | DTS_DIODE_REG_PASS_ONCE;
	avg_ptat->reg_value = reg;

	/* calibrate the PTAT digital value */
	tmp = avg_ptat->bits.digital_value;
	tmp = iwl_mvm_dts_calibrate_ptat_deviation(mvm, tmp);
	avg_ptat->bits.digital_value = tmp;

	/*
	 * fill vrefs fields, based on the avgVrefs results
	 * and the diode value
	 */
	return dts_get_adjacent_vrefs(mvm, avg_ptat) &&
		DTS_DIODE_VALID(avg_ptat->bits.flags);
}

static s32 calculate_nic_temperature(union dts_diode_results avg_ptat,
				     u16 volt_band_gap)
{
	u32 tmp_result;
	u8 vrefs_diff;
	/*
	 * For temperature calculation (at the end, shift right by 23)
	 * LUT[(D2-D1)] = ROUND{ 2^23 / ((D2-D1)*9*10) }
	 * (D2-D1) ==   43    44    45    46    47    48    49    50    51
	 */
	static const u16 calc_lut[CALC_LUT_SIZE] = {
		2168, 2118, 2071, 2026, 1983, 1942, 1902, 1864, 1828,
	};

	/*
	 * The diff between the high and low voltage-references is assumed
	 * to be strictly be in range of [60,68]
	 */
	vrefs_diff = avg_ptat.bits.vref_high - avg_ptat.bits.vref_low;

	if (vrefs_diff < CALC_VREFS_MIN_DIFF ||
	    vrefs_diff > CALC_VREFS_MAX_DIFF)
		return TEMPERATURE_ERROR;

	/* calculate the result: */
	tmp_result =
		vrefs_diff * (DTS_DIODE_GET_VREFS_ID(avg_ptat.bits.flags) + 9);
	tmp_result += avg_ptat.bits.digital_value;
	tmp_result -= avg_ptat.bits.vref_high;

	/* multiply by the LUT value (based on the diff) */
	tmp_result *= calc_lut[vrefs_diff - CALC_LUT_INDEX_OFFSET];

	/*
	 * Get the BandGap (the voltage refereces source) error data
	 * (temperature offset)
	 */
	tmp_result *= volt_band_gap;

	/*
	 * here, tmp_result value can be up to 32-bits. We want to right-shift
	 * it *without* sign-extend.
	 */
	tmp_result = tmp_result >> CALC_TEMPERATURE_RESULT_SHIFT_OFFSET;

	/*
	 * at this point, tmp_result should be in the range:
	 * 200 <= tmp_result <= 365
	 */
	return (s16)tmp_result - 240;
}

static s32 check_nic_temperature(struct iwl_mvm *mvm)
{
	u16 volt_band_gap;
	union dts_diode_results avg_ptat;

	volt_band_gap = iwl_mvm_dts_get_volt_band_gap(mvm);

	/* disable DTS */
	iwl_write_prph(mvm->trans, SHR_MISC_WFM_DTS_EN, 0);

	/* SV initialization */
	iwl_write_prph(mvm->trans, SHR_MISC_WFM_DTS_EN, 1);
	iwl_write_prph(mvm->trans, DTSC_CFG_MODE,
		       DTSC_CFG_MODE_PERIODIC);

	/* wait for results */
	msleep(100);
	if (!dts_read_ptat_avg_results(mvm, &avg_ptat))
		return TEMPERATURE_ERROR;

	/* disable DTS */
	iwl_write_prph(mvm->trans, SHR_MISC_WFM_DTS_EN, 0);

	return calculate_nic_temperature(avg_ptat, volt_band_gap);
}

static void iwl_mvm_enter_ctkill(struct iwl_mvm *mvm)
{
	u32 duration = mvm->thermal_throttle.params->ct_kill_duration;

	IWL_ERR(mvm, "Enter CT Kill\n");
	iwl_mvm_set_hw_ctkill_state(mvm, true);
	schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
			      round_jiffies_relative(duration * HZ));
}

static void iwl_mvm_exit_ctkill(struct iwl_mvm *mvm)
{
	IWL_ERR(mvm, "Exit CT Kill\n");
	iwl_mvm_set_hw_ctkill_state(mvm, false);
}

static void check_exit_ctkill(struct work_struct *work)
{
	struct iwl_mvm_tt_mgmt *tt;
	struct iwl_mvm *mvm;
	u32 duration;
	s32 temp;

	tt = container_of(work, struct iwl_mvm_tt_mgmt, ct_kill_exit.work);
	mvm = container_of(tt, struct iwl_mvm, thermal_throttle);

	duration = tt->params->ct_kill_duration;

	iwl_trans_start_hw(mvm->trans);
	temp = check_nic_temperature(mvm);
	iwl_trans_stop_hw(mvm->trans, false);

	if (temp < MIN_TEMPERATURE || temp > MAX_TEMPERATURE) {
		IWL_DEBUG_TEMP(mvm, "Failed to measure NIC temperature\n");
		goto reschedule;
	}
	IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", temp);

	if (temp <= tt->params->ct_kill_exit) {
		iwl_mvm_exit_ctkill(mvm);
		return;
	}

reschedule:
	schedule_delayed_work(&mvm->thermal_throttle.ct_kill_exit,
			      round_jiffies(duration * HZ));
}

static void iwl_mvm_tt_smps_iterator(void *_data, u8 *mac,
				     struct ieee80211_vif *vif)
{
	struct iwl_mvm *mvm = _data;
	enum ieee80211_smps_mode smps_mode;

	lockdep_assert_held(&mvm->mutex);

	if (mvm->thermal_throttle.dynamic_smps)
		smps_mode = IEEE80211_SMPS_DYNAMIC;
	else
		smps_mode = IEEE80211_SMPS_AUTOMATIC;

374 375 376
	if (vif->type != NL80211_IFTYPE_STATION)
		return;

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT, smps_mode);
}

static void iwl_mvm_tt_tx_protection(struct iwl_mvm *mvm, bool enable)
{
	struct ieee80211_sta *sta;
	struct iwl_mvm_sta *mvmsta;
	int i, err;

	for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
						lockdep_is_held(&mvm->mutex));
		if (IS_ERR_OR_NULL(sta))
			continue;
		mvmsta = (void *)sta->drv_priv;
		if (enable == mvmsta->tt_tx_protection)
			continue;
		err = iwl_mvm_tx_protection(mvm, &mvmsta->lq_sta.lq,
					    mvmsta, enable);
		if (err) {
			IWL_ERR(mvm, "Failed to %s Tx protection\n",
				enable ? "enable" : "disable");
		} else {
			IWL_DEBUG_TEMP(mvm, "%s Tx protection\n",
				       enable ? "Enable" : "Disable");
			mvmsta->tt_tx_protection = enable;
		}
	}
}

static void iwl_mvm_tt_tx_backoff(struct iwl_mvm *mvm, u32 backoff)
{
	struct iwl_host_cmd cmd = {
		.id = REPLY_THERMAL_MNG_BACKOFF,
		.len = { sizeof(u32), },
		.data = { &backoff, },
		.flags = CMD_SYNC,
	};

	if (iwl_mvm_send_cmd(mvm, &cmd) == 0) {
		IWL_DEBUG_TEMP(mvm, "Set Thermal Tx backoff to: %u\n",
			       backoff);
		mvm->thermal_throttle.tx_backoff = backoff;
	} else {
		IWL_ERR(mvm, "Failed to change Thermal Tx backoff\n");
	}
}

void iwl_mvm_tt_handler(struct iwl_mvm *mvm)
{
	const struct iwl_tt_params *params = mvm->thermal_throttle.params;
	struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;
	s32 temperature = mvm->temperature;
	int i;
	u32 tx_backoff;

	IWL_DEBUG_TEMP(mvm, "NIC temperature: %d\n", mvm->temperature);

	if (params->support_ct_kill && temperature >= params->ct_kill_entry) {
		iwl_mvm_enter_ctkill(mvm);
		return;
	}

	if (params->support_dynamic_smps) {
		if (!tt->dynamic_smps &&
		    temperature >= params->dynamic_smps_entry) {
			IWL_DEBUG_TEMP(mvm, "Enable dynamic SMPS\n");
			tt->dynamic_smps = true;
			ieee80211_iterate_active_interfaces_atomic(
					mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
					iwl_mvm_tt_smps_iterator, mvm);
		} else if (tt->dynamic_smps &&
			   temperature <= params->dynamic_smps_exit) {
			IWL_DEBUG_TEMP(mvm, "Disable dynamic SMPS\n");
			tt->dynamic_smps = false;
			ieee80211_iterate_active_interfaces_atomic(
					mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
					iwl_mvm_tt_smps_iterator, mvm);
		}
	}

	if (params->support_tx_protection) {
		if (temperature >= params->tx_protection_entry)
			iwl_mvm_tt_tx_protection(mvm, true);
		else if (temperature <= params->tx_protection_exit)
			iwl_mvm_tt_tx_protection(mvm, false);
	}

	if (params->support_tx_backoff) {
		tx_backoff = 0;
		for (i = 0; i < TT_TX_BACKOFF_SIZE; i++) {
			if (temperature < params->tx_backoff[i].temperature)
				break;
			tx_backoff = params->tx_backoff[i].backoff;
		}
		if (tt->tx_backoff != tx_backoff)
			iwl_mvm_tt_tx_backoff(mvm, tx_backoff);
	}
}

static const struct iwl_tt_params iwl7000_tt_params = {
	.ct_kill_entry = 118,
	.ct_kill_exit = 96,
	.ct_kill_duration = 5,
	.dynamic_smps_entry = 114,
	.dynamic_smps_exit = 110,
	.tx_protection_entry = 114,
	.tx_protection_exit = 108,
	.tx_backoff = {
		{.temperature = 112, .backoff = 200},
		{.temperature = 113, .backoff = 600},
		{.temperature = 114, .backoff = 1200},
		{.temperature = 115, .backoff = 2000},
		{.temperature = 116, .backoff = 4000},
		{.temperature = 117, .backoff = 10000},
	},
	.support_ct_kill = true,
	.support_dynamic_smps = true,
	.support_tx_protection = true,
	.support_tx_backoff = true,
};

void iwl_mvm_tt_initialize(struct iwl_mvm *mvm)
{
	struct iwl_mvm_tt_mgmt *tt = &mvm->thermal_throttle;

	IWL_DEBUG_TEMP(mvm, "Initialize Thermal Throttling\n");
	tt->params = &iwl7000_tt_params;
	INIT_DELAYED_WORK(&tt->ct_kill_exit, check_exit_ctkill);
}

void iwl_mvm_tt_exit(struct iwl_mvm *mvm)
{
	cancel_delayed_work_sync(&mvm->thermal_throttle.ct_kill_exit);
	IWL_DEBUG_TEMP(mvm, "Exit Thermal Throttling\n");
}