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) 2017        Intel Deutschland GmbH
9  * Copyright (C) 2019 - 2020 Intel Corporation
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * The full GNU General Public License is included in this distribution
21  * in the file called COPYING.
22  *
23  * Contact Information:
24  *  Intel Linux Wireless <linuxwifi@intel.com>
25  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26  *
27  * BSD LICENSE
28  *
29  * Copyright(c) 2017        Intel Deutschland GmbH
30  * Copyright (C) 2019 - 2020 Intel Corporation
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  *
37  *  * Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  *  * Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in
41  *    the documentation and/or other materials provided with the
42  *    distribution.
43  *  * Neither the name Intel Corporation nor the names of its
44  *    contributors may be used to endorse or promote products derived
45  *    from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  *
59  *****************************************************************************/
60 
61 #include "iwl-drv.h"
62 #include "iwl-debug.h"
63 #include "acpi.h"
64 #include "fw/runtime.h"
65 
66 void *iwl_acpi_get_object(struct device *dev, acpi_string method)
67 {
68 	acpi_handle root_handle;
69 	acpi_handle handle;
70 	struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
71 	acpi_status status;
72 
73 	root_handle = ACPI_HANDLE(dev);
74 	if (!root_handle) {
75 		IWL_DEBUG_DEV_RADIO(dev,
76 				    "Could not retrieve root port ACPI handle\n");
77 		return ERR_PTR(-ENOENT);
78 	}
79 
80 	/* Get the method's handle */
81 	status = acpi_get_handle(root_handle, method, &handle);
82 	if (ACPI_FAILURE(status)) {
83 		IWL_DEBUG_DEV_RADIO(dev, "%s method not found\n", method);
84 		return ERR_PTR(-ENOENT);
85 	}
86 
87 	/* Call the method with no arguments */
88 	status = acpi_evaluate_object(handle, NULL, NULL, &buf);
89 	if (ACPI_FAILURE(status)) {
90 		IWL_DEBUG_DEV_RADIO(dev, "%s invocation failed (0x%x)\n",
91 				    method, status);
92 		return ERR_PTR(-ENOENT);
93 	}
94 
95 	return buf.pointer;
96 }
97 IWL_EXPORT_SYMBOL(iwl_acpi_get_object);
98 
99 union acpi_object *iwl_acpi_get_wifi_pkg(struct device *dev,
100 					 union acpi_object *data,
101 					 int data_size, int *tbl_rev)
102 {
103 	int i;
104 	union acpi_object *wifi_pkg;
105 
106 	/*
107 	 * We need at least one entry in the wifi package that
108 	 * describes the domain, and one more entry, otherwise there's
109 	 * no point in reading it.
110 	 */
111 	if (WARN_ON_ONCE(data_size < 2))
112 		return ERR_PTR(-EINVAL);
113 
114 	/*
115 	 * We need at least two packages, one for the revision and one
116 	 * for the data itself.  Also check that the revision is valid
117 	 * (i.e. it is an integer smaller than 2, as we currently support only
118 	 * 2 revisions).
119 	 */
120 	if (data->type != ACPI_TYPE_PACKAGE ||
121 	    data->package.count < 2 ||
122 	    data->package.elements[0].type != ACPI_TYPE_INTEGER ||
123 	    data->package.elements[0].integer.value > 1) {
124 		IWL_DEBUG_DEV_RADIO(dev, "Unsupported packages structure\n");
125 		return ERR_PTR(-EINVAL);
126 	}
127 
128 	*tbl_rev = data->package.elements[0].integer.value;
129 
130 	/* loop through all the packages to find the one for WiFi */
131 	for (i = 1; i < data->package.count; i++) {
132 		union acpi_object *domain;
133 
134 		wifi_pkg = &data->package.elements[i];
135 
136 		/* skip entries that are not a package with the right size */
137 		if (wifi_pkg->type != ACPI_TYPE_PACKAGE ||
138 		    wifi_pkg->package.count != data_size)
139 			continue;
140 
141 		domain = &wifi_pkg->package.elements[0];
142 		if (domain->type == ACPI_TYPE_INTEGER &&
143 		    domain->integer.value == ACPI_WIFI_DOMAIN)
144 			goto found;
145 	}
146 
147 	return ERR_PTR(-ENOENT);
148 
149 found:
150 	return wifi_pkg;
151 }
152 IWL_EXPORT_SYMBOL(iwl_acpi_get_wifi_pkg);
153 
154 int iwl_acpi_get_mcc(struct device *dev, char *mcc)
155 {
156 	union acpi_object *wifi_pkg, *data;
157 	u32 mcc_val;
158 	int ret, tbl_rev;
159 
160 	data = iwl_acpi_get_object(dev, ACPI_WRDD_METHOD);
161 	if (IS_ERR(data))
162 		return PTR_ERR(data);
163 
164 	wifi_pkg = iwl_acpi_get_wifi_pkg(dev, data, ACPI_WRDD_WIFI_DATA_SIZE,
165 					 &tbl_rev);
166 	if (IS_ERR(wifi_pkg)) {
167 		ret = PTR_ERR(wifi_pkg);
168 		goto out_free;
169 	}
170 
171 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
172 	    tbl_rev != 0) {
173 		ret = -EINVAL;
174 		goto out_free;
175 	}
176 
177 	mcc_val = wifi_pkg->package.elements[1].integer.value;
178 
179 	mcc[0] = (mcc_val >> 8) & 0xff;
180 	mcc[1] = mcc_val & 0xff;
181 	mcc[2] = '\0';
182 
183 	ret = 0;
184 out_free:
185 	kfree(data);
186 	return ret;
187 }
188 IWL_EXPORT_SYMBOL(iwl_acpi_get_mcc);
189 
190 u64 iwl_acpi_get_pwr_limit(struct device *dev)
191 {
192 	union acpi_object *data, *wifi_pkg;
193 	u64 dflt_pwr_limit;
194 	int tbl_rev;
195 
196 	data = iwl_acpi_get_object(dev, ACPI_SPLC_METHOD);
197 	if (IS_ERR(data)) {
198 		dflt_pwr_limit = 0;
199 		goto out;
200 	}
201 
202 	wifi_pkg = iwl_acpi_get_wifi_pkg(dev, data,
203 					 ACPI_SPLC_WIFI_DATA_SIZE, &tbl_rev);
204 	if (IS_ERR(wifi_pkg) || tbl_rev != 0 ||
205 	    wifi_pkg->package.elements[1].integer.value != ACPI_TYPE_INTEGER) {
206 		dflt_pwr_limit = 0;
207 		goto out_free;
208 	}
209 
210 	dflt_pwr_limit = wifi_pkg->package.elements[1].integer.value;
211 out_free:
212 	kfree(data);
213 out:
214 	return dflt_pwr_limit;
215 }
216 IWL_EXPORT_SYMBOL(iwl_acpi_get_pwr_limit);
217 
218 int iwl_acpi_get_eckv(struct device *dev, u32 *extl_clk)
219 {
220 	union acpi_object *wifi_pkg, *data;
221 	int ret, tbl_rev;
222 
223 	data = iwl_acpi_get_object(dev, ACPI_ECKV_METHOD);
224 	if (IS_ERR(data))
225 		return PTR_ERR(data);
226 
227 	wifi_pkg = iwl_acpi_get_wifi_pkg(dev, data, ACPI_ECKV_WIFI_DATA_SIZE,
228 					 &tbl_rev);
229 	if (IS_ERR(wifi_pkg)) {
230 		ret = PTR_ERR(wifi_pkg);
231 		goto out_free;
232 	}
233 
234 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
235 	    tbl_rev != 0) {
236 		ret = -EINVAL;
237 		goto out_free;
238 	}
239 
240 	*extl_clk = wifi_pkg->package.elements[1].integer.value;
241 
242 	ret = 0;
243 
244 out_free:
245 	kfree(data);
246 	return ret;
247 }
248 IWL_EXPORT_SYMBOL(iwl_acpi_get_eckv);
249 
250 int iwl_sar_set_profile(union acpi_object *table,
251 			struct iwl_sar_profile *profile,
252 			bool enabled)
253 {
254 	int i;
255 
256 	profile->enabled = enabled;
257 
258 	for (i = 0; i < ACPI_SAR_TABLE_SIZE; i++) {
259 		if (table[i].type != ACPI_TYPE_INTEGER ||
260 		    table[i].integer.value > U8_MAX)
261 			return -EINVAL;
262 
263 		profile->table[i] = table[i].integer.value;
264 	}
265 
266 	return 0;
267 }
268 IWL_EXPORT_SYMBOL(iwl_sar_set_profile);
269 
270 int iwl_sar_select_profile(struct iwl_fw_runtime *fwrt,
271 			   __le16 per_chain_restriction[][IWL_NUM_SUB_BANDS],
272 			   int prof_a, int prof_b)
273 {
274 	int i, j, idx;
275 	int profs[ACPI_SAR_NUM_CHAIN_LIMITS] = { prof_a, prof_b };
276 
277 	BUILD_BUG_ON(ACPI_SAR_NUM_CHAIN_LIMITS < 2);
278 	BUILD_BUG_ON(ACPI_SAR_NUM_CHAIN_LIMITS * ACPI_SAR_NUM_SUB_BANDS !=
279 		     ACPI_SAR_TABLE_SIZE);
280 
281 	for (i = 0; i < ACPI_SAR_NUM_CHAIN_LIMITS; i++) {
282 		struct iwl_sar_profile *prof;
283 
284 		/* don't allow SAR to be disabled (profile 0 means disable) */
285 		if (profs[i] == 0)
286 			return -EPERM;
287 
288 		/* we are off by one, so allow up to ACPI_SAR_PROFILE_NUM */
289 		if (profs[i] > ACPI_SAR_PROFILE_NUM)
290 			return -EINVAL;
291 
292 		/* profiles go from 1 to 4, so decrement to access the array */
293 		prof = &fwrt->sar_profiles[profs[i] - 1];
294 
295 		/* if the profile is disabled, do nothing */
296 		if (!prof->enabled) {
297 			IWL_DEBUG_RADIO(fwrt, "SAR profile %d is disabled.\n",
298 					profs[i]);
299 			/* if one of the profiles is disabled, we fail all */
300 			return -ENOENT;
301 		}
302 		IWL_DEBUG_INFO(fwrt,
303 			       "SAR EWRD: chain %d profile index %d\n",
304 			       i, profs[i]);
305 		IWL_DEBUG_RADIO(fwrt, "  Chain[%d]:\n", i);
306 		for (j = 0; j < ACPI_SAR_NUM_SUB_BANDS; j++) {
307 			idx = (i * ACPI_SAR_NUM_SUB_BANDS) + j;
308 			per_chain_restriction[i][j] =
309 				cpu_to_le16(prof->table[idx]);
310 			IWL_DEBUG_RADIO(fwrt, "    Band[%d] = %d * .125dBm\n",
311 					j, prof->table[idx]);
312 		}
313 	}
314 
315 	return 0;
316 }
317 IWL_EXPORT_SYMBOL(iwl_sar_select_profile);
318 
319 int iwl_sar_get_wrds_table(struct iwl_fw_runtime *fwrt)
320 {
321 	union acpi_object *wifi_pkg, *table, *data;
322 	bool enabled;
323 	int ret, tbl_rev;
324 
325 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WRDS_METHOD);
326 	if (IS_ERR(data))
327 		return PTR_ERR(data);
328 
329 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
330 					 ACPI_WRDS_WIFI_DATA_SIZE, &tbl_rev);
331 	if (IS_ERR(wifi_pkg) || tbl_rev != 0) {
332 		ret = PTR_ERR(wifi_pkg);
333 		goto out_free;
334 	}
335 
336 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
337 		ret = -EINVAL;
338 		goto out_free;
339 	}
340 
341 	enabled = !!(wifi_pkg->package.elements[1].integer.value);
342 
343 	/* position of the actual table */
344 	table = &wifi_pkg->package.elements[2];
345 
346 	/* The profile from WRDS is officially profile 1, but goes
347 	 * into sar_profiles[0] (because we don't have a profile 0).
348 	 */
349 	ret = iwl_sar_set_profile(table, &fwrt->sar_profiles[0], enabled);
350 out_free:
351 	kfree(data);
352 	return ret;
353 }
354 IWL_EXPORT_SYMBOL(iwl_sar_get_wrds_table);
355 
356 int iwl_sar_get_ewrd_table(struct iwl_fw_runtime *fwrt)
357 {
358 	union acpi_object *wifi_pkg, *data;
359 	bool enabled;
360 	int i, n_profiles, tbl_rev, pos;
361 	int ret = 0;
362 
363 	data = iwl_acpi_get_object(fwrt->dev, ACPI_EWRD_METHOD);
364 	if (IS_ERR(data))
365 		return PTR_ERR(data);
366 
367 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
368 					 ACPI_EWRD_WIFI_DATA_SIZE, &tbl_rev);
369 	if (IS_ERR(wifi_pkg) || tbl_rev != 0) {
370 		ret = PTR_ERR(wifi_pkg);
371 		goto out_free;
372 	}
373 
374 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
375 	    wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER) {
376 		ret = -EINVAL;
377 		goto out_free;
378 	}
379 
380 	enabled = !!(wifi_pkg->package.elements[1].integer.value);
381 	n_profiles = wifi_pkg->package.elements[2].integer.value;
382 
383 	/*
384 	 * Check the validity of n_profiles.  The EWRD profiles start
385 	 * from index 1, so the maximum value allowed here is
386 	 * ACPI_SAR_PROFILES_NUM - 1.
387 	 */
388 	if (n_profiles <= 0 || n_profiles >= ACPI_SAR_PROFILE_NUM) {
389 		ret = -EINVAL;
390 		goto out_free;
391 	}
392 
393 	/* the tables start at element 3 */
394 	pos = 3;
395 
396 	for (i = 0; i < n_profiles; i++) {
397 		/* The EWRD profiles officially go from 2 to 4, but we
398 		 * save them in sar_profiles[1-3] (because we don't
399 		 * have profile 0).  So in the array we start from 1.
400 		 */
401 		ret = iwl_sar_set_profile(&wifi_pkg->package.elements[pos],
402 					  &fwrt->sar_profiles[i + 1],
403 					  enabled);
404 		if (ret < 0)
405 			break;
406 
407 		/* go to the next table */
408 		pos += ACPI_SAR_TABLE_SIZE;
409 	}
410 
411 out_free:
412 	kfree(data);
413 	return ret;
414 }
415 IWL_EXPORT_SYMBOL(iwl_sar_get_ewrd_table);
416 
417 int iwl_sar_get_wgds_table(struct iwl_fw_runtime *fwrt)
418 {
419 	union acpi_object *wifi_pkg, *data;
420 	int i, j, ret, tbl_rev;
421 	int idx = 1;
422 
423 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WGDS_METHOD);
424 	if (IS_ERR(data))
425 		return PTR_ERR(data);
426 
427 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
428 					 ACPI_WGDS_WIFI_DATA_SIZE, &tbl_rev);
429 	if (IS_ERR(wifi_pkg) || tbl_rev > 1) {
430 		ret = PTR_ERR(wifi_pkg);
431 		goto out_free;
432 	}
433 
434 	fwrt->geo_rev = tbl_rev;
435 	for (i = 0; i < ACPI_NUM_GEO_PROFILES; i++) {
436 		for (j = 0; j < ACPI_GEO_TABLE_SIZE; j++) {
437 			union acpi_object *entry;
438 
439 			entry = &wifi_pkg->package.elements[idx++];
440 			if (entry->type != ACPI_TYPE_INTEGER ||
441 			    entry->integer.value > U8_MAX) {
442 				ret = -EINVAL;
443 				goto out_free;
444 			}
445 
446 			fwrt->geo_profiles[i].values[j] = entry->integer.value;
447 		}
448 	}
449 	ret = 0;
450 out_free:
451 	kfree(data);
452 	return ret;
453 }
454 IWL_EXPORT_SYMBOL(iwl_sar_get_wgds_table);
455 
456 bool iwl_sar_geo_support(struct iwl_fw_runtime *fwrt)
457 {
458 	/*
459 	 * The GEO_TX_POWER_LIMIT command is not supported on earlier
460 	 * firmware versions.  Unfortunately, we don't have a TLV API
461 	 * flag to rely on, so rely on the major version which is in
462 	 * the first byte of ucode_ver.  This was implemented
463 	 * initially on version 38 and then backported to 17.  It was
464 	 * also backported to 29, but only for 7265D devices.  The
465 	 * intention was to have it in 36 as well, but not all 8000
466 	 * family got this feature enabled.  The 8000 family is the
467 	 * only one using version 36, so skip this version entirely.
468 	 */
469 	return IWL_UCODE_SERIAL(fwrt->fw->ucode_ver) >= 38 ||
470 	       IWL_UCODE_SERIAL(fwrt->fw->ucode_ver) == 17 ||
471 	       (IWL_UCODE_SERIAL(fwrt->fw->ucode_ver) == 29 &&
472 		((fwrt->trans->hw_rev & CSR_HW_REV_TYPE_MSK) ==
473 		 CSR_HW_REV_TYPE_7265D));
474 }
475 IWL_EXPORT_SYMBOL(iwl_sar_geo_support);
476 
477 int iwl_validate_sar_geo_profile(struct iwl_fw_runtime *fwrt,
478 				 struct iwl_host_cmd *cmd)
479 {
480 	struct iwl_geo_tx_power_profiles_resp *resp;
481 	int ret;
482 
483 	resp = (void *)cmd->resp_pkt->data;
484 	ret = le32_to_cpu(resp->profile_idx);
485 	if (WARN_ON(ret > ACPI_NUM_GEO_PROFILES)) {
486 		ret = -EIO;
487 		IWL_WARN(fwrt, "Invalid geographic profile idx (%d)\n", ret);
488 	}
489 
490 	return ret;
491 }
492 IWL_EXPORT_SYMBOL(iwl_validate_sar_geo_profile);
493 
494 int iwl_sar_geo_init(struct iwl_fw_runtime *fwrt,
495 		     struct iwl_per_chain_offset_group *table)
496 {
497 	int ret, i, j;
498 
499 	if (!iwl_sar_geo_support(fwrt))
500 		return -EOPNOTSUPP;
501 
502 	ret = iwl_sar_get_wgds_table(fwrt);
503 	if (ret < 0) {
504 		IWL_DEBUG_RADIO(fwrt,
505 				"Geo SAR BIOS table invalid or unavailable. (%d)\n",
506 				ret);
507 		/* we don't fail if the table is not available */
508 		return -ENOENT;
509 	}
510 
511 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES * ACPI_WGDS_NUM_BANDS *
512 		     ACPI_WGDS_TABLE_SIZE + 1 !=  ACPI_WGDS_WIFI_DATA_SIZE);
513 
514 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES > IWL_NUM_GEO_PROFILES);
515 
516 	for (i = 0; i < ACPI_NUM_GEO_PROFILES; i++) {
517 		struct iwl_per_chain_offset *chain =
518 			(struct iwl_per_chain_offset *)&table[i];
519 
520 		for (j = 0; j < ACPI_WGDS_NUM_BANDS; j++) {
521 			u8 *value;
522 
523 			value = &fwrt->geo_profiles[i].values[j *
524 				ACPI_GEO_PER_CHAIN_SIZE];
525 			chain[j].max_tx_power = cpu_to_le16(value[0]);
526 			chain[j].chain_a = value[1];
527 			chain[j].chain_b = value[2];
528 			IWL_DEBUG_RADIO(fwrt,
529 					"SAR geographic profile[%d] Band[%d]: chain A = %d chain B = %d max_tx_power = %d\n",
530 					i, j, value[1], value[2], value[0]);
531 		}
532 	}
533 
534 	return 0;
535 }
536 IWL_EXPORT_SYMBOL(iwl_sar_geo_init);
537