1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RPM CN10K driver
4 * Copyright (C) 2020 Marvell.
7 #include <linux/bitfield.h>
14 #define LMT_TBL_OP_READ 0
15 #define LMT_TBL_OP_WRITE 1
16 #define LMT_MAP_TABLE_SIZE (128 * 1024)
17 #define LMT_MAPTBL_ENTRY_SIZE 16
19 /* Function to perform operations (read/write) on lmtst map table */
20 static int lmtst_map_table_ops(struct rvu *rvu, u32 index, u64 *val,
23 void __iomem *lmt_map_base;
26 tbl_base = rvu_read64(rvu, BLKADDR_APR, APR_AF_LMT_MAP_BASE);
28 lmt_map_base = ioremap_wc(tbl_base, LMT_MAP_TABLE_SIZE);
30 dev_err(rvu->dev, "Failed to setup lmt map table mapping!!\n");
34 if (lmt_tbl_op == LMT_TBL_OP_READ) {
35 *val = readq(lmt_map_base + index);
37 writeq((*val), (lmt_map_base + index));
38 /* Flushing the AP interceptor cache to make APR_LMT_MAP_ENTRY_S
39 * changes effective. Write 1 for flush and read is being used as a
40 * barrier and sets up a data dependency. Write to 0 after a write
41 * to 1 to complete the flush.
43 rvu_write64(rvu, BLKADDR_APR, APR_AF_LMT_CTL, BIT_ULL(0));
44 rvu_read64(rvu, BLKADDR_APR, APR_AF_LMT_CTL);
45 rvu_write64(rvu, BLKADDR_APR, APR_AF_LMT_CTL, 0x00);
48 iounmap(lmt_map_base);
52 #define LMT_MAP_TBL_W1_OFF 8
53 static u32 rvu_get_lmtst_tbl_index(struct rvu *rvu, u16 pcifunc)
55 return ((rvu_get_pf(pcifunc) * rvu->hw->total_vfs) +
56 (pcifunc & RVU_PFVF_FUNC_MASK)) * LMT_MAPTBL_ENTRY_SIZE;
59 static int rvu_get_lmtaddr(struct rvu *rvu, u16 pcifunc,
60 u64 iova, u64 *lmt_addr)
66 dev_err(rvu->dev, "%s Requested Null address for transulation\n", __func__);
70 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_SMMU_ADDR_REQ, iova);
71 pf = rvu_get_pf(pcifunc) & 0x1F;
72 val = BIT_ULL(63) | BIT_ULL(14) | BIT_ULL(13) | pf << 8 |
73 ((pcifunc & RVU_PFVF_FUNC_MASK) & 0xFF);
74 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_SMMU_TXN_REQ, val);
76 err = rvu_poll_reg(rvu, BLKADDR_RVUM, RVU_AF_SMMU_ADDR_RSP_STS, BIT_ULL(0), false);
78 dev_err(rvu->dev, "%s LMTLINE iova transulation failed\n", __func__);
81 val = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_SMMU_ADDR_RSP_STS);
83 dev_err(rvu->dev, "%s LMTLINE iova transulation failed err:%llx\n", __func__, val);
86 /* PA[51:12] = RVU_AF_SMMU_TLN_FLIT0[57:18]
87 * PA[11:0] = IOVA[11:0]
89 pa = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_SMMU_TLN_FLIT0) >> 18;
90 pa &= GENMASK_ULL(39, 0);
91 *lmt_addr = (pa << 12) | (iova & 0xFFF);
96 static int rvu_update_lmtaddr(struct rvu *rvu, u16 pcifunc, u64 lmt_addr)
98 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
103 /* Read the current lmt addr of pcifunc */
104 tbl_idx = rvu_get_lmtst_tbl_index(rvu, pcifunc);
105 err = lmtst_map_table_ops(rvu, tbl_idx, &val, LMT_TBL_OP_READ);
108 "Failed to read LMT map table: index 0x%x err %d\n",
113 /* Storing the seondary's lmt base address as this needs to be
114 * reverted in FLR. Also making sure this default value doesn't
115 * get overwritten on multiple calls to this mailbox.
117 if (!pfvf->lmt_base_addr)
118 pfvf->lmt_base_addr = val;
120 /* Update the LMT table with new addr */
121 err = lmtst_map_table_ops(rvu, tbl_idx, &lmt_addr, LMT_TBL_OP_WRITE);
124 "Failed to update LMT map table: index 0x%x err %d\n",
131 int rvu_mbox_handler_lmtst_tbl_setup(struct rvu *rvu,
132 struct lmtst_tbl_setup_req *req,
135 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
136 u32 pri_tbl_idx, tbl_idx;
141 /* Check if PF_FUNC wants to use it's own local memory as LMTLINE
142 * region, if so, convert that IOVA to physical address and
143 * populate LMT table with that address
145 if (req->use_local_lmt_region) {
146 err = rvu_get_lmtaddr(rvu, req->hdr.pcifunc,
147 req->lmt_iova, &lmt_addr);
151 /* Update the lmt addr for this PFFUNC in the LMT table */
152 err = rvu_update_lmtaddr(rvu, req->hdr.pcifunc, lmt_addr);
157 /* Reconfiguring lmtst map table in lmt region shared mode i.e. make
158 * multiple PF_FUNCs to share an LMTLINE region, so primary/base
159 * pcifunc (which is passed as an argument to mailbox) is the one
160 * whose lmt base address will be shared among other secondary
161 * pcifunc (will be the one who is calling this mailbox).
163 if (req->base_pcifunc) {
164 /* Calculating the LMT table index equivalent to primary
167 pri_tbl_idx = rvu_get_lmtst_tbl_index(rvu, req->base_pcifunc);
169 /* Read the base lmt addr of the primary pcifunc */
170 err = lmtst_map_table_ops(rvu, pri_tbl_idx, &val,
174 "Failed to read LMT map table: index 0x%x err %d\n",
179 /* Update the base lmt addr of secondary with primary's base
182 err = rvu_update_lmtaddr(rvu, req->hdr.pcifunc, val);
187 /* This mailbox can also be used to update word1 of APR_LMT_MAP_ENTRY_S
188 * like enabling scheduled LMTST, disable LMTLINE prefetch, disable
189 * early completion for ordered LMTST.
191 if (req->sch_ena || req->dis_sched_early_comp || req->dis_line_pref) {
192 tbl_idx = rvu_get_lmtst_tbl_index(rvu, req->hdr.pcifunc);
193 err = lmtst_map_table_ops(rvu, tbl_idx + LMT_MAP_TBL_W1_OFF,
194 &val, LMT_TBL_OP_READ);
197 "Failed to read LMT map table: index 0x%x err %d\n",
198 tbl_idx + LMT_MAP_TBL_W1_OFF, err);
202 /* Storing lmt map table entry word1 default value as this needs
203 * to be reverted in FLR. Also making sure this default value
204 * doesn't get overwritten on multiple calls to this mailbox.
206 if (!pfvf->lmt_map_ent_w1)
207 pfvf->lmt_map_ent_w1 = val;
209 /* Disable early completion for Ordered LMTSTs. */
210 if (req->dis_sched_early_comp)
211 val |= (req->dis_sched_early_comp <<
212 APR_LMT_MAP_ENT_DIS_SCH_CMP_SHIFT);
213 /* Enable scheduled LMTST */
215 val |= (req->sch_ena << APR_LMT_MAP_ENT_SCH_ENA_SHIFT) |
217 /* Disables LMTLINE prefetch before receiving store data. */
218 if (req->dis_line_pref)
219 val |= (req->dis_line_pref <<
220 APR_LMT_MAP_ENT_DIS_LINE_PREF_SHIFT);
222 err = lmtst_map_table_ops(rvu, tbl_idx + LMT_MAP_TBL_W1_OFF,
223 &val, LMT_TBL_OP_WRITE);
226 "Failed to update LMT map table: index 0x%x err %d\n",
227 tbl_idx + LMT_MAP_TBL_W1_OFF, err);
236 /* Resetting the lmtst map table to original base addresses */
237 void rvu_reset_lmt_map_tbl(struct rvu *rvu, u16 pcifunc)
239 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
243 if (is_rvu_otx2(rvu))
246 if (pfvf->lmt_base_addr || pfvf->lmt_map_ent_w1) {
247 /* This corresponds to lmt map table index */
248 tbl_idx = rvu_get_lmtst_tbl_index(rvu, pcifunc);
249 /* Reverting back original lmt base addr for respective
252 if (pfvf->lmt_base_addr) {
253 err = lmtst_map_table_ops(rvu, tbl_idx,
254 &pfvf->lmt_base_addr,
258 "Failed to update LMT map table: index 0x%x err %d\n",
260 pfvf->lmt_base_addr = 0;
262 /* Reverting back to orginal word1 val of lmtst map table entry
263 * which underwent changes.
265 if (pfvf->lmt_map_ent_w1) {
266 err = lmtst_map_table_ops(rvu,
267 tbl_idx + LMT_MAP_TBL_W1_OFF,
268 &pfvf->lmt_map_ent_w1,
272 "Failed to update LMT map table: index 0x%x err %d\n",
273 tbl_idx + LMT_MAP_TBL_W1_OFF, err);
274 pfvf->lmt_map_ent_w1 = 0;
279 int rvu_set_channels_base(struct rvu *rvu)
281 u16 nr_lbk_chans, nr_sdp_chans, nr_cgx_chans, nr_cpt_chans;
282 u16 sdp_chan_base, cgx_chan_base, cpt_chan_base;
283 struct rvu_hwinfo *hw = rvu->hw;
284 u64 nix_const, nix_const1;
287 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, 0);
291 nix_const = rvu_read64(rvu, blkaddr, NIX_AF_CONST);
292 nix_const1 = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
294 hw->cgx = (nix_const >> 12) & 0xFULL;
295 hw->lmac_per_cgx = (nix_const >> 8) & 0xFULL;
296 hw->cgx_links = hw->cgx * hw->lmac_per_cgx;
297 hw->lbk_links = (nix_const >> 24) & 0xFULL;
298 hw->cpt_links = (nix_const >> 44) & 0xFULL;
301 hw->cgx_chan_base = NIX_CHAN_CGX_LMAC_CHX(0, 0, 0);
302 hw->lbk_chan_base = NIX_CHAN_LBK_CHX(0, 0);
303 hw->sdp_chan_base = NIX_CHAN_SDP_CH_START;
305 /* No Programmable channels */
306 if (!(nix_const & BIT_ULL(60)))
309 hw->cap.programmable_chans = true;
311 /* If programmable channels are present then configure
312 * channels such that all channel numbers are contiguous
313 * leaving no holes. This way the new CPT channels can be
314 * accomodated. The order of channel numbers assigned is
315 * LBK, SDP, CGX and CPT. Also the base channel number
316 * of a block must be multiple of number of channels
319 nr_lbk_chans = (nix_const >> 16) & 0xFFULL;
320 nr_sdp_chans = nix_const1 & 0xFFFULL;
321 nr_cgx_chans = nix_const & 0xFFULL;
322 nr_cpt_chans = (nix_const >> 32) & 0xFFFULL;
324 sdp_chan_base = hw->lbk_chan_base + hw->lbk_links * nr_lbk_chans;
325 /* Round up base channel to multiple of number of channels */
326 hw->sdp_chan_base = ALIGN(sdp_chan_base, nr_sdp_chans);
328 cgx_chan_base = hw->sdp_chan_base + hw->sdp_links * nr_sdp_chans;
329 hw->cgx_chan_base = ALIGN(cgx_chan_base, nr_cgx_chans);
331 cpt_chan_base = hw->cgx_chan_base + hw->cgx_links * nr_cgx_chans;
332 hw->cpt_chan_base = ALIGN(cpt_chan_base, nr_cpt_chans);
334 /* Out of 4096 channels start CPT from 2048 so
335 * that MSB for CPT channels is always set
337 if (cpt_chan_base <= NIX_CHAN_CPT_CH_START) {
338 hw->cpt_chan_base = NIX_CHAN_CPT_CH_START;
341 "CPT channels could not fit in the range 2048-4095\n");
348 #define LBK_CONNECT_NIXX(a) (0x0 + (a))
350 static void __rvu_lbk_set_chans(struct rvu *rvu, void __iomem *base,
351 u64 offset, int lbkid, u16 chans)
353 struct rvu_hwinfo *hw = rvu->hw;
356 cfg = readq(base + offset);
357 cfg &= ~(LBK_LINK_CFG_RANGE_MASK |
358 LBK_LINK_CFG_ID_MASK | LBK_LINK_CFG_BASE_MASK);
359 cfg |= FIELD_PREP(LBK_LINK_CFG_RANGE_MASK, ilog2(chans));
360 cfg |= FIELD_PREP(LBK_LINK_CFG_ID_MASK, lbkid);
361 cfg |= FIELD_PREP(LBK_LINK_CFG_BASE_MASK, hw->lbk_chan_base);
363 writeq(cfg, base + offset);
366 static void rvu_lbk_set_channels(struct rvu *rvu)
368 struct pci_dev *pdev = NULL;
374 /* To loopback packets between multiple NIX blocks
375 * mutliple LBK blocks are needed. With two NIX blocks,
376 * four LBK blocks are needed and each LBK block
377 * source and destination are as follows:
378 * LBK0 - source NIX0 and destination NIX1
379 * LBK1 - source NIX0 and destination NIX1
380 * LBK2 - source NIX1 and destination NIX0
381 * LBK3 - source NIX1 and destination NIX1
382 * As per the HRM channel numbers should be programmed as:
383 * P2X and X2P of LBK0 as same
384 * P2X and X2P of LBK3 as same
385 * P2X of LBK1 and X2P of LBK2 as same
386 * P2X of LBK2 and X2P of LBK1 as same
389 pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
390 PCI_DEVID_OCTEONTX2_LBK, pdev);
394 base = pci_ioremap_bar(pdev, 0);
398 lbk_const = readq(base + LBK_CONST);
399 chans = FIELD_GET(LBK_CONST_CHANS, lbk_const);
400 dst = FIELD_GET(LBK_CONST_DST, lbk_const);
401 src = FIELD_GET(LBK_CONST_SRC, lbk_const);
404 if (src == LBK_CONNECT_NIXX(0)) { /* LBK0 */
405 __rvu_lbk_set_chans(rvu, base, LBK_LINK_CFG_X2P,
407 __rvu_lbk_set_chans(rvu, base, LBK_LINK_CFG_P2X,
409 } else if (src == LBK_CONNECT_NIXX(1)) { /* LBK3 */
410 __rvu_lbk_set_chans(rvu, base, LBK_LINK_CFG_X2P,
412 __rvu_lbk_set_chans(rvu, base, LBK_LINK_CFG_P2X,
416 if (src == LBK_CONNECT_NIXX(0)) { /* LBK1 */
417 __rvu_lbk_set_chans(rvu, base, LBK_LINK_CFG_X2P,
419 __rvu_lbk_set_chans(rvu, base, LBK_LINK_CFG_P2X,
421 } else if (src == LBK_CONNECT_NIXX(1)) { /* LBK2 */
422 __rvu_lbk_set_chans(rvu, base, LBK_LINK_CFG_X2P,
424 __rvu_lbk_set_chans(rvu, base, LBK_LINK_CFG_P2X,
434 static void __rvu_nix_set_channels(struct rvu *rvu, int blkaddr)
436 u64 nix_const1 = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
437 u64 nix_const = rvu_read64(rvu, blkaddr, NIX_AF_CONST);
438 u16 cgx_chans, lbk_chans, sdp_chans, cpt_chans;
439 struct rvu_hwinfo *hw = rvu->hw;
440 int link, nix_link = 0;
444 cgx_chans = nix_const & 0xFFULL;
445 lbk_chans = (nix_const >> 16) & 0xFFULL;
446 sdp_chans = nix_const1 & 0xFFFULL;
447 cpt_chans = (nix_const >> 32) & 0xFFFULL;
449 start = hw->cgx_chan_base;
450 for (link = 0; link < hw->cgx_links; link++, nix_link++) {
451 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LINKX_CFG(nix_link));
452 cfg &= ~(NIX_AF_LINKX_BASE_MASK | NIX_AF_LINKX_RANGE_MASK);
453 cfg |= FIELD_PREP(NIX_AF_LINKX_RANGE_MASK, ilog2(cgx_chans));
454 cfg |= FIELD_PREP(NIX_AF_LINKX_BASE_MASK, start);
455 rvu_write64(rvu, blkaddr, NIX_AF_LINKX_CFG(nix_link), cfg);
459 start = hw->lbk_chan_base;
460 for (link = 0; link < hw->lbk_links; link++, nix_link++) {
461 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LINKX_CFG(nix_link));
462 cfg &= ~(NIX_AF_LINKX_BASE_MASK | NIX_AF_LINKX_RANGE_MASK);
463 cfg |= FIELD_PREP(NIX_AF_LINKX_RANGE_MASK, ilog2(lbk_chans));
464 cfg |= FIELD_PREP(NIX_AF_LINKX_BASE_MASK, start);
465 rvu_write64(rvu, blkaddr, NIX_AF_LINKX_CFG(nix_link), cfg);
469 start = hw->sdp_chan_base;
470 for (link = 0; link < hw->sdp_links; link++, nix_link++) {
471 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LINKX_CFG(nix_link));
472 cfg &= ~(NIX_AF_LINKX_BASE_MASK | NIX_AF_LINKX_RANGE_MASK);
473 cfg |= FIELD_PREP(NIX_AF_LINKX_RANGE_MASK, ilog2(sdp_chans));
474 cfg |= FIELD_PREP(NIX_AF_LINKX_BASE_MASK, start);
475 rvu_write64(rvu, blkaddr, NIX_AF_LINKX_CFG(nix_link), cfg);
479 start = hw->cpt_chan_base;
480 for (link = 0; link < hw->cpt_links; link++, nix_link++) {
481 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LINKX_CFG(nix_link));
482 cfg &= ~(NIX_AF_LINKX_BASE_MASK | NIX_AF_LINKX_RANGE_MASK);
483 cfg |= FIELD_PREP(NIX_AF_LINKX_RANGE_MASK, ilog2(cpt_chans));
484 cfg |= FIELD_PREP(NIX_AF_LINKX_BASE_MASK, start);
485 rvu_write64(rvu, blkaddr, NIX_AF_LINKX_CFG(nix_link), cfg);
490 static void rvu_nix_set_channels(struct rvu *rvu)
494 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
496 __rvu_nix_set_channels(rvu, blkaddr);
497 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
501 static void __rvu_rpm_set_channels(int cgxid, int lmacid, u16 base)
505 cfg = cgx_lmac_read(cgxid, lmacid, RPMX_CMRX_LINK_CFG);
506 cfg &= ~(RPMX_CMRX_LINK_BASE_MASK | RPMX_CMRX_LINK_RANGE_MASK);
508 /* There is no read-only constant register to read
509 * the number of channels for LMAC and it is always 16.
511 cfg |= FIELD_PREP(RPMX_CMRX_LINK_RANGE_MASK, ilog2(16));
512 cfg |= FIELD_PREP(RPMX_CMRX_LINK_BASE_MASK, base);
513 cgx_lmac_write(cgxid, lmacid, RPMX_CMRX_LINK_CFG, cfg);
516 static void rvu_rpm_set_channels(struct rvu *rvu)
518 struct rvu_hwinfo *hw = rvu->hw;
519 u16 base = hw->cgx_chan_base;
522 for (cgx = 0; cgx < rvu->cgx_cnt_max; cgx++) {
523 for (lmac = 0; lmac < hw->lmac_per_cgx; lmac++) {
524 __rvu_rpm_set_channels(cgx, lmac, base);
530 void rvu_program_channels(struct rvu *rvu)
532 struct rvu_hwinfo *hw = rvu->hw;
534 if (!hw->cap.programmable_chans)
537 rvu_nix_set_channels(rvu);
538 rvu_lbk_set_channels(rvu);
539 rvu_rpm_set_channels(rvu);