xref: /freebsd/sys/contrib/dev/iwlwifi/pcie/ctxt-info-v2.c (revision 6b627f88584ce13118e0a24951b503c0b1f2d5a7)
1*6b627f88SBjoern A. Zeeb // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2*6b627f88SBjoern A. Zeeb /*
3*6b627f88SBjoern A. Zeeb  * Copyright (C) 2018-2025 Intel Corporation
4*6b627f88SBjoern A. Zeeb  */
5*6b627f88SBjoern A. Zeeb #include <linux/dmi.h>
6*6b627f88SBjoern A. Zeeb #include "iwl-trans.h"
7*6b627f88SBjoern A. Zeeb #include "iwl-fh.h"
8*6b627f88SBjoern A. Zeeb #include "iwl-context-info-v2.h"
9*6b627f88SBjoern A. Zeeb #include "gen1_2/internal.h"
10*6b627f88SBjoern A. Zeeb #include "iwl-prph.h"
11*6b627f88SBjoern A. Zeeb 
12*6b627f88SBjoern A. Zeeb static const struct dmi_system_id dmi_force_scu_active_approved_list[] = {
13*6b627f88SBjoern A. Zeeb 	{ .ident = "DELL",
14*6b627f88SBjoern A. Zeeb 	  .matches = {
15*6b627f88SBjoern A. Zeeb 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
16*6b627f88SBjoern A. Zeeb 		},
17*6b627f88SBjoern A. Zeeb 	},
18*6b627f88SBjoern A. Zeeb 	{ .ident = "DELL",
19*6b627f88SBjoern A. Zeeb 	  .matches = {
20*6b627f88SBjoern A. Zeeb 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
21*6b627f88SBjoern A. Zeeb 		},
22*6b627f88SBjoern A. Zeeb 	},
23*6b627f88SBjoern A. Zeeb 	/* keep last */
24*6b627f88SBjoern A. Zeeb 	{}
25*6b627f88SBjoern A. Zeeb };
26*6b627f88SBjoern A. Zeeb 
iwl_is_force_scu_active_approved(void)27*6b627f88SBjoern A. Zeeb static bool iwl_is_force_scu_active_approved(void)
28*6b627f88SBjoern A. Zeeb {
29*6b627f88SBjoern A. Zeeb 	return !!dmi_check_system(dmi_force_scu_active_approved_list);
30*6b627f88SBjoern A. Zeeb }
31*6b627f88SBjoern A. Zeeb 
32*6b627f88SBjoern A. Zeeb static void
iwl_pcie_ctxt_info_dbg_enable(struct iwl_trans * trans,struct iwl_prph_scratch_hwm_cfg * dbg_cfg,u32 * control_flags)33*6b627f88SBjoern A. Zeeb iwl_pcie_ctxt_info_dbg_enable(struct iwl_trans *trans,
34*6b627f88SBjoern A. Zeeb 			      struct iwl_prph_scratch_hwm_cfg *dbg_cfg,
35*6b627f88SBjoern A. Zeeb 			      u32 *control_flags)
36*6b627f88SBjoern A. Zeeb {
37*6b627f88SBjoern A. Zeeb 	enum iwl_fw_ini_allocation_id alloc_id = IWL_FW_INI_ALLOCATION_ID_DBGC1;
38*6b627f88SBjoern A. Zeeb 	struct iwl_fw_ini_allocation_tlv *fw_mon_cfg;
39*6b627f88SBjoern A. Zeeb 	u32 dbg_flags = 0;
40*6b627f88SBjoern A. Zeeb 
41*6b627f88SBjoern A. Zeeb 	if (!iwl_trans_dbg_ini_valid(trans)) {
42*6b627f88SBjoern A. Zeeb 		struct iwl_dram_data *fw_mon = &trans->dbg.fw_mon;
43*6b627f88SBjoern A. Zeeb 
44*6b627f88SBjoern A. Zeeb 		iwl_pcie_alloc_fw_monitor(trans, 0);
45*6b627f88SBjoern A. Zeeb 
46*6b627f88SBjoern A. Zeeb 		if (fw_mon->size) {
47*6b627f88SBjoern A. Zeeb 			dbg_flags |= IWL_PRPH_SCRATCH_EDBG_DEST_DRAM;
48*6b627f88SBjoern A. Zeeb 
49*6b627f88SBjoern A. Zeeb 			IWL_DEBUG_FW(trans,
50*6b627f88SBjoern A. Zeeb 				     "WRT: Applying DRAM buffer destination\n");
51*6b627f88SBjoern A. Zeeb 
52*6b627f88SBjoern A. Zeeb 			dbg_cfg->hwm_base_addr = cpu_to_le64(fw_mon->physical);
53*6b627f88SBjoern A. Zeeb 			dbg_cfg->hwm_size = cpu_to_le32(fw_mon->size);
54*6b627f88SBjoern A. Zeeb 		}
55*6b627f88SBjoern A. Zeeb 
56*6b627f88SBjoern A. Zeeb 		goto out;
57*6b627f88SBjoern A. Zeeb 	}
58*6b627f88SBjoern A. Zeeb 
59*6b627f88SBjoern A. Zeeb 	fw_mon_cfg = &trans->dbg.fw_mon_cfg[alloc_id];
60*6b627f88SBjoern A. Zeeb 
61*6b627f88SBjoern A. Zeeb 	switch (le32_to_cpu(fw_mon_cfg->buf_location)) {
62*6b627f88SBjoern A. Zeeb 	case IWL_FW_INI_LOCATION_SRAM_PATH:
63*6b627f88SBjoern A. Zeeb 		dbg_flags |= IWL_PRPH_SCRATCH_EDBG_DEST_INTERNAL;
64*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans,
65*6b627f88SBjoern A. Zeeb 				"WRT: Applying SMEM buffer destination\n");
66*6b627f88SBjoern A. Zeeb 		break;
67*6b627f88SBjoern A. Zeeb 
68*6b627f88SBjoern A. Zeeb 	case IWL_FW_INI_LOCATION_NPK_PATH:
69*6b627f88SBjoern A. Zeeb 		dbg_flags |= IWL_PRPH_SCRATCH_EDBG_DEST_TB22DTF;
70*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans,
71*6b627f88SBjoern A. Zeeb 			     "WRT: Applying NPK buffer destination\n");
72*6b627f88SBjoern A. Zeeb 		break;
73*6b627f88SBjoern A. Zeeb 
74*6b627f88SBjoern A. Zeeb 	case IWL_FW_INI_LOCATION_DRAM_PATH:
75*6b627f88SBjoern A. Zeeb 		if (trans->dbg.fw_mon_ini[alloc_id].num_frags) {
76*6b627f88SBjoern A. Zeeb 			struct iwl_dram_data *frag =
77*6b627f88SBjoern A. Zeeb 				&trans->dbg.fw_mon_ini[alloc_id].frags[0];
78*6b627f88SBjoern A. Zeeb 			dbg_flags |= IWL_PRPH_SCRATCH_EDBG_DEST_DRAM;
79*6b627f88SBjoern A. Zeeb 			dbg_cfg->hwm_base_addr = cpu_to_le64(frag->physical);
80*6b627f88SBjoern A. Zeeb 			dbg_cfg->hwm_size = cpu_to_le32(frag->size);
81*6b627f88SBjoern A. Zeeb 			dbg_cfg->debug_token_config = cpu_to_le32(trans->dbg.ucode_preset);
82*6b627f88SBjoern A. Zeeb 			IWL_DEBUG_FW(trans,
83*6b627f88SBjoern A. Zeeb 				     "WRT: Applying DRAM destination (debug_token_config=%u)\n",
84*6b627f88SBjoern A. Zeeb 				     dbg_cfg->debug_token_config);
85*6b627f88SBjoern A. Zeeb 			IWL_DEBUG_FW(trans,
86*6b627f88SBjoern A. Zeeb 				     "WRT: Applying DRAM destination (alloc_id=%u, num_frags=%u)\n",
87*6b627f88SBjoern A. Zeeb 				     alloc_id,
88*6b627f88SBjoern A. Zeeb 				     trans->dbg.fw_mon_ini[alloc_id].num_frags);
89*6b627f88SBjoern A. Zeeb 		}
90*6b627f88SBjoern A. Zeeb 		break;
91*6b627f88SBjoern A. Zeeb 	default:
92*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans, "WRT: Invalid buffer destination (%d)\n",
93*6b627f88SBjoern A. Zeeb 			     le32_to_cpu(fw_mon_cfg->buf_location));
94*6b627f88SBjoern A. Zeeb 	}
95*6b627f88SBjoern A. Zeeb out:
96*6b627f88SBjoern A. Zeeb 	if (dbg_flags)
97*6b627f88SBjoern A. Zeeb 		*control_flags |= IWL_PRPH_SCRATCH_EARLY_DEBUG_EN | dbg_flags;
98*6b627f88SBjoern A. Zeeb }
99*6b627f88SBjoern A. Zeeb 
iwl_pcie_ctxt_info_v2_alloc(struct iwl_trans * trans,const struct iwl_fw * fw,const struct fw_img * img)100*6b627f88SBjoern A. Zeeb int iwl_pcie_ctxt_info_v2_alloc(struct iwl_trans *trans,
101*6b627f88SBjoern A. Zeeb 				const struct iwl_fw *fw,
102*6b627f88SBjoern A. Zeeb 				const struct fw_img *img)
103*6b627f88SBjoern A. Zeeb {
104*6b627f88SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
105*6b627f88SBjoern A. Zeeb 	struct iwl_context_info_v2 *ctxt_info_v2;
106*6b627f88SBjoern A. Zeeb 	struct iwl_prph_scratch *prph_scratch;
107*6b627f88SBjoern A. Zeeb 	struct iwl_prph_scratch_ctrl_cfg *prph_sc_ctrl;
108*6b627f88SBjoern A. Zeeb 	struct iwl_prph_info *prph_info;
109*6b627f88SBjoern A. Zeeb 	u32 control_flags = 0;
110*6b627f88SBjoern A. Zeeb 	u32 control_flags_ext = 0;
111*6b627f88SBjoern A. Zeeb 	int ret;
112*6b627f88SBjoern A. Zeeb 	int cmdq_size = max_t(u32, IWL_CMD_QUEUE_SIZE,
113*6b627f88SBjoern A. Zeeb 			      trans->mac_cfg->base->min_txq_size);
114*6b627f88SBjoern A. Zeeb 
115*6b627f88SBjoern A. Zeeb 	switch (trans->conf.rx_buf_size) {
116*6b627f88SBjoern A. Zeeb 	case IWL_AMSDU_DEF:
117*6b627f88SBjoern A. Zeeb 		return -EINVAL;
118*6b627f88SBjoern A. Zeeb 	case IWL_AMSDU_2K:
119*6b627f88SBjoern A. Zeeb 		break;
120*6b627f88SBjoern A. Zeeb 	case IWL_AMSDU_4K:
121*6b627f88SBjoern A. Zeeb 		control_flags |= IWL_PRPH_SCRATCH_RB_SIZE_4K;
122*6b627f88SBjoern A. Zeeb 		break;
123*6b627f88SBjoern A. Zeeb 	case IWL_AMSDU_8K:
124*6b627f88SBjoern A. Zeeb 		control_flags |= IWL_PRPH_SCRATCH_RB_SIZE_4K;
125*6b627f88SBjoern A. Zeeb 		/* if firmware supports the ext size, tell it */
126*6b627f88SBjoern A. Zeeb 		control_flags |= IWL_PRPH_SCRATCH_RB_SIZE_EXT_8K;
127*6b627f88SBjoern A. Zeeb 		break;
128*6b627f88SBjoern A. Zeeb 	case IWL_AMSDU_12K:
129*6b627f88SBjoern A. Zeeb 		control_flags |= IWL_PRPH_SCRATCH_RB_SIZE_4K;
130*6b627f88SBjoern A. Zeeb 		/* if firmware supports the ext size, tell it */
131*6b627f88SBjoern A. Zeeb 		control_flags |= IWL_PRPH_SCRATCH_RB_SIZE_EXT_16K;
132*6b627f88SBjoern A. Zeeb 		break;
133*6b627f88SBjoern A. Zeeb 	}
134*6b627f88SBjoern A. Zeeb 
135*6b627f88SBjoern A. Zeeb 	if (trans->conf.dsbr_urm_fw_dependent)
136*6b627f88SBjoern A. Zeeb 		control_flags_ext |= IWL_PRPH_SCRATCH_EXT_URM_FW;
137*6b627f88SBjoern A. Zeeb 
138*6b627f88SBjoern A. Zeeb 	if (trans->conf.dsbr_urm_permanent)
139*6b627f88SBjoern A. Zeeb 		control_flags_ext |= IWL_PRPH_SCRATCH_EXT_URM_PERM;
140*6b627f88SBjoern A. Zeeb 
141*6b627f88SBjoern A. Zeeb 	if (trans->conf.ext_32khz_clock_valid)
142*6b627f88SBjoern A. Zeeb 		control_flags_ext |= IWL_PRPH_SCRATCH_EXT_32KHZ_CLK_VALID;
143*6b627f88SBjoern A. Zeeb 
144*6b627f88SBjoern A. Zeeb 	/* Allocate prph scratch */
145*6b627f88SBjoern A. Zeeb 	prph_scratch = dma_alloc_coherent(trans->dev, sizeof(*prph_scratch),
146*6b627f88SBjoern A. Zeeb 					  &trans_pcie->prph_scratch_dma_addr,
147*6b627f88SBjoern A. Zeeb 					  GFP_KERNEL);
148*6b627f88SBjoern A. Zeeb 	if (!prph_scratch)
149*6b627f88SBjoern A. Zeeb 		return -ENOMEM;
150*6b627f88SBjoern A. Zeeb 
151*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl = &prph_scratch->ctrl_cfg;
152*6b627f88SBjoern A. Zeeb 
153*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->version.version = 0;
154*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->version.mac_id =
155*6b627f88SBjoern A. Zeeb 		cpu_to_le16((u16)trans->info.hw_rev);
156*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->version.size = cpu_to_le16(sizeof(*prph_scratch) / 4);
157*6b627f88SBjoern A. Zeeb 
158*6b627f88SBjoern A. Zeeb 	control_flags |= IWL_PRPH_SCRATCH_MTR_MODE;
159*6b627f88SBjoern A. Zeeb 	control_flags |= IWL_PRPH_MTR_FORMAT_256B & IWL_PRPH_SCRATCH_MTR_FORMAT;
160*6b627f88SBjoern A. Zeeb 
161*6b627f88SBjoern A. Zeeb 	if (trans->mac_cfg->imr_enabled)
162*6b627f88SBjoern A. Zeeb 		control_flags |= IWL_PRPH_SCRATCH_IMR_DEBUG_EN;
163*6b627f88SBjoern A. Zeeb 
164*6b627f88SBjoern A. Zeeb 	if (CSR_HW_REV_TYPE(trans->info.hw_rev) == IWL_CFG_MAC_TYPE_GL &&
165*6b627f88SBjoern A. Zeeb 	    iwl_is_force_scu_active_approved()) {
166*6b627f88SBjoern A. Zeeb 		control_flags |= IWL_PRPH_SCRATCH_SCU_FORCE_ACTIVE;
167*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans,
168*6b627f88SBjoern A. Zeeb 			     "Context Info: Set SCU_FORCE_ACTIVE (0x%x) in control_flags\n",
169*6b627f88SBjoern A. Zeeb 			     IWL_PRPH_SCRATCH_SCU_FORCE_ACTIVE);
170*6b627f88SBjoern A. Zeeb 	}
171*6b627f88SBjoern A. Zeeb 
172*6b627f88SBjoern A. Zeeb 	if (trans->do_top_reset) {
173*6b627f88SBjoern A. Zeeb 		WARN_ON(trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_SC);
174*6b627f88SBjoern A. Zeeb 		control_flags |= IWL_PRPH_SCRATCH_TOP_RESET;
175*6b627f88SBjoern A. Zeeb 	}
176*6b627f88SBjoern A. Zeeb 
177*6b627f88SBjoern A. Zeeb 	/* initialize RX default queue */
178*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->rbd_cfg.free_rbd_addr =
179*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->rxq->bd_dma);
180*6b627f88SBjoern A. Zeeb 
181*6b627f88SBjoern A. Zeeb 	iwl_pcie_ctxt_info_dbg_enable(trans, &prph_sc_ctrl->hwm_cfg,
182*6b627f88SBjoern A. Zeeb 				      &control_flags);
183*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->control.control_flags = cpu_to_le32(control_flags);
184*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->control.control_flags_ext = cpu_to_le32(control_flags_ext);
185*6b627f88SBjoern A. Zeeb 
186*6b627f88SBjoern A. Zeeb 	/* initialize the Step equalizer data */
187*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->step_cfg.mbx_addr_0 =
188*6b627f88SBjoern A. Zeeb 		cpu_to_le32(trans->conf.mbx_addr_0_step);
189*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->step_cfg.mbx_addr_1 =
190*6b627f88SBjoern A. Zeeb 		cpu_to_le32(trans->conf.mbx_addr_1_step);
191*6b627f88SBjoern A. Zeeb 
192*6b627f88SBjoern A. Zeeb 	/* allocate ucode sections in dram and set addresses */
193*6b627f88SBjoern A. Zeeb 	ret = iwl_pcie_init_fw_sec(trans, img, &prph_scratch->dram.common);
194*6b627f88SBjoern A. Zeeb 	if (ret)
195*6b627f88SBjoern A. Zeeb 		goto err_free_prph_scratch;
196*6b627f88SBjoern A. Zeeb 
197*6b627f88SBjoern A. Zeeb 	/* Allocate prph information
198*6b627f88SBjoern A. Zeeb 	 * currently we don't assign to the prph info anything, but it would get
199*6b627f88SBjoern A. Zeeb 	 * assigned later
200*6b627f88SBjoern A. Zeeb 	 *
201*6b627f88SBjoern A. Zeeb 	 * We also use the second half of this page to give the device some
202*6b627f88SBjoern A. Zeeb 	 * dummy TR/CR tail pointers - which shouldn't be necessary as we don't
203*6b627f88SBjoern A. Zeeb 	 * use this, but the hardware still reads/writes there and we can't let
204*6b627f88SBjoern A. Zeeb 	 * it go do that with a NULL pointer.
205*6b627f88SBjoern A. Zeeb 	 */
206*6b627f88SBjoern A. Zeeb 	BUILD_BUG_ON(sizeof(*prph_info) > PAGE_SIZE / 2);
207*6b627f88SBjoern A. Zeeb 	prph_info = dma_alloc_coherent(trans->dev, PAGE_SIZE,
208*6b627f88SBjoern A. Zeeb 				       &trans_pcie->prph_info_dma_addr,
209*6b627f88SBjoern A. Zeeb 				       GFP_KERNEL);
210*6b627f88SBjoern A. Zeeb 	if (!prph_info) {
211*6b627f88SBjoern A. Zeeb 		ret = -ENOMEM;
212*6b627f88SBjoern A. Zeeb 		goto err_free_prph_scratch;
213*6b627f88SBjoern A. Zeeb 	}
214*6b627f88SBjoern A. Zeeb 
215*6b627f88SBjoern A. Zeeb 	/* Allocate context info */
216*6b627f88SBjoern A. Zeeb 	ctxt_info_v2 = dma_alloc_coherent(trans->dev,
217*6b627f88SBjoern A. Zeeb 					  sizeof(*ctxt_info_v2),
218*6b627f88SBjoern A. Zeeb 					  &trans_pcie->ctxt_info_dma_addr,
219*6b627f88SBjoern A. Zeeb 					  GFP_KERNEL);
220*6b627f88SBjoern A. Zeeb 	if (!ctxt_info_v2) {
221*6b627f88SBjoern A. Zeeb 		ret = -ENOMEM;
222*6b627f88SBjoern A. Zeeb 		goto err_free_prph_info;
223*6b627f88SBjoern A. Zeeb 	}
224*6b627f88SBjoern A. Zeeb 
225*6b627f88SBjoern A. Zeeb 	ctxt_info_v2->prph_info_base_addr =
226*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->prph_info_dma_addr);
227*6b627f88SBjoern A. Zeeb 	ctxt_info_v2->prph_scratch_base_addr =
228*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->prph_scratch_dma_addr);
229*6b627f88SBjoern A. Zeeb 
230*6b627f88SBjoern A. Zeeb 	/*
231*6b627f88SBjoern A. Zeeb 	 * This code assumes the FSEQ is last and we can make that
232*6b627f88SBjoern A. Zeeb 	 * optional; old devices _should_ be fine with a bigger size,
233*6b627f88SBjoern A. Zeeb 	 * but in simulation we check the size more precisely.
234*6b627f88SBjoern A. Zeeb 	 */
235*6b627f88SBjoern A. Zeeb 	BUILD_BUG_ON(offsetofend(typeof(*prph_scratch), dram.common) +
236*6b627f88SBjoern A. Zeeb 		     sizeof(prph_scratch->dram.fseq_img) !=
237*6b627f88SBjoern A. Zeeb 		     sizeof(*prph_scratch));
238*6b627f88SBjoern A. Zeeb 	if (control_flags_ext & IWL_PRPH_SCRATCH_EXT_EXT_FSEQ)
239*6b627f88SBjoern A. Zeeb 		ctxt_info_v2->prph_scratch_size =
240*6b627f88SBjoern A. Zeeb 			cpu_to_le32(sizeof(*prph_scratch));
241*6b627f88SBjoern A. Zeeb 	else
242*6b627f88SBjoern A. Zeeb 		ctxt_info_v2->prph_scratch_size =
243*6b627f88SBjoern A. Zeeb 			cpu_to_le32(offsetofend(typeof(*prph_scratch),
244*6b627f88SBjoern A. Zeeb 						dram.common));
245*6b627f88SBjoern A. Zeeb 
246*6b627f88SBjoern A. Zeeb 	ctxt_info_v2->cr_head_idx_arr_base_addr =
247*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->rxq->rb_stts_dma);
248*6b627f88SBjoern A. Zeeb 	ctxt_info_v2->tr_tail_idx_arr_base_addr =
249*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->prph_info_dma_addr + PAGE_SIZE / 2);
250*6b627f88SBjoern A. Zeeb 	ctxt_info_v2->cr_tail_idx_arr_base_addr =
251*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->prph_info_dma_addr + 3 * PAGE_SIZE / 4);
252*6b627f88SBjoern A. Zeeb 	ctxt_info_v2->mtr_base_addr =
253*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->txqs.txq[trans->conf.cmd_queue]->dma_addr);
254*6b627f88SBjoern A. Zeeb 	ctxt_info_v2->mcr_base_addr =
255*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->rxq->used_bd_dma);
256*6b627f88SBjoern A. Zeeb 	ctxt_info_v2->mtr_size =
257*6b627f88SBjoern A. Zeeb 		cpu_to_le16(TFD_QUEUE_CB_SIZE(cmdq_size));
258*6b627f88SBjoern A. Zeeb 	ctxt_info_v2->mcr_size =
259*6b627f88SBjoern A. Zeeb 		cpu_to_le16(RX_QUEUE_CB_SIZE(iwl_trans_get_num_rbds(trans)));
260*6b627f88SBjoern A. Zeeb 
261*6b627f88SBjoern A. Zeeb 	trans_pcie->ctxt_info_v2 = ctxt_info_v2;
262*6b627f88SBjoern A. Zeeb 	trans_pcie->prph_info = prph_info;
263*6b627f88SBjoern A. Zeeb 	trans_pcie->prph_scratch = prph_scratch;
264*6b627f88SBjoern A. Zeeb 
265*6b627f88SBjoern A. Zeeb 	/* Allocate IML */
266*6b627f88SBjoern A. Zeeb 	trans_pcie->iml_len = fw->iml_len;
267*6b627f88SBjoern A. Zeeb 	trans_pcie->iml = dma_alloc_coherent(trans->dev, fw->iml_len,
268*6b627f88SBjoern A. Zeeb 					     &trans_pcie->iml_dma_addr,
269*6b627f88SBjoern A. Zeeb 					     GFP_KERNEL);
270*6b627f88SBjoern A. Zeeb 	if (!trans_pcie->iml) {
271*6b627f88SBjoern A. Zeeb 		ret = -ENOMEM;
272*6b627f88SBjoern A. Zeeb 		goto err_free_ctxt_info;
273*6b627f88SBjoern A. Zeeb 	}
274*6b627f88SBjoern A. Zeeb 
275*6b627f88SBjoern A. Zeeb 	memcpy(trans_pcie->iml, fw->iml, fw->iml_len);
276*6b627f88SBjoern A. Zeeb 
277*6b627f88SBjoern A. Zeeb 	return 0;
278*6b627f88SBjoern A. Zeeb 
279*6b627f88SBjoern A. Zeeb err_free_ctxt_info:
280*6b627f88SBjoern A. Zeeb 	dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info_v2),
281*6b627f88SBjoern A. Zeeb 			  trans_pcie->ctxt_info_v2,
282*6b627f88SBjoern A. Zeeb 			  trans_pcie->ctxt_info_dma_addr);
283*6b627f88SBjoern A. Zeeb 	trans_pcie->ctxt_info_v2 = NULL;
284*6b627f88SBjoern A. Zeeb err_free_prph_info:
285*6b627f88SBjoern A. Zeeb 	dma_free_coherent(trans->dev, PAGE_SIZE, prph_info,
286*6b627f88SBjoern A. Zeeb 			  trans_pcie->prph_info_dma_addr);
287*6b627f88SBjoern A. Zeeb 
288*6b627f88SBjoern A. Zeeb err_free_prph_scratch:
289*6b627f88SBjoern A. Zeeb 	dma_free_coherent(trans->dev,
290*6b627f88SBjoern A. Zeeb 			  sizeof(*prph_scratch),
291*6b627f88SBjoern A. Zeeb 			prph_scratch,
292*6b627f88SBjoern A. Zeeb 			trans_pcie->prph_scratch_dma_addr);
293*6b627f88SBjoern A. Zeeb 	return ret;
294*6b627f88SBjoern A. Zeeb 
295*6b627f88SBjoern A. Zeeb }
296*6b627f88SBjoern A. Zeeb 
iwl_pcie_ctxt_info_v2_kick(struct iwl_trans * trans)297*6b627f88SBjoern A. Zeeb void iwl_pcie_ctxt_info_v2_kick(struct iwl_trans *trans)
298*6b627f88SBjoern A. Zeeb {
299*6b627f88SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
300*6b627f88SBjoern A. Zeeb 
301*6b627f88SBjoern A. Zeeb 	iwl_enable_fw_load_int_ctx_info(trans, trans->do_top_reset);
302*6b627f88SBjoern A. Zeeb 
303*6b627f88SBjoern A. Zeeb 	/* kick FW self load */
304*6b627f88SBjoern A. Zeeb 	iwl_write64(trans, CSR_CTXT_INFO_ADDR, trans_pcie->ctxt_info_dma_addr);
305*6b627f88SBjoern A. Zeeb 	iwl_write64(trans, CSR_IML_DATA_ADDR, trans_pcie->iml_dma_addr);
306*6b627f88SBjoern A. Zeeb 	iwl_write32(trans, CSR_IML_SIZE_ADDR, trans_pcie->iml_len);
307*6b627f88SBjoern A. Zeeb 
308*6b627f88SBjoern A. Zeeb 	iwl_set_bit(trans, CSR_CTXT_INFO_BOOT_CTRL,
309*6b627f88SBjoern A. Zeeb 		    CSR_AUTO_FUNC_BOOT_ENA);
310*6b627f88SBjoern A. Zeeb }
311*6b627f88SBjoern A. Zeeb 
iwl_pcie_ctxt_info_v2_free(struct iwl_trans * trans,bool alive)312*6b627f88SBjoern A. Zeeb void iwl_pcie_ctxt_info_v2_free(struct iwl_trans *trans, bool alive)
313*6b627f88SBjoern A. Zeeb {
314*6b627f88SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
315*6b627f88SBjoern A. Zeeb 
316*6b627f88SBjoern A. Zeeb 	if (trans_pcie->iml) {
317*6b627f88SBjoern A. Zeeb 		dma_free_coherent(trans->dev, trans_pcie->iml_len,
318*6b627f88SBjoern A. Zeeb 				  trans_pcie->iml,
319*6b627f88SBjoern A. Zeeb 				  trans_pcie->iml_dma_addr);
320*6b627f88SBjoern A. Zeeb 		trans_pcie->iml_dma_addr = 0;
321*6b627f88SBjoern A. Zeeb 		trans_pcie->iml_len = 0;
322*6b627f88SBjoern A. Zeeb 		trans_pcie->iml = NULL;
323*6b627f88SBjoern A. Zeeb 	}
324*6b627f88SBjoern A. Zeeb 
325*6b627f88SBjoern A. Zeeb 	iwl_pcie_ctxt_info_free_fw_img(trans);
326*6b627f88SBjoern A. Zeeb 
327*6b627f88SBjoern A. Zeeb 	if (alive)
328*6b627f88SBjoern A. Zeeb 		return;
329*6b627f88SBjoern A. Zeeb 
330*6b627f88SBjoern A. Zeeb 	if (!trans_pcie->ctxt_info_v2)
331*6b627f88SBjoern A. Zeeb 		return;
332*6b627f88SBjoern A. Zeeb 
333*6b627f88SBjoern A. Zeeb 	/* ctxt_info_v2 and prph_scratch are still needed for PNVM load */
334*6b627f88SBjoern A. Zeeb 	dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info_v2),
335*6b627f88SBjoern A. Zeeb 			  trans_pcie->ctxt_info_v2,
336*6b627f88SBjoern A. Zeeb 			  trans_pcie->ctxt_info_dma_addr);
337*6b627f88SBjoern A. Zeeb 	trans_pcie->ctxt_info_dma_addr = 0;
338*6b627f88SBjoern A. Zeeb 	trans_pcie->ctxt_info_v2 = NULL;
339*6b627f88SBjoern A. Zeeb 
340*6b627f88SBjoern A. Zeeb 	dma_free_coherent(trans->dev, sizeof(*trans_pcie->prph_scratch),
341*6b627f88SBjoern A. Zeeb 			  trans_pcie->prph_scratch,
342*6b627f88SBjoern A. Zeeb 			  trans_pcie->prph_scratch_dma_addr);
343*6b627f88SBjoern A. Zeeb 	trans_pcie->prph_scratch_dma_addr = 0;
344*6b627f88SBjoern A. Zeeb 	trans_pcie->prph_scratch = NULL;
345*6b627f88SBjoern A. Zeeb 
346*6b627f88SBjoern A. Zeeb 	/* this is needed for the entire lifetime */
347*6b627f88SBjoern A. Zeeb 	dma_free_coherent(trans->dev, PAGE_SIZE, trans_pcie->prph_info,
348*6b627f88SBjoern A. Zeeb 			  trans_pcie->prph_info_dma_addr);
349*6b627f88SBjoern A. Zeeb 	trans_pcie->prph_info_dma_addr = 0;
350*6b627f88SBjoern A. Zeeb 	trans_pcie->prph_info = NULL;
351*6b627f88SBjoern A. Zeeb }
352*6b627f88SBjoern A. Zeeb 
iwl_pcie_load_payloads_contig(struct iwl_trans * trans,const struct iwl_pnvm_image * pnvm_data,struct iwl_dram_data * dram)353*6b627f88SBjoern A. Zeeb static int iwl_pcie_load_payloads_contig(struct iwl_trans *trans,
354*6b627f88SBjoern A. Zeeb 					 const struct iwl_pnvm_image *pnvm_data,
355*6b627f88SBjoern A. Zeeb 					 struct iwl_dram_data *dram)
356*6b627f88SBjoern A. Zeeb {
357*6b627f88SBjoern A. Zeeb 	u32 len, len0, len1;
358*6b627f88SBjoern A. Zeeb 
359*6b627f88SBjoern A. Zeeb 	if (pnvm_data->n_chunks != UNFRAGMENTED_PNVM_PAYLOADS_NUMBER) {
360*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans, "expected 2 payloads, got %d.\n",
361*6b627f88SBjoern A. Zeeb 			     pnvm_data->n_chunks);
362*6b627f88SBjoern A. Zeeb 		return -EINVAL;
363*6b627f88SBjoern A. Zeeb 	}
364*6b627f88SBjoern A. Zeeb 
365*6b627f88SBjoern A. Zeeb 	len0 = pnvm_data->chunks[0].len;
366*6b627f88SBjoern A. Zeeb 	len1 = pnvm_data->chunks[1].len;
367*6b627f88SBjoern A. Zeeb 	if (len1 > 0xFFFFFFFF - len0) {
368*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans, "sizes of payloads overflow.\n");
369*6b627f88SBjoern A. Zeeb 		return -EINVAL;
370*6b627f88SBjoern A. Zeeb 	}
371*6b627f88SBjoern A. Zeeb 	len = len0 + len1;
372*6b627f88SBjoern A. Zeeb 
373*6b627f88SBjoern A. Zeeb 	dram->block = iwl_pcie_ctxt_info_dma_alloc_coherent(trans, len,
374*6b627f88SBjoern A. Zeeb 							    &dram->physical);
375*6b627f88SBjoern A. Zeeb 	if (!dram->block) {
376*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans, "Failed to allocate PNVM DMA.\n");
377*6b627f88SBjoern A. Zeeb 		return -ENOMEM;
378*6b627f88SBjoern A. Zeeb 	}
379*6b627f88SBjoern A. Zeeb 
380*6b627f88SBjoern A. Zeeb 	dram->size = len;
381*6b627f88SBjoern A. Zeeb 	memcpy(dram->block, pnvm_data->chunks[0].data, len0);
382*6b627f88SBjoern A. Zeeb 	memcpy((u8 *)dram->block + len0, pnvm_data->chunks[1].data, len1);
383*6b627f88SBjoern A. Zeeb 
384*6b627f88SBjoern A. Zeeb 	return 0;
385*6b627f88SBjoern A. Zeeb }
386*6b627f88SBjoern A. Zeeb 
iwl_pcie_load_payloads_segments(struct iwl_trans * trans,struct iwl_dram_regions * dram_regions,const struct iwl_pnvm_image * pnvm_data)387*6b627f88SBjoern A. Zeeb static int iwl_pcie_load_payloads_segments
388*6b627f88SBjoern A. Zeeb 				(struct iwl_trans *trans,
389*6b627f88SBjoern A. Zeeb 				 struct iwl_dram_regions *dram_regions,
390*6b627f88SBjoern A. Zeeb 				 const struct iwl_pnvm_image *pnvm_data)
391*6b627f88SBjoern A. Zeeb {
392*6b627f88SBjoern A. Zeeb 	struct iwl_dram_data *cur_payload_dram = &dram_regions->drams[0];
393*6b627f88SBjoern A. Zeeb 	struct iwl_dram_data *desc_dram = &dram_regions->prph_scratch_mem_desc;
394*6b627f88SBjoern A. Zeeb 	struct iwl_prph_scratch_mem_desc_addr_array *addresses;
395*6b627f88SBjoern A. Zeeb 	const void *data;
396*6b627f88SBjoern A. Zeeb 	u32 len;
397*6b627f88SBjoern A. Zeeb 	int i;
398*6b627f88SBjoern A. Zeeb 
399*6b627f88SBjoern A. Zeeb 	/* allocate and init DRAM descriptors array */
400*6b627f88SBjoern A. Zeeb 	len = sizeof(struct iwl_prph_scratch_mem_desc_addr_array);
401*6b627f88SBjoern A. Zeeb 	desc_dram->block = iwl_pcie_ctxt_info_dma_alloc_coherent
402*6b627f88SBjoern A. Zeeb 						(trans,
403*6b627f88SBjoern A. Zeeb 						 len,
404*6b627f88SBjoern A. Zeeb 						 &desc_dram->physical);
405*6b627f88SBjoern A. Zeeb 	if (!desc_dram->block) {
406*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans, "Failed to allocate PNVM DMA.\n");
407*6b627f88SBjoern A. Zeeb 		return -ENOMEM;
408*6b627f88SBjoern A. Zeeb 	}
409*6b627f88SBjoern A. Zeeb 	desc_dram->size = len;
410*6b627f88SBjoern A. Zeeb 	memset(desc_dram->block, 0, len);
411*6b627f88SBjoern A. Zeeb 
412*6b627f88SBjoern A. Zeeb 	/* allocate DRAM region for each payload */
413*6b627f88SBjoern A. Zeeb 	dram_regions->n_regions = 0;
414*6b627f88SBjoern A. Zeeb 	for (i = 0; i < pnvm_data->n_chunks; i++) {
415*6b627f88SBjoern A. Zeeb 		len = pnvm_data->chunks[i].len;
416*6b627f88SBjoern A. Zeeb 		data = pnvm_data->chunks[i].data;
417*6b627f88SBjoern A. Zeeb 
418*6b627f88SBjoern A. Zeeb 		if (iwl_pcie_ctxt_info_alloc_dma(trans,
419*6b627f88SBjoern A. Zeeb 						 data,
420*6b627f88SBjoern A. Zeeb 						 len,
421*6b627f88SBjoern A. Zeeb 						 cur_payload_dram)) {
422*6b627f88SBjoern A. Zeeb 			iwl_trans_pcie_free_pnvm_dram_regions(dram_regions,
423*6b627f88SBjoern A. Zeeb 							      trans->dev);
424*6b627f88SBjoern A. Zeeb 			return -ENOMEM;
425*6b627f88SBjoern A. Zeeb 		}
426*6b627f88SBjoern A. Zeeb 
427*6b627f88SBjoern A. Zeeb 		dram_regions->n_regions++;
428*6b627f88SBjoern A. Zeeb 		cur_payload_dram++;
429*6b627f88SBjoern A. Zeeb 	}
430*6b627f88SBjoern A. Zeeb 
431*6b627f88SBjoern A. Zeeb 	/* fill desc with the DRAM payloads addresses */
432*6b627f88SBjoern A. Zeeb 	addresses = desc_dram->block;
433*6b627f88SBjoern A. Zeeb 	for (i = 0; i < pnvm_data->n_chunks; i++) {
434*6b627f88SBjoern A. Zeeb 		addresses->mem_descs[i] =
435*6b627f88SBjoern A. Zeeb 			cpu_to_le64(dram_regions->drams[i].physical);
436*6b627f88SBjoern A. Zeeb 	}
437*6b627f88SBjoern A. Zeeb 
438*6b627f88SBjoern A. Zeeb 	return 0;
439*6b627f88SBjoern A. Zeeb 
440*6b627f88SBjoern A. Zeeb }
441*6b627f88SBjoern A. Zeeb 
iwl_trans_pcie_ctx_info_v2_load_pnvm(struct iwl_trans * trans,const struct iwl_pnvm_image * pnvm_payloads,const struct iwl_ucode_capabilities * capa)442*6b627f88SBjoern A. Zeeb int iwl_trans_pcie_ctx_info_v2_load_pnvm(struct iwl_trans *trans,
443*6b627f88SBjoern A. Zeeb 					 const struct iwl_pnvm_image *pnvm_payloads,
444*6b627f88SBjoern A. Zeeb 					 const struct iwl_ucode_capabilities *capa)
445*6b627f88SBjoern A. Zeeb {
446*6b627f88SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
447*6b627f88SBjoern A. Zeeb 	struct iwl_prph_scratch_ctrl_cfg *prph_sc_ctrl =
448*6b627f88SBjoern A. Zeeb 		&trans_pcie->prph_scratch->ctrl_cfg;
449*6b627f88SBjoern A. Zeeb 	struct iwl_dram_regions *dram_regions = &trans_pcie->pnvm_data;
450*6b627f88SBjoern A. Zeeb 	int ret = 0;
451*6b627f88SBjoern A. Zeeb 
452*6b627f88SBjoern A. Zeeb 	/* only allocate the DRAM if not allocated yet */
453*6b627f88SBjoern A. Zeeb 	if (trans->pnvm_loaded)
454*6b627f88SBjoern A. Zeeb 		return 0;
455*6b627f88SBjoern A. Zeeb 
456*6b627f88SBjoern A. Zeeb 	if (WARN_ON(prph_sc_ctrl->pnvm_cfg.pnvm_size))
457*6b627f88SBjoern A. Zeeb 		return -EBUSY;
458*6b627f88SBjoern A. Zeeb 
459*6b627f88SBjoern A. Zeeb 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
460*6b627f88SBjoern A. Zeeb 		return 0;
461*6b627f88SBjoern A. Zeeb 
462*6b627f88SBjoern A. Zeeb 	if (!pnvm_payloads->n_chunks) {
463*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans, "no payloads\n");
464*6b627f88SBjoern A. Zeeb 		return -EINVAL;
465*6b627f88SBjoern A. Zeeb 	}
466*6b627f88SBjoern A. Zeeb 
467*6b627f88SBjoern A. Zeeb 	/* save payloads in several DRAM sections */
468*6b627f88SBjoern A. Zeeb 	if (fw_has_capa(capa, IWL_UCODE_TLV_CAPA_FRAGMENTED_PNVM_IMG)) {
469*6b627f88SBjoern A. Zeeb 		ret = iwl_pcie_load_payloads_segments(trans,
470*6b627f88SBjoern A. Zeeb 						      dram_regions,
471*6b627f88SBjoern A. Zeeb 						      pnvm_payloads);
472*6b627f88SBjoern A. Zeeb 		if (!ret)
473*6b627f88SBjoern A. Zeeb 			trans->pnvm_loaded = true;
474*6b627f88SBjoern A. Zeeb 	} else {
475*6b627f88SBjoern A. Zeeb 		/* save only in one DRAM section */
476*6b627f88SBjoern A. Zeeb 		ret = iwl_pcie_load_payloads_contig(trans, pnvm_payloads,
477*6b627f88SBjoern A. Zeeb 						    &dram_regions->drams[0]);
478*6b627f88SBjoern A. Zeeb 		if (!ret) {
479*6b627f88SBjoern A. Zeeb 			dram_regions->n_regions = 1;
480*6b627f88SBjoern A. Zeeb 			trans->pnvm_loaded = true;
481*6b627f88SBjoern A. Zeeb 		}
482*6b627f88SBjoern A. Zeeb 	}
483*6b627f88SBjoern A. Zeeb 
484*6b627f88SBjoern A. Zeeb 	return ret;
485*6b627f88SBjoern A. Zeeb }
486*6b627f88SBjoern A. Zeeb 
487*6b627f88SBjoern A. Zeeb static inline size_t
iwl_dram_regions_size(const struct iwl_dram_regions * dram_regions)488*6b627f88SBjoern A. Zeeb iwl_dram_regions_size(const struct iwl_dram_regions *dram_regions)
489*6b627f88SBjoern A. Zeeb {
490*6b627f88SBjoern A. Zeeb 	size_t total_size = 0;
491*6b627f88SBjoern A. Zeeb 	int i;
492*6b627f88SBjoern A. Zeeb 
493*6b627f88SBjoern A. Zeeb 	for (i = 0; i < dram_regions->n_regions; i++)
494*6b627f88SBjoern A. Zeeb 		total_size += dram_regions->drams[i].size;
495*6b627f88SBjoern A. Zeeb 
496*6b627f88SBjoern A. Zeeb 	return total_size;
497*6b627f88SBjoern A. Zeeb }
498*6b627f88SBjoern A. Zeeb 
iwl_pcie_set_pnvm_segments(struct iwl_trans * trans)499*6b627f88SBjoern A. Zeeb static void iwl_pcie_set_pnvm_segments(struct iwl_trans *trans)
500*6b627f88SBjoern A. Zeeb {
501*6b627f88SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
502*6b627f88SBjoern A. Zeeb 	struct iwl_prph_scratch_ctrl_cfg *prph_sc_ctrl =
503*6b627f88SBjoern A. Zeeb 		&trans_pcie->prph_scratch->ctrl_cfg;
504*6b627f88SBjoern A. Zeeb 	struct iwl_dram_regions *dram_regions = &trans_pcie->pnvm_data;
505*6b627f88SBjoern A. Zeeb 
506*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->pnvm_cfg.pnvm_base_addr =
507*6b627f88SBjoern A. Zeeb 		cpu_to_le64(dram_regions->prph_scratch_mem_desc.physical);
508*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->pnvm_cfg.pnvm_size =
509*6b627f88SBjoern A. Zeeb 		cpu_to_le32(iwl_dram_regions_size(dram_regions));
510*6b627f88SBjoern A. Zeeb }
511*6b627f88SBjoern A. Zeeb 
iwl_pcie_set_contig_pnvm(struct iwl_trans * trans)512*6b627f88SBjoern A. Zeeb static void iwl_pcie_set_contig_pnvm(struct iwl_trans *trans)
513*6b627f88SBjoern A. Zeeb {
514*6b627f88SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
515*6b627f88SBjoern A. Zeeb 	struct iwl_prph_scratch_ctrl_cfg *prph_sc_ctrl =
516*6b627f88SBjoern A. Zeeb 		&trans_pcie->prph_scratch->ctrl_cfg;
517*6b627f88SBjoern A. Zeeb 
518*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->pnvm_cfg.pnvm_base_addr =
519*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->pnvm_data.drams[0].physical);
520*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->pnvm_cfg.pnvm_size =
521*6b627f88SBjoern A. Zeeb 		cpu_to_le32(trans_pcie->pnvm_data.drams[0].size);
522*6b627f88SBjoern A. Zeeb }
523*6b627f88SBjoern A. Zeeb 
iwl_trans_pcie_ctx_info_v2_set_pnvm(struct iwl_trans * trans,const struct iwl_ucode_capabilities * capa)524*6b627f88SBjoern A. Zeeb void iwl_trans_pcie_ctx_info_v2_set_pnvm(struct iwl_trans *trans,
525*6b627f88SBjoern A. Zeeb 					 const struct iwl_ucode_capabilities *capa)
526*6b627f88SBjoern A. Zeeb {
527*6b627f88SBjoern A. Zeeb 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
528*6b627f88SBjoern A. Zeeb 		return;
529*6b627f88SBjoern A. Zeeb 
530*6b627f88SBjoern A. Zeeb 	if (fw_has_capa(capa, IWL_UCODE_TLV_CAPA_FRAGMENTED_PNVM_IMG))
531*6b627f88SBjoern A. Zeeb 		iwl_pcie_set_pnvm_segments(trans);
532*6b627f88SBjoern A. Zeeb 	else
533*6b627f88SBjoern A. Zeeb 		iwl_pcie_set_contig_pnvm(trans);
534*6b627f88SBjoern A. Zeeb }
535*6b627f88SBjoern A. Zeeb 
iwl_trans_pcie_ctx_info_v2_load_reduce_power(struct iwl_trans * trans,const struct iwl_pnvm_image * payloads,const struct iwl_ucode_capabilities * capa)536*6b627f88SBjoern A. Zeeb int iwl_trans_pcie_ctx_info_v2_load_reduce_power(struct iwl_trans *trans,
537*6b627f88SBjoern A. Zeeb 						 const struct iwl_pnvm_image *payloads,
538*6b627f88SBjoern A. Zeeb 						 const struct iwl_ucode_capabilities *capa)
539*6b627f88SBjoern A. Zeeb {
540*6b627f88SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
541*6b627f88SBjoern A. Zeeb 	struct iwl_prph_scratch_ctrl_cfg *prph_sc_ctrl =
542*6b627f88SBjoern A. Zeeb 		&trans_pcie->prph_scratch->ctrl_cfg;
543*6b627f88SBjoern A. Zeeb 	struct iwl_dram_regions *dram_regions = &trans_pcie->reduced_tables_data;
544*6b627f88SBjoern A. Zeeb 	int ret = 0;
545*6b627f88SBjoern A. Zeeb 
546*6b627f88SBjoern A. Zeeb 	/* only allocate the DRAM if not allocated yet */
547*6b627f88SBjoern A. Zeeb 	if (trans->reduce_power_loaded)
548*6b627f88SBjoern A. Zeeb 		return 0;
549*6b627f88SBjoern A. Zeeb 
550*6b627f88SBjoern A. Zeeb 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
551*6b627f88SBjoern A. Zeeb 		return 0;
552*6b627f88SBjoern A. Zeeb 
553*6b627f88SBjoern A. Zeeb 	if (WARN_ON(prph_sc_ctrl->reduce_power_cfg.size))
554*6b627f88SBjoern A. Zeeb 		return -EBUSY;
555*6b627f88SBjoern A. Zeeb 
556*6b627f88SBjoern A. Zeeb 	if (!payloads->n_chunks) {
557*6b627f88SBjoern A. Zeeb 		IWL_DEBUG_FW(trans, "no payloads\n");
558*6b627f88SBjoern A. Zeeb 		return -EINVAL;
559*6b627f88SBjoern A. Zeeb 	}
560*6b627f88SBjoern A. Zeeb 
561*6b627f88SBjoern A. Zeeb 	/* save payloads in several DRAM sections */
562*6b627f88SBjoern A. Zeeb 	if (fw_has_capa(capa, IWL_UCODE_TLV_CAPA_FRAGMENTED_PNVM_IMG)) {
563*6b627f88SBjoern A. Zeeb 		ret = iwl_pcie_load_payloads_segments(trans,
564*6b627f88SBjoern A. Zeeb 						      dram_regions,
565*6b627f88SBjoern A. Zeeb 						      payloads);
566*6b627f88SBjoern A. Zeeb 		if (!ret)
567*6b627f88SBjoern A. Zeeb 			trans->reduce_power_loaded = true;
568*6b627f88SBjoern A. Zeeb 	} else {
569*6b627f88SBjoern A. Zeeb 		/* save only in one DRAM section */
570*6b627f88SBjoern A. Zeeb 		ret = iwl_pcie_load_payloads_contig(trans, payloads,
571*6b627f88SBjoern A. Zeeb 						    &dram_regions->drams[0]);
572*6b627f88SBjoern A. Zeeb 		if (!ret) {
573*6b627f88SBjoern A. Zeeb 			dram_regions->n_regions = 1;
574*6b627f88SBjoern A. Zeeb 			trans->reduce_power_loaded = true;
575*6b627f88SBjoern A. Zeeb 		}
576*6b627f88SBjoern A. Zeeb 	}
577*6b627f88SBjoern A. Zeeb 
578*6b627f88SBjoern A. Zeeb 	return ret;
579*6b627f88SBjoern A. Zeeb }
580*6b627f88SBjoern A. Zeeb 
iwl_pcie_set_reduce_power_segments(struct iwl_trans * trans)581*6b627f88SBjoern A. Zeeb static void iwl_pcie_set_reduce_power_segments(struct iwl_trans *trans)
582*6b627f88SBjoern A. Zeeb {
583*6b627f88SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
584*6b627f88SBjoern A. Zeeb 	struct iwl_prph_scratch_ctrl_cfg *prph_sc_ctrl =
585*6b627f88SBjoern A. Zeeb 		&trans_pcie->prph_scratch->ctrl_cfg;
586*6b627f88SBjoern A. Zeeb 	struct iwl_dram_regions *dram_regions = &trans_pcie->reduced_tables_data;
587*6b627f88SBjoern A. Zeeb 
588*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->reduce_power_cfg.base_addr =
589*6b627f88SBjoern A. Zeeb 		cpu_to_le64(dram_regions->prph_scratch_mem_desc.physical);
590*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->reduce_power_cfg.size =
591*6b627f88SBjoern A. Zeeb 		cpu_to_le32(iwl_dram_regions_size(dram_regions));
592*6b627f88SBjoern A. Zeeb }
593*6b627f88SBjoern A. Zeeb 
iwl_pcie_set_contig_reduce_power(struct iwl_trans * trans)594*6b627f88SBjoern A. Zeeb static void iwl_pcie_set_contig_reduce_power(struct iwl_trans *trans)
595*6b627f88SBjoern A. Zeeb {
596*6b627f88SBjoern A. Zeeb 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
597*6b627f88SBjoern A. Zeeb 	struct iwl_prph_scratch_ctrl_cfg *prph_sc_ctrl =
598*6b627f88SBjoern A. Zeeb 		&trans_pcie->prph_scratch->ctrl_cfg;
599*6b627f88SBjoern A. Zeeb 
600*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->reduce_power_cfg.base_addr =
601*6b627f88SBjoern A. Zeeb 		cpu_to_le64(trans_pcie->reduced_tables_data.drams[0].physical);
602*6b627f88SBjoern A. Zeeb 	prph_sc_ctrl->reduce_power_cfg.size =
603*6b627f88SBjoern A. Zeeb 		cpu_to_le32(trans_pcie->reduced_tables_data.drams[0].size);
604*6b627f88SBjoern A. Zeeb }
605*6b627f88SBjoern A. Zeeb 
606*6b627f88SBjoern A. Zeeb void
iwl_trans_pcie_ctx_info_v2_set_reduce_power(struct iwl_trans * trans,const struct iwl_ucode_capabilities * capa)607*6b627f88SBjoern A. Zeeb iwl_trans_pcie_ctx_info_v2_set_reduce_power(struct iwl_trans *trans,
608*6b627f88SBjoern A. Zeeb 					    const struct iwl_ucode_capabilities *capa)
609*6b627f88SBjoern A. Zeeb {
610*6b627f88SBjoern A. Zeeb 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
611*6b627f88SBjoern A. Zeeb 		return;
612*6b627f88SBjoern A. Zeeb 
613*6b627f88SBjoern A. Zeeb 	if (fw_has_capa(capa, IWL_UCODE_TLV_CAPA_FRAGMENTED_PNVM_IMG))
614*6b627f88SBjoern A. Zeeb 		iwl_pcie_set_reduce_power_segments(trans);
615*6b627f88SBjoern A. Zeeb 	else
616*6b627f88SBjoern A. Zeeb 		iwl_pcie_set_contig_reduce_power(trans);
617*6b627f88SBjoern A. Zeeb }
618*6b627f88SBjoern A. Zeeb 
619