184c3c995SLuca Coelho // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 284c3c995SLuca Coelho /* 384c3c995SLuca Coelho * Copyright(c) 2021 Intel Corporation 484c3c995SLuca Coelho */ 584c3c995SLuca Coelho 684c3c995SLuca Coelho #include "iwl-drv.h" 784c3c995SLuca Coelho #include "pnvm.h" 884c3c995SLuca Coelho #include "iwl-prph.h" 984c3c995SLuca Coelho #include "iwl-io.h" 1084c3c995SLuca Coelho 1184c3c995SLuca Coelho #include "fw/uefi.h" 129dad325fSLuca Coelho #include "fw/api/alive.h" 1384c3c995SLuca Coelho #include <linux/efi.h> 14c593d2faSAyala Barazani #include "fw/runtime.h" 1584c3c995SLuca Coelho 1684c3c995SLuca Coelho #define IWL_EFI_VAR_GUID EFI_GUID(0x92daaf2f, 0xc02b, 0x455b, \ 1784c3c995SLuca Coelho 0xb2, 0xec, 0xf5, 0xa3, \ 1884c3c995SLuca Coelho 0x59, 0x4f, 0x4a, 0xea) 1984c3c995SLuca Coelho 2084c3c995SLuca Coelho void *iwl_uefi_get_pnvm(struct iwl_trans *trans, size_t *len) 2184c3c995SLuca Coelho { 2284c3c995SLuca Coelho void *data; 2384c3c995SLuca Coelho unsigned long package_size; 24*0c4bad7fSArd Biesheuvel efi_status_t status; 2584c3c995SLuca Coelho 269dad325fSLuca Coelho *len = 0; 279dad325fSLuca Coelho 28*0c4bad7fSArd Biesheuvel if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) 29*0c4bad7fSArd Biesheuvel return ERR_PTR(-ENODEV); 3084c3c995SLuca Coelho 3184c3c995SLuca Coelho /* 3284c3c995SLuca Coelho * TODO: we hardcode a maximum length here, because reading 3384c3c995SLuca Coelho * from the UEFI is not working. To implement this properly, 3484c3c995SLuca Coelho * we have to call efivar_entry_size(). 3584c3c995SLuca Coelho */ 3684c3c995SLuca Coelho package_size = IWL_HARDCODED_PNVM_SIZE; 3784c3c995SLuca Coelho 3884c3c995SLuca Coelho data = kmalloc(package_size, GFP_KERNEL); 39*0c4bad7fSArd Biesheuvel if (!data) 40*0c4bad7fSArd Biesheuvel return ERR_PTR(-ENOMEM); 4184c3c995SLuca Coelho 42*0c4bad7fSArd Biesheuvel status = efi.get_variable(IWL_UEFI_OEM_PNVM_NAME, &IWL_EFI_VAR_GUID, 43*0c4bad7fSArd Biesheuvel NULL, &package_size, data); 44*0c4bad7fSArd Biesheuvel if (status != EFI_SUCCESS) { 4584c3c995SLuca Coelho IWL_DEBUG_FW(trans, 46*0c4bad7fSArd Biesheuvel "PNVM UEFI variable not found 0x%lx (len %lu)\n", 47*0c4bad7fSArd Biesheuvel status, package_size); 4884c3c995SLuca Coelho kfree(data); 49*0c4bad7fSArd Biesheuvel return ERR_PTR(-ENOENT); 5084c3c995SLuca Coelho } 5184c3c995SLuca Coelho 521476ff21SLinus Torvalds IWL_DEBUG_FW(trans, "Read PNVM from UEFI with size %lu\n", package_size); 5384c3c995SLuca Coelho *len = package_size; 5484c3c995SLuca Coelho 5584c3c995SLuca Coelho return data; 5684c3c995SLuca Coelho } 579dad325fSLuca Coelho 589dad325fSLuca Coelho static void *iwl_uefi_reduce_power_section(struct iwl_trans *trans, 599dad325fSLuca Coelho const u8 *data, size_t len) 609dad325fSLuca Coelho { 6186e8e657SJohannes Berg const struct iwl_ucode_tlv *tlv; 629dad325fSLuca Coelho u8 *reduce_power_data = NULL, *tmp; 639dad325fSLuca Coelho u32 size = 0; 649dad325fSLuca Coelho 659dad325fSLuca Coelho IWL_DEBUG_FW(trans, "Handling REDUCE_POWER section\n"); 669dad325fSLuca Coelho 679dad325fSLuca Coelho while (len >= sizeof(*tlv)) { 689dad325fSLuca Coelho u32 tlv_len, tlv_type; 699dad325fSLuca Coelho 709dad325fSLuca Coelho len -= sizeof(*tlv); 7186e8e657SJohannes Berg tlv = (const void *)data; 729dad325fSLuca Coelho 739dad325fSLuca Coelho tlv_len = le32_to_cpu(tlv->length); 749dad325fSLuca Coelho tlv_type = le32_to_cpu(tlv->type); 759dad325fSLuca Coelho 769dad325fSLuca Coelho if (len < tlv_len) { 779dad325fSLuca Coelho IWL_ERR(trans, "invalid TLV len: %zd/%u\n", 789dad325fSLuca Coelho len, tlv_len); 79a571bc28SChristophe JAILLET kfree(reduce_power_data); 809dad325fSLuca Coelho reduce_power_data = ERR_PTR(-EINVAL); 819dad325fSLuca Coelho goto out; 829dad325fSLuca Coelho } 839dad325fSLuca Coelho 849dad325fSLuca Coelho data += sizeof(*tlv); 859dad325fSLuca Coelho 869dad325fSLuca Coelho switch (tlv_type) { 879dad325fSLuca Coelho case IWL_UCODE_TLV_MEM_DESC: { 889dad325fSLuca Coelho IWL_DEBUG_FW(trans, 899dad325fSLuca Coelho "Got IWL_UCODE_TLV_MEM_DESC len %d\n", 909dad325fSLuca Coelho tlv_len); 919dad325fSLuca Coelho 929dad325fSLuca Coelho IWL_DEBUG_FW(trans, "Adding data (size %d)\n", tlv_len); 939dad325fSLuca Coelho 949dad325fSLuca Coelho tmp = krealloc(reduce_power_data, size + tlv_len, GFP_KERNEL); 959dad325fSLuca Coelho if (!tmp) { 969dad325fSLuca Coelho IWL_DEBUG_FW(trans, 979dad325fSLuca Coelho "Couldn't allocate (more) reduce_power_data\n"); 989dad325fSLuca Coelho 99a571bc28SChristophe JAILLET kfree(reduce_power_data); 1009dad325fSLuca Coelho reduce_power_data = ERR_PTR(-ENOMEM); 1019dad325fSLuca Coelho goto out; 1029dad325fSLuca Coelho } 1039dad325fSLuca Coelho 1049dad325fSLuca Coelho reduce_power_data = tmp; 1059dad325fSLuca Coelho 1069dad325fSLuca Coelho memcpy(reduce_power_data + size, data, tlv_len); 1079dad325fSLuca Coelho 1089dad325fSLuca Coelho size += tlv_len; 1099dad325fSLuca Coelho 1109dad325fSLuca Coelho break; 1119dad325fSLuca Coelho } 1129dad325fSLuca Coelho case IWL_UCODE_TLV_PNVM_SKU: 1139dad325fSLuca Coelho IWL_DEBUG_FW(trans, 1149dad325fSLuca Coelho "New REDUCE_POWER section started, stop parsing.\n"); 1159dad325fSLuca Coelho goto done; 1169dad325fSLuca Coelho default: 1179dad325fSLuca Coelho IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n", 1189dad325fSLuca Coelho tlv_type, tlv_len); 1199dad325fSLuca Coelho break; 1209dad325fSLuca Coelho } 1219dad325fSLuca Coelho 1229dad325fSLuca Coelho len -= ALIGN(tlv_len, 4); 1239dad325fSLuca Coelho data += ALIGN(tlv_len, 4); 1249dad325fSLuca Coelho } 1259dad325fSLuca Coelho 1269dad325fSLuca Coelho done: 1279dad325fSLuca Coelho if (!size) { 1289dad325fSLuca Coelho IWL_DEBUG_FW(trans, "Empty REDUCE_POWER, skipping.\n"); 129a571bc28SChristophe JAILLET /* Better safe than sorry, but 'reduce_power_data' should 130a571bc28SChristophe JAILLET * always be NULL if !size. 131a571bc28SChristophe JAILLET */ 132a571bc28SChristophe JAILLET kfree(reduce_power_data); 1339dad325fSLuca Coelho reduce_power_data = ERR_PTR(-ENOENT); 1349dad325fSLuca Coelho goto out; 1359dad325fSLuca Coelho } 1369dad325fSLuca Coelho 1379dad325fSLuca Coelho IWL_INFO(trans, "loaded REDUCE_POWER\n"); 1389dad325fSLuca Coelho 1399dad325fSLuca Coelho out: 1409dad325fSLuca Coelho return reduce_power_data; 1419dad325fSLuca Coelho } 1429dad325fSLuca Coelho 1439dad325fSLuca Coelho static void *iwl_uefi_reduce_power_parse(struct iwl_trans *trans, 1449dad325fSLuca Coelho const u8 *data, size_t len) 1459dad325fSLuca Coelho { 14686e8e657SJohannes Berg const struct iwl_ucode_tlv *tlv; 1479dad325fSLuca Coelho void *sec_data; 1489dad325fSLuca Coelho 1499dad325fSLuca Coelho IWL_DEBUG_FW(trans, "Parsing REDUCE_POWER data\n"); 1509dad325fSLuca Coelho 1519dad325fSLuca Coelho while (len >= sizeof(*tlv)) { 1529dad325fSLuca Coelho u32 tlv_len, tlv_type; 1539dad325fSLuca Coelho 1549dad325fSLuca Coelho len -= sizeof(*tlv); 15586e8e657SJohannes Berg tlv = (const void *)data; 1569dad325fSLuca Coelho 1579dad325fSLuca Coelho tlv_len = le32_to_cpu(tlv->length); 1589dad325fSLuca Coelho tlv_type = le32_to_cpu(tlv->type); 1599dad325fSLuca Coelho 1609dad325fSLuca Coelho if (len < tlv_len) { 1619dad325fSLuca Coelho IWL_ERR(trans, "invalid TLV len: %zd/%u\n", 1629dad325fSLuca Coelho len, tlv_len); 1639dad325fSLuca Coelho return ERR_PTR(-EINVAL); 1649dad325fSLuca Coelho } 1659dad325fSLuca Coelho 1669dad325fSLuca Coelho if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) { 16786e8e657SJohannes Berg const struct iwl_sku_id *sku_id = 16886e8e657SJohannes Berg (const void *)(data + sizeof(*tlv)); 1699dad325fSLuca Coelho 1709dad325fSLuca Coelho IWL_DEBUG_FW(trans, 1719dad325fSLuca Coelho "Got IWL_UCODE_TLV_PNVM_SKU len %d\n", 1729dad325fSLuca Coelho tlv_len); 1739dad325fSLuca Coelho IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n", 1749dad325fSLuca Coelho le32_to_cpu(sku_id->data[0]), 1759dad325fSLuca Coelho le32_to_cpu(sku_id->data[1]), 1769dad325fSLuca Coelho le32_to_cpu(sku_id->data[2])); 1779dad325fSLuca Coelho 1789dad325fSLuca Coelho data += sizeof(*tlv) + ALIGN(tlv_len, 4); 1799dad325fSLuca Coelho len -= ALIGN(tlv_len, 4); 1809dad325fSLuca Coelho 1819dad325fSLuca Coelho if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) && 1829dad325fSLuca Coelho trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) && 1839dad325fSLuca Coelho trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) { 1849dad325fSLuca Coelho sec_data = iwl_uefi_reduce_power_section(trans, 1859dad325fSLuca Coelho data, 1869dad325fSLuca Coelho len); 1879dad325fSLuca Coelho if (!IS_ERR(sec_data)) 1889dad325fSLuca Coelho return sec_data; 1899dad325fSLuca Coelho } else { 1909dad325fSLuca Coelho IWL_DEBUG_FW(trans, "SKU ID didn't match!\n"); 1919dad325fSLuca Coelho } 1929dad325fSLuca Coelho } else { 1939dad325fSLuca Coelho data += sizeof(*tlv) + ALIGN(tlv_len, 4); 1949dad325fSLuca Coelho len -= ALIGN(tlv_len, 4); 1959dad325fSLuca Coelho } 1969dad325fSLuca Coelho } 1979dad325fSLuca Coelho 1989dad325fSLuca Coelho return ERR_PTR(-ENOENT); 1999dad325fSLuca Coelho } 2009dad325fSLuca Coelho 2019dad325fSLuca Coelho void *iwl_uefi_get_reduced_power(struct iwl_trans *trans, size_t *len) 2029dad325fSLuca Coelho { 2039dad325fSLuca Coelho struct pnvm_sku_package *package; 2049dad325fSLuca Coelho void *data = NULL; 2059dad325fSLuca Coelho unsigned long package_size; 206*0c4bad7fSArd Biesheuvel efi_status_t status; 2079dad325fSLuca Coelho 2089dad325fSLuca Coelho *len = 0; 2099dad325fSLuca Coelho 210*0c4bad7fSArd Biesheuvel if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) 211*0c4bad7fSArd Biesheuvel return ERR_PTR(-ENODEV); 2129dad325fSLuca Coelho 2139dad325fSLuca Coelho /* 2149dad325fSLuca Coelho * TODO: we hardcode a maximum length here, because reading 2159dad325fSLuca Coelho * from the UEFI is not working. To implement this properly, 2169dad325fSLuca Coelho * we have to call efivar_entry_size(). 2179dad325fSLuca Coelho */ 2189dad325fSLuca Coelho package_size = IWL_HARDCODED_REDUCE_POWER_SIZE; 2199dad325fSLuca Coelho 2209dad325fSLuca Coelho package = kmalloc(package_size, GFP_KERNEL); 221*0c4bad7fSArd Biesheuvel if (!package) 222*0c4bad7fSArd Biesheuvel return ERR_PTR(-ENOMEM); 2239dad325fSLuca Coelho 224*0c4bad7fSArd Biesheuvel status = efi.get_variable(IWL_UEFI_REDUCED_POWER_NAME, &IWL_EFI_VAR_GUID, 225*0c4bad7fSArd Biesheuvel NULL, &package_size, data); 226*0c4bad7fSArd Biesheuvel if (status != EFI_SUCCESS) { 2279dad325fSLuca Coelho IWL_DEBUG_FW(trans, 228*0c4bad7fSArd Biesheuvel "Reduced Power UEFI variable not found 0x%lx (len %lu)\n", 229*0c4bad7fSArd Biesheuvel status, package_size); 2309dad325fSLuca Coelho kfree(package); 231*0c4bad7fSArd Biesheuvel return ERR_PTR(-ENOENT); 2329dad325fSLuca Coelho } 2339dad325fSLuca Coelho 2349dad325fSLuca Coelho IWL_DEBUG_FW(trans, "Read reduced power from UEFI with size %lu\n", 2359dad325fSLuca Coelho package_size); 2369dad325fSLuca Coelho *len = package_size; 2379dad325fSLuca Coelho 2389dad325fSLuca Coelho IWL_DEBUG_FW(trans, "rev %d, total_size %d, n_skus %d\n", 2399dad325fSLuca Coelho package->rev, package->total_size, package->n_skus); 2409dad325fSLuca Coelho 2419dad325fSLuca Coelho data = iwl_uefi_reduce_power_parse(trans, package->data, 2429dad325fSLuca Coelho *len - sizeof(*package)); 2439dad325fSLuca Coelho 2449dad325fSLuca Coelho kfree(package); 2459dad325fSLuca Coelho 2469dad325fSLuca Coelho return data; 2479dad325fSLuca Coelho } 248c593d2faSAyala Barazani 249c593d2faSAyala Barazani #ifdef CONFIG_ACPI 250c593d2faSAyala Barazani static int iwl_uefi_sgom_parse(struct uefi_cnv_wlan_sgom_data *sgom_data, 251c593d2faSAyala Barazani struct iwl_fw_runtime *fwrt) 252c593d2faSAyala Barazani { 253c593d2faSAyala Barazani int i, j; 254c593d2faSAyala Barazani 255c593d2faSAyala Barazani if (sgom_data->revision != 1) 256c593d2faSAyala Barazani return -EINVAL; 257c593d2faSAyala Barazani 258c593d2faSAyala Barazani memcpy(fwrt->sgom_table.offset_map, sgom_data->offset_map, 259c593d2faSAyala Barazani sizeof(fwrt->sgom_table.offset_map)); 260c593d2faSAyala Barazani 261c593d2faSAyala Barazani for (i = 0; i < MCC_TO_SAR_OFFSET_TABLE_ROW_SIZE; i++) { 262c593d2faSAyala Barazani for (j = 0; j < MCC_TO_SAR_OFFSET_TABLE_COL_SIZE; j++) { 263c593d2faSAyala Barazani /* since each byte is composed of to values, */ 264c593d2faSAyala Barazani /* one for each letter, */ 265c593d2faSAyala Barazani /* extract and check each of them separately */ 266c593d2faSAyala Barazani u8 value = fwrt->sgom_table.offset_map[i][j]; 267c593d2faSAyala Barazani u8 low = value & 0xF; 268c593d2faSAyala Barazani u8 high = (value & 0xF0) >> 4; 269c593d2faSAyala Barazani 270c593d2faSAyala Barazani if (high > fwrt->geo_num_profiles) 271c593d2faSAyala Barazani high = 0; 272c593d2faSAyala Barazani if (low > fwrt->geo_num_profiles) 273c593d2faSAyala Barazani low = 0; 274c593d2faSAyala Barazani fwrt->sgom_table.offset_map[i][j] = (high << 4) | low; 275c593d2faSAyala Barazani } 276c593d2faSAyala Barazani } 277c593d2faSAyala Barazani 278c593d2faSAyala Barazani fwrt->sgom_enabled = true; 279c593d2faSAyala Barazani return 0; 280c593d2faSAyala Barazani } 281c593d2faSAyala Barazani 282c593d2faSAyala Barazani void iwl_uefi_get_sgom_table(struct iwl_trans *trans, 283c593d2faSAyala Barazani struct iwl_fw_runtime *fwrt) 284c593d2faSAyala Barazani { 285c593d2faSAyala Barazani struct uefi_cnv_wlan_sgom_data *data; 286c593d2faSAyala Barazani unsigned long package_size; 287*0c4bad7fSArd Biesheuvel efi_status_t status; 288*0c4bad7fSArd Biesheuvel int ret; 289c593d2faSAyala Barazani 290*0c4bad7fSArd Biesheuvel if (!fwrt->geo_enabled || 291*0c4bad7fSArd Biesheuvel !efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) 292c593d2faSAyala Barazani return; 293c593d2faSAyala Barazani 294c593d2faSAyala Barazani /* TODO: we hardcode a maximum length here, because reading 295c593d2faSAyala Barazani * from the UEFI is not working. To implement this properly, 296c593d2faSAyala Barazani * we have to call efivar_entry_size(). 297c593d2faSAyala Barazani */ 298c593d2faSAyala Barazani package_size = IWL_HARDCODED_SGOM_SIZE; 299c593d2faSAyala Barazani 300c593d2faSAyala Barazani data = kmalloc(package_size, GFP_KERNEL); 301*0c4bad7fSArd Biesheuvel if (!data) 302*0c4bad7fSArd Biesheuvel return; 303c593d2faSAyala Barazani 304*0c4bad7fSArd Biesheuvel status = efi.get_variable(IWL_UEFI_SGOM_NAME, &IWL_EFI_VAR_GUID, 305*0c4bad7fSArd Biesheuvel NULL, &package_size, data); 306*0c4bad7fSArd Biesheuvel if (status != EFI_SUCCESS) { 307c593d2faSAyala Barazani IWL_DEBUG_FW(trans, 308*0c4bad7fSArd Biesheuvel "SGOM UEFI variable not found 0x%lx\n", status); 309c593d2faSAyala Barazani goto out_free; 310c593d2faSAyala Barazani } 311c593d2faSAyala Barazani 312c593d2faSAyala Barazani IWL_DEBUG_FW(trans, "Read SGOM from UEFI with size %lu\n", 313c593d2faSAyala Barazani package_size); 314c593d2faSAyala Barazani 315c593d2faSAyala Barazani ret = iwl_uefi_sgom_parse(data, fwrt); 316c593d2faSAyala Barazani if (ret < 0) 317c593d2faSAyala Barazani IWL_DEBUG_FW(trans, "Cannot read SGOM tables. rev is invalid\n"); 318c593d2faSAyala Barazani 319c593d2faSAyala Barazani out_free: 320c593d2faSAyala Barazani kfree(data); 321c593d2faSAyala Barazani 322c593d2faSAyala Barazani } 323c593d2faSAyala Barazani IWL_EXPORT_SYMBOL(iwl_uefi_get_sgom_table); 324c593d2faSAyala Barazani #endif /* CONFIG_ACPI */ 325