1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Cavium, Inc.
6 #include <linux/device.h>
7 #include <linux/firmware.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/pci.h>
12 #include <linux/printk.h>
13 #include <linux/version.h>
17 #define DRV_NAME "thunder-cpt"
18 #define DRV_VERSION "1.0"
20 static u32 num_vfs
= 4; /* Default 4 VF enabled */
21 module_param(num_vfs
, uint
, 0444);
22 MODULE_PARM_DESC(num_vfs
, "Number of VFs to enable(1-16)");
25 * Disable cores specified by coremask
27 static void cpt_disable_cores(struct cpt_device
*cpt
, u64 coremask
,
33 struct device
*dev
= &cpt
->pdev
->dev
;
36 coremask
= (coremask
<< cpt
->max_se_cores
);
38 /* Disengage the cores from groups */
39 grpmask
= cpt_read_csr64(cpt
->reg_base
, CPTX_PF_GX_EN(0, grp
));
40 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_GX_EN(0, grp
),
41 (grpmask
& ~coremask
));
43 grp
= cpt_read_csr64(cpt
->reg_base
, CPTX_PF_EXEC_BUSY(0));
44 while (grp
& coremask
) {
45 dev_err(dev
, "Cores still busy %llx", coremask
);
46 grp
= cpt_read_csr64(cpt
->reg_base
,
47 CPTX_PF_EXEC_BUSY(0));
54 /* Disable the cores */
55 pf_exe_ctl
= cpt_read_csr64(cpt
->reg_base
, CPTX_PF_EXE_CTL(0));
56 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_EXE_CTL(0),
57 (pf_exe_ctl
& ~coremask
));
62 * Enable cores specified by coremask
64 static void cpt_enable_cores(struct cpt_device
*cpt
, u64 coremask
,
70 coremask
= (coremask
<< cpt
->max_se_cores
);
72 pf_exe_ctl
= cpt_read_csr64(cpt
->reg_base
, CPTX_PF_EXE_CTL(0));
73 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_EXE_CTL(0),
74 (pf_exe_ctl
| coremask
));
78 static void cpt_configure_group(struct cpt_device
*cpt
, u8 grp
,
79 u64 coremask
, u8 type
)
84 coremask
= (coremask
<< cpt
->max_se_cores
);
86 pf_gx_en
= cpt_read_csr64(cpt
->reg_base
, CPTX_PF_GX_EN(0, grp
));
87 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_GX_EN(0, grp
),
88 (pf_gx_en
| coremask
));
92 static void cpt_disable_mbox_interrupts(struct cpt_device
*cpt
)
94 /* Clear mbox(0) interupts for all vfs */
95 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_MBOX_ENA_W1CX(0, 0), ~0ull);
98 static void cpt_disable_ecc_interrupts(struct cpt_device
*cpt
)
100 /* Clear ecc(0) interupts for all vfs */
101 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_ECC0_ENA_W1C(0), ~0ull);
104 static void cpt_disable_exec_interrupts(struct cpt_device
*cpt
)
106 /* Clear exec interupts for all vfs */
107 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_EXEC_ENA_W1C(0), ~0ull);
110 static void cpt_disable_all_interrupts(struct cpt_device
*cpt
)
112 cpt_disable_mbox_interrupts(cpt
);
113 cpt_disable_ecc_interrupts(cpt
);
114 cpt_disable_exec_interrupts(cpt
);
117 static void cpt_enable_mbox_interrupts(struct cpt_device
*cpt
)
119 /* Set mbox(0) interupts for all vfs */
120 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_MBOX_ENA_W1SX(0, 0), ~0ull);
123 static int cpt_load_microcode(struct cpt_device
*cpt
, struct microcode
*mcode
)
125 int ret
= 0, core
= 0, shift
= 0;
127 struct device
*dev
= &cpt
->pdev
->dev
;
129 if (!mcode
|| !mcode
->code
) {
130 dev_err(dev
, "Either the mcode is null or data is NULL\n");
134 if (mcode
->code_size
== 0) {
135 dev_err(dev
, "microcode size is 0\n");
139 /* Assumes 0-9 are SE cores for UCODE_BASE registers and
140 * AE core bases follow
143 core
= CPT_MAX_SE_CORES
; /* start couting from 10 */
144 total_cores
= CPT_MAX_TOTAL_CORES
; /* upto 15 */
146 core
= 0; /* start couting from 0 */
147 total_cores
= CPT_MAX_SE_CORES
; /* upto 9 */
150 /* Point to microcode for each core of the group */
151 for (; core
< total_cores
; core
++, shift
++) {
152 if (mcode
->core_mask
& (1 << shift
)) {
153 cpt_write_csr64(cpt
->reg_base
,
154 CPTX_PF_ENGX_UCODE_BASE(0, core
),
155 (u64
)mcode
->phys_base
);
161 static int do_cpt_init(struct cpt_device
*cpt
, struct microcode
*mcode
)
164 struct device
*dev
= &cpt
->pdev
->dev
;
166 /* Make device not ready */
167 cpt
->flags
&= ~CPT_FLAG_DEVICE_READY
;
168 /* Disable All PF interrupts */
169 cpt_disable_all_interrupts(cpt
);
170 /* Calculate mcode group and coremasks */
172 if (mcode
->num_cores
> cpt
->max_ae_cores
) {
173 dev_err(dev
, "Requested for more cores than available AE cores\n");
178 if (cpt
->next_group
>= CPT_MAX_CORE_GROUPS
) {
179 dev_err(dev
, "Can't load, all eight microcode groups in use");
183 mcode
->group
= cpt
->next_group
;
184 /* Convert requested cores to mask */
185 mcode
->core_mask
= GENMASK(mcode
->num_cores
, 0);
186 cpt_disable_cores(cpt
, mcode
->core_mask
, AE_TYPES
,
188 /* Load microcode for AE engines */
189 ret
= cpt_load_microcode(cpt
, mcode
);
191 dev_err(dev
, "Microcode load Failed for %s\n",
196 /* Configure group mask for the mcode */
197 cpt_configure_group(cpt
, mcode
->group
, mcode
->core_mask
,
199 /* Enable AE cores for the group mask */
200 cpt_enable_cores(cpt
, mcode
->core_mask
, AE_TYPES
);
202 if (mcode
->num_cores
> cpt
->max_se_cores
) {
203 dev_err(dev
, "Requested for more cores than available SE cores\n");
207 if (cpt
->next_group
>= CPT_MAX_CORE_GROUPS
) {
208 dev_err(dev
, "Can't load, all eight microcode groups in use");
212 mcode
->group
= cpt
->next_group
;
213 /* Covert requested cores to mask */
214 mcode
->core_mask
= GENMASK(mcode
->num_cores
, 0);
215 cpt_disable_cores(cpt
, mcode
->core_mask
, SE_TYPES
,
217 /* Load microcode for SE engines */
218 ret
= cpt_load_microcode(cpt
, mcode
);
220 dev_err(dev
, "Microcode load Failed for %s\n",
225 /* Configure group mask for the mcode */
226 cpt_configure_group(cpt
, mcode
->group
, mcode
->core_mask
,
228 /* Enable SE cores for the group mask */
229 cpt_enable_cores(cpt
, mcode
->core_mask
, SE_TYPES
);
232 /* Enabled PF mailbox interrupts */
233 cpt_enable_mbox_interrupts(cpt
);
234 cpt
->flags
|= CPT_FLAG_DEVICE_READY
;
239 /* Enabled PF mailbox interrupts */
240 cpt_enable_mbox_interrupts(cpt
);
245 struct ucode_header
{
246 u8 version
[CPT_UCODE_VERSION_SZ
];
252 static int cpt_ucode_load_fw(struct cpt_device
*cpt
, const u8
*fw
, bool is_ae
)
254 const struct firmware
*fw_entry
;
255 struct device
*dev
= &cpt
->pdev
->dev
;
256 struct ucode_header
*ucode
;
257 struct microcode
*mcode
;
260 ret
= request_firmware(&fw_entry
, fw
, dev
);
264 ucode
= (struct ucode_header
*)fw_entry
->data
;
265 mcode
= &cpt
->mcode
[cpt
->next_mc_idx
];
266 memcpy(mcode
->version
, (u8
*)fw_entry
->data
, CPT_UCODE_VERSION_SZ
);
267 mcode
->code_size
= ntohl(ucode
->code_length
) * 2;
268 if (!mcode
->code_size
) {
273 mcode
->is_ae
= is_ae
;
274 mcode
->core_mask
= 0ULL;
275 mcode
->num_cores
= is_ae
? 6 : 10;
277 /* Allocate DMAable space */
278 mcode
->code
= dma_alloc_coherent(&cpt
->pdev
->dev
, mcode
->code_size
,
279 &mcode
->phys_base
, GFP_KERNEL
);
281 dev_err(dev
, "Unable to allocate space for microcode");
286 memcpy((void *)mcode
->code
, (void *)(fw_entry
->data
+ sizeof(*ucode
)),
289 /* Byte swap 64-bit */
290 for (j
= 0; j
< (mcode
->code_size
/ 8); j
++)
291 ((u64
*)mcode
->code
)[j
] = cpu_to_be64(((u64
*)mcode
->code
)[j
]);
292 /* MC needs 16-bit swap */
293 for (j
= 0; j
< (mcode
->code_size
/ 2); j
++)
294 ((u16
*)mcode
->code
)[j
] = cpu_to_be16(((u16
*)mcode
->code
)[j
]);
296 dev_dbg(dev
, "mcode->code_size = %u\n", mcode
->code_size
);
297 dev_dbg(dev
, "mcode->is_ae = %u\n", mcode
->is_ae
);
298 dev_dbg(dev
, "mcode->num_cores = %u\n", mcode
->num_cores
);
299 dev_dbg(dev
, "mcode->code = %llx\n", (u64
)mcode
->code
);
300 dev_dbg(dev
, "mcode->phys_base = %llx\n", mcode
->phys_base
);
302 ret
= do_cpt_init(cpt
, mcode
);
304 dev_err(dev
, "do_cpt_init failed with ret: %d\n", ret
);
308 dev_info(dev
, "Microcode Loaded %s\n", mcode
->version
);
309 mcode
->is_mc_valid
= 1;
313 release_firmware(fw_entry
);
318 static int cpt_ucode_load(struct cpt_device
*cpt
)
321 struct device
*dev
= &cpt
->pdev
->dev
;
323 ret
= cpt_ucode_load_fw(cpt
, "cpt8x-mc-ae.out", true);
325 dev_err(dev
, "ae:cpt_ucode_load failed with ret: %d\n", ret
);
328 ret
= cpt_ucode_load_fw(cpt
, "cpt8x-mc-se.out", false);
330 dev_err(dev
, "se:cpt_ucode_load failed with ret: %d\n", ret
);
337 static irqreturn_t
cpt_mbx0_intr_handler(int irq
, void *cpt_irq
)
339 struct cpt_device
*cpt
= (struct cpt_device
*)cpt_irq
;
341 cpt_mbox_intr_handler(cpt
, 0);
346 static void cpt_reset(struct cpt_device
*cpt
)
348 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_RESET(0), 1);
351 static void cpt_find_max_enabled_cores(struct cpt_device
*cpt
)
353 union cptx_pf_constants pf_cnsts
= {0};
355 pf_cnsts
.u
= cpt_read_csr64(cpt
->reg_base
, CPTX_PF_CONSTANTS(0));
356 cpt
->max_se_cores
= pf_cnsts
.s
.se
;
357 cpt
->max_ae_cores
= pf_cnsts
.s
.ae
;
360 static u32
cpt_check_bist_status(struct cpt_device
*cpt
)
362 union cptx_pf_bist_status bist_sts
= {0};
364 bist_sts
.u
= cpt_read_csr64(cpt
->reg_base
,
365 CPTX_PF_BIST_STATUS(0));
370 static u64
cpt_check_exe_bist_status(struct cpt_device
*cpt
)
372 union cptx_pf_exe_bist_status bist_sts
= {0};
374 bist_sts
.u
= cpt_read_csr64(cpt
->reg_base
,
375 CPTX_PF_EXE_BIST_STATUS(0));
380 static void cpt_disable_all_cores(struct cpt_device
*cpt
)
382 u32 grp
, timeout
= 100;
383 struct device
*dev
= &cpt
->pdev
->dev
;
385 /* Disengage the cores from groups */
386 for (grp
= 0; grp
< CPT_MAX_CORE_GROUPS
; grp
++) {
387 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_GX_EN(0, grp
), 0);
391 grp
= cpt_read_csr64(cpt
->reg_base
, CPTX_PF_EXEC_BUSY(0));
393 dev_err(dev
, "Cores still busy");
394 grp
= cpt_read_csr64(cpt
->reg_base
,
395 CPTX_PF_EXEC_BUSY(0));
401 /* Disable the cores */
402 cpt_write_csr64(cpt
->reg_base
, CPTX_PF_EXE_CTL(0), 0);
406 * Ensure all cores are disengaged from all groups by
407 * calling cpt_disable_all_cores() before calling this
410 static void cpt_unload_microcode(struct cpt_device
*cpt
)
414 /* Free microcode bases and reset group masks */
415 for (grp
= 0; grp
< CPT_MAX_CORE_GROUPS
; grp
++) {
416 struct microcode
*mcode
= &cpt
->mcode
[grp
];
418 if (cpt
->mcode
[grp
].code
)
419 dma_free_coherent(&cpt
->pdev
->dev
, mcode
->code_size
,
420 mcode
->code
, mcode
->phys_base
);
423 /* Clear UCODE_BASE registers for all engines */
424 for (core
= 0; core
< CPT_MAX_TOTAL_CORES
; core
++)
425 cpt_write_csr64(cpt
->reg_base
,
426 CPTX_PF_ENGX_UCODE_BASE(0, core
), 0ull);
429 static int cpt_device_init(struct cpt_device
*cpt
)
432 struct device
*dev
= &cpt
->pdev
->dev
;
434 /* Reset the PF when probed first */
438 /*Check BIST status*/
439 bist
= (u64
)cpt_check_bist_status(cpt
);
441 dev_err(dev
, "RAM BIST failed with code 0x%llx", bist
);
445 bist
= cpt_check_exe_bist_status(cpt
);
447 dev_err(dev
, "Engine BIST failed with code 0x%llx", bist
);
451 /*Get CLK frequency*/
452 /*Get max enabled cores */
453 cpt_find_max_enabled_cores(cpt
);
454 /*Disable all cores*/
455 cpt_disable_all_cores(cpt
);
456 /*Reset device parameters*/
457 cpt
->next_mc_idx
= 0;
460 cpt
->flags
|= CPT_FLAG_DEVICE_READY
;
465 static int cpt_register_interrupts(struct cpt_device
*cpt
)
468 struct device
*dev
= &cpt
->pdev
->dev
;
471 ret
= pci_alloc_irq_vectors(cpt
->pdev
, CPT_PF_MSIX_VECTORS
,
472 CPT_PF_MSIX_VECTORS
, PCI_IRQ_MSIX
);
474 dev_err(&cpt
->pdev
->dev
, "Request for #%d msix vectors failed\n",
475 CPT_PF_MSIX_VECTORS
);
479 /* Register mailbox interrupt handlers */
480 ret
= request_irq(pci_irq_vector(cpt
->pdev
, CPT_PF_INT_VEC_E_MBOXX(0)),
481 cpt_mbx0_intr_handler
, 0, "CPT Mbox0", cpt
);
485 /* Enable mailbox interrupt */
486 cpt_enable_mbox_interrupts(cpt
);
490 dev_err(dev
, "Request irq failed\n");
491 pci_disable_msix(cpt
->pdev
);
495 static void cpt_unregister_interrupts(struct cpt_device
*cpt
)
497 free_irq(pci_irq_vector(cpt
->pdev
, CPT_PF_INT_VEC_E_MBOXX(0)), cpt
);
498 pci_disable_msix(cpt
->pdev
);
501 static int cpt_sriov_init(struct cpt_device
*cpt
, int num_vfs
)
506 struct pci_dev
*pdev
= cpt
->pdev
;
508 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_SRIOV
);
510 dev_err(&pdev
->dev
, "SRIOV capability is not found in PCIe config space\n");
514 cpt
->num_vf_en
= num_vfs
; /* User requested VFs */
515 pci_read_config_word(pdev
, (pos
+ PCI_SRIOV_TOTAL_VF
), &total_vf_cnt
);
516 if (total_vf_cnt
< cpt
->num_vf_en
)
517 cpt
->num_vf_en
= total_vf_cnt
;
522 /*Enabled the available VFs */
523 err
= pci_enable_sriov(pdev
, cpt
->num_vf_en
);
525 dev_err(&pdev
->dev
, "SRIOV enable failed, num VF is %d\n",
531 /* TODO: Optionally enable static VQ priorities feature */
533 dev_info(&pdev
->dev
, "SRIOV enabled, number of VF available %d\n",
536 cpt
->flags
|= CPT_FLAG_SRIOV_ENABLED
;
541 static int cpt_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
543 struct device
*dev
= &pdev
->dev
;
544 struct cpt_device
*cpt
;
547 if (num_vfs
> 16 || num_vfs
< 4) {
548 dev_warn(dev
, "Invalid vf count %d, Resetting it to 4(default)\n",
553 cpt
= devm_kzalloc(dev
, sizeof(*cpt
), GFP_KERNEL
);
557 pci_set_drvdata(pdev
, cpt
);
559 err
= pci_enable_device(pdev
);
561 dev_err(dev
, "Failed to enable PCI device\n");
562 pci_set_drvdata(pdev
, NULL
);
566 err
= pci_request_regions(pdev
, DRV_NAME
);
568 dev_err(dev
, "PCI request regions failed 0x%x\n", err
);
569 goto cpt_err_disable_device
;
572 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(48));
574 dev_err(dev
, "Unable to get usable DMA configuration\n");
575 goto cpt_err_release_regions
;
578 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(48));
580 dev_err(dev
, "Unable to get 48-bit DMA for consistent allocations\n");
581 goto cpt_err_release_regions
;
584 /* MAP PF's configuration registers */
585 cpt
->reg_base
= pcim_iomap(pdev
, 0, 0);
586 if (!cpt
->reg_base
) {
587 dev_err(dev
, "Cannot map config register space, aborting\n");
589 goto cpt_err_release_regions
;
592 /* CPT device HW initialization */
593 cpt_device_init(cpt
);
595 /* Register interrupts */
596 err
= cpt_register_interrupts(cpt
);
598 goto cpt_err_release_regions
;
600 err
= cpt_ucode_load(cpt
);
602 goto cpt_err_unregister_interrupts
;
604 /* Configure SRIOV */
605 err
= cpt_sriov_init(cpt
, num_vfs
);
607 goto cpt_err_unregister_interrupts
;
611 cpt_err_unregister_interrupts
:
612 cpt_unregister_interrupts(cpt
);
613 cpt_err_release_regions
:
614 pci_release_regions(pdev
);
615 cpt_err_disable_device
:
616 pci_disable_device(pdev
);
617 pci_set_drvdata(pdev
, NULL
);
621 static void cpt_remove(struct pci_dev
*pdev
)
623 struct cpt_device
*cpt
= pci_get_drvdata(pdev
);
625 /* Disengage SE and AE cores from all groups*/
626 cpt_disable_all_cores(cpt
);
627 /* Unload microcodes */
628 cpt_unload_microcode(cpt
);
629 cpt_unregister_interrupts(cpt
);
630 pci_disable_sriov(pdev
);
631 pci_release_regions(pdev
);
632 pci_disable_device(pdev
);
633 pci_set_drvdata(pdev
, NULL
);
636 static void cpt_shutdown(struct pci_dev
*pdev
)
638 struct cpt_device
*cpt
= pci_get_drvdata(pdev
);
643 dev_info(&pdev
->dev
, "Shutdown device %x:%x.\n",
644 (u32
)pdev
->vendor
, (u32
)pdev
->device
);
646 cpt_unregister_interrupts(cpt
);
647 pci_release_regions(pdev
);
648 pci_disable_device(pdev
);
649 pci_set_drvdata(pdev
, NULL
);
652 /* Supported devices */
653 static const struct pci_device_id cpt_id_table
[] = {
654 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM
, CPT_81XX_PCI_PF_DEVICE_ID
) },
655 { 0, } /* end of table */
658 static struct pci_driver cpt_pci_driver
= {
660 .id_table
= cpt_id_table
,
662 .remove
= cpt_remove
,
663 .shutdown
= cpt_shutdown
,
666 module_pci_driver(cpt_pci_driver
);
668 MODULE_AUTHOR("George Cherian <george.cherian@cavium.com>");
669 MODULE_DESCRIPTION("Cavium Thunder CPT Physical Function Driver");
670 MODULE_LICENSE("GPL v2");
671 MODULE_VERSION(DRV_VERSION
);
672 MODULE_DEVICE_TABLE(pci
, cpt_id_table
);