brintos

brintos / linux-shallow public Read only

0
0
Text · 9.7 KiB · 1195e70 Raw
414 lines · c
1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause2/*3 * Copyright(c) 2020-2024 Intel Corporation4 */5 6#include "iwl-drv.h"7#include "pnvm.h"8#include "iwl-prph.h"9#include "iwl-io.h"10#include "fw/api/commands.h"11#include "fw/api/nvm-reg.h"12#include "fw/api/alive.h"13#include "fw/uefi.h"14 15#define IWL_PNVM_REDUCED_CAP_BIT BIT(25)16 17struct iwl_pnvm_section {18	__le32 offset;19	const u8 data[];20} __packed;21 22static bool iwl_pnvm_complete_fn(struct iwl_notif_wait_data *notif_wait,23				 struct iwl_rx_packet *pkt, void *data)24{25	struct iwl_trans *trans = (struct iwl_trans *)data;26	struct iwl_pnvm_init_complete_ntfy *pnvm_ntf = (void *)pkt->data;27 28	IWL_DEBUG_FW(trans,29		     "PNVM complete notification received with status 0x%0x\n",30		     le32_to_cpu(pnvm_ntf->status));31 32	return true;33}34 35static int iwl_pnvm_handle_section(struct iwl_trans *trans, const u8 *data,36				   size_t len,37				   struct iwl_pnvm_image *pnvm_data)38{39	const struct iwl_ucode_tlv *tlv;40	u32 sha1 = 0;41	u16 mac_type = 0, rf_id = 0;42	bool hw_match = false;43 44	IWL_DEBUG_FW(trans, "Handling PNVM section\n");45 46	memset(pnvm_data, 0, sizeof(*pnvm_data));47 48	while (len >= sizeof(*tlv)) {49		u32 tlv_len, tlv_type;50 51		len -= sizeof(*tlv);52		tlv = (const void *)data;53 54		tlv_len = le32_to_cpu(tlv->length);55		tlv_type = le32_to_cpu(tlv->type);56 57		if (len < tlv_len) {58			IWL_ERR(trans, "invalid TLV len: %zd/%u\n",59				len, tlv_len);60			return -EINVAL;61		}62 63		data += sizeof(*tlv);64 65		switch (tlv_type) {66		case IWL_UCODE_TLV_PNVM_VERSION:67			if (tlv_len < sizeof(__le32)) {68				IWL_DEBUG_FW(trans,69					     "Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n",70					     sizeof(__le32), tlv_len);71				break;72			}73 74			sha1 = le32_to_cpup((const __le32 *)data);75 76			IWL_DEBUG_FW(trans,77				     "Got IWL_UCODE_TLV_PNVM_VERSION %0x\n",78				     sha1);79			pnvm_data->version = sha1;80			break;81		case IWL_UCODE_TLV_HW_TYPE:82			if (tlv_len < 2 * sizeof(__le16)) {83				IWL_DEBUG_FW(trans,84					     "Invalid size for IWL_UCODE_TLV_HW_TYPE (expected %zd, got %d)\n",85					     2 * sizeof(__le16), tlv_len);86				break;87			}88 89			if (hw_match)90				break;91 92			mac_type = le16_to_cpup((const __le16 *)data);93			rf_id = le16_to_cpup((const __le16 *)(data + sizeof(__le16)));94 95			IWL_DEBUG_FW(trans,96				     "Got IWL_UCODE_TLV_HW_TYPE mac_type 0x%0x rf_id 0x%0x\n",97				     mac_type, rf_id);98 99			if (mac_type == CSR_HW_REV_TYPE(trans->hw_rev) &&100			    rf_id == CSR_HW_RFID_TYPE(trans->hw_rf_id))101				hw_match = true;102			break;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);106 107			IWL_DEBUG_FW(trans,108				     "Got IWL_UCODE_TLV_SEC_RT len %d\n",109				     tlv_len);110 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");114				break;115			}116 117			if (pnvm_data->n_chunks == IPC_DRAM_MAP_ENTRY_NUM_MAX) {118				IWL_DEBUG_FW(trans,119					     "too many payloads to allocate in DRAM.\n");120				return -EINVAL;121			}122 123			IWL_DEBUG_FW(trans, "Adding data (size %d)\n",124				     data_len);125 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++;129 130			break;131		}132		case IWL_UCODE_TLV_MEM_DESC:133			if (iwl_uefi_handle_tlv_mem_desc(trans, data, tlv_len,134							 pnvm_data))135				return -EINVAL;136			break;137		case IWL_UCODE_TLV_PNVM_SKU:138			IWL_DEBUG_FW(trans,139				     "New PNVM section started, stop parsing.\n");140			goto done;141		default:142			IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n",143				     tlv_type, tlv_len);144			break;145		}146 147		len -= ALIGN(tlv_len, 4);148		data += ALIGN(tlv_len, 4);149	}150 151done:152	if (!hw_match) {153		IWL_DEBUG_FW(trans,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));157		return -ENOENT;158	}159 160	if (!pnvm_data->n_chunks) {161		IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n");162		return -ENOENT;163	}164 165	return 0;166}167 168static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data,169			  size_t len,170			  struct iwl_pnvm_image *pnvm_data)171{172	const struct iwl_ucode_tlv *tlv;173 174	IWL_DEBUG_FW(trans, "Parsing PNVM file\n");175 176	while (len >= sizeof(*tlv)) {177		u32 tlv_len, tlv_type;178		u32 rf_type;179 180		len -= sizeof(*tlv);181		tlv = (const void *)data;182 183		tlv_len = le32_to_cpu(tlv->length);184		tlv_type = le32_to_cpu(tlv->type);185 186		if (len < tlv_len) {187			IWL_ERR(trans, "invalid TLV len: %zd/%u\n",188				len, tlv_len);189			return -EINVAL;190		}191 192		if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) {193			const struct iwl_sku_id *sku_id =194				(const void *)(data + sizeof(*tlv));195 196			IWL_DEBUG_FW(trans,197				     "Got IWL_UCODE_TLV_PNVM_SKU len %d\n",198				     tlv_len);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]));203 204			data += sizeof(*tlv) + ALIGN(tlv_len, 4);205			len -= ALIGN(tlv_len, 4);206 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;212 213			IWL_DEBUG_FW(trans,214				     "Reduced SKU device %d\n",215				     trans->reduced_cap_sku);216 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])) {220				int ret;221 222				ret = iwl_pnvm_handle_section(trans, data, len,223							      pnvm_data);224				if (!ret)225					return 0;226			} else {227				IWL_DEBUG_FW(trans, "SKU ID didn't match!\n");228			}229		} else {230			data += sizeof(*tlv) + ALIGN(tlv_len, 4);231			len -= ALIGN(tlv_len, 4);232		}233	}234 235	return -ENOENT;236}237 238static int iwl_pnvm_get_from_fs(struct iwl_trans *trans, u8 **data, size_t *len)239{240	const struct firmware *pnvm;241	char pnvm_name[MAX_PNVM_NAME];242	size_t new_len;243	int ret;244 245	iwl_pnvm_get_fs_name(trans, pnvm_name, sizeof(pnvm_name));246 247	ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev);248	if (ret) {249		IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n",250			     pnvm_name, ret);251		return ret;252	}253 254	new_len = pnvm->size;255	*data = kvmemdup(pnvm->data, pnvm->size, GFP_KERNEL);256	release_firmware(pnvm);257 258	if (!*data)259		return -ENOMEM;260 261	*len = new_len;262 263	return 0;264}265 266static u8 *iwl_get_pnvm_image(struct iwl_trans *trans_p, size_t *len)267{268	struct pnvm_sku_package *package;269	u8 *image = NULL;270 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,279						 *len, GFP_KERNEL);280			}281			/*282			 * free package regardless of whether kmemdup283			 * succeeded284			 */285			kfree(package);286			if (image)287				return image;288		}289	}290 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))293		return NULL;294	return image;295}296 297static void iwl_pnvm_load_pnvm_to_trans(struct iwl_trans *trans,298					const struct iwl_ucode_capabilities *capa)299{300	struct iwl_pnvm_image *pnvm_data = NULL;301	u8 *data = NULL;302	size_t length;303	int ret;304 305	/* failed to get/parse the image in the past, no use trying again */306	if (trans->fail_to_parse_pnvm_image)307		return;308 309	if (trans->pnvm_loaded)310		goto set;311 312	data = iwl_get_pnvm_image(trans, &length);313	if (!data) {314		trans->fail_to_parse_pnvm_image = true;315		return;316	}317 318	pnvm_data = kzalloc(sizeof(*pnvm_data), GFP_KERNEL);319	if (!pnvm_data)320		goto free;321 322	ret = iwl_pnvm_parse(trans, data, length, pnvm_data);323	if (ret) {324		trans->fail_to_parse_pnvm_image = true;325		goto free;326	}327 328	ret = iwl_trans_load_pnvm(trans, pnvm_data, capa);329	if (ret)330		goto free;331	IWL_INFO(trans, "loaded PNVM version %08x\n", pnvm_data->version);332 333set:334	iwl_trans_set_pnvm(trans, capa);335free:336	kvfree(data);337	kfree(pnvm_data);338}339 340static void341iwl_pnvm_load_reduce_power_to_trans(struct iwl_trans *trans,342				    const struct iwl_ucode_capabilities *capa)343{344	struct iwl_pnvm_image *pnvm_data = NULL;345	u8 *data = NULL;346	size_t length;347	int ret;348 349	if (trans->failed_to_load_reduce_power_image)350		return;351 352	if (trans->reduce_power_loaded)353		goto set;354 355	data = iwl_uefi_get_reduced_power(trans, &length);356	if (IS_ERR(data)) {357		trans->failed_to_load_reduce_power_image = true;358		return;359	}360 361	pnvm_data = kzalloc(sizeof(*pnvm_data), GFP_KERNEL);362	if (!pnvm_data)363		goto free;364 365	ret = iwl_uefi_reduce_power_parse(trans, data, length, pnvm_data);366	if (ret) {367		trans->failed_to_load_reduce_power_image = true;368		goto free;369	}370 371	ret = iwl_trans_load_reduce_power(trans, pnvm_data, capa);372	if (ret) {373		IWL_DEBUG_FW(trans,374			     "Failed to load reduce power table %d\n",375			     ret);376		trans->failed_to_load_reduce_power_image = true;377		goto free;378	}379 380set:381	iwl_trans_set_reduce_power(trans, capa);382free:383	kfree(data);384	kfree(pnvm_data);385}386 387int iwl_pnvm_load(struct iwl_trans *trans,388		  struct iwl_notif_wait_data *notif_wait,389		  const struct iwl_ucode_capabilities *capa)390{391	struct iwl_notification_wait pnvm_wait;392	static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP,393						PNVM_INIT_COMPLETE_NTFY) };394 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])397		return 0;398 399	iwl_pnvm_load_pnvm_to_trans(trans, capa);400	iwl_pnvm_load_reduce_power_to_trans(trans, capa);401 402	iwl_init_notification_wait(notif_wait, &pnvm_wait,403				   ntf_cmds, ARRAY_SIZE(ntf_cmds),404				   iwl_pnvm_complete_fn, trans);405 406	/* kick the doorbell */407	iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,408			    UREG_DOORBELL_TO_ISR6_PNVM);409 410	return iwl_wait_notification(notif_wait, &pnvm_wait,411				     MVM_UCODE_PNVM_TIMEOUT);412}413IWL_EXPORT_SYMBOL(iwl_pnvm_load);414