1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright(c) 2020-2024 Intel Corporation
10 #include "fw/api/commands.h"
11 #include "fw/api/nvm-reg.h"
12 #include "fw/api/alive.h"
15 #define IWL_PNVM_REDUCED_CAP_BIT BIT(25)
17 struct iwl_pnvm_section {
22 static bool iwl_pnvm_complete_fn(struct iwl_notif_wait_data *notif_wait,
23 struct iwl_rx_packet *pkt, void *data)
25 struct iwl_trans *trans = (struct iwl_trans *)data;
26 struct iwl_pnvm_init_complete_ntfy *pnvm_ntf = (void *)pkt->data;
29 "PNVM complete notification received with status 0x%0x\n",
30 le32_to_cpu(pnvm_ntf->status));
35 static int iwl_pnvm_handle_section(struct iwl_trans *trans, const u8 *data,
37 struct iwl_pnvm_image *pnvm_data)
39 const struct iwl_ucode_tlv *tlv;
41 u16 mac_type = 0, rf_id = 0;
42 bool hw_match = false;
44 IWL_DEBUG_FW(trans, "Handling PNVM section\n");
46 memset(pnvm_data, 0, sizeof(*pnvm_data));
48 while (len >= sizeof(*tlv)) {
49 u32 tlv_len, tlv_type;
52 tlv = (const void *)data;
54 tlv_len = le32_to_cpu(tlv->length);
55 tlv_type = le32_to_cpu(tlv->type);
58 IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
66 case IWL_UCODE_TLV_PNVM_VERSION:
67 if (tlv_len < sizeof(__le32)) {
69 "Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n",
70 sizeof(__le32), tlv_len);
74 sha1 = le32_to_cpup((const __le32 *)data);
77 "Got IWL_UCODE_TLV_PNVM_VERSION %0x\n",
79 pnvm_data->version = sha1;
81 case IWL_UCODE_TLV_HW_TYPE:
82 if (tlv_len < 2 * sizeof(__le16)) {
84 "Invalid size for IWL_UCODE_TLV_HW_TYPE (expected %zd, got %d)\n",
85 2 * sizeof(__le16), tlv_len);
92 mac_type = le16_to_cpup((const __le16 *)data);
93 rf_id = le16_to_cpup((const __le16 *)(data + sizeof(__le16)));
96 "Got IWL_UCODE_TLV_HW_TYPE mac_type 0x%0x rf_id 0x%0x\n",
99 if (mac_type == CSR_HW_REV_TYPE(trans->hw_rev) &&
100 rf_id == CSR_HW_RFID_TYPE(trans->hw_rf_id))
103 case IWL_UCODE_TLV_SEC_RT: {
104 const struct iwl_pnvm_section *section = (const void *)data;
105 u32 data_len = tlv_len - sizeof(*section);
108 "Got IWL_UCODE_TLV_SEC_RT len %d\n",
111 /* TODO: remove, this is a deprecated separator */
112 if (le32_to_cpup((const __le32 *)data) == 0xddddeeee) {
113 IWL_DEBUG_FW(trans, "Ignoring separator.\n");
117 if (pnvm_data->n_chunks == IPC_DRAM_MAP_ENTRY_NUM_MAX) {
119 "too many payloads to allocate in DRAM.\n");
123 IWL_DEBUG_FW(trans, "Adding data (size %d)\n",
126 pnvm_data->chunks[pnvm_data->n_chunks].data = section->data;
127 pnvm_data->chunks[pnvm_data->n_chunks].len = data_len;
128 pnvm_data->n_chunks++;
132 case IWL_UCODE_TLV_MEM_DESC:
133 if (iwl_uefi_handle_tlv_mem_desc(trans, data, tlv_len,
137 case IWL_UCODE_TLV_PNVM_SKU:
139 "New PNVM section started, stop parsing.\n");
142 IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n",
147 len -= ALIGN(tlv_len, 4);
148 data += ALIGN(tlv_len, 4);
154 "HW mismatch, skipping PNVM section (need mac_type 0x%x rf_id 0x%x)\n",
155 CSR_HW_REV_TYPE(trans->hw_rev),
156 CSR_HW_RFID_TYPE(trans->hw_rf_id));
160 if (!pnvm_data->n_chunks) {
161 IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n");
168 static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data,
170 struct iwl_pnvm_image *pnvm_data)
172 const struct iwl_ucode_tlv *tlv;
174 IWL_DEBUG_FW(trans, "Parsing PNVM file\n");
176 while (len >= sizeof(*tlv)) {
177 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 trans->reduced_cap_sku = false;
208 rf_type = CSR_HW_RFID_TYPE(trans->hw_rf_id);
209 if ((trans->sku_id[0] & IWL_PNVM_REDUCED_CAP_BIT) &&
210 rf_type == IWL_CFG_RF_TYPE_FM)
211 trans->reduced_cap_sku = true;
214 "Reduced SKU device %d\n",
215 trans->reduced_cap_sku);
217 if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) &&
218 trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) &&
219 trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) {
222 ret = iwl_pnvm_handle_section(trans, data, len,
227 IWL_DEBUG_FW(trans, "SKU ID didn't match!\n");
230 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
231 len -= ALIGN(tlv_len, 4);
238 static int iwl_pnvm_get_from_fs(struct iwl_trans *trans, u8 **data, size_t *len)
240 const struct firmware *pnvm;
241 char pnvm_name[MAX_PNVM_NAME];
245 iwl_pnvm_get_fs_name(trans, pnvm_name, sizeof(pnvm_name));
247 ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev);
249 IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n",
254 new_len = pnvm->size;
255 *data = kvmemdup(pnvm->data, pnvm->size, GFP_KERNEL);
256 release_firmware(pnvm);
266 static u8 *iwl_get_pnvm_image(struct iwl_trans *trans_p, size_t *len)
268 struct pnvm_sku_package *package;
271 /* Get PNVM from BIOS for non-Intel SKU */
272 if (trans_p->sku_id[2]) {
273 package = iwl_uefi_get_pnvm(trans_p, len);
274 if (!IS_ERR_OR_NULL(package)) {
275 if (*len >= sizeof(*package)) {
276 /* we need only the data */
277 *len -= sizeof(*package);
278 image = kvmemdup(package->data,
282 * free package regardless of whether kmemdup
291 /* If it's not available, or for Intel SKU, try from the filesystem */
292 if (iwl_pnvm_get_from_fs(trans_p, &image, len))
297 static void iwl_pnvm_load_pnvm_to_trans(struct iwl_trans *trans,
298 const struct iwl_ucode_capabilities *capa)
300 struct iwl_pnvm_image *pnvm_data = NULL;
305 /* failed to get/parse the image in the past, no use trying again */
306 if (trans->fail_to_parse_pnvm_image)
309 if (trans->pnvm_loaded)
312 data = iwl_get_pnvm_image(trans, &length);
314 trans->fail_to_parse_pnvm_image = true;
318 pnvm_data = kzalloc(sizeof(*pnvm_data), GFP_KERNEL);
322 ret = iwl_pnvm_parse(trans, data, length, pnvm_data);
324 trans->fail_to_parse_pnvm_image = true;
328 ret = iwl_trans_load_pnvm(trans, pnvm_data, capa);
331 IWL_INFO(trans, "loaded PNVM version %08x\n", pnvm_data->version);
334 iwl_trans_set_pnvm(trans, capa);
341 iwl_pnvm_load_reduce_power_to_trans(struct iwl_trans *trans,
342 const struct iwl_ucode_capabilities *capa)
344 struct iwl_pnvm_image *pnvm_data = NULL;
349 if (trans->failed_to_load_reduce_power_image)
352 if (trans->reduce_power_loaded)
355 data = iwl_uefi_get_reduced_power(trans, &length);
357 trans->failed_to_load_reduce_power_image = true;
361 pnvm_data = kzalloc(sizeof(*pnvm_data), GFP_KERNEL);
365 ret = iwl_uefi_reduce_power_parse(trans, data, length, pnvm_data);
367 trans->failed_to_load_reduce_power_image = true;
371 ret = iwl_trans_load_reduce_power(trans, pnvm_data, capa);
374 "Failed to load reduce power table %d\n",
376 trans->failed_to_load_reduce_power_image = true;
381 iwl_trans_set_reduce_power(trans, capa);
387 int iwl_pnvm_load(struct iwl_trans *trans,
388 struct iwl_notif_wait_data *notif_wait,
389 const struct iwl_ucode_capabilities *capa)
391 struct iwl_notification_wait pnvm_wait;
392 static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP,
393 PNVM_INIT_COMPLETE_NTFY) };
395 /* if the SKU_ID is empty, there's nothing to do */
396 if (!trans->sku_id[0] && !trans->sku_id[1] && !trans->sku_id[2])
399 iwl_pnvm_load_pnvm_to_trans(trans, capa);
400 iwl_pnvm_load_reduce_power_to_trans(trans, capa);
402 iwl_init_notification_wait(notif_wait, &pnvm_wait,
403 ntf_cmds, ARRAY_SIZE(ntf_cmds),
404 iwl_pnvm_complete_fn, trans);
406 /* kick the doorbell */
407 iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,
408 UREG_DOORBELL_TO_ISR6_PNVM);
410 return iwl_wait_notification(notif_wait, &pnvm_wait,
411 MVM_UCODE_PNVM_TIMEOUT);
413 IWL_EXPORT_SYMBOL(iwl_pnvm_load);