xref: /linux/drivers/net/wireless/intel/iwlwifi/fw/acpi.c (revision 2fe3c78a2c26dd5ee811024a1b7d6cfb4d654319)
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 	if (mcc_val != BIOS_MCC_CHINA) {
361 		ret = -EINVAL;
362 		IWL_DEBUG_RADIO(fwrt, "ACPI WRDD is supported only for CN\n");
363 		goto out_free;
364 	}
365 
366 	mcc[0] = (mcc_val >> 8) & 0xff;
367 	mcc[1] = mcc_val & 0xff;
368 	mcc[2] = '\0';
369 
370 	ret = 0;
371 out_free:
372 	kfree(data);
373 	return ret;
374 }
375 
376 int iwl_acpi_get_pwr_limit(struct iwl_fw_runtime *fwrt, u64 *dflt_pwr_limit)
377 {
378 	union acpi_object *data, *wifi_pkg;
379 	int tbl_rev, ret = -EINVAL;
380 
381 	*dflt_pwr_limit = 0;
382 	data = iwl_acpi_get_object(fwrt->dev, ACPI_SPLC_METHOD);
383 	if (IS_ERR(data))
384 		goto out;
385 
386 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
387 					 ACPI_SPLC_WIFI_DATA_SIZE, &tbl_rev);
388 	if (IS_ERR(wifi_pkg) || tbl_rev != 0 ||
389 	    wifi_pkg->package.elements[1].integer.value != ACPI_TYPE_INTEGER)
390 		goto out_free;
391 
392 	*dflt_pwr_limit = wifi_pkg->package.elements[1].integer.value;
393 	ret = 0;
394 out_free:
395 	kfree(data);
396 out:
397 	return ret;
398 }
399 
400 int iwl_acpi_get_eckv(struct iwl_fw_runtime *fwrt, u32 *extl_clk)
401 {
402 	union acpi_object *wifi_pkg, *data;
403 	int ret, tbl_rev;
404 
405 	data = iwl_acpi_get_object(fwrt->dev, ACPI_ECKV_METHOD);
406 	if (IS_ERR(data))
407 		return PTR_ERR(data);
408 
409 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
410 					 ACPI_ECKV_WIFI_DATA_SIZE,
411 					 &tbl_rev);
412 	if (IS_ERR(wifi_pkg)) {
413 		ret = PTR_ERR(wifi_pkg);
414 		goto out_free;
415 	}
416 
417 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
418 	    tbl_rev != 0) {
419 		ret = -EINVAL;
420 		goto out_free;
421 	}
422 
423 	*extl_clk = wifi_pkg->package.elements[1].integer.value;
424 
425 	ret = 0;
426 
427 out_free:
428 	kfree(data);
429 	return ret;
430 }
431 
432 static int iwl_acpi_sar_set_profile(union acpi_object *table,
433 				    struct iwl_sar_profile *profile,
434 				    bool enabled, u8 num_chains,
435 				    u8 num_sub_bands)
436 {
437 	int i, j, idx = 0;
438 
439 	/*
440 	 * The table from ACPI is flat, but we store it in a
441 	 * structured array.
442 	 */
443 	for (i = 0; i < BIOS_SAR_MAX_CHAINS_PER_PROFILE; i++) {
444 		for (j = 0; j < BIOS_SAR_MAX_SUB_BANDS_NUM; j++) {
445 			/* if we don't have the values, use the default */
446 			if (i >= num_chains || j >= num_sub_bands) {
447 				profile->chains[i].subbands[j] = 0;
448 			} else {
449 				if (table[idx].type != ACPI_TYPE_INTEGER ||
450 				    table[idx].integer.value > U8_MAX)
451 					return -EINVAL;
452 
453 				profile->chains[i].subbands[j] =
454 					table[idx].integer.value;
455 
456 				idx++;
457 			}
458 		}
459 	}
460 
461 	/* Only if all values were valid can the profile be enabled */
462 	profile->enabled = enabled;
463 
464 	return 0;
465 }
466 
467 int iwl_acpi_get_wrds_table(struct iwl_fw_runtime *fwrt)
468 {
469 	union acpi_object *wifi_pkg, *table, *data;
470 	int ret, tbl_rev;
471 	u32 flags;
472 	u8 num_chains, num_sub_bands;
473 
474 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WRDS_METHOD);
475 	if (IS_ERR(data))
476 		return PTR_ERR(data);
477 
478 	/* start by trying to read revision 2 */
479 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
480 					 ACPI_WRDS_WIFI_DATA_SIZE_REV2,
481 					 &tbl_rev);
482 	if (!IS_ERR(wifi_pkg)) {
483 		if (tbl_rev != 2) {
484 			ret = -EINVAL;
485 			goto out_free;
486 		}
487 
488 		num_chains = ACPI_SAR_NUM_CHAINS_REV2;
489 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV2;
490 
491 		goto read_table;
492 	}
493 
494 	/* then try revision 1 */
495 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
496 					 ACPI_WRDS_WIFI_DATA_SIZE_REV1,
497 					 &tbl_rev);
498 	if (!IS_ERR(wifi_pkg)) {
499 		if (tbl_rev != 1) {
500 			ret = -EINVAL;
501 			goto out_free;
502 		}
503 
504 		num_chains = ACPI_SAR_NUM_CHAINS_REV1;
505 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV1;
506 
507 		goto read_table;
508 	}
509 
510 	/* then finally revision 0 */
511 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
512 					 ACPI_WRDS_WIFI_DATA_SIZE_REV0,
513 					 &tbl_rev);
514 	if (!IS_ERR(wifi_pkg)) {
515 		if (tbl_rev != 0) {
516 			ret = -EINVAL;
517 			goto out_free;
518 		}
519 
520 		num_chains = ACPI_SAR_NUM_CHAINS_REV0;
521 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV0;
522 
523 		goto read_table;
524 	}
525 
526 	ret = PTR_ERR(wifi_pkg);
527 	goto out_free;
528 
529 read_table:
530 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER) {
531 		ret = -EINVAL;
532 		goto out_free;
533 	}
534 
535 	IWL_DEBUG_RADIO(fwrt, "Reading WRDS tbl_rev=%d\n", tbl_rev);
536 
537 	flags = wifi_pkg->package.elements[1].integer.value;
538 	fwrt->reduced_power_flags = flags >> IWL_REDUCE_POWER_FLAGS_POS;
539 
540 	/* position of the actual table */
541 	table = &wifi_pkg->package.elements[2];
542 
543 	/* The profile from WRDS is officially profile 1, but goes
544 	 * into sar_profiles[0] (because we don't have a profile 0).
545 	 */
546 	ret = iwl_acpi_sar_set_profile(table, &fwrt->sar_profiles[0],
547 				       flags & IWL_SAR_ENABLE_MSK,
548 				       num_chains, num_sub_bands);
549 out_free:
550 	kfree(data);
551 	return ret;
552 }
553 
554 int iwl_acpi_get_ewrd_table(struct iwl_fw_runtime *fwrt)
555 {
556 	union acpi_object *wifi_pkg, *data;
557 	bool enabled;
558 	int i, n_profiles, tbl_rev, pos;
559 	int ret = 0;
560 	u8 num_chains, num_sub_bands;
561 
562 	data = iwl_acpi_get_object(fwrt->dev, ACPI_EWRD_METHOD);
563 	if (IS_ERR(data))
564 		return PTR_ERR(data);
565 
566 	/* start by trying to read revision 2 */
567 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
568 					 ACPI_EWRD_WIFI_DATA_SIZE_REV2,
569 					 &tbl_rev);
570 	if (!IS_ERR(wifi_pkg)) {
571 		if (tbl_rev != 2) {
572 			ret = -EINVAL;
573 			goto out_free;
574 		}
575 
576 		num_chains = ACPI_SAR_NUM_CHAINS_REV2;
577 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV2;
578 
579 		goto read_table;
580 	}
581 
582 	/* then try revision 1 */
583 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
584 					 ACPI_EWRD_WIFI_DATA_SIZE_REV1,
585 					 &tbl_rev);
586 	if (!IS_ERR(wifi_pkg)) {
587 		if (tbl_rev != 1) {
588 			ret = -EINVAL;
589 			goto out_free;
590 		}
591 
592 		num_chains = ACPI_SAR_NUM_CHAINS_REV1;
593 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV1;
594 
595 		goto read_table;
596 	}
597 
598 	/* then finally revision 0 */
599 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
600 					 ACPI_EWRD_WIFI_DATA_SIZE_REV0,
601 					 &tbl_rev);
602 	if (!IS_ERR(wifi_pkg)) {
603 		if (tbl_rev != 0) {
604 			ret = -EINVAL;
605 			goto out_free;
606 		}
607 
608 		num_chains = ACPI_SAR_NUM_CHAINS_REV0;
609 		num_sub_bands = ACPI_SAR_NUM_SUB_BANDS_REV0;
610 
611 		goto read_table;
612 	}
613 
614 	ret = PTR_ERR(wifi_pkg);
615 	goto out_free;
616 
617 read_table:
618 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
619 	    wifi_pkg->package.elements[2].type != ACPI_TYPE_INTEGER) {
620 		ret = -EINVAL;
621 		goto out_free;
622 	}
623 
624 	enabled = !!(wifi_pkg->package.elements[1].integer.value);
625 	n_profiles = wifi_pkg->package.elements[2].integer.value;
626 
627 	/*
628 	 * Check the validity of n_profiles.  The EWRD profiles start
629 	 * from index 1, so the maximum value allowed here is
630 	 * ACPI_SAR_PROFILES_NUM - 1.
631 	 */
632 	if (n_profiles >= BIOS_SAR_MAX_PROFILE_NUM) {
633 		ret = -EINVAL;
634 		goto out_free;
635 	}
636 
637 	/* the tables start at element 3 */
638 	pos = 3;
639 
640 	for (i = 0; i < n_profiles; i++) {
641 		union acpi_object *table = &wifi_pkg->package.elements[pos];
642 		/* The EWRD profiles officially go from 2 to 4, but we
643 		 * save them in sar_profiles[1-3] (because we don't
644 		 * have profile 0).  So in the array we start from 1.
645 		 */
646 		ret = iwl_acpi_sar_set_profile(table,
647 					       &fwrt->sar_profiles[i + 1],
648 					       enabled, num_chains,
649 					       num_sub_bands);
650 		if (ret < 0)
651 			break;
652 
653 		/* go to the next table */
654 		pos += num_chains * num_sub_bands;
655 	}
656 
657 out_free:
658 	kfree(data);
659 	return ret;
660 }
661 
662 int iwl_acpi_get_wgds_table(struct iwl_fw_runtime *fwrt)
663 {
664 	union acpi_object *wifi_pkg, *data;
665 	int i, j, k, ret, tbl_rev;
666 	u8 num_bands, num_profiles;
667 	static const struct {
668 		u8 revisions;
669 		u8 bands;
670 		u8 profiles;
671 		u8 min_profiles;
672 	} rev_data[] = {
673 		{
674 			.revisions = BIT(3),
675 			.bands = ACPI_GEO_NUM_BANDS_REV2,
676 			.profiles = ACPI_NUM_GEO_PROFILES_REV3,
677 			.min_profiles = BIOS_GEO_MIN_PROFILE_NUM,
678 		},
679 		{
680 			.revisions = BIT(2),
681 			.bands = ACPI_GEO_NUM_BANDS_REV2,
682 			.profiles = ACPI_NUM_GEO_PROFILES,
683 		},
684 		{
685 			.revisions = BIT(0) | BIT(1),
686 			.bands = ACPI_GEO_NUM_BANDS_REV0,
687 			.profiles = ACPI_NUM_GEO_PROFILES,
688 		},
689 	};
690 	int idx;
691 	/* start from one to skip the domain */
692 	int entry_idx = 1;
693 
694 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES_REV3 != IWL_NUM_GEO_PROFILES_V3);
695 	BUILD_BUG_ON(ACPI_NUM_GEO_PROFILES != IWL_NUM_GEO_PROFILES);
696 
697 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WGDS_METHOD);
698 	if (IS_ERR(data))
699 		return PTR_ERR(data);
700 
701 	/* read the highest revision we understand first */
702 	for (idx = 0; idx < ARRAY_SIZE(rev_data); idx++) {
703 		/* min_profiles != 0 requires num_profiles header */
704 		u32 hdr_size = 1 + !!rev_data[idx].min_profiles;
705 		u32 profile_size = ACPI_GEO_PER_CHAIN_SIZE *
706 				   rev_data[idx].bands;
707 		u32 max_size = hdr_size + profile_size * rev_data[idx].profiles;
708 		u32 min_size;
709 
710 		if (!rev_data[idx].min_profiles)
711 			min_size = max_size;
712 		else
713 			min_size = hdr_size +
714 				   profile_size * rev_data[idx].min_profiles;
715 
716 		wifi_pkg = iwl_acpi_get_wifi_pkg_range(fwrt->dev, data,
717 						       min_size, max_size,
718 						       &tbl_rev);
719 		if (!IS_ERR(wifi_pkg)) {
720 			if (!(BIT(tbl_rev) & rev_data[idx].revisions))
721 				continue;
722 
723 			num_bands = rev_data[idx].bands;
724 			num_profiles = rev_data[idx].profiles;
725 
726 			if (rev_data[idx].min_profiles) {
727 				/* read header that says # of profiles */
728 				union acpi_object *entry;
729 
730 				entry = &wifi_pkg->package.elements[entry_idx];
731 				entry_idx++;
732 				if (entry->type != ACPI_TYPE_INTEGER ||
733 				    entry->integer.value > num_profiles ||
734 				    entry->integer.value <
735 					rev_data[idx].min_profiles) {
736 					ret = -EINVAL;
737 					goto out_free;
738 				}
739 
740 				/*
741 				 * Check to see if we received package count
742 				 * same as max # of profiles
743 				 */
744 				if (wifi_pkg->package.count !=
745 				    hdr_size + profile_size * num_profiles) {
746 					ret = -EINVAL;
747 					goto out_free;
748 				}
749 
750 				/* Number of valid profiles */
751 				num_profiles = entry->integer.value;
752 			}
753 			goto read_table;
754 		}
755 	}
756 
757 	if (idx < ARRAY_SIZE(rev_data))
758 		ret = PTR_ERR(wifi_pkg);
759 	else
760 		ret = -ENOENT;
761 	goto out_free;
762 
763 read_table:
764 	fwrt->geo_rev = tbl_rev;
765 	for (i = 0; i < num_profiles; i++) {
766 		for (j = 0; j < BIOS_GEO_MAX_NUM_BANDS; j++) {
767 			union acpi_object *entry;
768 
769 			/*
770 			 * num_bands is either 2 or 3, if it's only 2 then
771 			 * fill the third band (6 GHz) with the values from
772 			 * 5 GHz (second band)
773 			 */
774 			if (j >= num_bands) {
775 				fwrt->geo_profiles[i].bands[j].max =
776 					fwrt->geo_profiles[i].bands[1].max;
777 			} else {
778 				entry = &wifi_pkg->package.elements[entry_idx];
779 				entry_idx++;
780 				if (entry->type != ACPI_TYPE_INTEGER ||
781 				    entry->integer.value > U8_MAX) {
782 					ret = -EINVAL;
783 					goto out_free;
784 				}
785 
786 				fwrt->geo_profiles[i].bands[j].max =
787 					entry->integer.value;
788 			}
789 
790 			for (k = 0; k < BIOS_GEO_NUM_CHAINS; k++) {
791 				/* same here as above */
792 				if (j >= num_bands) {
793 					fwrt->geo_profiles[i].bands[j].chains[k] =
794 						fwrt->geo_profiles[i].bands[1].chains[k];
795 				} else {
796 					entry = &wifi_pkg->package.elements[entry_idx];
797 					entry_idx++;
798 					if (entry->type != ACPI_TYPE_INTEGER ||
799 					    entry->integer.value > U8_MAX) {
800 						ret = -EINVAL;
801 						goto out_free;
802 					}
803 
804 					fwrt->geo_profiles[i].bands[j].chains[k] =
805 						entry->integer.value;
806 				}
807 			}
808 		}
809 	}
810 
811 	fwrt->geo_num_profiles = num_profiles;
812 	fwrt->geo_enabled = true;
813 	ret = 0;
814 out_free:
815 	kfree(data);
816 	return ret;
817 }
818 
819 int iwl_acpi_get_ppag_table(struct iwl_fw_runtime *fwrt)
820 {
821 	union acpi_object *wifi_pkg, *data, *flags;
822 	int i, j, ret, tbl_rev, num_sub_bands = 0;
823 	int idx = 2;
824 
825 	data = iwl_acpi_get_object(fwrt->dev, ACPI_PPAG_METHOD);
826 	if (IS_ERR(data))
827 		return PTR_ERR(data);
828 
829 	/* try to read ppag table rev 3, 2 or 1 (all have the same data size) */
830 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
831 				ACPI_PPAG_WIFI_DATA_SIZE_V2, &tbl_rev);
832 
833 	if (!IS_ERR(wifi_pkg)) {
834 		if (tbl_rev >= 1 && tbl_rev <= 3) {
835 			num_sub_bands = IWL_NUM_SUB_BANDS_V2;
836 			IWL_DEBUG_RADIO(fwrt,
837 					"Reading PPAG table (tbl_rev=%d)\n",
838 					tbl_rev);
839 			goto read_table;
840 		} else {
841 			ret = -EINVAL;
842 			goto out_free;
843 		}
844 	}
845 
846 	/* try to read ppag table revision 0 */
847 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
848 			ACPI_PPAG_WIFI_DATA_SIZE_V1, &tbl_rev);
849 
850 	if (!IS_ERR(wifi_pkg)) {
851 		if (tbl_rev != 0) {
852 			ret = -EINVAL;
853 			goto out_free;
854 		}
855 		num_sub_bands = IWL_NUM_SUB_BANDS_V1;
856 		IWL_DEBUG_RADIO(fwrt, "Reading PPAG table v1 (tbl_rev=0)\n");
857 		goto read_table;
858 	}
859 
860 	ret = PTR_ERR(wifi_pkg);
861 	goto out_free;
862 
863 read_table:
864 	fwrt->ppag_ver = tbl_rev;
865 	flags = &wifi_pkg->package.elements[1];
866 
867 	if (flags->type != ACPI_TYPE_INTEGER) {
868 		ret = -EINVAL;
869 		goto out_free;
870 	}
871 
872 	fwrt->ppag_flags = iwl_bios_get_ppag_flags(flags->integer.value,
873 						   fwrt->ppag_ver);
874 
875 	/*
876 	 * read, verify gain values and save them into the PPAG table.
877 	 * first sub-band (j=0) corresponds to Low-Band (2.4GHz), and the
878 	 * following sub-bands to High-Band (5GHz).
879 	 */
880 	for (i = 0; i < IWL_NUM_CHAIN_LIMITS; i++) {
881 		for (j = 0; j < num_sub_bands; j++) {
882 			union acpi_object *ent;
883 
884 			ent = &wifi_pkg->package.elements[idx++];
885 			if (ent->type != ACPI_TYPE_INTEGER) {
886 				ret = -EINVAL;
887 				goto out_free;
888 			}
889 
890 			fwrt->ppag_chains[i].subbands[j] = ent->integer.value;
891 		}
892 	}
893 
894 	ret = 0;
895 
896 out_free:
897 	kfree(data);
898 	return ret;
899 }
900 
901 void iwl_acpi_get_phy_filters(struct iwl_fw_runtime *fwrt,
902 			      struct iwl_phy_specific_cfg *filters)
903 {
904 	struct iwl_phy_specific_cfg tmp = {};
905 	union acpi_object *wifi_pkg, *data;
906 	int tbl_rev, i;
907 
908 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WPFC_METHOD);
909 	if (IS_ERR(data))
910 		return;
911 
912 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
913 					 ACPI_WPFC_WIFI_DATA_SIZE,
914 					 &tbl_rev);
915 	if (IS_ERR(wifi_pkg))
916 		goto out_free;
917 
918 	if (tbl_rev != 0)
919 		goto out_free;
920 
921 	BUILD_BUG_ON(ARRAY_SIZE(filters->filter_cfg_chains) !=
922 		     ACPI_WPFC_WIFI_DATA_SIZE - 1);
923 
924 	for (i = 0; i < ARRAY_SIZE(filters->filter_cfg_chains); i++) {
925 		if (wifi_pkg->package.elements[i + 1].type != ACPI_TYPE_INTEGER)
926 			goto out_free;
927 		tmp.filter_cfg_chains[i] =
928 			cpu_to_le32(wifi_pkg->package.elements[i + 1].integer.value);
929 	}
930 
931 	IWL_DEBUG_RADIO(fwrt, "Loaded WPFC filter config from ACPI\n");
932 	*filters = tmp;
933 out_free:
934 	kfree(data);
935 }
936 IWL_EXPORT_SYMBOL(iwl_acpi_get_phy_filters);
937 
938 void iwl_acpi_get_guid_lock_status(struct iwl_fw_runtime *fwrt)
939 {
940 	union acpi_object *wifi_pkg, *data;
941 	int tbl_rev;
942 
943 	data = iwl_acpi_get_object(fwrt->dev, ACPI_GLAI_METHOD);
944 	if (IS_ERR(data))
945 		return;
946 
947 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
948 					 ACPI_GLAI_WIFI_DATA_SIZE,
949 					 &tbl_rev);
950 	if (IS_ERR(wifi_pkg))
951 		goto out_free;
952 
953 	if (tbl_rev != 0) {
954 		IWL_DEBUG_RADIO(fwrt, "Invalid GLAI revision: %d\n", tbl_rev);
955 		goto out_free;
956 	}
957 
958 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER ||
959 	    wifi_pkg->package.elements[1].integer.value > ACPI_GLAI_MAX_STATUS)
960 		goto out_free;
961 
962 	fwrt->uefi_tables_lock_status =
963 		wifi_pkg->package.elements[1].integer.value;
964 
965 	IWL_DEBUG_RADIO(fwrt,
966 			"Loaded UEFI WIFI GUID lock status: %d from ACPI\n",
967 			fwrt->uefi_tables_lock_status);
968 out_free:
969 	kfree(data);
970 }
971 IWL_EXPORT_SYMBOL(iwl_acpi_get_guid_lock_status);
972 
973 int iwl_acpi_get_wbem(struct iwl_fw_runtime *fwrt, u32 *value)
974 {
975 	union acpi_object *wifi_pkg, *data;
976 	int ret = -ENOENT;
977 	int tbl_rev;
978 
979 	data = iwl_acpi_get_object(fwrt->dev, ACPI_WBEM_METHOD);
980 	if (IS_ERR(data))
981 		return ret;
982 
983 	wifi_pkg = iwl_acpi_get_wifi_pkg(fwrt->dev, data,
984 					 ACPI_WBEM_WIFI_DATA_SIZE,
985 					 &tbl_rev);
986 	if (IS_ERR(wifi_pkg))
987 		goto out_free;
988 
989 	if (tbl_rev != IWL_ACPI_WBEM_REVISION) {
990 		IWL_DEBUG_RADIO(fwrt, "Unsupported ACPI WBEM revision:%d\n",
991 				tbl_rev);
992 		goto out_free;
993 	}
994 
995 	if (wifi_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
996 		goto out_free;
997 
998 	*value = wifi_pkg->package.elements[1].integer.value &
999 		 IWL_ACPI_WBEM_REV0_MASK;
1000 	IWL_DEBUG_RADIO(fwrt, "Loaded WBEM config from ACPI\n");
1001 	ret = 0;
1002 out_free:
1003 	kfree(data);
1004 	return ret;
1005 }
1006