1*4882a593Smuzhiyun /******************************************************************************
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * This file is provided under a dual BSD/GPLv2 license. When using or
4*4882a593Smuzhiyun * redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * GPL LICENSE SUMMARY
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Copyright(c) 2017 Intel Deutschland GmbH
9*4882a593Smuzhiyun * Copyright(c) 2018 - 2021 Intel Corporation
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
12*4882a593Smuzhiyun * it under the terms of version 2 of the GNU General Public License as
13*4882a593Smuzhiyun * published by the Free Software Foundation.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful, but
16*4882a593Smuzhiyun * WITHOUT ANY WARRANTY; without even the implied warranty of
17*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18*4882a593Smuzhiyun * General Public License for more details.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * BSD LICENSE
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * Copyright(c) 2017 Intel Deutschland GmbH
23*4882a593Smuzhiyun * Copyright(c) 2018 - 2020 Intel Corporation
24*4882a593Smuzhiyun * All rights reserved.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
27*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
28*4882a593Smuzhiyun * are met:
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * * Redistributions of source code must retain the above copyright
31*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
32*4882a593Smuzhiyun * * Redistributions in binary form must reproduce the above copyright
33*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in
34*4882a593Smuzhiyun * the documentation and/or other materials provided with the
35*4882a593Smuzhiyun * distribution.
36*4882a593Smuzhiyun * * Neither the name Intel Corporation nor the names of its
37*4882a593Smuzhiyun * contributors may be used to endorse or promote products derived
38*4882a593Smuzhiyun * from this software without specific prior written permission.
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun *****************************************************************************/
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #include "iwl-trans.h"
55*4882a593Smuzhiyun #include "iwl-fh.h"
56*4882a593Smuzhiyun #include "iwl-context-info.h"
57*4882a593Smuzhiyun #include "internal.h"
58*4882a593Smuzhiyun #include "iwl-prph.h"
59*4882a593Smuzhiyun
_iwl_pcie_ctxt_info_dma_alloc_coherent(struct iwl_trans * trans,size_t size,dma_addr_t * phys,int depth)60*4882a593Smuzhiyun static void *_iwl_pcie_ctxt_info_dma_alloc_coherent(struct iwl_trans *trans,
61*4882a593Smuzhiyun size_t size,
62*4882a593Smuzhiyun dma_addr_t *phys,
63*4882a593Smuzhiyun int depth)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun void *result;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun if (WARN(depth > 2,
68*4882a593Smuzhiyun "failed to allocate DMA memory not crossing 2^32 boundary"))
69*4882a593Smuzhiyun return NULL;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun result = dma_alloc_coherent(trans->dev, size, phys, GFP_KERNEL);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun if (!result)
74*4882a593Smuzhiyun return NULL;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun if (unlikely(iwl_txq_crosses_4g_boundary(*phys, size))) {
77*4882a593Smuzhiyun void *old = result;
78*4882a593Smuzhiyun dma_addr_t oldphys = *phys;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun result = _iwl_pcie_ctxt_info_dma_alloc_coherent(trans, size,
81*4882a593Smuzhiyun phys,
82*4882a593Smuzhiyun depth + 1);
83*4882a593Smuzhiyun dma_free_coherent(trans->dev, size, old, oldphys);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun return result;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
iwl_pcie_ctxt_info_dma_alloc_coherent(struct iwl_trans * trans,size_t size,dma_addr_t * phys)89*4882a593Smuzhiyun static void *iwl_pcie_ctxt_info_dma_alloc_coherent(struct iwl_trans *trans,
90*4882a593Smuzhiyun size_t size,
91*4882a593Smuzhiyun dma_addr_t *phys)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun return _iwl_pcie_ctxt_info_dma_alloc_coherent(trans, size, phys, 0);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
iwl_pcie_ctxt_info_alloc_dma(struct iwl_trans * trans,const void * data,u32 len,struct iwl_dram_data * dram)96*4882a593Smuzhiyun int iwl_pcie_ctxt_info_alloc_dma(struct iwl_trans *trans,
97*4882a593Smuzhiyun const void *data, u32 len,
98*4882a593Smuzhiyun struct iwl_dram_data *dram)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun dram->block = iwl_pcie_ctxt_info_dma_alloc_coherent(trans, len,
101*4882a593Smuzhiyun &dram->physical);
102*4882a593Smuzhiyun if (!dram->block)
103*4882a593Smuzhiyun return -ENOMEM;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun dram->size = len;
106*4882a593Smuzhiyun memcpy(dram->block, data, len);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun return 0;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
iwl_pcie_ctxt_info_free_paging(struct iwl_trans * trans)111*4882a593Smuzhiyun void iwl_pcie_ctxt_info_free_paging(struct iwl_trans *trans)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun struct iwl_self_init_dram *dram = &trans->init_dram;
114*4882a593Smuzhiyun int i;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun if (!dram->paging) {
117*4882a593Smuzhiyun WARN_ON(dram->paging_cnt);
118*4882a593Smuzhiyun return;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* free paging*/
122*4882a593Smuzhiyun for (i = 0; i < dram->paging_cnt; i++)
123*4882a593Smuzhiyun dma_free_coherent(trans->dev, dram->paging[i].size,
124*4882a593Smuzhiyun dram->paging[i].block,
125*4882a593Smuzhiyun dram->paging[i].physical);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun kfree(dram->paging);
128*4882a593Smuzhiyun dram->paging_cnt = 0;
129*4882a593Smuzhiyun dram->paging = NULL;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
iwl_pcie_init_fw_sec(struct iwl_trans * trans,const struct fw_img * fw,struct iwl_context_info_dram * ctxt_dram)132*4882a593Smuzhiyun int iwl_pcie_init_fw_sec(struct iwl_trans *trans,
133*4882a593Smuzhiyun const struct fw_img *fw,
134*4882a593Smuzhiyun struct iwl_context_info_dram *ctxt_dram)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun struct iwl_self_init_dram *dram = &trans->init_dram;
137*4882a593Smuzhiyun int i, ret, lmac_cnt, umac_cnt, paging_cnt;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun if (WARN(dram->paging,
140*4882a593Smuzhiyun "paging shouldn't already be initialized (%d pages)\n",
141*4882a593Smuzhiyun dram->paging_cnt))
142*4882a593Smuzhiyun iwl_pcie_ctxt_info_free_paging(trans);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun lmac_cnt = iwl_pcie_get_num_sections(fw, 0);
145*4882a593Smuzhiyun /* add 1 due to separator */
146*4882a593Smuzhiyun umac_cnt = iwl_pcie_get_num_sections(fw, lmac_cnt + 1);
147*4882a593Smuzhiyun /* add 2 due to separators */
148*4882a593Smuzhiyun paging_cnt = iwl_pcie_get_num_sections(fw, lmac_cnt + umac_cnt + 2);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun dram->fw = kcalloc(umac_cnt + lmac_cnt, sizeof(*dram->fw), GFP_KERNEL);
151*4882a593Smuzhiyun if (!dram->fw)
152*4882a593Smuzhiyun return -ENOMEM;
153*4882a593Smuzhiyun dram->paging = kcalloc(paging_cnt, sizeof(*dram->paging), GFP_KERNEL);
154*4882a593Smuzhiyun if (!dram->paging)
155*4882a593Smuzhiyun return -ENOMEM;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /* initialize lmac sections */
158*4882a593Smuzhiyun for (i = 0; i < lmac_cnt; i++) {
159*4882a593Smuzhiyun ret = iwl_pcie_ctxt_info_alloc_dma(trans, fw->sec[i].data,
160*4882a593Smuzhiyun fw->sec[i].len,
161*4882a593Smuzhiyun &dram->fw[dram->fw_cnt]);
162*4882a593Smuzhiyun if (ret)
163*4882a593Smuzhiyun return ret;
164*4882a593Smuzhiyun ctxt_dram->lmac_img[i] =
165*4882a593Smuzhiyun cpu_to_le64(dram->fw[dram->fw_cnt].physical);
166*4882a593Smuzhiyun dram->fw_cnt++;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /* initialize umac sections */
170*4882a593Smuzhiyun for (i = 0; i < umac_cnt; i++) {
171*4882a593Smuzhiyun /* access FW with +1 to make up for lmac separator */
172*4882a593Smuzhiyun ret = iwl_pcie_ctxt_info_alloc_dma(trans,
173*4882a593Smuzhiyun fw->sec[dram->fw_cnt + 1].data,
174*4882a593Smuzhiyun fw->sec[dram->fw_cnt + 1].len,
175*4882a593Smuzhiyun &dram->fw[dram->fw_cnt]);
176*4882a593Smuzhiyun if (ret)
177*4882a593Smuzhiyun return ret;
178*4882a593Smuzhiyun ctxt_dram->umac_img[i] =
179*4882a593Smuzhiyun cpu_to_le64(dram->fw[dram->fw_cnt].physical);
180*4882a593Smuzhiyun dram->fw_cnt++;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * Initialize paging.
185*4882a593Smuzhiyun * Paging memory isn't stored in dram->fw as the umac and lmac - it is
186*4882a593Smuzhiyun * stored separately.
187*4882a593Smuzhiyun * This is since the timing of its release is different -
188*4882a593Smuzhiyun * while fw memory can be released on alive, the paging memory can be
189*4882a593Smuzhiyun * freed only when the device goes down.
190*4882a593Smuzhiyun * Given that, the logic here in accessing the fw image is a bit
191*4882a593Smuzhiyun * different - fw_cnt isn't changing so loop counter is added to it.
192*4882a593Smuzhiyun */
193*4882a593Smuzhiyun for (i = 0; i < paging_cnt; i++) {
194*4882a593Smuzhiyun /* access FW with +2 to make up for lmac & umac separators */
195*4882a593Smuzhiyun int fw_idx = dram->fw_cnt + i + 2;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun ret = iwl_pcie_ctxt_info_alloc_dma(trans, fw->sec[fw_idx].data,
198*4882a593Smuzhiyun fw->sec[fw_idx].len,
199*4882a593Smuzhiyun &dram->paging[i]);
200*4882a593Smuzhiyun if (ret)
201*4882a593Smuzhiyun return ret;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun ctxt_dram->virtual_img[i] =
204*4882a593Smuzhiyun cpu_to_le64(dram->paging[i].physical);
205*4882a593Smuzhiyun dram->paging_cnt++;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun return 0;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
iwl_pcie_ctxt_info_init(struct iwl_trans * trans,const struct fw_img * fw)211*4882a593Smuzhiyun int iwl_pcie_ctxt_info_init(struct iwl_trans *trans,
212*4882a593Smuzhiyun const struct fw_img *fw)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
215*4882a593Smuzhiyun struct iwl_context_info *ctxt_info;
216*4882a593Smuzhiyun struct iwl_context_info_rbd_cfg *rx_cfg;
217*4882a593Smuzhiyun u32 control_flags = 0, rb_size;
218*4882a593Smuzhiyun dma_addr_t phys;
219*4882a593Smuzhiyun int ret;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun ctxt_info = iwl_pcie_ctxt_info_dma_alloc_coherent(trans,
222*4882a593Smuzhiyun sizeof(*ctxt_info),
223*4882a593Smuzhiyun &phys);
224*4882a593Smuzhiyun if (!ctxt_info)
225*4882a593Smuzhiyun return -ENOMEM;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun trans_pcie->ctxt_info_dma_addr = phys;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun ctxt_info->version.version = 0;
230*4882a593Smuzhiyun ctxt_info->version.mac_id =
231*4882a593Smuzhiyun cpu_to_le16((u16)iwl_read32(trans, CSR_HW_REV));
232*4882a593Smuzhiyun /* size is in DWs */
233*4882a593Smuzhiyun ctxt_info->version.size = cpu_to_le16(sizeof(*ctxt_info) / 4);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun switch (trans_pcie->rx_buf_size) {
236*4882a593Smuzhiyun case IWL_AMSDU_2K:
237*4882a593Smuzhiyun rb_size = IWL_CTXT_INFO_RB_SIZE_2K;
238*4882a593Smuzhiyun break;
239*4882a593Smuzhiyun case IWL_AMSDU_4K:
240*4882a593Smuzhiyun rb_size = IWL_CTXT_INFO_RB_SIZE_4K;
241*4882a593Smuzhiyun break;
242*4882a593Smuzhiyun case IWL_AMSDU_8K:
243*4882a593Smuzhiyun rb_size = IWL_CTXT_INFO_RB_SIZE_8K;
244*4882a593Smuzhiyun break;
245*4882a593Smuzhiyun case IWL_AMSDU_12K:
246*4882a593Smuzhiyun rb_size = IWL_CTXT_INFO_RB_SIZE_12K;
247*4882a593Smuzhiyun break;
248*4882a593Smuzhiyun default:
249*4882a593Smuzhiyun WARN_ON(1);
250*4882a593Smuzhiyun rb_size = IWL_CTXT_INFO_RB_SIZE_4K;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun WARN_ON(RX_QUEUE_CB_SIZE(trans->cfg->num_rbds) > 12);
254*4882a593Smuzhiyun control_flags = IWL_CTXT_INFO_TFD_FORMAT_LONG;
255*4882a593Smuzhiyun control_flags |=
256*4882a593Smuzhiyun u32_encode_bits(RX_QUEUE_CB_SIZE(trans->cfg->num_rbds),
257*4882a593Smuzhiyun IWL_CTXT_INFO_RB_CB_SIZE);
258*4882a593Smuzhiyun control_flags |= u32_encode_bits(rb_size, IWL_CTXT_INFO_RB_SIZE);
259*4882a593Smuzhiyun ctxt_info->control.control_flags = cpu_to_le32(control_flags);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /* initialize RX default queue */
262*4882a593Smuzhiyun rx_cfg = &ctxt_info->rbd_cfg;
263*4882a593Smuzhiyun rx_cfg->free_rbd_addr = cpu_to_le64(trans_pcie->rxq->bd_dma);
264*4882a593Smuzhiyun rx_cfg->used_rbd_addr = cpu_to_le64(trans_pcie->rxq->used_bd_dma);
265*4882a593Smuzhiyun rx_cfg->status_wr_ptr = cpu_to_le64(trans_pcie->rxq->rb_stts_dma);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun /* initialize TX command queue */
268*4882a593Smuzhiyun ctxt_info->hcmd_cfg.cmd_queue_addr =
269*4882a593Smuzhiyun cpu_to_le64(trans->txqs.txq[trans->txqs.cmd.q_id]->dma_addr);
270*4882a593Smuzhiyun ctxt_info->hcmd_cfg.cmd_queue_size =
271*4882a593Smuzhiyun TFD_QUEUE_CB_SIZE(IWL_CMD_QUEUE_SIZE);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /* allocate ucode sections in dram and set addresses */
274*4882a593Smuzhiyun ret = iwl_pcie_init_fw_sec(trans, fw, &ctxt_info->dram);
275*4882a593Smuzhiyun if (ret) {
276*4882a593Smuzhiyun dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info),
277*4882a593Smuzhiyun ctxt_info, trans_pcie->ctxt_info_dma_addr);
278*4882a593Smuzhiyun return ret;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun trans_pcie->ctxt_info = ctxt_info;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun iwl_enable_fw_load_int_ctx_info(trans);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /* Configure debug, if exists */
286*4882a593Smuzhiyun if (iwl_pcie_dbg_on(trans))
287*4882a593Smuzhiyun iwl_pcie_apply_destination(trans);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /* kick FW self load */
290*4882a593Smuzhiyun iwl_write64(trans, CSR_CTXT_INFO_BA, trans_pcie->ctxt_info_dma_addr);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /* Context info will be released upon alive or failure to get one */
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun return 0;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
iwl_pcie_ctxt_info_free(struct iwl_trans * trans)297*4882a593Smuzhiyun void iwl_pcie_ctxt_info_free(struct iwl_trans *trans)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun if (!trans_pcie->ctxt_info)
302*4882a593Smuzhiyun return;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info),
305*4882a593Smuzhiyun trans_pcie->ctxt_info,
306*4882a593Smuzhiyun trans_pcie->ctxt_info_dma_addr);
307*4882a593Smuzhiyun trans_pcie->ctxt_info_dma_addr = 0;
308*4882a593Smuzhiyun trans_pcie->ctxt_info = NULL;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun iwl_pcie_ctxt_info_free_fw_img(trans);
311*4882a593Smuzhiyun }
312