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