1bfe1d560SDave Jiang // SPDX-License-Identifier: GPL-2.0 2bfe1d560SDave Jiang /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */ 3bfe1d560SDave Jiang #include <linux/init.h> 4bfe1d560SDave Jiang #include <linux/kernel.h> 5bfe1d560SDave Jiang #include <linux/module.h> 6bfe1d560SDave Jiang #include <linux/slab.h> 7bfe1d560SDave Jiang #include <linux/pci.h> 8bfe1d560SDave Jiang #include <linux/interrupt.h> 9bfe1d560SDave Jiang #include <linux/delay.h> 10bfe1d560SDave Jiang #include <linux/dma-mapping.h> 11bfe1d560SDave Jiang #include <linux/workqueue.h> 12bfe1d560SDave Jiang #include <linux/aer.h> 13bfe1d560SDave Jiang #include <linux/fs.h> 14bfe1d560SDave Jiang #include <linux/io-64-nonatomic-lo-hi.h> 15bfe1d560SDave Jiang #include <linux/device.h> 16bfe1d560SDave Jiang #include <linux/idr.h> 178e50d392SDave Jiang #include <linux/intel-svm.h> 188e50d392SDave Jiang #include <linux/iommu.h> 19bfe1d560SDave Jiang #include <uapi/linux/idxd.h> 208f47d1a5SDave Jiang #include <linux/dmaengine.h> 218f47d1a5SDave Jiang #include "../dmaengine.h" 22bfe1d560SDave Jiang #include "registers.h" 23bfe1d560SDave Jiang #include "idxd.h" 24bfe1d560SDave Jiang 25bfe1d560SDave Jiang MODULE_VERSION(IDXD_DRIVER_VERSION); 26bfe1d560SDave Jiang MODULE_LICENSE("GPL v2"); 27bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation"); 28bfe1d560SDave Jiang 2903d939c7SDave Jiang static bool sva = true; 3003d939c7SDave Jiang module_param(sva, bool, 0644); 3103d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off"); 3203d939c7SDave Jiang 33bfe1d560SDave Jiang #define DRV_NAME "idxd" 34bfe1d560SDave Jiang 358e50d392SDave Jiang bool support_enqcmd; 364b73e4ebSDave Jiang DEFINE_IDA(idxd_ida); 37bfe1d560SDave Jiang 38435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = { 39435b512dSDave Jiang [IDXD_TYPE_DSA] = { 40435b512dSDave Jiang .name_prefix = "dsa", 41435b512dSDave Jiang .type = IDXD_TYPE_DSA, 42435b512dSDave Jiang .compl_size = sizeof(struct dsa_completion_record), 43435b512dSDave Jiang .align = 32, 44435b512dSDave Jiang .dev_type = &dsa_device_type, 45435b512dSDave Jiang }, 46435b512dSDave Jiang [IDXD_TYPE_IAX] = { 47435b512dSDave Jiang .name_prefix = "iax", 48435b512dSDave Jiang .type = IDXD_TYPE_IAX, 49435b512dSDave Jiang .compl_size = sizeof(struct iax_completion_record), 50435b512dSDave Jiang .align = 64, 51435b512dSDave Jiang .dev_type = &iax_device_type, 52435b512dSDave Jiang }, 53435b512dSDave Jiang }; 54435b512dSDave Jiang 55bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = { 56bfe1d560SDave Jiang /* DSA ver 1.0 platforms */ 57435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) }, 58f25b4638SDave Jiang 59f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 60435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) }, 61bfe1d560SDave Jiang { 0, } 62bfe1d560SDave Jiang }; 63bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 64bfe1d560SDave Jiang 65bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd) 66bfe1d560SDave Jiang { 67bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 68bfe1d560SDave Jiang struct device *dev = &pdev->dev; 69bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 70bfe1d560SDave Jiang int i, msixcnt; 71bfe1d560SDave Jiang int rc = 0; 72bfe1d560SDave Jiang 73bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev); 74bfe1d560SDave Jiang if (msixcnt < 0) { 75bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n"); 765fc8e85fSDave Jiang return -ENOSPC; 77bfe1d560SDave Jiang } 78bfe1d560SDave Jiang 795fc8e85fSDave Jiang rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); 805fc8e85fSDave Jiang if (rc != msixcnt) { 815fc8e85fSDave Jiang dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); 825fc8e85fSDave Jiang return -ENOSPC; 83bfe1d560SDave Jiang } 84bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 85bfe1d560SDave Jiang 86bfe1d560SDave Jiang /* 87bfe1d560SDave Jiang * We implement 1 completion list per MSI-X entry except for 88bfe1d560SDave Jiang * entry 0, which is for errors and others. 89bfe1d560SDave Jiang */ 9047c16ac2SDave Jiang idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry), 9147c16ac2SDave Jiang GFP_KERNEL, dev_to_node(dev)); 92bfe1d560SDave Jiang if (!idxd->irq_entries) { 93bfe1d560SDave Jiang rc = -ENOMEM; 945fc8e85fSDave Jiang goto err_irq_entries; 95bfe1d560SDave Jiang } 96bfe1d560SDave Jiang 97bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 98bfe1d560SDave Jiang idxd->irq_entries[i].id = i; 99bfe1d560SDave Jiang idxd->irq_entries[i].idxd = idxd; 1005fc8e85fSDave Jiang idxd->irq_entries[i].vector = pci_irq_vector(pdev, i); 101e4f4d8cdSDave Jiang spin_lock_init(&idxd->irq_entries[i].list_lock); 102bfe1d560SDave Jiang } 103bfe1d560SDave Jiang 104bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[0]; 1055fc8e85fSDave Jiang rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler, idxd_misc_thread, 1065fc8e85fSDave Jiang 0, "idxd-misc", irq_entry); 107bfe1d560SDave Jiang if (rc < 0) { 108bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n"); 1095fc8e85fSDave Jiang goto err_misc_irq; 110bfe1d560SDave Jiang } 111bfe1d560SDave Jiang 1125fc8e85fSDave Jiang dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector); 113bfe1d560SDave Jiang 114bfe1d560SDave Jiang /* first MSI-X entry is not for wq interrupts */ 115bfe1d560SDave Jiang idxd->num_wq_irqs = msixcnt - 1; 116bfe1d560SDave Jiang 117bfe1d560SDave Jiang for (i = 1; i < msixcnt; i++) { 118bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 119bfe1d560SDave Jiang 120bfe1d560SDave Jiang init_llist_head(&idxd->irq_entries[i].pending_llist); 121bfe1d560SDave Jiang INIT_LIST_HEAD(&idxd->irq_entries[i].work_list); 1225fc8e85fSDave Jiang rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler, 1235fc8e85fSDave Jiang idxd_wq_thread, 0, "idxd-portal", irq_entry); 124bfe1d560SDave Jiang if (rc < 0) { 1255fc8e85fSDave Jiang dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector); 1265fc8e85fSDave Jiang goto err_wq_irqs; 127bfe1d560SDave Jiang } 1285fc8e85fSDave Jiang dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector); 129bfe1d560SDave Jiang } 130bfe1d560SDave Jiang 131bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd); 1326df0e6c5SDave Jiang idxd_msix_perm_setup(idxd); 133bfe1d560SDave Jiang return 0; 134bfe1d560SDave Jiang 1355fc8e85fSDave Jiang err_wq_irqs: 1365fc8e85fSDave Jiang while (--i >= 0) { 1375fc8e85fSDave Jiang irq_entry = &idxd->irq_entries[i]; 1385fc8e85fSDave Jiang free_irq(irq_entry->vector, irq_entry); 1395fc8e85fSDave Jiang } 1405fc8e85fSDave Jiang err_misc_irq: 141bfe1d560SDave Jiang /* Disable error interrupt generation */ 142bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 1435fc8e85fSDave Jiang err_irq_entries: 1445fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 145bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n"); 146bfe1d560SDave Jiang return rc; 147bfe1d560SDave Jiang } 148bfe1d560SDave Jiang 1497c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd) 1507c5dd23eSDave Jiang { 1517c5dd23eSDave Jiang struct device *dev = &idxd->pdev->dev; 1527c5dd23eSDave Jiang struct idxd_wq *wq; 1537c5dd23eSDave Jiang int i, rc; 1547c5dd23eSDave Jiang 1557c5dd23eSDave Jiang idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 1567c5dd23eSDave Jiang GFP_KERNEL, dev_to_node(dev)); 1577c5dd23eSDave Jiang if (!idxd->wqs) 1587c5dd23eSDave Jiang return -ENOMEM; 1597c5dd23eSDave Jiang 1607c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 1617c5dd23eSDave Jiang wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); 1627c5dd23eSDave Jiang if (!wq) { 1637c5dd23eSDave Jiang rc = -ENOMEM; 1647c5dd23eSDave Jiang goto err; 1657c5dd23eSDave Jiang } 1667c5dd23eSDave Jiang 1677c5dd23eSDave Jiang wq->id = i; 1687c5dd23eSDave Jiang wq->idxd = idxd; 1697c5dd23eSDave Jiang device_initialize(&wq->conf_dev); 1707c5dd23eSDave Jiang wq->conf_dev.parent = &idxd->conf_dev; 1714b73e4ebSDave Jiang wq->conf_dev.bus = &dsa_bus_type; 1727c5dd23eSDave Jiang wq->conf_dev.type = &idxd_wq_device_type; 1737c5dd23eSDave Jiang rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id); 1747c5dd23eSDave Jiang if (rc < 0) { 1757c5dd23eSDave Jiang put_device(&wq->conf_dev); 1767c5dd23eSDave Jiang goto err; 1777c5dd23eSDave Jiang } 1787c5dd23eSDave Jiang 1797c5dd23eSDave Jiang mutex_init(&wq->wq_lock); 18004922b74SDave Jiang init_waitqueue_head(&wq->err_queue); 181*93a40a6dSDave Jiang init_completion(&wq->wq_dead); 1827c5dd23eSDave Jiang wq->max_xfer_bytes = idxd->max_xfer_bytes; 1837c5dd23eSDave Jiang wq->max_batch_size = idxd->max_batch_size; 1847c5dd23eSDave Jiang wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); 1857c5dd23eSDave Jiang if (!wq->wqcfg) { 1867c5dd23eSDave Jiang put_device(&wq->conf_dev); 1877c5dd23eSDave Jiang rc = -ENOMEM; 1887c5dd23eSDave Jiang goto err; 1897c5dd23eSDave Jiang } 1907c5dd23eSDave Jiang idxd->wqs[i] = wq; 1917c5dd23eSDave Jiang } 1927c5dd23eSDave Jiang 1937c5dd23eSDave Jiang return 0; 1947c5dd23eSDave Jiang 1957c5dd23eSDave Jiang err: 1967c5dd23eSDave Jiang while (--i >= 0) 1977c5dd23eSDave Jiang put_device(&idxd->wqs[i]->conf_dev); 1987c5dd23eSDave Jiang return rc; 1997c5dd23eSDave Jiang } 2007c5dd23eSDave Jiang 20175b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd) 20275b91130SDave Jiang { 20375b91130SDave Jiang struct idxd_engine *engine; 20475b91130SDave Jiang struct device *dev = &idxd->pdev->dev; 20575b91130SDave Jiang int i, rc; 20675b91130SDave Jiang 20775b91130SDave Jiang idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), 20875b91130SDave Jiang GFP_KERNEL, dev_to_node(dev)); 20975b91130SDave Jiang if (!idxd->engines) 21075b91130SDave Jiang return -ENOMEM; 21175b91130SDave Jiang 21275b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) { 21375b91130SDave Jiang engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); 21475b91130SDave Jiang if (!engine) { 21575b91130SDave Jiang rc = -ENOMEM; 21675b91130SDave Jiang goto err; 21775b91130SDave Jiang } 21875b91130SDave Jiang 21975b91130SDave Jiang engine->id = i; 22075b91130SDave Jiang engine->idxd = idxd; 22175b91130SDave Jiang device_initialize(&engine->conf_dev); 22275b91130SDave Jiang engine->conf_dev.parent = &idxd->conf_dev; 22375b91130SDave Jiang engine->conf_dev.type = &idxd_engine_device_type; 22475b91130SDave Jiang rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id); 22575b91130SDave Jiang if (rc < 0) { 22675b91130SDave Jiang put_device(&engine->conf_dev); 22775b91130SDave Jiang goto err; 22875b91130SDave Jiang } 22975b91130SDave Jiang 23075b91130SDave Jiang idxd->engines[i] = engine; 23175b91130SDave Jiang } 23275b91130SDave Jiang 23375b91130SDave Jiang return 0; 23475b91130SDave Jiang 23575b91130SDave Jiang err: 23675b91130SDave Jiang while (--i >= 0) 23775b91130SDave Jiang put_device(&idxd->engines[i]->conf_dev); 23875b91130SDave Jiang return rc; 23975b91130SDave Jiang } 24075b91130SDave Jiang 241defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd) 242defe49f9SDave Jiang { 243defe49f9SDave Jiang struct device *dev = &idxd->pdev->dev; 244defe49f9SDave Jiang struct idxd_group *group; 245defe49f9SDave Jiang int i, rc; 246defe49f9SDave Jiang 247defe49f9SDave Jiang idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), 248defe49f9SDave Jiang GFP_KERNEL, dev_to_node(dev)); 249defe49f9SDave Jiang if (!idxd->groups) 250defe49f9SDave Jiang return -ENOMEM; 251defe49f9SDave Jiang 252defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) { 253defe49f9SDave Jiang group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); 254defe49f9SDave Jiang if (!group) { 255defe49f9SDave Jiang rc = -ENOMEM; 256defe49f9SDave Jiang goto err; 257defe49f9SDave Jiang } 258defe49f9SDave Jiang 259defe49f9SDave Jiang group->id = i; 260defe49f9SDave Jiang group->idxd = idxd; 261defe49f9SDave Jiang device_initialize(&group->conf_dev); 262defe49f9SDave Jiang group->conf_dev.parent = &idxd->conf_dev; 2634b73e4ebSDave Jiang group->conf_dev.bus = &dsa_bus_type; 264defe49f9SDave Jiang group->conf_dev.type = &idxd_group_device_type; 265defe49f9SDave Jiang rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id); 266defe49f9SDave Jiang if (rc < 0) { 267defe49f9SDave Jiang put_device(&group->conf_dev); 268defe49f9SDave Jiang goto err; 269defe49f9SDave Jiang } 270defe49f9SDave Jiang 271defe49f9SDave Jiang idxd->groups[i] = group; 272defe49f9SDave Jiang group->tc_a = -1; 273defe49f9SDave Jiang group->tc_b = -1; 274defe49f9SDave Jiang } 275defe49f9SDave Jiang 276defe49f9SDave Jiang return 0; 277defe49f9SDave Jiang 278defe49f9SDave Jiang err: 279defe49f9SDave Jiang while (--i >= 0) 280defe49f9SDave Jiang put_device(&idxd->groups[i]->conf_dev); 281defe49f9SDave Jiang return rc; 282defe49f9SDave Jiang } 283defe49f9SDave Jiang 284bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd) 285bfe1d560SDave Jiang { 286bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 287defe49f9SDave Jiang int rc, i; 288bfe1d560SDave Jiang 2890d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq); 2907c5dd23eSDave Jiang 2917c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd); 2927c5dd23eSDave Jiang if (rc < 0) 2937c5dd23eSDave Jiang return rc; 2947c5dd23eSDave Jiang 29575b91130SDave Jiang rc = idxd_setup_engines(idxd); 29675b91130SDave Jiang if (rc < 0) 29775b91130SDave Jiang goto err_engine; 29875b91130SDave Jiang 299defe49f9SDave Jiang rc = idxd_setup_groups(idxd); 300defe49f9SDave Jiang if (rc < 0) 301defe49f9SDave Jiang goto err_group; 302bfe1d560SDave Jiang 3030d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 3047c5dd23eSDave Jiang if (!idxd->wq) { 3057c5dd23eSDave Jiang rc = -ENOMEM; 306defe49f9SDave Jiang goto err_wkq_create; 3077c5dd23eSDave Jiang } 3080d5c10b4SDave Jiang 309bfe1d560SDave Jiang return 0; 3107c5dd23eSDave Jiang 311defe49f9SDave Jiang err_wkq_create: 312defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) 313defe49f9SDave Jiang put_device(&idxd->groups[i]->conf_dev); 314defe49f9SDave Jiang err_group: 31575b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) 31675b91130SDave Jiang put_device(&idxd->engines[i]->conf_dev); 31775b91130SDave Jiang err_engine: 3187c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) 3197c5dd23eSDave Jiang put_device(&idxd->wqs[i]->conf_dev); 3207c5dd23eSDave Jiang return rc; 321bfe1d560SDave Jiang } 322bfe1d560SDave Jiang 323bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 324bfe1d560SDave Jiang { 325bfe1d560SDave Jiang union offsets_reg offsets; 326bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 327bfe1d560SDave Jiang 328bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 3292f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 3302f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 331bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 3322f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 3332f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 3342f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 3352f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 3362f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 337bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 338bfe1d560SDave Jiang } 339bfe1d560SDave Jiang 340bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 341bfe1d560SDave Jiang { 342bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 343bfe1d560SDave Jiang int i; 344bfe1d560SDave Jiang 345bfe1d560SDave Jiang /* reading generic capabilities */ 346bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 347bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 348bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 349bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 350bfe1d560SDave Jiang idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift; 351bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 352bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 353bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 354bfe1d560SDave Jiang 355bfe1d560SDave Jiang /* reading group capabilities */ 356bfe1d560SDave Jiang idxd->hw.group_cap.bits = 357bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 358bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 359bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 360bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 361bfe1d560SDave Jiang idxd->max_tokens = idxd->hw.group_cap.total_tokens; 362bfe1d560SDave Jiang dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens); 363c52ca478SDave Jiang idxd->nr_tokens = idxd->max_tokens; 364bfe1d560SDave Jiang 365bfe1d560SDave Jiang /* read engine capabilities */ 366bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 367bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 368bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 369bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 370bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 371bfe1d560SDave Jiang 372bfe1d560SDave Jiang /* read workqueue capabilities */ 373bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 374bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 375bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 376bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 377bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 378bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 379d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 380d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 381bfe1d560SDave Jiang 382bfe1d560SDave Jiang /* reading operation capabilities */ 383bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 384bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 385bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 386bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 387bfe1d560SDave Jiang } 388bfe1d560SDave Jiang } 389bfe1d560SDave Jiang 390435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) 391bfe1d560SDave Jiang { 392bfe1d560SDave Jiang struct device *dev = &pdev->dev; 393bfe1d560SDave Jiang struct idxd_device *idxd; 39447c16ac2SDave Jiang int rc; 395bfe1d560SDave Jiang 39647c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 397bfe1d560SDave Jiang if (!idxd) 398bfe1d560SDave Jiang return NULL; 399bfe1d560SDave Jiang 400bfe1d560SDave Jiang idxd->pdev = pdev; 401435b512dSDave Jiang idxd->data = data; 4024b73e4ebSDave Jiang idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); 40347c16ac2SDave Jiang if (idxd->id < 0) 40447c16ac2SDave Jiang return NULL; 40547c16ac2SDave Jiang 40647c16ac2SDave Jiang device_initialize(&idxd->conf_dev); 40747c16ac2SDave Jiang idxd->conf_dev.parent = dev; 4084b73e4ebSDave Jiang idxd->conf_dev.bus = &dsa_bus_type; 409435b512dSDave Jiang idxd->conf_dev.type = idxd->data->dev_type; 410435b512dSDave Jiang rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); 41147c16ac2SDave Jiang if (rc < 0) { 41247c16ac2SDave Jiang put_device(&idxd->conf_dev); 41347c16ac2SDave Jiang return NULL; 41447c16ac2SDave Jiang } 41547c16ac2SDave Jiang 416bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 417bfe1d560SDave Jiang 418bfe1d560SDave Jiang return idxd; 419bfe1d560SDave Jiang } 420bfe1d560SDave Jiang 4218e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 4228e50d392SDave Jiang { 4238e50d392SDave Jiang int flags; 4248e50d392SDave Jiang unsigned int pasid; 4258e50d392SDave Jiang struct iommu_sva *sva; 4268e50d392SDave Jiang 4278e50d392SDave Jiang flags = SVM_FLAG_SUPERVISOR_MODE; 4288e50d392SDave Jiang 4298e50d392SDave Jiang sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags); 4308e50d392SDave Jiang if (IS_ERR(sva)) { 4318e50d392SDave Jiang dev_warn(&idxd->pdev->dev, 4328e50d392SDave Jiang "iommu sva bind failed: %ld\n", PTR_ERR(sva)); 4338e50d392SDave Jiang return PTR_ERR(sva); 4348e50d392SDave Jiang } 4358e50d392SDave Jiang 4368e50d392SDave Jiang pasid = iommu_sva_get_pasid(sva); 4378e50d392SDave Jiang if (pasid == IOMMU_PASID_INVALID) { 4388e50d392SDave Jiang iommu_sva_unbind_device(sva); 4398e50d392SDave Jiang return -ENODEV; 4408e50d392SDave Jiang } 4418e50d392SDave Jiang 4428e50d392SDave Jiang idxd->sva = sva; 4438e50d392SDave Jiang idxd->pasid = pasid; 4448e50d392SDave Jiang dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid); 4458e50d392SDave Jiang return 0; 4468e50d392SDave Jiang } 4478e50d392SDave Jiang 4488e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 4498e50d392SDave Jiang { 4508e50d392SDave Jiang 4518e50d392SDave Jiang iommu_sva_unbind_device(idxd->sva); 4528e50d392SDave Jiang idxd->sva = NULL; 4538e50d392SDave Jiang } 4548e50d392SDave Jiang 455bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 456bfe1d560SDave Jiang { 457bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 458bfe1d560SDave Jiang struct device *dev = &pdev->dev; 459bfe1d560SDave Jiang int rc; 460bfe1d560SDave Jiang 461bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 46289e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 46389e3becdSDave Jiang if (rc < 0) 46489e3becdSDave Jiang return rc; 46589e3becdSDave Jiang 466bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 467bfe1d560SDave Jiang 46803d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 4698e50d392SDave Jiang rc = idxd_enable_system_pasid(idxd); 4708e50d392SDave Jiang if (rc < 0) 4718e50d392SDave Jiang dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); 4728e50d392SDave Jiang else 4738e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 47403d939c7SDave Jiang } else if (!sva) { 47503d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n"); 4768e50d392SDave Jiang } 4778e50d392SDave Jiang 478bfe1d560SDave Jiang idxd_read_caps(idxd); 479bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 480bfe1d560SDave Jiang 481bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 482bfe1d560SDave Jiang if (rc) 4837c5dd23eSDave Jiang goto err; 484bfe1d560SDave Jiang 485bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 486bfe1d560SDave Jiang if (rc) 4877c5dd23eSDave Jiang goto err; 488bfe1d560SDave Jiang 489bfe1d560SDave Jiang dev_dbg(dev, "IDXD interrupt setup complete.\n"); 490bfe1d560SDave Jiang 49142d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 49242d279f9SDave Jiang 493bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 494bfe1d560SDave Jiang return 0; 495bfe1d560SDave Jiang 4967c5dd23eSDave Jiang err: 4978e50d392SDave Jiang if (device_pasid_enabled(idxd)) 4988e50d392SDave Jiang idxd_disable_system_pasid(idxd); 499bfe1d560SDave Jiang return rc; 500bfe1d560SDave Jiang } 501bfe1d560SDave Jiang 502bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 503bfe1d560SDave Jiang { 504bfe1d560SDave Jiang struct device *dev = &pdev->dev; 505bfe1d560SDave Jiang struct idxd_device *idxd; 506435b512dSDave Jiang struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data; 507bfe1d560SDave Jiang int rc; 508bfe1d560SDave Jiang 509a39c7cd0SDave Jiang rc = pci_enable_device(pdev); 510bfe1d560SDave Jiang if (rc) 511bfe1d560SDave Jiang return rc; 512bfe1d560SDave Jiang 5138e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 514435b512dSDave Jiang idxd = idxd_alloc(pdev, data); 515a39c7cd0SDave Jiang if (!idxd) { 516a39c7cd0SDave Jiang rc = -ENOMEM; 517a39c7cd0SDave Jiang goto err_idxd_alloc; 518a39c7cd0SDave Jiang } 519bfe1d560SDave Jiang 5208e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 521a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 522a39c7cd0SDave Jiang if (!idxd->reg_base) { 523a39c7cd0SDave Jiang rc = -ENOMEM; 524a39c7cd0SDave Jiang goto err_iomap; 525a39c7cd0SDave Jiang } 526bfe1d560SDave Jiang 527bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 528bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 529bfe1d560SDave Jiang if (rc) 530bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 531bfe1d560SDave Jiang if (rc) 532a39c7cd0SDave Jiang goto err; 533bfe1d560SDave Jiang 534bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 535bfe1d560SDave Jiang if (rc) 536bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 537bfe1d560SDave Jiang if (rc) 538a39c7cd0SDave Jiang goto err; 539bfe1d560SDave Jiang 540bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 541bfe1d560SDave Jiang pci_set_master(pdev); 542bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 543bfe1d560SDave Jiang 544bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 545bfe1d560SDave Jiang rc = idxd_probe(idxd); 546bfe1d560SDave Jiang if (rc) { 547bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 548a39c7cd0SDave Jiang goto err; 549bfe1d560SDave Jiang } 550bfe1d560SDave Jiang 55147c16ac2SDave Jiang rc = idxd_register_devices(idxd); 552c52ca478SDave Jiang if (rc) { 553c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 554a39c7cd0SDave Jiang goto err; 555c52ca478SDave Jiang } 556c52ca478SDave Jiang 557c52ca478SDave Jiang idxd->state = IDXD_DEV_CONF_READY; 558c52ca478SDave Jiang 559bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 560bfe1d560SDave Jiang idxd->hw.version); 561bfe1d560SDave Jiang 562bfe1d560SDave Jiang return 0; 563a39c7cd0SDave Jiang 564a39c7cd0SDave Jiang err: 565a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 566a39c7cd0SDave Jiang err_iomap: 56747c16ac2SDave Jiang put_device(&idxd->conf_dev); 568a39c7cd0SDave Jiang err_idxd_alloc: 569a39c7cd0SDave Jiang pci_disable_device(pdev); 570a39c7cd0SDave Jiang return rc; 571bfe1d560SDave Jiang } 572bfe1d560SDave Jiang 5738f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) 5748f47d1a5SDave Jiang { 5758f47d1a5SDave Jiang struct idxd_desc *desc, *itr; 5768f47d1a5SDave Jiang struct llist_node *head; 5778f47d1a5SDave Jiang 5788f47d1a5SDave Jiang head = llist_del_all(&ie->pending_llist); 5798f47d1a5SDave Jiang if (!head) 5808f47d1a5SDave Jiang return; 5818f47d1a5SDave Jiang 5828f47d1a5SDave Jiang llist_for_each_entry_safe(desc, itr, head, llnode) { 5838f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 5848f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 5858f47d1a5SDave Jiang } 5868f47d1a5SDave Jiang } 5878f47d1a5SDave Jiang 5888f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie) 5898f47d1a5SDave Jiang { 5908f47d1a5SDave Jiang struct idxd_desc *desc, *iter; 5918f47d1a5SDave Jiang 5928f47d1a5SDave Jiang list_for_each_entry_safe(desc, iter, &ie->work_list, list) { 5938f47d1a5SDave Jiang list_del(&desc->list); 5948f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 5958f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 5968f47d1a5SDave Jiang } 5978f47d1a5SDave Jiang } 5988f47d1a5SDave Jiang 599bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 600bfe1d560SDave Jiang { 601bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 602bfe1d560SDave Jiang int rc, i; 603bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 604bfe1d560SDave Jiang int msixcnt = pci_msix_vec_count(pdev); 605bfe1d560SDave Jiang 606bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 607bfe1d560SDave Jiang if (rc) 608bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 609bfe1d560SDave Jiang 610bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 611bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 612bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 613bfe1d560SDave Jiang 614bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 615bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 6165fc8e85fSDave Jiang synchronize_irq(irq_entry->vector); 6175fc8e85fSDave Jiang free_irq(irq_entry->vector, irq_entry); 618bfe1d560SDave Jiang if (i == 0) 619bfe1d560SDave Jiang continue; 6208f47d1a5SDave Jiang idxd_flush_pending_llist(irq_entry); 6218f47d1a5SDave Jiang idxd_flush_work_list(irq_entry); 622bfe1d560SDave Jiang } 6230d5c10b4SDave Jiang 6246df0e6c5SDave Jiang idxd_msix_perm_clear(idxd); 6255fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 626a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 627a39c7cd0SDave Jiang pci_disable_device(pdev); 6280d5c10b4SDave Jiang destroy_workqueue(idxd->wq); 629bfe1d560SDave Jiang } 630bfe1d560SDave Jiang 631bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 632bfe1d560SDave Jiang { 633bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 634bfe1d560SDave Jiang 635bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 636bfe1d560SDave Jiang idxd_shutdown(pdev); 6378e50d392SDave Jiang if (device_pasid_enabled(idxd)) 6388e50d392SDave Jiang idxd_disable_system_pasid(idxd); 63947c16ac2SDave Jiang idxd_unregister_devices(idxd); 640bfe1d560SDave Jiang } 641bfe1d560SDave Jiang 642bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 643bfe1d560SDave Jiang .name = DRV_NAME, 644bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 645bfe1d560SDave Jiang .probe = idxd_pci_probe, 646bfe1d560SDave Jiang .remove = idxd_remove, 647bfe1d560SDave Jiang .shutdown = idxd_shutdown, 648bfe1d560SDave Jiang }; 649bfe1d560SDave Jiang 650bfe1d560SDave Jiang static int __init idxd_init_module(void) 651bfe1d560SDave Jiang { 6524b73e4ebSDave Jiang int err; 653bfe1d560SDave Jiang 654bfe1d560SDave Jiang /* 6558e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 656bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 657bfe1d560SDave Jiang */ 658bfe1d560SDave Jiang if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) { 659bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 660bfe1d560SDave Jiang return -ENODEV; 661bfe1d560SDave Jiang } 662bfe1d560SDave Jiang 6638e50d392SDave Jiang if (!boot_cpu_has(X86_FEATURE_ENQCMD)) 6648e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 6658e50d392SDave Jiang else 6668e50d392SDave Jiang support_enqcmd = true; 667bfe1d560SDave Jiang 668c52ca478SDave Jiang err = idxd_register_bus_type(); 669c52ca478SDave Jiang if (err < 0) 670bfe1d560SDave Jiang return err; 671bfe1d560SDave Jiang 672c52ca478SDave Jiang err = idxd_register_driver(); 673c52ca478SDave Jiang if (err < 0) 674c52ca478SDave Jiang goto err_idxd_driver_register; 675c52ca478SDave Jiang 67642d279f9SDave Jiang err = idxd_cdev_register(); 67742d279f9SDave Jiang if (err) 67842d279f9SDave Jiang goto err_cdev_register; 67942d279f9SDave Jiang 680c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 681c52ca478SDave Jiang if (err) 682c52ca478SDave Jiang goto err_pci_register; 683c52ca478SDave Jiang 684bfe1d560SDave Jiang return 0; 685c52ca478SDave Jiang 686c52ca478SDave Jiang err_pci_register: 68742d279f9SDave Jiang idxd_cdev_remove(); 68842d279f9SDave Jiang err_cdev_register: 689c52ca478SDave Jiang idxd_unregister_driver(); 690c52ca478SDave Jiang err_idxd_driver_register: 691c52ca478SDave Jiang idxd_unregister_bus_type(); 692c52ca478SDave Jiang return err; 693bfe1d560SDave Jiang } 694bfe1d560SDave Jiang module_init(idxd_init_module); 695bfe1d560SDave Jiang 696bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 697bfe1d560SDave Jiang { 698bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 69942d279f9SDave Jiang idxd_cdev_remove(); 700c52ca478SDave Jiang idxd_unregister_bus_type(); 701bfe1d560SDave Jiang } 702bfe1d560SDave Jiang module_exit(idxd_exit_module); 703