Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2016 Cavium, Inc. */ #include <linux/device.h> #include <linux/firmware.h> #include <linux/interrupt.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/pci.h> #include <linux/printk.h> #include "cptpf.h" #define DRV_NAME "thunder-cpt" #define DRV_VERSION "1.0" static u32 num_vfs = 4; /* Default 4 VF enabled */ module_param(num_vfs, uint, 0444); MODULE_PARM_DESC(num_vfs, "Number of VFs to enable(1-16)"); /* * Disable cores specified by coremask */ static void cpt_disable_cores(struct cpt_device *cpt, u64 coremask, u8 type, u8 grp) { u64 pf_exe_ctl; u32 timeout = 100; u64 grpmask = 0; struct device *dev = &cpt->pdev->dev; if (type == AE_TYPES) coremask = (coremask << cpt->max_se_cores); /* Disengage the cores from groups */ grpmask = cpt_read_csr64(cpt->reg_base, CPTX_PF_GX_EN(0, grp)); cpt_write_csr64(cpt->reg_base, CPTX_PF_GX_EN(0, grp), (grpmask & ~coremask)); udelay(CSR_DELAY); grp = cpt_read_csr64(cpt->reg_base, CPTX_PF_EXEC_BUSY(0)); while (grp & coremask) { dev_err(dev, "Cores still busy %llx", coremask); grp = cpt_read_csr64(cpt->reg_base, CPTX_PF_EXEC_BUSY(0)); if (timeout--) break; udelay(CSR_DELAY); } /* Disable the cores */ pf_exe_ctl = cpt_read_csr64(cpt->reg_base, CPTX_PF_EXE_CTL(0)); cpt_write_csr64(cpt->reg_base, CPTX_PF_EXE_CTL(0), (pf_exe_ctl & ~coremask)); udelay(CSR_DELAY); } /* * Enable cores specified by coremask */ static void cpt_enable_cores(struct cpt_device *cpt, u64 coremask, u8 type) { u64 pf_exe_ctl; if (type == AE_TYPES) coremask = (coremask << cpt->max_se_cores); pf_exe_ctl = cpt_read_csr64(cpt->reg_base, CPTX_PF_EXE_CTL(0)); cpt_write_csr64(cpt->reg_base, CPTX_PF_EXE_CTL(0), (pf_exe_ctl | coremask)); udelay(CSR_DELAY); } static void cpt_configure_group(struct cpt_device *cpt, u8 grp, u64 coremask, u8 type) { u64 pf_gx_en = 0; if (type == AE_TYPES) coremask = (coremask << cpt->max_se_cores); pf_gx_en = cpt_read_csr64(cpt->reg_base, CPTX_PF_GX_EN(0, grp)); cpt_write_csr64(cpt->reg_base, CPTX_PF_GX_EN(0, grp), (pf_gx_en | coremask)); udelay(CSR_DELAY); } static void cpt_disable_mbox_interrupts(struct cpt_device *cpt) { /* Clear mbox(0) interupts for all vfs */ cpt_write_csr64(cpt->reg_base, CPTX_PF_MBOX_ENA_W1CX(0, 0), ~0ull); } static void cpt_disable_ecc_interrupts(struct cpt_device *cpt) { /* Clear ecc(0) interupts for all vfs */ cpt_write_csr64(cpt->reg_base, CPTX_PF_ECC0_ENA_W1C(0), ~0ull); } static void cpt_disable_exec_interrupts(struct cpt_device *cpt) { /* Clear exec interupts for all vfs */ cpt_write_csr64(cpt->reg_base, CPTX_PF_EXEC_ENA_W1C(0), ~0ull); } static void cpt_disable_all_interrupts(struct cpt_device *cpt) { cpt_disable_mbox_interrupts(cpt); cpt_disable_ecc_interrupts(cpt); cpt_disable_exec_interrupts(cpt); } static void cpt_enable_mbox_interrupts(struct cpt_device *cpt) { /* Set mbox(0) interupts for all vfs */ cpt_write_csr64(cpt->reg_base, CPTX_PF_MBOX_ENA_W1SX(0, 0), ~0ull); } static int cpt_load_microcode(struct cpt_device *cpt, struct microcode *mcode) { int ret = 0, core = 0, shift = 0; u32 total_cores = 0; struct device *dev = &cpt->pdev->dev; if (!mcode || !mcode->code) { dev_err(dev, "Either the mcode is null or data is NULL\n"); return -EINVAL; } if (mcode->code_size == 0) { dev_err(dev, "microcode size is 0\n"); return -EINVAL; } /* Assumes 0-9 are SE cores for UCODE_BASE registers and * AE core bases follow */ if (mcode->is_ae) { core = CPT_MAX_SE_CORES; /* start couting from 10 */ total_cores = CPT_MAX_TOTAL_CORES; /* upto 15 */ } else { core = 0; /* start couting from 0 */ total_cores = CPT_MAX_SE_CORES; /* upto 9 */ } /* Point to microcode for each core of the group */ for (; core < total_cores ; core++, shift++) { if (mcode->core_mask & (1 << shift)) { cpt_write_csr64(cpt->reg_base, CPTX_PF_ENGX_UCODE_BASE(0, core), (u64)mcode->phys_base); } } return ret; } static int do_cpt_init(struct cpt_device *cpt, struct microcode *mcode) { int ret = 0; struct device *dev = &cpt->pdev->dev; /* Make device not ready */ cpt->flags &= ~CPT_FLAG_DEVICE_READY; /* Disable All PF interrupts */ cpt_disable_all_interrupts(cpt); /* Calculate mcode group and coremasks */ if (mcode->is_ae) { if (mcode->num_cores > cpt->max_ae_cores) { dev_err(dev, "Requested for more cores than available AE cores\n"); ret = -EINVAL; goto cpt_init_fail; } if (cpt->next_group >= CPT_MAX_CORE_GROUPS) { dev_err(dev, "Can't load, all eight microcode groups in use"); return -ENFILE; } mcode->group = cpt->next_group; /* Convert requested cores to mask */ mcode->core_mask = GENMASK(mcode->num_cores, 0); cpt_disable_cores(cpt, mcode->core_mask, AE_TYPES, mcode->group); /* Load microcode for AE engines */ ret = cpt_load_microcode(cpt, mcode); if (ret) { dev_err(dev, "Microcode load Failed for %s\n", mcode->version); goto cpt_init_fail; } cpt->next_group++; /* Configure group mask for the mcode */ cpt_configure_group(cpt, mcode->group, mcode->core_mask, AE_TYPES); /* Enable AE cores for the group mask */ cpt_enable_cores(cpt, mcode->core_mask, AE_TYPES); } else { if (mcode->num_cores > cpt->max_se_cores) { dev_err(dev, "Requested for more cores than available SE cores\n"); ret = -EINVAL; goto cpt_init_fail; } if (cpt->next_group >= CPT_MAX_CORE_GROUPS) { dev_err(dev, "Can't load, all eight microcode groups in use"); return -ENFILE; } mcode->group = cpt->next_group; /* Covert requested cores to mask */ mcode->core_mask = GENMASK(mcode->num_cores, 0); cpt_disable_cores(cpt, mcode->core_mask, SE_TYPES, mcode->group); /* Load microcode for SE engines */ ret = cpt_load_microcode(cpt, mcode); if (ret) { dev_err(dev, "Microcode load Failed for %s\n", mcode->version); goto cpt_init_fail; } cpt->next_group++; /* Configure group mask for the mcode */ cpt_configure_group(cpt, mcode->group, mcode->core_mask, SE_TYPES); /* Enable SE cores for the group mask */ cpt_enable_cores(cpt, mcode->core_mask, SE_TYPES); } /* Enabled PF mailbox interrupts */ cpt_enable_mbox_interrupts(cpt); cpt->flags |= CPT_FLAG_DEVICE_READY; return ret; cpt_init_fail: /* Enabled PF mailbox interrupts */ cpt_enable_mbox_interrupts(cpt); return ret; } struct ucode_header { u8 version[CPT_UCODE_VERSION_SZ]; __be32 code_length; u32 data_length; u64 sram_address; }; static int cpt_ucode_load_fw(struct cpt_device *cpt, const u8 *fw, bool is_ae) { const struct firmware *fw_entry; struct device *dev = &cpt->pdev->dev; struct ucode_header *ucode; unsigned int code_length; struct microcode *mcode; int j, ret = 0; ret = request_firmware(&fw_entry, fw, dev); if (ret) return ret; ucode = (struct ucode_header *)fw_entry->data; mcode = &cpt->mcode[cpt->next_mc_idx]; memcpy(mcode->version, (u8 *)fw_entry->data, CPT_UCODE_VERSION_SZ); code_length = ntohl(ucode->code_length); if (code_length == 0 || code_length >= INT_MAX / 2) { ret = -EINVAL; goto fw_release; } mcode->code_size = code_length * 2; mcode->is_ae = is_ae; mcode->core_mask = 0ULL; mcode->num_cores = is_ae ? 6 : 10; /* Allocate DMAable space */ mcode->code = dma_alloc_coherent(&cpt->pdev->dev, mcode->code_size, &mcode->phys_base, GFP_KERNEL); if (!mcode->code) { dev_err(dev, "Unable to allocate space for microcode"); ret = -ENOMEM; goto fw_release; } memcpy((void *)mcode->code, (void *)(fw_entry->data + sizeof(*ucode)), mcode->code_size); /* Byte swap 64-bit */ for (j = 0; j < (mcode->code_size / 8); j++) ((__be64 *)mcode->code)[j] = cpu_to_be64(((u64 *)mcode->code)[j]); /* MC needs 16-bit swap */ for (j = 0; j < (mcode->code_size / 2); j++) ((__be16 *)mcode->code)[j] = cpu_to_be16(((u16 *)mcode->code)[j]); dev_dbg(dev, "mcode->code_size = %u\n", mcode->code_size); dev_dbg(dev, "mcode->is_ae = %u\n", mcode->is_ae); dev_dbg(dev, "mcode->num_cores = %u\n", mcode->num_cores); dev_dbg(dev, "mcode->code = %llx\n", (u64)mcode->code); dev_dbg(dev, "mcode->phys_base = %llx\n", mcode->phys_base); ret = do_cpt_init(cpt, mcode); if (ret) { dev_err(dev, "do_cpt_init failed with ret: %d\n", ret); goto fw_release; } dev_info(dev, "Microcode Loaded %s\n", mcode->version); mcode->is_mc_valid = 1; cpt->next_mc_idx++; fw_release: release_firmware(fw_entry); return ret; } static int cpt_ucode_load(struct cpt_device *cpt) { int ret = 0; struct device *dev = &cpt->pdev->dev; ret = cpt_ucode_load_fw(cpt, "cpt8x-mc-ae.out", true); if (ret) { dev_err(dev, "ae:cpt_ucode_load failed with ret: %d\n", ret); return ret; } ret = cpt_ucode_load_fw(cpt, "cpt8x-mc-se.out", false); if (ret) { dev_err(dev, "se:cpt_ucode_load failed with ret: %d\n", ret); return ret; } return ret; } static irqreturn_t cpt_mbx0_intr_handler(int irq, void *cpt_irq) { struct cpt_device *cpt = (struct cpt_device *)cpt_irq; cpt_mbox_intr_handler(cpt, 0); return IRQ_HANDLED; } static void cpt_reset(struct cpt_device *cpt) { cpt_write_csr64(cpt->reg_base, CPTX_PF_RESET(0), 1); } static void cpt_find_max_enabled_cores(struct cpt_device *cpt) { union cptx_pf_constants pf_cnsts = {0}; pf_cnsts.u = cpt_read_csr64(cpt->reg_base, CPTX_PF_CONSTANTS(0)); cpt->max_se_cores = pf_cnsts.s.se; cpt->max_ae_cores = pf_cnsts.s.ae; } static u32 cpt_check_bist_status(struct cpt_device *cpt) { union cptx_pf_bist_status bist_sts = {0}; bist_sts.u = cpt_read_csr64(cpt->reg_base, CPTX_PF_BIST_STATUS(0)); return bist_sts.u; } static u64 cpt_check_exe_bist_status(struct cpt_device *cpt) { union cptx_pf_exe_bist_status bist_sts = {0}; bist_sts.u = cpt_read_csr64(cpt->reg_base, CPTX_PF_EXE_BIST_STATUS(0)); return bist_sts.u; } static void cpt_disable_all_cores(struct cpt_device *cpt) { u32 grp, timeout = 100; struct device *dev = &cpt->pdev->dev; /* Disengage the cores from groups */ for (grp = 0; grp < CPT_MAX_CORE_GROUPS; grp++) { cpt_write_csr64(cpt->reg_base, CPTX_PF_GX_EN(0, grp), 0); udelay(CSR_DELAY); } grp = cpt_read_csr64(cpt->reg_base, CPTX_PF_EXEC_BUSY(0)); while (grp) { dev_err(dev, "Cores still busy"); grp = cpt_read_csr64(cpt->reg_base, CPTX_PF_EXEC_BUSY(0)); if (timeout--) break; udelay(CSR_DELAY); } /* Disable the cores */ cpt_write_csr64(cpt->reg_base, CPTX_PF_EXE_CTL(0), 0); } /* * Ensure all cores are disengaged from all groups by * calling cpt_disable_all_cores() before calling this * function. */ static void cpt_unload_microcode(struct cpt_device *cpt) { u32 grp = 0, core; /* Free microcode bases and reset group masks */ for (grp = 0; grp < CPT_MAX_CORE_GROUPS; grp++) { struct microcode *mcode = &cpt->mcode[grp]; if (cpt->mcode[grp].code) dma_free_coherent(&cpt->pdev->dev, mcode->code_size, mcode->code, mcode->phys_base); mcode->code = NULL; } /* Clear UCODE_BASE registers for all engines */ for (core = 0; core < CPT_MAX_TOTAL_CORES; core++) cpt_write_csr64(cpt->reg_base, CPTX_PF_ENGX_UCODE_BASE(0, core), 0ull); } static int cpt_device_init(struct cpt_device *cpt) { u64 bist; struct device *dev = &cpt->pdev->dev; /* Reset the PF when probed first */ cpt_reset(cpt); msleep(100); /*Check BIST status*/ bist = (u64)cpt_check_bist_status(cpt); if (bist) { dev_err(dev, "RAM BIST failed with code 0x%llx", bist); return -ENODEV; } bist = cpt_check_exe_bist_status(cpt); if (bist) { dev_err(dev, "Engine BIST failed with code 0x%llx", bist); return -ENODEV; } /*Get CLK frequency*/ /*Get max enabled cores */ cpt_find_max_enabled_cores(cpt); /*Disable all cores*/ cpt_disable_all_cores(cpt); /*Reset device parameters*/ cpt->next_mc_idx = 0; cpt->next_group = 0; /* PF is ready */ cpt->flags |= CPT_FLAG_DEVICE_READY; return 0; } static int cpt_register_interrupts(struct cpt_device *cpt) { int ret; struct device *dev = &cpt->pdev->dev; /* Enable MSI-X */ ret = pci_alloc_irq_vectors(cpt->pdev, CPT_PF_MSIX_VECTORS, CPT_PF_MSIX_VECTORS, PCI_IRQ_MSIX); if (ret < 0) { dev_err(&cpt->pdev->dev, "Request for #%d msix vectors failed\n", CPT_PF_MSIX_VECTORS); return ret; } /* Register mailbox interrupt handlers */ ret = request_irq(pci_irq_vector(cpt->pdev, CPT_PF_INT_VEC_E_MBOXX(0)), cpt_mbx0_intr_handler, 0, "CPT Mbox0", cpt); if (ret) goto fail; /* Enable mailbox interrupt */ cpt_enable_mbox_interrupts(cpt); return 0; fail: dev_err(dev, "Request irq failed\n"); pci_disable_msix(cpt->pdev); return ret; } static void cpt_unregister_interrupts(struct cpt_device *cpt) { free_irq(pci_irq_vector(cpt->pdev, CPT_PF_INT_VEC_E_MBOXX(0)), cpt); pci_disable_msix(cpt->pdev); } static int cpt_sriov_init(struct cpt_device *cpt, int num_vfs) { int pos = 0; int err; u16 total_vf_cnt; struct pci_dev *pdev = cpt->pdev; pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); if (!pos) { dev_err(&pdev->dev, "SRIOV capability is not found in PCIe config space\n"); return -ENODEV; } cpt->num_vf_en = num_vfs; /* User requested VFs */ pci_read_config_word(pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf_cnt); if (total_vf_cnt < cpt->num_vf_en) cpt->num_vf_en = total_vf_cnt; if (!total_vf_cnt) return 0; /*Enabled the available VFs */ err = pci_enable_sriov(pdev, cpt->num_vf_en); if (err) { dev_err(&pdev->dev, "SRIOV enable failed, num VF is %d\n", cpt->num_vf_en); cpt->num_vf_en = 0; return err; } /* TODO: Optionally enable static VQ priorities feature */ dev_info(&pdev->dev, "SRIOV enabled, number of VF available %d\n", cpt->num_vf_en); cpt->flags |= CPT_FLAG_SRIOV_ENABLED; return 0; } static int cpt_probe(struct pci_dev *pdev, const struct pci_device_id *ent) { struct device *dev = &pdev->dev; struct cpt_device *cpt; int err; if (num_vfs > 16 || num_vfs < 4) { dev_warn(dev, "Invalid vf count %d, Resetting it to 4(default)\n", num_vfs); num_vfs = 4; } cpt = devm_kzalloc(dev, sizeof(*cpt), GFP_KERNEL); if (!cpt) return -ENOMEM; pci_set_drvdata(pdev, cpt); cpt->pdev = pdev; err = pci_enable_device(pdev); if (err) { dev_err(dev, "Failed to enable PCI device\n"); pci_set_drvdata(pdev, NULL); return err; } err = pci_request_regions(pdev, DRV_NAME); if (err) { dev_err(dev, "PCI request regions failed 0x%x\n", err); goto cpt_err_disable_device; } err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48)); if (err) { dev_err(dev, "Unable to get usable 48-bit DMA configuration\n"); goto cpt_err_release_regions; } /* MAP PF's configuration registers */ cpt->reg_base = pcim_iomap(pdev, 0, 0); if (!cpt->reg_base) { dev_err(dev, "Cannot map config register space, aborting\n"); err = -ENOMEM; goto cpt_err_release_regions; } /* CPT device HW initialization */ cpt_device_init(cpt); /* Register interrupts */ err = cpt_register_interrupts(cpt); if (err) goto cpt_err_release_regions; err = cpt_ucode_load(cpt); if (err) goto cpt_err_unregister_interrupts; /* Configure SRIOV */ err = cpt_sriov_init(cpt, num_vfs); if (err) goto cpt_err_unregister_interrupts; return 0; cpt_err_unregister_interrupts: cpt_unregister_interrupts(cpt); cpt_err_release_regions: pci_release_regions(pdev); cpt_err_disable_device: pci_disable_device(pdev); pci_set_drvdata(pdev, NULL); return err; } static void cpt_remove(struct pci_dev *pdev) { struct cpt_device *cpt = pci_get_drvdata(pdev); /* Disengage SE and AE cores from all groups*/ cpt_disable_all_cores(cpt); /* Unload microcodes */ cpt_unload_microcode(cpt); cpt_unregister_interrupts(cpt); pci_disable_sriov(pdev); pci_release_regions(pdev); pci_disable_device(pdev); pci_set_drvdata(pdev, NULL); } static void cpt_shutdown(struct pci_dev *pdev) { struct cpt_device *cpt = pci_get_drvdata(pdev); if (!cpt) return; dev_info(&pdev->dev, "Shutdown device %x:%x.\n", (u32)pdev->vendor, (u32)pdev->device); cpt_unregister_interrupts(cpt); pci_release_regions(pdev); pci_disable_device(pdev); pci_set_drvdata(pdev, NULL); } /* Supported devices */ static const struct pci_device_id cpt_id_table[] = { { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, CPT_81XX_PCI_PF_DEVICE_ID) }, { 0, } /* end of table */ }; static struct pci_driver cpt_pci_driver = { .name = DRV_NAME, .id_table = cpt_id_table, .probe = cpt_probe, .remove = cpt_remove, .shutdown = cpt_shutdown, }; module_pci_driver(cpt_pci_driver); MODULE_AUTHOR("George Cherian <george.cherian@cavium.com>"); MODULE_DESCRIPTION("Cavium Thunder CPT Physical Function Driver"); MODULE_LICENSE("GPL v2"); MODULE_VERSION(DRV_VERSION); MODULE_DEVICE_TABLE(pci, cpt_id_table); |