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