Path: blob/main/sys/contrib/dev/iwlwifi/pcie/ctxt-info.c
48372 views
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause1/*2* Copyright (C) 2017 Intel Deutschland GmbH3* Copyright (C) 2018-2025 Intel Corporation4*/5#include "iwl-trans.h"6#include "iwl-fh.h"7#include "iwl-context-info.h"8#include "gen1_2/internal.h"9#include "iwl-prph.h"1011static void *_iwl_pcie_ctxt_info_dma_alloc_coherent(struct iwl_trans *trans,12size_t size,13dma_addr_t *phys,14int depth)15{16void *result;1718if (WARN(depth > 2,19"failed to allocate DMA memory not crossing 2^32 boundary"))20return NULL;2122result = dma_alloc_coherent(trans->dev, size, phys, GFP_KERNEL);2324if (!result)25return NULL;2627if (unlikely(iwl_txq_crosses_4g_boundary(*phys, size))) {28void *old = result;29dma_addr_t oldphys = *phys;3031result = _iwl_pcie_ctxt_info_dma_alloc_coherent(trans, size,32phys,33depth + 1);34dma_free_coherent(trans->dev, size, old, oldphys);35}3637return result;38}3940void *iwl_pcie_ctxt_info_dma_alloc_coherent(struct iwl_trans *trans,41size_t size,42dma_addr_t *phys)43{44return _iwl_pcie_ctxt_info_dma_alloc_coherent(trans, size, phys, 0);45}4647int iwl_pcie_ctxt_info_alloc_dma(struct iwl_trans *trans,48const void *data, u32 len,49struct iwl_dram_data *dram)50{51dram->block = iwl_pcie_ctxt_info_dma_alloc_coherent(trans, len,52&dram->physical);53if (!dram->block)54return -ENOMEM;5556dram->size = len;57memcpy(dram->block, data, len);5859return 0;60}6162void iwl_pcie_ctxt_info_free_paging(struct iwl_trans *trans)63{64struct iwl_self_init_dram *dram = &trans->init_dram;65int i;6667if (!dram->paging) {68WARN_ON(dram->paging_cnt);69return;70}7172/* free paging*/73for (i = 0; i < dram->paging_cnt; i++)74dma_free_coherent(trans->dev, dram->paging[i].size,75dram->paging[i].block,76dram->paging[i].physical);7778kfree(dram->paging);79dram->paging_cnt = 0;80dram->paging = NULL;81}8283int iwl_pcie_init_fw_sec(struct iwl_trans *trans,84const struct fw_img *fw,85struct iwl_context_info_dram_nonfseq *ctxt_dram)86{87struct iwl_self_init_dram *dram = &trans->init_dram;88int i, ret, lmac_cnt, umac_cnt, paging_cnt;8990if (WARN(dram->paging,91"paging shouldn't already be initialized (%d pages)\n",92dram->paging_cnt))93iwl_pcie_ctxt_info_free_paging(trans);9495lmac_cnt = iwl_pcie_get_num_sections(fw, 0);96/* add 1 due to separator */97umac_cnt = iwl_pcie_get_num_sections(fw, lmac_cnt + 1);98/* add 2 due to separators */99paging_cnt = iwl_pcie_get_num_sections(fw, lmac_cnt + umac_cnt + 2);100101dram->fw = kcalloc(umac_cnt + lmac_cnt, sizeof(*dram->fw), GFP_KERNEL);102if (!dram->fw)103return -ENOMEM;104dram->paging = kcalloc(paging_cnt, sizeof(*dram->paging), GFP_KERNEL);105if (!dram->paging)106return -ENOMEM;107108/* initialize lmac sections */109for (i = 0; i < lmac_cnt; i++) {110ret = iwl_pcie_ctxt_info_alloc_dma(trans, fw->sec[i].data,111fw->sec[i].len,112&dram->fw[dram->fw_cnt]);113if (ret)114return ret;115ctxt_dram->lmac_img[i] =116cpu_to_le64(dram->fw[dram->fw_cnt].physical);117dram->fw_cnt++;118}119120/* initialize umac sections */121for (i = 0; i < umac_cnt; i++) {122/* access FW with +1 to make up for lmac separator */123ret = iwl_pcie_ctxt_info_alloc_dma(trans,124fw->sec[dram->fw_cnt + 1].data,125fw->sec[dram->fw_cnt + 1].len,126&dram->fw[dram->fw_cnt]);127if (ret)128return ret;129ctxt_dram->umac_img[i] =130cpu_to_le64(dram->fw[dram->fw_cnt].physical);131dram->fw_cnt++;132}133134/*135* Initialize paging.136* Paging memory isn't stored in dram->fw as the umac and lmac - it is137* stored separately.138* This is since the timing of its release is different -139* while fw memory can be released on alive, the paging memory can be140* freed only when the device goes down.141* Given that, the logic here in accessing the fw image is a bit142* different - fw_cnt isn't changing so loop counter is added to it.143*/144for (i = 0; i < paging_cnt; i++) {145/* access FW with +2 to make up for lmac & umac separators */146int fw_idx = dram->fw_cnt + i + 2;147148ret = iwl_pcie_ctxt_info_alloc_dma(trans, fw->sec[fw_idx].data,149fw->sec[fw_idx].len,150&dram->paging[i]);151if (ret)152return ret;153154ctxt_dram->virtual_img[i] =155cpu_to_le64(dram->paging[i].physical);156dram->paging_cnt++;157}158159return 0;160}161162int iwl_pcie_ctxt_info_init(struct iwl_trans *trans,163const struct fw_img *img)164{165struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);166struct iwl_context_info *ctxt_info;167struct iwl_context_info_rbd_cfg *rx_cfg;168u32 control_flags = 0, rb_size, cb_size;169dma_addr_t phys;170int ret;171172ctxt_info = iwl_pcie_ctxt_info_dma_alloc_coherent(trans,173sizeof(*ctxt_info),174&phys);175if (!ctxt_info)176return -ENOMEM;177178trans_pcie->ctxt_info_dma_addr = phys;179180ctxt_info->version.version = 0;181ctxt_info->version.mac_id =182cpu_to_le16((u16)trans->info.hw_rev);183/* size is in DWs */184ctxt_info->version.size = cpu_to_le16(sizeof(*ctxt_info) / 4);185186switch (trans->conf.rx_buf_size) {187case IWL_AMSDU_2K:188rb_size = IWL_CTXT_INFO_RB_SIZE_2K;189break;190case IWL_AMSDU_4K:191rb_size = IWL_CTXT_INFO_RB_SIZE_4K;192break;193case IWL_AMSDU_8K:194rb_size = IWL_CTXT_INFO_RB_SIZE_8K;195break;196case IWL_AMSDU_12K:197rb_size = IWL_CTXT_INFO_RB_SIZE_16K;198break;199default:200WARN_ON(1);201rb_size = IWL_CTXT_INFO_RB_SIZE_4K;202}203204cb_size = RX_QUEUE_CB_SIZE(iwl_trans_get_num_rbds(trans));205if (WARN_ON(cb_size > 12))206cb_size = 12;207208control_flags = IWL_CTXT_INFO_TFD_FORMAT_LONG;209control_flags |= u32_encode_bits(cb_size, IWL_CTXT_INFO_RB_CB_SIZE);210control_flags |= u32_encode_bits(rb_size, IWL_CTXT_INFO_RB_SIZE);211ctxt_info->control.control_flags = cpu_to_le32(control_flags);212213/* initialize RX default queue */214rx_cfg = &ctxt_info->rbd_cfg;215rx_cfg->free_rbd_addr = cpu_to_le64(trans_pcie->rxq->bd_dma);216rx_cfg->used_rbd_addr = cpu_to_le64(trans_pcie->rxq->used_bd_dma);217rx_cfg->status_wr_ptr = cpu_to_le64(trans_pcie->rxq->rb_stts_dma);218219/* initialize TX command queue */220ctxt_info->hcmd_cfg.cmd_queue_addr =221cpu_to_le64(trans_pcie->txqs.txq[trans->conf.cmd_queue]->dma_addr);222ctxt_info->hcmd_cfg.cmd_queue_size =223TFD_QUEUE_CB_SIZE(IWL_CMD_QUEUE_SIZE);224225/* allocate ucode sections in dram and set addresses */226ret = iwl_pcie_init_fw_sec(trans, img, &ctxt_info->dram);227if (ret) {228dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info),229ctxt_info, trans_pcie->ctxt_info_dma_addr);230return ret;231}232233trans_pcie->ctxt_info = ctxt_info;234235iwl_enable_fw_load_int_ctx_info(trans, false);236237/* Configure debug, if exists */238if (iwl_pcie_dbg_on(trans))239iwl_pcie_apply_destination(trans);240241/* kick FW self load */242iwl_write64(trans, CSR_CTXT_INFO_BA, trans_pcie->ctxt_info_dma_addr);243244/* Context info will be released upon alive or failure to get one */245246return 0;247}248249void iwl_pcie_ctxt_info_free(struct iwl_trans *trans)250{251struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);252253if (!trans_pcie->ctxt_info)254return;255256dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info),257trans_pcie->ctxt_info,258trans_pcie->ctxt_info_dma_addr);259trans_pcie->ctxt_info_dma_addr = 0;260trans_pcie->ctxt_info = NULL;261262iwl_pcie_ctxt_info_free_fw_img(trans);263}264265266