xref: /freebsd/sys/contrib/dev/iwlwifi/fw/acpi.c (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2017 Intel Deutschland GmbH
4  * Copyright (C) 2019-2024 Intel Corporation
5  */
6 #include <linux/uuid.h>
7 #include "iwl-drv.h"
8 #include "iwl-debug.h"
9 #include "acpi.h"
10 #include "fw/runtime.h"
11 
12 const guid_t iwl_guid = GUID_INIT(0xF21202BF, 0x8F78, 0x4DC6,
13 				  0xA5, 0xB3, 0x1F, 0x73,
14 				  0x8E, 0x28, 0x5A, 0xDE);
15 
16 static const size_t acpi_dsm_size[DSM_FUNC_NUM_FUNCS] = {
17 	[DSM_FUNC_QUERY] =			sizeof(u32),
18 	[DSM_FUNC_DISABLE_SRD] =		sizeof(u8),
19 	[DSM_FUNC_ENABLE_INDONESIA_5G2] =	sizeof(u8),
20 	[DSM_FUNC_ENABLE_6E] =			sizeof(u32),
21 	[DSM_FUNC_REGULATORY_CONFIG] =		sizeof(u32),
22 	/* Not supported in driver */
23 	[5] =					(size_t)0,
24 	[DSM_FUNC_11AX_ENABLEMENT] =		sizeof(u32),
25 	[DSM_FUNC_ENABLE_UNII4_CHAN] =		sizeof(u32),
26 	[DSM_FUNC_ACTIVATE_CHANNEL] =		sizeof(u32),
27 	[DSM_FUNC_FORCE_DISABLE_CHANNELS] =	sizeof(u32),
28 	[DSM_FUNC_ENERGY_DETECTION_THRESHOLD] =	sizeof(u32),
29 	[DSM_FUNC_RFI_CONFIG] =			sizeof(u32),
30 	[DSM_FUNC_ENABLE_11BE] =		sizeof(u32),
31 };
32 
33 static int iwl_acpi_get_handle(struct device *dev, acpi_string method,
34 			       acpi_handle *ret_handle)
35 {
36 	acpi_handle root_handle;
37 	acpi_status status;
38 
39 	root_handle = ACPI_HANDLE(dev);
40 	if (!root_handle) {
41 		IWL_DEBUG_DEV_RADIO(dev,
42 				    "ACPI: Could not retrieve root port handle\n");
43 		return -ENOENT;
44 	}
45 
46 	status = acpi_get_handle(root_handle, method, ret_handle);
47 	if (ACPI_FAILURE(status)) {
48 		IWL_DEBUG_DEV_RADIO(dev,
49 				    "ACPI: %s method not found\n", method);
50 		return -ENOENT;
51 	}
52 	return 0;
53 }
54 
55 static void *iwl_acpi_get_object(struct device *dev, acpi_string method)
56 {
57 	struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
58 	acpi_handle handle;
59 	acpi_status status;
60 	int ret;
61 
62 	ret = iwl_acpi_get_handle(dev, method, &handle);
63 	if (ret)
64 		return ERR_PTR(-ENOENT);
65 
66 	/* Call the method with no arguments */
67 	status = acpi_evaluate_object(handle, NULL, NULL, &buf);
68 	if (ACPI_FAILURE(status)) {
69 		IWL_DEBUG_DEV_RADIO(dev,
70 				    "ACPI: %s method invocation failed (status: 0x%x)\n",
71 				    method, status);
72 		return ERR_PTR(-ENOENT);
73 	}
74 	return buf.pointer;
75 }
76 
77 /*
78  * Generic function for evaluating a method defined in the device specific
79  * method (DSM) interface. The returned acpi object must be freed by calling
80  * function.
81  */
82 static void *iwl_acpi_get_dsm_object(struct device *dev, int rev, int func,
83 				     union acpi_object *args,
84 				     const guid_t *guid)
85 {
86 	union acpi_object *obj;
87 
88 	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), guid, rev, func,
89 				args);
90 	if (!obj) {
91 		IWL_DEBUG_DEV_RADIO(dev,
92 				    "ACPI: DSM method invocation failed (rev: %d, func:%d)\n",
93 				    rev, func);
94 		return ERR_PTR(-ENOENT);
95 	}
96 	return obj;
97 }
98 
99 /*
100  * Generic function to evaluate a DSM with no arguments
101  * and an integer return value,
102  * (as an integer object or inside a buffer object),
103  * verify and assign the value in the "value" parameter.
104  * return 0 in success and the appropriate errno otherwise.
105  */
106 static int iwl_acpi_get_dsm_integer(struct device *dev, int rev, int func,
107 				    const guid_t *guid, u64 *value,
108 				    size_t expected_size)
109 {
110 	union acpi_object *obj;
111 	int ret = 0;
112 
113 	obj = iwl_acpi_get_dsm_object(dev, rev, func, NULL, guid);
114 	if (IS_ERR(obj)) {
115 		IWL_DEBUG_DEV_RADIO(dev,
116 				    "Failed to get  DSM object. func= %d\n",
117 				    func);
118 		return -ENOENT;
119 	}
120 
121 	if (obj->type == ACPI_TYPE_INTEGER) {
122 		*value = obj->integer.value;
123 	} else if (obj->type == ACPI_TYPE_BUFFER) {
124 		__le64 le_value = 0;
125 
126 		if (WARN_ON_ONCE(expected_size > sizeof(le_value)))
127 			return -EINVAL;
128 
129 		/* if the buffer size doesn't match the expected size */
130 		if (obj->buffer.length != expected_size)
131 			IWL_DEBUG_DEV_RADIO(dev,
132 					    "ACPI: DSM invalid buffer size, padding or truncating (%d)\n",
133 					    obj->buffer.length);
134 
135 		 /* assuming LE from Intel BIOS spec */
136 		memcpy(&le_value, obj->buffer.pointer,
137 		       min_t(size_t, expected_size, (size_t)obj->buffer.length));
138 		*value = le64_to_cpu(le_value);
139 	} else {
140 		IWL_DEBUG_DEV_RADIO(dev,
141 				    "ACPI: DSM method did not return a valid object, type=%d\n",
142 				    obj->type);
143 		ret = -EINVAL;
144 		goto out;
145 	}
146 
147 	IWL_DEBUG_DEV_RADIO(dev,
148 			    "ACPI: DSM method evaluated: func=%d, ret=%d\n",
149 			    func, ret);
150 out:
151 	ACPI_FREE(obj);
152 	return ret;
153 }
154 
155 /*
156  * This function receives a DSM function number, calculates its expected size
157  * according to Intel BIOS spec, and fills in the value in a 32-bit field.
158  * In case the expected size is smaller than 32-bit, padding will be added.
159  */
160 int iwl_acpi_get_dsm(struct iwl_fw_runtime *fwrt,
161 		     enum iwl_dsm_funcs func, u32 *value)
162 {
163 	size_t expected_size;
164 	u64 tmp;
165 	int ret;
166 
167 	BUILD_BUG_ON(ARRAY_SIZE(acpi_dsm_size) != DSM_FUNC_NUM_FUNCS);
168 
169 	if (WARN_ON(func >= ARRAY_SIZE(acpi_dsm_size)))
170 		return -EINVAL;
171 
172 	expected_size = acpi_dsm_size[func];
173 
174 	/* Currently all ACPI DSMs are either 8-bit or 32-bit */
175 	if (expected_size != sizeof(u8) && expected_size != sizeof(u32))
176 		return -EOPNOTSUPP;
177 
178 	ret = iwl_acpi_get_dsm_integer(fwrt->dev, ACPI_DSM_REV, func,
179 				       &iwl_guid, &tmp, expected_size);
180 	if (ret)
181 		return ret;
182 
183 	if ((expected_size == sizeof(u8) && tmp != (u8)tmp) ||
184 	    (expected_size == sizeof(u32) && tmp != (u32)tmp))
185 		IWL_DEBUG_RADIO(fwrt,
186 				"DSM value overflows the expected size, truncating\n");
187 	*value = (u32)tmp;
188 
189 	return 0;
190 }
191 
192 static union acpi_object *
193 iwl_acpi_get_wifi_pkg_range(struct device *dev,
194 			    union acpi_object *data,
195 			    int min_data_size,
196 			    int max_data_size,
197 			    int *tbl_rev)
198 {
199 	int i;
200 	union acpi_object *wifi_pkg;
201 
202 	/*
203 	 * We need at least one entry in the wifi package that
204 	 * describes the domain, and one more entry, otherwise there's
205 	 * no point in reading it.
206 	 */
207 	if (WARN_ON_ONCE(min_data_size < 2 || min_data_size > max_data_size))
208 		return ERR_PTR(-EINVAL);
209 
210 	/*
211 	 * We need at least two packages, one for the revision and one
212 	 * for the data itself.  Also check that the revision is valid
213 	 * (i.e. it is an integer (each caller has to check by itself
214 	 * if the returned revision is supported)).
215 	 */
216 	if (data->type != ACPI_TYPE_PACKAGE ||
217 	    data->package.count < 2 ||
218 	    data->package.elements[0].type != ACPI_TYPE_INTEGER) {
219 		IWL_DEBUG_DEV_RADIO(dev, "Invalid packages structure\n");
220 		return ERR_PTR(-EINVAL);
221 	}
222 
223 	*tbl_rev = data->package.elements[0].integer.value;
224 
225 	/* loop through all the packages to find the one for WiFi */
226 	for (i = 1; i < data->package.count; i++) {
227 		union acpi_object *domain;
228 
229 		wifi_pkg = &data->package.elements[i];
230 
231 		/* skip entries that are not a package with the right size */
232 		if (wifi_pkg->type != ACPI_TYPE_PACKAGE ||
233 		    wifi_pkg->package.count < min_data_size ||
234 		    wifi_pkg->package.count > max_data_size)
235 			continue;
236 
237 		domain = &wifi_pkg->package.elements[0];
238 		if (domain->type == ACPI_TYPE_INTEGER &&
239 		    domain->integer.value == ACPI_WIFI_DOMAIN)
240 			goto found;
241 	}
242 
243 	return ERR_PTR(-ENOENT);
244 
245 found:
246 	return wifi_pkg;
247 }
248 
249 static union acpi_object *
250 iwl_acpi_get_wifi_pkg(struct device *dev,
251 		      union acpi_object *data,
252 		      int data_size, int *tbl_rev)
253 {
254 	return iwl_acpi_get_wifi_pkg_range(dev, data, data_size, data_size,
255 					   tbl_rev);
256 }
257 
258 int iwl_acpi_get_tas_table(struct iwl_fw_runtime *fwrt,
259 			   struct iwl_tas_data *tas_data)
260 {
261 	union acpi_object *wifi_pkg, *data;
262 	int ret, tbl_rev, i, block_list_size, enabled;
263 
264 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WTAS_METHOD);
265 	if (IS_ERR(data))
266 		return PTR_ERR(data);
267 
268 	/* try to read wtas table revision 1 or revision 0*/
269 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
270 					 ACPI_WTAS_WIFI_DATA_SIZE,
271 					 &tbl_rev);
272 	if (IS_ERR(wifi_pkg)) {
273 		ret = PTR_ERR(wifi_pkg);
274 		goto out_free;
275 	}
276 
277 	if (tbl_rev == 1 && wifi_pkg->package.elements[1].type ==
278 		ACPI_TYPE_INTEGER) {
279 		u32 tas_selection =
280 			(u32)wifi_pkg->package.elements[1].integer.value;
281 
282 		enabled = iwl_parse_tas_selection(fwrt, tas_data,
283 						  tas_selection);
284 
285 	} else if (tbl_rev == 0 &&
286 		wifi_pkg->package.elements[1].type == ACPI_TYPE_INTEGER) {
287 		enabled = !!wifi_pkg->package.elements[1].integer.value;
288 	} else {
289 		ret = -EINVAL;
290 		goto out_free;
291 	}
292 
293 	if (!enabled) {
294 		IWL_DEBUG_RADIO(fwrt, "TAS not enabled\n");
295 		ret = 0;
296 		goto out_free;
297 	}
298 
299 	IWL_DEBUG_RADIO(fwrt, "Reading TAS table revision %d\n", tbl_rev);
300 	if (wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER ||
301 	    wifi_pkg->package.elements[2].integer.value >
302 	    IWL_WTAS_BLACK_LIST_MAX) {
303 		IWL_DEBUG_RADIO(fwrt, "TAS invalid array size %llu\n",
304 				wifi_pkg->package.elements[2].integer.value);
305 		ret = -EINVAL;
306 		goto out_free;
307 	}
308 	block_list_size = wifi_pkg->package.elements[2].integer.value;
309 	tas_data->block_list_size = cpu_to_le32(block_list_size);
310 
311 	IWL_DEBUG_RADIO(fwrt, "TAS array size %u\n", block_list_size);
312 
313 	for (i = 0; i < block_list_size; i++) {
314 		u32 country;
315 
316 		if (wifi_pkg->package.elements[3 + i].type !=
317 		    ACPI_TYPE_INTEGER) {
318 			IWL_DEBUG_RADIO(fwrt,
319 					"TAS invalid array elem %d\n", 3 + i);
320 			ret = -EINVAL;
321 			goto out_free;
322 		}
323 
324 		country = wifi_pkg->package.elements[3 + i].integer.value;
325 		tas_data->block_list_array[i] = cpu_to_le32(country);
326 		IWL_DEBUG_RADIO(fwrt, "TAS block list country %d\n", country);
327 	}
328 
329 	ret = 1;
330 out_free:
331 	kfree(data);
332 	return ret;
333 }
334 
335 int iwl_acpi_get_mcc(struct iwl_fw_runtime *fwrt, char *mcc)
336 {
337 	union acpi_object *wifi_pkg, *data;
338 	u32 mcc_val;
339 	int ret, tbl_rev;
340 
341 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WRDD_METHOD);
342 	if (IS_ERR(data))
343 		return PTR_ERR(data);
344 
345 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
346 					 ACPI_WRDD_WIFI_DATA_SIZE,
347 					 &tbl_rev);
348 	if (IS_ERR(wifi_pkg)) {
349 		ret = PTR_ERR(wifi_pkg);
350 		goto out_free;
351 	}
352 
353 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
354 	    tbl_rev != 0) {
355 		ret = -EINVAL;
356 		goto out_free;
357 	}
358 
359 	mcc_val = wifi_pkg->package.elements[1].integer.value;
360 
361 	mcc[0] = (mcc_val >> 8) & 0xff;
362 	mcc[1] = mcc_val & 0xff;
363 	mcc[2] = '\0';
364 
365 	ret = 0;
366 out_free:
367 	kfree(data);
368 	return ret;
369 }
370 
371 int iwl_acpi_get_pwr_limit(struct iwl_fw_runtime *fwrt, u64 *dflt_pwr_limit)
372 {
373 	union acpi_object *data, *wifi_pkg;
374 	int tbl_rev, ret = -EINVAL;
375 
376 	*dflt_pwr_limit = 0;
377 	data = iwl_acpi_get_object(fwrt->dev, ACPI_SPLC_METHOD);
378 	if (IS_ERR(data))
379 		goto out;
380 
381 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
382 					 ACPI_SPLC_WIFI_DATA_SIZE, &tbl_rev);
383 	if (IS_ERR(wifi_pkg) || tbl_rev != 0 ||
384 	    wifi_pkg->package.elements[1].integer.value != ACPI_TYPE_INTEGER)
385 		goto out_free;
386 
387 	*dflt_pwr_limit = wifi_pkg->package.elements[1].integer.value;
388 	ret = 0;
389 out_free:
390 	kfree(data);
391 out:
392 	return ret;
393 }
394 
395 int iwl_acpi_get_eckv(struct iwl_fw_runtime *fwrt, u32 *extl_clk)
396 {
397 	union acpi_object *wifi_pkg, *data;
398 	int ret, tbl_rev;
399 
400 	data = iwl_acpi_get_object(fwrt->dev, ACPI_ECKV_METHOD);
401 	if (IS_ERR(data))
402 		return PTR_ERR(data);
403 
404 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
405 					 ACPI_ECKV_WIFI_DATA_SIZE,
406 					 &tbl_rev);
407 	if (IS_ERR(wifi_pkg)) {
408 		ret = PTR_ERR(wifi_pkg);
409 		goto out_free;
410 	}
411 
412 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
413 	    tbl_rev != 0) {
414 		ret = -EINVAL;
415 		goto out_free;
416 	}
417 
418 	*extl_clk = wifi_pkg->package.elements[1].integer.value;
419 
420 	ret = 0;
421 
422 out_free:
423 	kfree(data);
424 	return ret;
425 }
426 
427 static int iwl_acpi_sar_set_profile(union acpi_object *table,
428 				    struct iwl_sar_profile *profile,
429 				    bool enabled, u8 num_chains,
430 				    u8 num_sub_bands)
431 {
432 	int i, j, idx = 0;
433 
434 	/*
435 	 * The table from ACPI is flat, but we store it in a
436 	 * structured array.
437 	 */
438 	for (i = 0; i < BIOS_SAR_MAX_CHAINS_PER_PROFILE; i++) {
439 		for (j = 0; j < BIOS_SAR_MAX_SUB_BANDS_NUM; j++) {
440 			/* if we don't have the values, use the default */
441 			if (i >= num_chains || j >= num_sub_bands) {
442 				profile->chains[i].subbands[j] = 0;
443 			} else {
444 				if (table[idx].type != ACPI_TYPE_INTEGER ||
445 				    table[idx].integer.value > U8_MAX)
446 					return -EINVAL;
447 
448 				profile->chains[i].subbands[j] =
449 					table[idx].integer.value;
450 
451 				idx++;
452 			}
453 		}
454 	}
455 
456 	/* Only if all values were valid can the profile be enabled */
457 	profile->enabled = enabled;
458 
459 	return 0;
460 }
461 
462 int iwl_acpi_get_wrds_table(struct iwl_fw_runtime *fwrt)
463 {
464 	union acpi_object *wifi_pkg, *table, *data;
465 	int ret, tbl_rev;
466 	u32 flags;
467 	u8 num_chains, num_sub_bands;
468 
469 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WRDS_METHOD);
470 	if (IS_ERR(data))
471 		return PTR_ERR(data);
472 
473 	/* start by trying to read revision 2 */
474 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
475 					 ACPI_WRDS_WIFI_DATA_SIZE_REV2,
476 					 &tbl_rev);
477 	if (!IS_ERR(wifi_pkg)) {
478 		if (tbl_rev != 2) {
479 			ret = -EINVAL;
480 			goto out_free;
481 		}
482 
483 		num_chains = ACPI_SAR_NUM_CHAINS_REV2;
484 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV2;
485 
486 		goto read_table;
487 	}
488 
489 	/* then try revision 1 */
490 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
491 					 ACPI_WRDS_WIFI_DATA_SIZE_REV1,
492 					 &tbl_rev);
493 	if (!IS_ERR(wifi_pkg)) {
494 		if (tbl_rev != 1) {
495 			ret = -EINVAL;
496 			goto out_free;
497 		}
498 
499 		num_chains = ACPI_SAR_NUM_CHAINS_REV1;
500 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV1;
501 
502 		goto read_table;
503 	}
504 
505 	/* then finally revision 0 */
506 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
507 					 ACPI_WRDS_WIFI_DATA_SIZE_REV0,
508 					 &tbl_rev);
509 	if (!IS_ERR(wifi_pkg)) {
510 		if (tbl_rev != 0) {
511 			ret = -EINVAL;
512 			goto out_free;
513 		}
514 
515 		num_chains = ACPI_SAR_NUM_CHAINS_REV0;
516 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV0;
517 
518 		goto read_table;
519 	}
520 
521 	ret = PTR_ERR(wifi_pkg);
522 	goto out_free;
523 
524 read_table:
525 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
526 		ret = -EINVAL;
527 		goto out_free;
528 	}
529 
530 	IWL_DEBUG_RADIO(fwrt, "Reading WRDS tbl_rev=%d\n", tbl_rev);
531 
532 	flags = wifi_pkg->package.elements[1].integer.value;
533 	fwrt->reduced_power_flags = flags >> IWL_REDUCE_POWER_FLAGS_POS;
534 
535 	/* position of the actual table */
536 	table = &wifi_pkg->package.elements[2];
537 
538 	/* The profile from WRDS is officially profile 1, but goes
539 	 * into sar_profiles[0] (because we don't have a profile 0).
540 	 */
541 	ret = iwl_acpi_sar_set_profile(table, &fwrt->sar_profiles[0],
542 				       flags & IWL_SAR_ENABLE_MSK,
543 				       num_chains, num_sub_bands);
544 out_free:
545 	kfree(data);
546 	return ret;
547 }
548 
549 int iwl_acpi_get_ewrd_table(struct iwl_fw_runtime *fwrt)
550 {
551 	union acpi_object *wifi_pkg, *data;
552 	bool enabled;
553 	int i, n_profiles, tbl_rev, pos;
554 	int ret = 0;
555 	u8 num_chains, num_sub_bands;
556 
557 	data = iwl_acpi_get_object(fwrt->dev, ACPI_EWRD_METHOD);
558 	if (IS_ERR(data))
559 		return PTR_ERR(data);
560 
561 	/* start by trying to read revision 2 */
562 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
563 					 ACPI_EWRD_WIFI_DATA_SIZE_REV2,
564 					 &tbl_rev);
565 	if (!IS_ERR(wifi_pkg)) {
566 		if (tbl_rev != 2) {
567 			ret = -EINVAL;
568 			goto out_free;
569 		}
570 
571 		num_chains = ACPI_SAR_NUM_CHAINS_REV2;
572 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV2;
573 
574 		goto read_table;
575 	}
576 
577 	/* then try revision 1 */
578 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
579 					 ACPI_EWRD_WIFI_DATA_SIZE_REV1,
580 					 &tbl_rev);
581 	if (!IS_ERR(wifi_pkg)) {
582 		if (tbl_rev != 1) {
583 			ret = -EINVAL;
584 			goto out_free;
585 		}
586 
587 		num_chains = ACPI_SAR_NUM_CHAINS_REV1;
588 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV1;
589 
590 		goto read_table;
591 	}
592 
593 	/* then finally revision 0 */
594 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
595 					 ACPI_EWRD_WIFI_DATA_SIZE_REV0,
596 					 &tbl_rev);
597 	if (!IS_ERR(wifi_pkg)) {
598 		if (tbl_rev != 0) {
599 			ret = -EINVAL;
600 			goto out_free;
601 		}
602 
603 		num_chains = ACPI_SAR_NUM_CHAINS_REV0;
604 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV0;
605 
606 		goto read_table;
607 	}
608 
609 	ret = PTR_ERR(wifi_pkg);
610 	goto out_free;
611 
612 read_table:
613 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
614 	    wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER) {
615 		ret = -EINVAL;
616 		goto out_free;
617 	}
618 
619 	enabled = !!(wifi_pkg->package.elements[1].integer.value);
620 	n_profiles = wifi_pkg->package.elements[2].integer.value;
621 
622 	/*
623 	 * Check the validity of n_profiles.  The EWRD profiles start
624 	 * from index 1, so the maximum value allowed here is
625 	 * ACPI_SAR_PROFILES_NUM - 1.
626 	 */
627 	if (n_profiles >= BIOS_SAR_MAX_PROFILE_NUM) {
628 		ret = -EINVAL;
629 		goto out_free;
630 	}
631 
632 	/* the tables start at element 3 */
633 	pos = 3;
634 
635 	for (i = 0; i < n_profiles; i++) {
636 		union acpi_object *table = &wifi_pkg->package.elements[pos];
637 		/* The EWRD profiles officially go from 2 to 4, but we
638 		 * save them in sar_profiles[1-3] (because we don't
639 		 * have profile 0).  So in the array we start from 1.
640 		 */
641 		ret = iwl_acpi_sar_set_profile(table,
642 					       &fwrt->sar_profiles[i + 1],
643 					       enabled, num_chains,
644 					       num_sub_bands);
645 		if (ret < 0)
646 			break;
647 
648 		/* go to the next table */
649 		pos += num_chains * num_sub_bands;
650 	}
651 
652 out_free:
653 	kfree(data);
654 	return ret;
655 }
656 
657 int iwl_acpi_get_wgds_table(struct iwl_fw_runtime *fwrt)
658 {
659 	union acpi_object *wifi_pkg, *data;
660 	int i, j, k, ret, tbl_rev;
661 	u8 num_bands, num_profiles;
662 	static const struct {
663 		u8 revisions;
664 		u8 bands;
665 		u8 profiles;
666 		u8 min_profiles;
667 	} rev_data[] = {
668 		{
669 			.revisions = BIT(3),
670 			.bands = ACPI_GEO_NUM_BANDS_REV2,
671 			.profiles = ACPI_NUM_GEO_PROFILES_REV3,
672 			.min_profiles = BIOS_GEO_MIN_PROFILE_NUM,
673 		},
674 		{
675 			.revisions = BIT(2),
676 			.bands = ACPI_GEO_NUM_BANDS_REV2,
677 			.profiles = ACPI_NUM_GEO_PROFILES,
678 		},
679 		{
680 			.revisions = BIT(0) | BIT(1),
681 			.bands = ACPI_GEO_NUM_BANDS_REV0,
682 			.profiles = ACPI_NUM_GEO_PROFILES,
683 		},
684 	};
685 	int idx;
686 	/* start from one to skip the domain */
687 	int entry_idx = 1;
688 
689 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES_REV3 != IWL_NUM_GEO_PROFILES_V3);
690 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES != IWL_NUM_GEO_PROFILES);
691 
692 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WGDS_METHOD);
693 	if (IS_ERR(data))
694 		return PTR_ERR(data);
695 
696 	/* read the highest revision we understand first */
697 	for (idx = 0; idx < ARRAY_SIZE(rev_data); idx++) {
698 		/* min_profiles != 0 requires num_profiles header */
699 		u32 hdr_size = 1 + !!rev_data[idx].min_profiles;
700 		u32 profile_size = ACPI_GEO_PER_CHAIN_SIZE *
701 				   rev_data[idx].bands;
702 		u32 max_size = hdr_size + profile_size * rev_data[idx].profiles;
703 		u32 min_size;
704 
705 		if (!rev_data[idx].min_profiles)
706 			min_size = max_size;
707 		else
708 			min_size = hdr_size +
709 				   profile_size * rev_data[idx].min_profiles;
710 
711 		wifi_pkg = iwl_acpi_get_wifi_pkg_range(fwrt->dev, data,
712 						       min_size, max_size,
713 						       &tbl_rev);
714 		if (!IS_ERR(wifi_pkg)) {
715 			if (!(BIT(tbl_rev) & rev_data[idx].revisions))
716 				continue;
717 
718 			num_bands = rev_data[idx].bands;
719 			num_profiles = rev_data[idx].profiles;
720 
721 			if (rev_data[idx].min_profiles) {
722 				/* read header that says # of profiles */
723 				union acpi_object *entry;
724 
725 				entry = &wifi_pkg->package.elements[entry_idx];
726 				entry_idx++;
727 				if (entry->type != ACPI_TYPE_INTEGER ||
728 				    entry->integer.value > num_profiles ||
729 				    entry->integer.value <
730 					rev_data[idx].min_profiles) {
731 					ret = -EINVAL;
732 					goto out_free;
733 				}
734 
735 				/*
736 				 * Check to see if we received package count
737 				 * same as max # of profiles
738 				 */
739 				if (wifi_pkg->package.count !=
740 				    hdr_size + profile_size * num_profiles) {
741 					ret = -EINVAL;
742 					goto out_free;
743 				}
744 
745 				/* Number of valid profiles */
746 				num_profiles = entry->integer.value;
747 			}
748 			goto read_table;
749 		}
750 	}
751 
752 	if (idx < ARRAY_SIZE(rev_data))
753 		ret = PTR_ERR(wifi_pkg);
754 	else
755 		ret = -ENOENT;
756 	goto out_free;
757 
758 read_table:
759 	fwrt->geo_rev = tbl_rev;
760 	for (i = 0; i < num_profiles; i++) {
761 		for (j = 0; j < BIOS_GEO_MAX_NUM_BANDS; j++) {
762 			union acpi_object *entry;
763 
764 			/*
765 			 * num_bands is either 2 or 3, if it's only 2 then
766 			 * fill the third band (6 GHz) with the values from
767 			 * 5 GHz (second band)
768 			 */
769 			if (j >= num_bands) {
770 				fwrt->geo_profiles[i].bands[j].max =
771 					fwrt->geo_profiles[i].bands[1].max;
772 			} else {
773 				entry = &wifi_pkg->package.elements[entry_idx];
774 				entry_idx++;
775 				if (entry->type != ACPI_TYPE_INTEGER ||
776 				    entry->integer.value > U8_MAX) {
777 					ret = -EINVAL;
778 					goto out_free;
779 				}
780 
781 				fwrt->geo_profiles[i].bands[j].max =
782 					entry->integer.value;
783 			}
784 
785 			for (k = 0; k < BIOS_GEO_NUM_CHAINS; k++) {
786 				/* same here as above */
787 				if (j >= num_bands) {
788 					fwrt->geo_profiles[i].bands[j].chains[k] =
789 						fwrt->geo_profiles[i].bands[1].chains[k];
790 				} else {
791 					entry = &wifi_pkg->package.elements[entry_idx];
792 					entry_idx++;
793 					if (entry->type != ACPI_TYPE_INTEGER ||
794 					    entry->integer.value > U8_MAX) {
795 						ret = -EINVAL;
796 						goto out_free;
797 					}
798 
799 					fwrt->geo_profiles[i].bands[j].chains[k] =
800 						entry->integer.value;
801 				}
802 			}
803 		}
804 	}
805 
806 	fwrt->geo_num_profiles = num_profiles;
807 	fwrt->geo_enabled = true;
808 	ret = 0;
809 out_free:
810 	kfree(data);
811 	return ret;
812 }
813 
814 int iwl_acpi_get_ppag_table(struct iwl_fw_runtime *fwrt)
815 {
816 	union acpi_object *wifi_pkg, *data, *flags;
817 	int i, j, ret, tbl_rev, num_sub_bands = 0;
818 	int idx = 2;
819 
820 	data = iwl_acpi_get_object(fwrt->dev, ACPI_PPAG_METHOD);
821 	if (IS_ERR(data))
822 		return PTR_ERR(data);
823 
824 	/* try to read ppag table rev 3, 2 or 1 (all have the same data size) */
825 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
826 				ACPI_PPAG_WIFI_DATA_SIZE_V2, &tbl_rev);
827 
828 	if (!IS_ERR(wifi_pkg)) {
829 		if (tbl_rev >= 1 && tbl_rev <= 3) {
830 			num_sub_bands = IWL_NUM_SUB_BANDS_V2;
831 			IWL_DEBUG_RADIO(fwrt,
832 					"Reading PPAG table (tbl_rev=%d)\n",
833 					tbl_rev);
834 			goto read_table;
835 		} else {
836 			ret = -EINVAL;
837 			goto out_free;
838 		}
839 	}
840 
841 	/* try to read ppag table revision 0 */
842 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
843 			ACPI_PPAG_WIFI_DATA_SIZE_V1, &tbl_rev);
844 
845 	if (!IS_ERR(wifi_pkg)) {
846 		if (tbl_rev != 0) {
847 			ret = -EINVAL;
848 			goto out_free;
849 		}
850 		num_sub_bands = IWL_NUM_SUB_BANDS_V1;
851 		IWL_DEBUG_RADIO(fwrt, "Reading PPAG table v1 (tbl_rev=0)\n");
852 		goto read_table;
853 	}
854 
855 	ret = PTR_ERR(wifi_pkg);
856 	goto out_free;
857 
858 read_table:
859 	fwrt->ppag_ver = tbl_rev;
860 	flags = &wifi_pkg->package.elements[1];
861 
862 	if (flags->type != ACPI_TYPE_INTEGER) {
863 		ret = -EINVAL;
864 		goto out_free;
865 	}
866 
867 	fwrt->ppag_flags = iwl_bios_get_ppag_flags(flags->integer.value,
868 						   fwrt->ppag_ver);
869 
870 	/*
871 	 * read, verify gain values and save them into the PPAG table.
872 	 * first sub-band (j=0) corresponds to Low-Band (2.4GHz), and the
873 	 * following sub-bands to High-Band (5GHz).
874 	 */
875 	for (i = 0; i < IWL_NUM_CHAIN_LIMITS; i++) {
876 		for (j = 0; j < num_sub_bands; j++) {
877 			union acpi_object *ent;
878 
879 			ent = &wifi_pkg->package.elements[idx++];
880 			if (ent->type != ACPI_TYPE_INTEGER) {
881 				ret = -EINVAL;
882 				goto out_free;
883 			}
884 
885 			fwrt->ppag_chains[i].subbands[j] = ent->integer.value;
886 		}
887 	}
888 
889 	ret = 0;
890 
891 out_free:
892 	kfree(data);
893 	return ret;
894 }
895 
896 void iwl_acpi_get_phy_filters(struct iwl_fw_runtime *fwrt,
897 			      struct iwl_phy_specific_cfg *filters)
898 {
899 	struct iwl_phy_specific_cfg tmp = {};
900 	union acpi_object *wifi_pkg, *data;
901 	int tbl_rev, i;
902 
903 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WPFC_METHOD);
904 	if (IS_ERR(data))
905 		return;
906 
907 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
908 					 ACPI_WPFC_WIFI_DATA_SIZE,
909 					 &tbl_rev);
910 	if (IS_ERR(wifi_pkg))
911 		goto out_free;
912 
913 	if (tbl_rev != 0)
914 		goto out_free;
915 
916 	BUILD_BUG_ON(ARRAY_SIZE(filters->filter_cfg_chains) !=
917 		     ACPI_WPFC_WIFI_DATA_SIZE - 1);
918 
919 	for (i = 0; i < ARRAY_SIZE(filters->filter_cfg_chains); i++) {
920 		if (wifi_pkg->package.elements[i + 1].type != ACPI_TYPE_INTEGER)
921 			goto out_free;
922 		tmp.filter_cfg_chains[i] =
923 			cpu_to_le32(wifi_pkg->package.elements[i + 1].integer.value);
924 	}
925 
926 	IWL_DEBUG_RADIO(fwrt, "Loaded WPFC filter config from ACPI\n");
927 	*filters = tmp;
928 out_free:
929 	kfree(data);
930 }
931 IWL_EXPORT_SYMBOL(iwl_acpi_get_phy_filters);
932 
933 void iwl_acpi_get_guid_lock_status(struct iwl_fw_runtime *fwrt)
934 {
935 	union acpi_object *wifi_pkg, *data;
936 	int tbl_rev;
937 
938 	data = iwl_acpi_get_object(fwrt->dev, ACPI_GLAI_METHOD);
939 	if (IS_ERR(data))
940 		return;
941 
942 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
943 					 ACPI_GLAI_WIFI_DATA_SIZE,
944 					 &tbl_rev);
945 	if (IS_ERR(wifi_pkg))
946 		goto out_free;
947 
948 	if (tbl_rev != 0) {
949 		IWL_DEBUG_RADIO(fwrt, "Invalid GLAI revision: %d\n", tbl_rev);
950 		goto out_free;
951 	}
952 
953 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
954 	    wifi_pkg->package.elements[1].integer.value > ACPI_GLAI_MAX_STATUS)
955 		goto out_free;
956 
957 	fwrt->uefi_tables_lock_status =
958 		wifi_pkg->package.elements[1].integer.value;
959 
960 	IWL_DEBUG_RADIO(fwrt,
961 			"Loaded UEFI WIFI GUID lock status: %d from ACPI\n",
962 			fwrt->uefi_tables_lock_status);
963 out_free:
964 	kfree(data);
965 }
966 IWL_EXPORT_SYMBOL(iwl_acpi_get_guid_lock_status);
967 
968 int iwl_acpi_get_wbem(struct iwl_fw_runtime *fwrt, u32 *value)
969 {
970 	union acpi_object *wifi_pkg, *data;
971 	int ret = -ENOENT;
972 	int tbl_rev;
973 
974 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WBEM_METHOD);
975 	if (IS_ERR(data))
976 		return ret;
977 
978 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
979 					 ACPI_WBEM_WIFI_DATA_SIZE,
980 					 &tbl_rev);
981 	if (IS_ERR(wifi_pkg))
982 		goto out_free;
983 
984 	if (tbl_rev != IWL_ACPI_WBEM_REVISION) {
985 		IWL_DEBUG_RADIO(fwrt, "Unsupported ACPI WBEM revision:%d\n",
986 				tbl_rev);
987 		goto out_free;
988 	}
989 
990 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
991 		goto out_free;
992 
993 	*value = wifi_pkg->package.elements[1].integer.value &
994 		 IWL_ACPI_WBEM_REV0_MASK;
995 	IWL_DEBUG_RADIO(fwrt, "Loaded WBEM config from ACPI\n");
996 	ret = 0;
997 out_free:
998 	kfree(data);
999 	return ret;
1000 }
1001