Path: blob/main/sys/contrib/dev/athk/ath10k/swap.c
48378 views
// SPDX-License-Identifier: ISC1/*2* Copyright (c) 2015-2016 Qualcomm Atheros, Inc.3*/45/* This file has implementation for code swap logic. With code swap feature,6* target can run the fw binary with even smaller IRAM size by using host7* memory to store some of the code segments.8*/910#include "core.h"11#include "bmi.h"12#include "debug.h"1314static int ath10k_swap_code_seg_fill(struct ath10k *ar,15struct ath10k_swap_code_seg_info *seg_info,16const void *data, size_t data_len)17{18u8 *virt_addr = seg_info->virt_address[0];19u8 swap_magic[ATH10K_SWAP_CODE_SEG_MAGIC_BYTES_SZ] = {};20const u8 *fw_data = data;21const union ath10k_swap_code_seg_item *swap_item;22u32 length = 0;23u32 payload_len;24u32 total_payload_len = 0;25u32 size_left = data_len;2627/* Parse swap bin and copy the content to host allocated memory.28* The format is Address, length and value. The last 4-bytes is29* target write address. Currently address field is not used.30*/31seg_info->target_addr = -1;32while (size_left >= sizeof(*swap_item)) {33swap_item = (const union ath10k_swap_code_seg_item *)fw_data;34payload_len = __le32_to_cpu(swap_item->tlv.length);35if ((payload_len > size_left) ||36(payload_len == 0 &&37size_left != sizeof(struct ath10k_swap_code_seg_tail))) {38ath10k_err(ar, "refusing to parse invalid tlv length %d\n",39payload_len);40return -EINVAL;41}4243if (payload_len == 0) {44if (memcmp(swap_item->tail.magic_signature, swap_magic,45ATH10K_SWAP_CODE_SEG_MAGIC_BYTES_SZ)) {46ath10k_err(ar, "refusing an invalid swap file\n");47return -EINVAL;48}49seg_info->target_addr =50__le32_to_cpu(swap_item->tail.bmi_write_addr);51break;52}5354memcpy(virt_addr, swap_item->tlv.data, payload_len);55virt_addr += payload_len;56length = payload_len + sizeof(struct ath10k_swap_code_seg_tlv);57size_left -= length;58fw_data += length;59total_payload_len += payload_len;60}6162if (seg_info->target_addr == -1) {63ath10k_err(ar, "failed to parse invalid swap file\n");64return -EINVAL;65}66seg_info->seg_hw_info.swap_size = __cpu_to_le32(total_payload_len);6768return 0;69}7071static void72ath10k_swap_code_seg_free(struct ath10k *ar,73struct ath10k_swap_code_seg_info *seg_info)74{75u32 seg_size;7677if (!seg_info)78return;7980if (!seg_info->virt_address[0])81return;8283seg_size = __le32_to_cpu(seg_info->seg_hw_info.size);84dma_free_coherent(ar->dev, seg_size, seg_info->virt_address[0],85seg_info->paddr[0]);86}8788static struct ath10k_swap_code_seg_info *89ath10k_swap_code_seg_alloc(struct ath10k *ar, size_t swap_bin_len)90{91struct ath10k_swap_code_seg_info *seg_info;92void *virt_addr;93dma_addr_t paddr;9495swap_bin_len = roundup(swap_bin_len, 2);96if (swap_bin_len > ATH10K_SWAP_CODE_SEG_BIN_LEN_MAX) {97ath10k_err(ar, "refusing code swap bin because it is too big %zu > %d\n",98swap_bin_len, ATH10K_SWAP_CODE_SEG_BIN_LEN_MAX);99return NULL;100}101102seg_info = devm_kzalloc(ar->dev, sizeof(*seg_info), GFP_KERNEL);103if (!seg_info)104return NULL;105106virt_addr = dma_alloc_coherent(ar->dev, swap_bin_len, &paddr,107GFP_KERNEL);108if (!virt_addr)109return NULL;110111seg_info->seg_hw_info.bus_addr[0] = __cpu_to_le32(paddr);112seg_info->seg_hw_info.size = __cpu_to_le32(swap_bin_len);113seg_info->seg_hw_info.swap_size = __cpu_to_le32(swap_bin_len);114seg_info->seg_hw_info.num_segs =115__cpu_to_le32(ATH10K_SWAP_CODE_SEG_NUM_SUPPORTED);116seg_info->seg_hw_info.size_log2 = __cpu_to_le32(ilog2(swap_bin_len));117seg_info->virt_address[0] = virt_addr;118seg_info->paddr[0] = paddr;119120return seg_info;121}122123int ath10k_swap_code_seg_configure(struct ath10k *ar,124const struct ath10k_fw_file *fw_file)125{126int ret;127struct ath10k_swap_code_seg_info *seg_info = NULL;128129if (!fw_file->firmware_swap_code_seg_info)130return 0;131132ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot found firmware code swap binary\n");133134seg_info = fw_file->firmware_swap_code_seg_info;135136ret = ath10k_bmi_write_memory(ar, seg_info->target_addr,137#if defined(__linux__)138&seg_info->seg_hw_info,139#elif defined(__FreeBSD__)140(void *)&seg_info->seg_hw_info,141#endif142sizeof(seg_info->seg_hw_info));143if (ret) {144ath10k_err(ar, "failed to write Code swap segment information (%d)\n",145ret);146return ret;147}148149return 0;150}151152void ath10k_swap_code_seg_release(struct ath10k *ar,153struct ath10k_fw_file *fw_file)154{155ath10k_swap_code_seg_free(ar, fw_file->firmware_swap_code_seg_info);156157/* FIXME: these two assignments look to bein wrong place! Shouldn't158* they be in ath10k_core_free_firmware_files() like the rest?159*/160fw_file->codeswap_data = NULL;161fw_file->codeswap_len = 0;162163fw_file->firmware_swap_code_seg_info = NULL;164}165166int ath10k_swap_code_seg_init(struct ath10k *ar, struct ath10k_fw_file *fw_file)167{168int ret;169struct ath10k_swap_code_seg_info *seg_info;170const void *codeswap_data;171size_t codeswap_len;172173codeswap_data = fw_file->codeswap_data;174codeswap_len = fw_file->codeswap_len;175176if (!codeswap_len || !codeswap_data)177return 0;178179seg_info = ath10k_swap_code_seg_alloc(ar, codeswap_len);180if (!seg_info) {181ath10k_err(ar, "failed to allocate fw code swap segment\n");182return -ENOMEM;183}184185ret = ath10k_swap_code_seg_fill(ar, seg_info,186codeswap_data, codeswap_len);187188if (ret) {189ath10k_warn(ar, "failed to initialize fw code swap segment: %d\n",190ret);191ath10k_swap_code_seg_free(ar, seg_info);192return ret;193}194195fw_file->firmware_swap_code_seg_info = seg_info;196197return 0;198}199200201