1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright(c) 2021-2023 Intel Corporation
12 #include "fw/api/alive.h"
13 #include <linux/efi.h>
14 #include "fw/runtime.h"
16 #define IWL_EFI_VAR_GUID EFI_GUID(0x92daaf2f, 0xc02b, 0x455b, \
17 0xb2, 0xec, 0xf5, 0xa3, \
18 0x59, 0x4f, 0x4a, 0xea)
20 struct iwl_uefi_pnvm_mem_desc {
26 static void *iwl_uefi_get_variable(efi_char16_t *name, efi_guid_t *guid,
27 unsigned long *data_size)
33 return ERR_PTR(-EINVAL);
35 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
36 return ERR_PTR(-ENODEV);
38 /* first call with NULL data to get the exact entry size */
40 status = efi.get_variable(name, guid, NULL, data_size, NULL);
41 if (status != EFI_BUFFER_TOO_SMALL || !*data_size)
44 data = kmalloc(*data_size, GFP_KERNEL);
46 return ERR_PTR(-ENOMEM);
48 status = efi.get_variable(name, guid, NULL, data_size, data);
49 if (status != EFI_SUCCESS) {
51 return ERR_PTR(-ENOENT);
57 void *iwl_uefi_get_pnvm(struct iwl_trans *trans, size_t *len)
59 unsigned long package_size;
64 data = iwl_uefi_get_variable(IWL_UEFI_OEM_PNVM_NAME, &IWL_EFI_VAR_GUID,
68 "PNVM UEFI variable not found 0x%lx (len %lu)\n",
69 PTR_ERR(data), package_size);
73 IWL_DEBUG_FW(trans, "Read PNVM from UEFI with size %lu\n", package_size);
79 int iwl_uefi_handle_tlv_mem_desc(struct iwl_trans *trans, const u8 *data,
80 u32 tlv_len, struct iwl_pnvm_image *pnvm_data)
82 const struct iwl_uefi_pnvm_mem_desc *desc = (const void *)data;
85 if (tlv_len < sizeof(*desc)) {
86 IWL_DEBUG_FW(trans, "TLV len (%d) is too small\n", tlv_len);
90 data_len = tlv_len - sizeof(*desc);
93 "Handle IWL_UCODE_TLV_MEM_DESC, len %d data_len %d\n",
96 if (le32_to_cpu(desc->size) != data_len) {
97 IWL_DEBUG_FW(trans, "invalid mem desc size %d\n", desc->size);
101 if (pnvm_data->n_chunks == IPC_DRAM_MAP_ENTRY_NUM_MAX) {
102 IWL_DEBUG_FW(trans, "too many payloads to allocate in DRAM.\n");
106 IWL_DEBUG_FW(trans, "Adding data (size %d)\n", data_len);
108 pnvm_data->chunks[pnvm_data->n_chunks].data = desc->data;
109 pnvm_data->chunks[pnvm_data->n_chunks].len = data_len;
110 pnvm_data->n_chunks++;
115 static int iwl_uefi_reduce_power_section(struct iwl_trans *trans,
116 const u8 *data, size_t len,
117 struct iwl_pnvm_image *pnvm_data)
119 const struct iwl_ucode_tlv *tlv;
121 IWL_DEBUG_FW(trans, "Handling REDUCE_POWER section\n");
122 memset(pnvm_data, 0, sizeof(*pnvm_data));
124 while (len >= sizeof(*tlv)) {
125 u32 tlv_len, tlv_type;
128 tlv = (const void *)data;
130 tlv_len = le32_to_cpu(tlv->length);
131 tlv_type = le32_to_cpu(tlv->type);
134 IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
139 data += sizeof(*tlv);
142 case IWL_UCODE_TLV_MEM_DESC:
143 if (iwl_uefi_handle_tlv_mem_desc(trans, data, tlv_len,
147 case IWL_UCODE_TLV_PNVM_SKU:
149 "New REDUCE_POWER section started, stop parsing.\n");
152 IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n",
157 len -= ALIGN(tlv_len, 4);
158 data += ALIGN(tlv_len, 4);
162 if (!pnvm_data->n_chunks) {
163 IWL_DEBUG_FW(trans, "Empty REDUCE_POWER, skipping.\n");
169 int iwl_uefi_reduce_power_parse(struct iwl_trans *trans,
170 const u8 *data, size_t len,
171 struct iwl_pnvm_image *pnvm_data)
173 const struct iwl_ucode_tlv *tlv;
175 IWL_DEBUG_FW(trans, "Parsing REDUCE_POWER data\n");
177 while (len >= sizeof(*tlv)) {
178 u32 tlv_len, tlv_type;
181 tlv = (const void *)data;
183 tlv_len = le32_to_cpu(tlv->length);
184 tlv_type = le32_to_cpu(tlv->type);
187 IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
192 if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) {
193 const struct iwl_sku_id *sku_id =
194 (const void *)(data + sizeof(*tlv));
197 "Got IWL_UCODE_TLV_PNVM_SKU len %d\n",
199 IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n",
200 le32_to_cpu(sku_id->data[0]),
201 le32_to_cpu(sku_id->data[1]),
202 le32_to_cpu(sku_id->data[2]));
204 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
205 len -= ALIGN(tlv_len, 4);
207 if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) &&
208 trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) &&
209 trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) {
210 int ret = iwl_uefi_reduce_power_section(trans,
216 IWL_DEBUG_FW(trans, "SKU ID didn't match!\n");
219 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
220 len -= ALIGN(tlv_len, 4);
227 u8 *iwl_uefi_get_reduced_power(struct iwl_trans *trans, size_t *len)
229 struct pnvm_sku_package *package;
230 unsigned long package_size;
233 package = iwl_uefi_get_variable(IWL_UEFI_REDUCED_POWER_NAME,
234 &IWL_EFI_VAR_GUID, &package_size);
236 if (IS_ERR(package)) {
238 "Reduced Power UEFI variable not found 0x%lx (len %lu)\n",
239 PTR_ERR(package), package_size);
240 return ERR_CAST(package);
243 if (package_size < sizeof(*package)) {
245 "Invalid Reduced Power UEFI variable len (%lu)\n",
248 return ERR_PTR(-EINVAL);
251 IWL_DEBUG_FW(trans, "Read reduced power from UEFI with size %lu\n",
254 IWL_DEBUG_FW(trans, "rev %d, total_size %d, n_skus %d\n",
255 package->rev, package->total_size, package->n_skus);
257 *len = package_size - sizeof(*package);
258 data = kmemdup(package->data, *len, GFP_KERNEL);
261 return ERR_PTR(-ENOMEM);
269 static int iwl_uefi_step_parse(struct uefi_cnv_common_step_data *common_step_data,
270 struct iwl_trans *trans)
272 if (common_step_data->revision != 1)
275 trans->mbx_addr_0_step = (u32)common_step_data->revision |
276 (u32)common_step_data->cnvi_eq_channel << 8 |
277 (u32)common_step_data->cnvr_eq_channel << 16 |
278 (u32)common_step_data->radio1 << 24;
279 trans->mbx_addr_1_step = (u32)common_step_data->radio2;
283 void iwl_uefi_get_step_table(struct iwl_trans *trans)
285 struct uefi_cnv_common_step_data *data;
286 unsigned long package_size;
289 if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
292 data = iwl_uefi_get_variable(IWL_UEFI_STEP_NAME, &IWL_EFI_VAR_GUID,
297 "STEP UEFI variable not found 0x%lx\n",
302 if (package_size < sizeof(*data)) {
304 "Invalid STEP table UEFI variable len (%lu)\n",
310 IWL_DEBUG_FW(trans, "Read STEP from UEFI with size %lu\n",
313 ret = iwl_uefi_step_parse(data, trans);
315 IWL_DEBUG_FW(trans, "Cannot read STEP tables. rev is invalid\n");
319 IWL_EXPORT_SYMBOL(iwl_uefi_get_step_table);
322 static int iwl_uefi_sgom_parse(struct uefi_cnv_wlan_sgom_data *sgom_data,
323 struct iwl_fw_runtime *fwrt)
327 if (sgom_data->revision != 1)
330 memcpy(fwrt->sgom_table.offset_map, sgom_data->offset_map,
331 sizeof(fwrt->sgom_table.offset_map));
333 for (i = 0; i < MCC_TO_SAR_OFFSET_TABLE_ROW_SIZE; i++) {
334 for (j = 0; j < MCC_TO_SAR_OFFSET_TABLE_COL_SIZE; j++) {
335 /* since each byte is composed of to values, */
336 /* one for each letter, */
337 /* extract and check each of them separately */
338 u8 value = fwrt->sgom_table.offset_map[i][j];
339 u8 low = value & 0xF;
340 u8 high = (value & 0xF0) >> 4;
342 if (high > fwrt->geo_num_profiles)
344 if (low > fwrt->geo_num_profiles)
346 fwrt->sgom_table.offset_map[i][j] = (high << 4) | low;
350 fwrt->sgom_enabled = true;
354 void iwl_uefi_get_sgom_table(struct iwl_trans *trans,
355 struct iwl_fw_runtime *fwrt)
357 struct uefi_cnv_wlan_sgom_data *data;
358 unsigned long package_size;
361 if (!fwrt->geo_enabled)
364 data = iwl_uefi_get_variable(IWL_UEFI_SGOM_NAME, &IWL_EFI_VAR_GUID,
368 "SGOM UEFI variable not found 0x%lx\n",
373 if (package_size < sizeof(*data)) {
375 "Invalid SGOM table UEFI variable len (%lu)\n",
381 IWL_DEBUG_FW(trans, "Read SGOM from UEFI with size %lu\n",
384 ret = iwl_uefi_sgom_parse(data, fwrt);
386 IWL_DEBUG_FW(trans, "Cannot read SGOM tables. rev is invalid\n");
390 IWL_EXPORT_SYMBOL(iwl_uefi_get_sgom_table);
392 static int iwl_uefi_uats_parse(struct uefi_cnv_wlan_uats_data *uats_data,
393 struct iwl_fw_runtime *fwrt)
395 if (uats_data->revision != 1)
398 memcpy(fwrt->uats_table.offset_map, uats_data->offset_map,
399 sizeof(fwrt->uats_table.offset_map));
403 int iwl_uefi_get_uats_table(struct iwl_trans *trans,
404 struct iwl_fw_runtime *fwrt)
406 struct uefi_cnv_wlan_uats_data *data;
407 unsigned long package_size;
410 data = iwl_uefi_get_variable(IWL_UEFI_UATS_NAME, &IWL_EFI_VAR_GUID,
414 "UATS UEFI variable not found 0x%lx\n",
419 if (package_size < sizeof(*data)) {
421 "Invalid UATS table UEFI variable len (%lu)\n",
427 IWL_DEBUG_FW(trans, "Read UATS from UEFI with size %lu\n",
430 ret = iwl_uefi_uats_parse(data, fwrt);
432 IWL_DEBUG_FW(trans, "Cannot read UATS table. rev is invalid\n");
440 IWL_EXPORT_SYMBOL(iwl_uefi_get_uats_table);
441 #endif /* CONFIG_ACPI */