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; 368e50d392SDave Jiang 37f7f77398SDave Jiang static struct ida idxd_idas[IDXD_TYPE_MAX]; 38bfe1d560SDave Jiang 39bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = { 40bfe1d560SDave Jiang /* DSA ver 1.0 platforms */ 41bfe1d560SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_DSA_SPR0) }, 42f25b4638SDave Jiang 43f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 44f25b4638SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IAX_SPR0) }, 45bfe1d560SDave Jiang { 0, } 46bfe1d560SDave Jiang }; 47bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 48bfe1d560SDave Jiang 49bfe1d560SDave Jiang static char *idxd_name[] = { 50bfe1d560SDave Jiang "dsa", 51f25b4638SDave Jiang "iax" 52bfe1d560SDave Jiang }; 53bfe1d560SDave Jiang 5447c16ac2SDave Jiang struct ida *idxd_ida(struct idxd_device *idxd) 5547c16ac2SDave Jiang { 5647c16ac2SDave Jiang return &idxd_idas[idxd->type]; 5747c16ac2SDave Jiang } 5847c16ac2SDave Jiang 59bfe1d560SDave Jiang const char *idxd_get_dev_name(struct idxd_device *idxd) 60bfe1d560SDave Jiang { 61bfe1d560SDave Jiang return idxd_name[idxd->type]; 62bfe1d560SDave Jiang } 63bfe1d560SDave Jiang 64bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd) 65bfe1d560SDave Jiang { 66bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 67bfe1d560SDave Jiang struct device *dev = &pdev->dev; 68bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 69bfe1d560SDave Jiang int i, msixcnt; 70bfe1d560SDave Jiang int rc = 0; 71bfe1d560SDave Jiang 72bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev); 73bfe1d560SDave Jiang if (msixcnt < 0) { 74bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n"); 755fc8e85fSDave Jiang return -ENOSPC; 76bfe1d560SDave Jiang } 77bfe1d560SDave Jiang 785fc8e85fSDave Jiang rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); 795fc8e85fSDave Jiang if (rc != msixcnt) { 805fc8e85fSDave Jiang dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); 815fc8e85fSDave Jiang return -ENOSPC; 82bfe1d560SDave Jiang } 83bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 84bfe1d560SDave Jiang 85bfe1d560SDave Jiang /* 86bfe1d560SDave Jiang * We implement 1 completion list per MSI-X entry except for 87bfe1d560SDave Jiang * entry 0, which is for errors and others. 88bfe1d560SDave Jiang */ 8947c16ac2SDave Jiang idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry), 9047c16ac2SDave Jiang GFP_KERNEL, dev_to_node(dev)); 91bfe1d560SDave Jiang if (!idxd->irq_entries) { 92bfe1d560SDave Jiang rc = -ENOMEM; 935fc8e85fSDave Jiang goto err_irq_entries; 94bfe1d560SDave Jiang } 95bfe1d560SDave Jiang 96bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 97bfe1d560SDave Jiang idxd->irq_entries[i].id = i; 98bfe1d560SDave Jiang idxd->irq_entries[i].idxd = idxd; 995fc8e85fSDave Jiang idxd->irq_entries[i].vector = pci_irq_vector(pdev, i); 100e4f4d8cdSDave Jiang spin_lock_init(&idxd->irq_entries[i].list_lock); 101bfe1d560SDave Jiang } 102bfe1d560SDave Jiang 103bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[0]; 1045fc8e85fSDave Jiang rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler, idxd_misc_thread, 1055fc8e85fSDave Jiang 0, "idxd-misc", irq_entry); 106bfe1d560SDave Jiang if (rc < 0) { 107bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n"); 1085fc8e85fSDave Jiang goto err_misc_irq; 109bfe1d560SDave Jiang } 110bfe1d560SDave Jiang 1115fc8e85fSDave Jiang dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector); 112bfe1d560SDave Jiang 113bfe1d560SDave Jiang /* first MSI-X entry is not for wq interrupts */ 114bfe1d560SDave Jiang idxd->num_wq_irqs = msixcnt - 1; 115bfe1d560SDave Jiang 116bfe1d560SDave Jiang for (i = 1; i < msixcnt; i++) { 117bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 118bfe1d560SDave Jiang 119bfe1d560SDave Jiang init_llist_head(&idxd->irq_entries[i].pending_llist); 120bfe1d560SDave Jiang INIT_LIST_HEAD(&idxd->irq_entries[i].work_list); 1215fc8e85fSDave Jiang rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler, 1225fc8e85fSDave Jiang idxd_wq_thread, 0, "idxd-portal", irq_entry); 123bfe1d560SDave Jiang if (rc < 0) { 1245fc8e85fSDave Jiang dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector); 1255fc8e85fSDave Jiang goto err_wq_irqs; 126bfe1d560SDave Jiang } 1275fc8e85fSDave Jiang dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector); 128bfe1d560SDave Jiang } 129bfe1d560SDave Jiang 130bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd); 1316df0e6c5SDave Jiang idxd_msix_perm_setup(idxd); 132bfe1d560SDave Jiang return 0; 133bfe1d560SDave Jiang 1345fc8e85fSDave Jiang err_wq_irqs: 1355fc8e85fSDave Jiang while (--i >= 0) { 1365fc8e85fSDave Jiang irq_entry = &idxd->irq_entries[i]; 1375fc8e85fSDave Jiang free_irq(irq_entry->vector, irq_entry); 1385fc8e85fSDave Jiang } 1395fc8e85fSDave Jiang err_misc_irq: 140bfe1d560SDave Jiang /* Disable error interrupt generation */ 141bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 1425fc8e85fSDave Jiang err_irq_entries: 1435fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 144bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n"); 145bfe1d560SDave Jiang return rc; 146bfe1d560SDave Jiang } 147bfe1d560SDave Jiang 1487c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd) 1497c5dd23eSDave Jiang { 1507c5dd23eSDave Jiang struct device *dev = &idxd->pdev->dev; 1517c5dd23eSDave Jiang struct idxd_wq *wq; 1527c5dd23eSDave Jiang int i, rc; 1537c5dd23eSDave Jiang 1547c5dd23eSDave Jiang idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 1557c5dd23eSDave Jiang GFP_KERNEL, dev_to_node(dev)); 1567c5dd23eSDave Jiang if (!idxd->wqs) 1577c5dd23eSDave Jiang return -ENOMEM; 1587c5dd23eSDave Jiang 1597c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 1607c5dd23eSDave Jiang wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); 1617c5dd23eSDave Jiang if (!wq) { 1627c5dd23eSDave Jiang rc = -ENOMEM; 1637c5dd23eSDave Jiang goto err; 1647c5dd23eSDave Jiang } 1657c5dd23eSDave Jiang 1667c5dd23eSDave Jiang wq->id = i; 1677c5dd23eSDave Jiang wq->idxd = idxd; 1687c5dd23eSDave Jiang device_initialize(&wq->conf_dev); 1697c5dd23eSDave Jiang wq->conf_dev.parent = &idxd->conf_dev; 1707c5dd23eSDave Jiang wq->conf_dev.bus = idxd_get_bus_type(idxd); 1717c5dd23eSDave Jiang wq->conf_dev.type = &idxd_wq_device_type; 1727c5dd23eSDave Jiang rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id); 1737c5dd23eSDave Jiang if (rc < 0) { 1747c5dd23eSDave Jiang put_device(&wq->conf_dev); 1757c5dd23eSDave Jiang goto err; 1767c5dd23eSDave Jiang } 1777c5dd23eSDave Jiang 1787c5dd23eSDave Jiang mutex_init(&wq->wq_lock); 1797c5dd23eSDave Jiang wq->idxd_cdev.minor = -1; 1807c5dd23eSDave Jiang wq->max_xfer_bytes = idxd->max_xfer_bytes; 1817c5dd23eSDave Jiang wq->max_batch_size = idxd->max_batch_size; 1827c5dd23eSDave Jiang wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); 1837c5dd23eSDave Jiang if (!wq->wqcfg) { 1847c5dd23eSDave Jiang put_device(&wq->conf_dev); 1857c5dd23eSDave Jiang rc = -ENOMEM; 1867c5dd23eSDave Jiang goto err; 1877c5dd23eSDave Jiang } 1887c5dd23eSDave Jiang idxd->wqs[i] = wq; 1897c5dd23eSDave Jiang } 1907c5dd23eSDave Jiang 1917c5dd23eSDave Jiang return 0; 1927c5dd23eSDave Jiang 1937c5dd23eSDave Jiang err: 1947c5dd23eSDave Jiang while (--i >= 0) 1957c5dd23eSDave Jiang put_device(&idxd->wqs[i]->conf_dev); 1967c5dd23eSDave Jiang return rc; 1977c5dd23eSDave Jiang } 1987c5dd23eSDave Jiang 19975b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd) 20075b91130SDave Jiang { 20175b91130SDave Jiang struct idxd_engine *engine; 20275b91130SDave Jiang struct device *dev = &idxd->pdev->dev; 20375b91130SDave Jiang int i, rc; 20475b91130SDave Jiang 20575b91130SDave Jiang idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), 20675b91130SDave Jiang GFP_KERNEL, dev_to_node(dev)); 20775b91130SDave Jiang if (!idxd->engines) 20875b91130SDave Jiang return -ENOMEM; 20975b91130SDave Jiang 21075b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) { 21175b91130SDave Jiang engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); 21275b91130SDave Jiang if (!engine) { 21375b91130SDave Jiang rc = -ENOMEM; 21475b91130SDave Jiang goto err; 21575b91130SDave Jiang } 21675b91130SDave Jiang 21775b91130SDave Jiang engine->id = i; 21875b91130SDave Jiang engine->idxd = idxd; 21975b91130SDave Jiang device_initialize(&engine->conf_dev); 22075b91130SDave Jiang engine->conf_dev.parent = &idxd->conf_dev; 22175b91130SDave Jiang engine->conf_dev.type = &idxd_engine_device_type; 22275b91130SDave Jiang rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id); 22375b91130SDave Jiang if (rc < 0) { 22475b91130SDave Jiang put_device(&engine->conf_dev); 22575b91130SDave Jiang goto err; 22675b91130SDave Jiang } 22775b91130SDave Jiang 22875b91130SDave Jiang idxd->engines[i] = engine; 22975b91130SDave Jiang } 23075b91130SDave Jiang 23175b91130SDave Jiang return 0; 23275b91130SDave Jiang 23375b91130SDave Jiang err: 23475b91130SDave Jiang while (--i >= 0) 23575b91130SDave Jiang put_device(&idxd->engines[i]->conf_dev); 23675b91130SDave Jiang return rc; 23775b91130SDave Jiang } 23875b91130SDave Jiang 239*defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd) 240*defe49f9SDave Jiang { 241*defe49f9SDave Jiang struct device *dev = &idxd->pdev->dev; 242*defe49f9SDave Jiang struct idxd_group *group; 243*defe49f9SDave Jiang int i, rc; 244*defe49f9SDave Jiang 245*defe49f9SDave Jiang idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), 246*defe49f9SDave Jiang GFP_KERNEL, dev_to_node(dev)); 247*defe49f9SDave Jiang if (!idxd->groups) 248*defe49f9SDave Jiang return -ENOMEM; 249*defe49f9SDave Jiang 250*defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) { 251*defe49f9SDave Jiang group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); 252*defe49f9SDave Jiang if (!group) { 253*defe49f9SDave Jiang rc = -ENOMEM; 254*defe49f9SDave Jiang goto err; 255*defe49f9SDave Jiang } 256*defe49f9SDave Jiang 257*defe49f9SDave Jiang group->id = i; 258*defe49f9SDave Jiang group->idxd = idxd; 259*defe49f9SDave Jiang device_initialize(&group->conf_dev); 260*defe49f9SDave Jiang group->conf_dev.parent = &idxd->conf_dev; 261*defe49f9SDave Jiang group->conf_dev.bus = idxd_get_bus_type(idxd); 262*defe49f9SDave Jiang group->conf_dev.type = &idxd_group_device_type; 263*defe49f9SDave Jiang rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id); 264*defe49f9SDave Jiang if (rc < 0) { 265*defe49f9SDave Jiang put_device(&group->conf_dev); 266*defe49f9SDave Jiang goto err; 267*defe49f9SDave Jiang } 268*defe49f9SDave Jiang 269*defe49f9SDave Jiang idxd->groups[i] = group; 270*defe49f9SDave Jiang group->tc_a = -1; 271*defe49f9SDave Jiang group->tc_b = -1; 272*defe49f9SDave Jiang } 273*defe49f9SDave Jiang 274*defe49f9SDave Jiang return 0; 275*defe49f9SDave Jiang 276*defe49f9SDave Jiang err: 277*defe49f9SDave Jiang while (--i >= 0) 278*defe49f9SDave Jiang put_device(&idxd->groups[i]->conf_dev); 279*defe49f9SDave Jiang return rc; 280*defe49f9SDave Jiang } 281*defe49f9SDave Jiang 282bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd) 283bfe1d560SDave Jiang { 284bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 285*defe49f9SDave Jiang int rc, i; 286bfe1d560SDave Jiang 2870d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq); 2887c5dd23eSDave Jiang 2897c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd); 2907c5dd23eSDave Jiang if (rc < 0) 2917c5dd23eSDave Jiang return rc; 2927c5dd23eSDave Jiang 29375b91130SDave Jiang rc = idxd_setup_engines(idxd); 29475b91130SDave Jiang if (rc < 0) 29575b91130SDave Jiang goto err_engine; 29675b91130SDave Jiang 297*defe49f9SDave Jiang rc = idxd_setup_groups(idxd); 298*defe49f9SDave Jiang if (rc < 0) 299*defe49f9SDave Jiang goto err_group; 300bfe1d560SDave Jiang 3010d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 3027c5dd23eSDave Jiang if (!idxd->wq) { 3037c5dd23eSDave Jiang rc = -ENOMEM; 304*defe49f9SDave Jiang goto err_wkq_create; 3057c5dd23eSDave Jiang } 3060d5c10b4SDave Jiang 307bfe1d560SDave Jiang return 0; 3087c5dd23eSDave Jiang 309*defe49f9SDave Jiang err_wkq_create: 310*defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) 311*defe49f9SDave Jiang put_device(&idxd->groups[i]->conf_dev); 312*defe49f9SDave Jiang err_group: 31375b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) 31475b91130SDave Jiang put_device(&idxd->engines[i]->conf_dev); 31575b91130SDave Jiang err_engine: 3167c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) 3177c5dd23eSDave Jiang put_device(&idxd->wqs[i]->conf_dev); 3187c5dd23eSDave Jiang return rc; 319bfe1d560SDave Jiang } 320bfe1d560SDave Jiang 321bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 322bfe1d560SDave Jiang { 323bfe1d560SDave Jiang union offsets_reg offsets; 324bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 325bfe1d560SDave Jiang 326bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 3272f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 3282f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 329bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 3302f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 3312f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 3322f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 3332f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 3342f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 335bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 336bfe1d560SDave Jiang } 337bfe1d560SDave Jiang 338bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 339bfe1d560SDave Jiang { 340bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 341bfe1d560SDave Jiang int i; 342bfe1d560SDave Jiang 343bfe1d560SDave Jiang /* reading generic capabilities */ 344bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 345bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 346bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 347bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 348bfe1d560SDave Jiang idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift; 349bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 350bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 351bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 352bfe1d560SDave Jiang 353bfe1d560SDave Jiang /* reading group capabilities */ 354bfe1d560SDave Jiang idxd->hw.group_cap.bits = 355bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 356bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 357bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 358bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 359bfe1d560SDave Jiang idxd->max_tokens = idxd->hw.group_cap.total_tokens; 360bfe1d560SDave Jiang dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens); 361c52ca478SDave Jiang idxd->nr_tokens = idxd->max_tokens; 362bfe1d560SDave Jiang 363bfe1d560SDave Jiang /* read engine capabilities */ 364bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 365bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 366bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 367bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 368bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 369bfe1d560SDave Jiang 370bfe1d560SDave Jiang /* read workqueue capabilities */ 371bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 372bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 373bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 374bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 375bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 376bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 377d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 378d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 379bfe1d560SDave Jiang 380bfe1d560SDave Jiang /* reading operation capabilities */ 381bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 382bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 383bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 384bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 385bfe1d560SDave Jiang } 386bfe1d560SDave Jiang } 387bfe1d560SDave Jiang 38847c16ac2SDave Jiang static inline void idxd_set_type(struct idxd_device *idxd) 38947c16ac2SDave Jiang { 39047c16ac2SDave Jiang struct pci_dev *pdev = idxd->pdev; 39147c16ac2SDave Jiang 39247c16ac2SDave Jiang if (pdev->device == PCI_DEVICE_ID_INTEL_DSA_SPR0) 39347c16ac2SDave Jiang idxd->type = IDXD_TYPE_DSA; 39447c16ac2SDave Jiang else if (pdev->device == PCI_DEVICE_ID_INTEL_IAX_SPR0) 39547c16ac2SDave Jiang idxd->type = IDXD_TYPE_IAX; 39647c16ac2SDave Jiang else 39747c16ac2SDave Jiang idxd->type = IDXD_TYPE_UNKNOWN; 39847c16ac2SDave Jiang } 39947c16ac2SDave Jiang 4008e50d392SDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev) 401bfe1d560SDave Jiang { 402bfe1d560SDave Jiang struct device *dev = &pdev->dev; 403bfe1d560SDave Jiang struct idxd_device *idxd; 40447c16ac2SDave Jiang int rc; 405bfe1d560SDave Jiang 40647c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 407bfe1d560SDave Jiang if (!idxd) 408bfe1d560SDave Jiang return NULL; 409bfe1d560SDave Jiang 410bfe1d560SDave Jiang idxd->pdev = pdev; 41147c16ac2SDave Jiang idxd_set_type(idxd); 41247c16ac2SDave Jiang idxd->id = ida_alloc(idxd_ida(idxd), GFP_KERNEL); 41347c16ac2SDave Jiang if (idxd->id < 0) 41447c16ac2SDave Jiang return NULL; 41547c16ac2SDave Jiang 41647c16ac2SDave Jiang device_initialize(&idxd->conf_dev); 41747c16ac2SDave Jiang idxd->conf_dev.parent = dev; 41847c16ac2SDave Jiang idxd->conf_dev.bus = idxd_get_bus_type(idxd); 41947c16ac2SDave Jiang idxd->conf_dev.type = idxd_get_device_type(idxd); 42047c16ac2SDave Jiang rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd_get_dev_name(idxd), idxd->id); 42147c16ac2SDave Jiang if (rc < 0) { 42247c16ac2SDave Jiang put_device(&idxd->conf_dev); 42347c16ac2SDave Jiang return NULL; 42447c16ac2SDave Jiang } 42547c16ac2SDave Jiang 426bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 427bfe1d560SDave Jiang 428bfe1d560SDave Jiang return idxd; 429bfe1d560SDave Jiang } 430bfe1d560SDave Jiang 4318e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 4328e50d392SDave Jiang { 4338e50d392SDave Jiang int flags; 4348e50d392SDave Jiang unsigned int pasid; 4358e50d392SDave Jiang struct iommu_sva *sva; 4368e50d392SDave Jiang 4378e50d392SDave Jiang flags = SVM_FLAG_SUPERVISOR_MODE; 4388e50d392SDave Jiang 4398e50d392SDave Jiang sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags); 4408e50d392SDave Jiang if (IS_ERR(sva)) { 4418e50d392SDave Jiang dev_warn(&idxd->pdev->dev, 4428e50d392SDave Jiang "iommu sva bind failed: %ld\n", PTR_ERR(sva)); 4438e50d392SDave Jiang return PTR_ERR(sva); 4448e50d392SDave Jiang } 4458e50d392SDave Jiang 4468e50d392SDave Jiang pasid = iommu_sva_get_pasid(sva); 4478e50d392SDave Jiang if (pasid == IOMMU_PASID_INVALID) { 4488e50d392SDave Jiang iommu_sva_unbind_device(sva); 4498e50d392SDave Jiang return -ENODEV; 4508e50d392SDave Jiang } 4518e50d392SDave Jiang 4528e50d392SDave Jiang idxd->sva = sva; 4538e50d392SDave Jiang idxd->pasid = pasid; 4548e50d392SDave Jiang dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid); 4558e50d392SDave Jiang return 0; 4568e50d392SDave Jiang } 4578e50d392SDave Jiang 4588e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 4598e50d392SDave Jiang { 4608e50d392SDave Jiang 4618e50d392SDave Jiang iommu_sva_unbind_device(idxd->sva); 4628e50d392SDave Jiang idxd->sva = NULL; 4638e50d392SDave Jiang } 4648e50d392SDave Jiang 465bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 466bfe1d560SDave Jiang { 467bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 468bfe1d560SDave Jiang struct device *dev = &pdev->dev; 469bfe1d560SDave Jiang int rc; 470bfe1d560SDave Jiang 471bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 47289e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 47389e3becdSDave Jiang if (rc < 0) 47489e3becdSDave Jiang return rc; 47589e3becdSDave Jiang 476bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 477bfe1d560SDave Jiang 47803d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 4798e50d392SDave Jiang rc = idxd_enable_system_pasid(idxd); 4808e50d392SDave Jiang if (rc < 0) 4818e50d392SDave Jiang dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); 4828e50d392SDave Jiang else 4838e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 48403d939c7SDave Jiang } else if (!sva) { 48503d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n"); 4868e50d392SDave Jiang } 4878e50d392SDave Jiang 488bfe1d560SDave Jiang idxd_read_caps(idxd); 489bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 490bfe1d560SDave Jiang 491bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 492bfe1d560SDave Jiang if (rc) 4937c5dd23eSDave Jiang goto err; 494bfe1d560SDave Jiang 495bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 496bfe1d560SDave Jiang if (rc) 4977c5dd23eSDave Jiang goto err; 498bfe1d560SDave Jiang 499bfe1d560SDave Jiang dev_dbg(dev, "IDXD interrupt setup complete.\n"); 500bfe1d560SDave Jiang 50142d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 50242d279f9SDave Jiang 503bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 504bfe1d560SDave Jiang return 0; 505bfe1d560SDave Jiang 5067c5dd23eSDave Jiang err: 5078e50d392SDave Jiang if (device_pasid_enabled(idxd)) 5088e50d392SDave Jiang idxd_disable_system_pasid(idxd); 509bfe1d560SDave Jiang return rc; 510bfe1d560SDave Jiang } 511bfe1d560SDave Jiang 512f25b4638SDave Jiang static void idxd_type_init(struct idxd_device *idxd) 513f25b4638SDave Jiang { 514f25b4638SDave Jiang if (idxd->type == IDXD_TYPE_DSA) 515f25b4638SDave Jiang idxd->compl_size = sizeof(struct dsa_completion_record); 516f25b4638SDave Jiang else if (idxd->type == IDXD_TYPE_IAX) 517f25b4638SDave Jiang idxd->compl_size = sizeof(struct iax_completion_record); 518f25b4638SDave Jiang } 519f25b4638SDave Jiang 520bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 521bfe1d560SDave Jiang { 522bfe1d560SDave Jiang struct device *dev = &pdev->dev; 523bfe1d560SDave Jiang struct idxd_device *idxd; 524bfe1d560SDave Jiang int rc; 525bfe1d560SDave Jiang 526a39c7cd0SDave Jiang rc = pci_enable_device(pdev); 527bfe1d560SDave Jiang if (rc) 528bfe1d560SDave Jiang return rc; 529bfe1d560SDave Jiang 5308e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 5318e50d392SDave Jiang idxd = idxd_alloc(pdev); 532a39c7cd0SDave Jiang if (!idxd) { 533a39c7cd0SDave Jiang rc = -ENOMEM; 534a39c7cd0SDave Jiang goto err_idxd_alloc; 535a39c7cd0SDave Jiang } 536bfe1d560SDave Jiang 5378e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 538a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 539a39c7cd0SDave Jiang if (!idxd->reg_base) { 540a39c7cd0SDave Jiang rc = -ENOMEM; 541a39c7cd0SDave Jiang goto err_iomap; 542a39c7cd0SDave Jiang } 543bfe1d560SDave Jiang 544bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 545bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 546bfe1d560SDave Jiang if (rc) 547bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 548bfe1d560SDave Jiang if (rc) 549a39c7cd0SDave Jiang goto err; 550bfe1d560SDave Jiang 551bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 552bfe1d560SDave Jiang if (rc) 553bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 554bfe1d560SDave Jiang if (rc) 555a39c7cd0SDave Jiang goto err; 556bfe1d560SDave Jiang 557bfe1d560SDave Jiang 558f25b4638SDave Jiang idxd_type_init(idxd); 559f25b4638SDave Jiang 560bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 561bfe1d560SDave Jiang pci_set_master(pdev); 562bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 563bfe1d560SDave Jiang 564bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 565bfe1d560SDave Jiang rc = idxd_probe(idxd); 566bfe1d560SDave Jiang if (rc) { 567bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 568a39c7cd0SDave Jiang goto err; 569bfe1d560SDave Jiang } 570bfe1d560SDave Jiang 57147c16ac2SDave Jiang rc = idxd_register_devices(idxd); 572c52ca478SDave Jiang if (rc) { 573c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 574a39c7cd0SDave Jiang goto err; 575c52ca478SDave Jiang } 576c52ca478SDave Jiang 577c52ca478SDave Jiang idxd->state = IDXD_DEV_CONF_READY; 578c52ca478SDave Jiang 579bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 580bfe1d560SDave Jiang idxd->hw.version); 581bfe1d560SDave Jiang 582bfe1d560SDave Jiang return 0; 583a39c7cd0SDave Jiang 584a39c7cd0SDave Jiang err: 585a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 586a39c7cd0SDave Jiang err_iomap: 58747c16ac2SDave Jiang put_device(&idxd->conf_dev); 588a39c7cd0SDave Jiang err_idxd_alloc: 589a39c7cd0SDave Jiang pci_disable_device(pdev); 590a39c7cd0SDave Jiang return rc; 591bfe1d560SDave Jiang } 592bfe1d560SDave Jiang 5938f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) 5948f47d1a5SDave Jiang { 5958f47d1a5SDave Jiang struct idxd_desc *desc, *itr; 5968f47d1a5SDave Jiang struct llist_node *head; 5978f47d1a5SDave Jiang 5988f47d1a5SDave Jiang head = llist_del_all(&ie->pending_llist); 5998f47d1a5SDave Jiang if (!head) 6008f47d1a5SDave Jiang return; 6018f47d1a5SDave Jiang 6028f47d1a5SDave Jiang llist_for_each_entry_safe(desc, itr, head, llnode) { 6038f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 6048f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 6058f47d1a5SDave Jiang } 6068f47d1a5SDave Jiang } 6078f47d1a5SDave Jiang 6088f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie) 6098f47d1a5SDave Jiang { 6108f47d1a5SDave Jiang struct idxd_desc *desc, *iter; 6118f47d1a5SDave Jiang 6128f47d1a5SDave Jiang list_for_each_entry_safe(desc, iter, &ie->work_list, list) { 6138f47d1a5SDave Jiang list_del(&desc->list); 6148f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 6158f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 6168f47d1a5SDave Jiang } 6178f47d1a5SDave Jiang } 6188f47d1a5SDave Jiang 619bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 620bfe1d560SDave Jiang { 621bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 622bfe1d560SDave Jiang int rc, i; 623bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 624bfe1d560SDave Jiang int msixcnt = pci_msix_vec_count(pdev); 625bfe1d560SDave Jiang 626bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 627bfe1d560SDave Jiang if (rc) 628bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 629bfe1d560SDave Jiang 630bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 631bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 632bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 633bfe1d560SDave Jiang 634bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 635bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 6365fc8e85fSDave Jiang synchronize_irq(irq_entry->vector); 6375fc8e85fSDave Jiang free_irq(irq_entry->vector, irq_entry); 638bfe1d560SDave Jiang if (i == 0) 639bfe1d560SDave Jiang continue; 6408f47d1a5SDave Jiang idxd_flush_pending_llist(irq_entry); 6418f47d1a5SDave Jiang idxd_flush_work_list(irq_entry); 642bfe1d560SDave Jiang } 6430d5c10b4SDave Jiang 6446df0e6c5SDave Jiang idxd_msix_perm_clear(idxd); 6455fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 646a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 647a39c7cd0SDave Jiang pci_disable_device(pdev); 6480d5c10b4SDave Jiang destroy_workqueue(idxd->wq); 649bfe1d560SDave Jiang } 650bfe1d560SDave Jiang 651bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 652bfe1d560SDave Jiang { 653bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 654bfe1d560SDave Jiang 655bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 656bfe1d560SDave Jiang idxd_shutdown(pdev); 6578e50d392SDave Jiang if (device_pasid_enabled(idxd)) 6588e50d392SDave Jiang idxd_disable_system_pasid(idxd); 65947c16ac2SDave Jiang idxd_unregister_devices(idxd); 660bfe1d560SDave Jiang } 661bfe1d560SDave Jiang 662bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 663bfe1d560SDave Jiang .name = DRV_NAME, 664bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 665bfe1d560SDave Jiang .probe = idxd_pci_probe, 666bfe1d560SDave Jiang .remove = idxd_remove, 667bfe1d560SDave Jiang .shutdown = idxd_shutdown, 668bfe1d560SDave Jiang }; 669bfe1d560SDave Jiang 670bfe1d560SDave Jiang static int __init idxd_init_module(void) 671bfe1d560SDave Jiang { 672bfe1d560SDave Jiang int err, i; 673bfe1d560SDave Jiang 674bfe1d560SDave Jiang /* 6758e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 676bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 677bfe1d560SDave Jiang */ 678bfe1d560SDave Jiang if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) { 679bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 680bfe1d560SDave Jiang return -ENODEV; 681bfe1d560SDave Jiang } 682bfe1d560SDave Jiang 6838e50d392SDave Jiang if (!boot_cpu_has(X86_FEATURE_ENQCMD)) 6848e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 6858e50d392SDave Jiang else 6868e50d392SDave Jiang support_enqcmd = true; 687bfe1d560SDave Jiang 688bfe1d560SDave Jiang for (i = 0; i < IDXD_TYPE_MAX; i++) 689f7f77398SDave Jiang ida_init(&idxd_idas[i]); 690bfe1d560SDave Jiang 691c52ca478SDave Jiang err = idxd_register_bus_type(); 692c52ca478SDave Jiang if (err < 0) 693bfe1d560SDave Jiang return err; 694bfe1d560SDave Jiang 695c52ca478SDave Jiang err = idxd_register_driver(); 696c52ca478SDave Jiang if (err < 0) 697c52ca478SDave Jiang goto err_idxd_driver_register; 698c52ca478SDave Jiang 69942d279f9SDave Jiang err = idxd_cdev_register(); 70042d279f9SDave Jiang if (err) 70142d279f9SDave Jiang goto err_cdev_register; 70242d279f9SDave Jiang 703c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 704c52ca478SDave Jiang if (err) 705c52ca478SDave Jiang goto err_pci_register; 706c52ca478SDave Jiang 707bfe1d560SDave Jiang return 0; 708c52ca478SDave Jiang 709c52ca478SDave Jiang err_pci_register: 71042d279f9SDave Jiang idxd_cdev_remove(); 71142d279f9SDave Jiang err_cdev_register: 712c52ca478SDave Jiang idxd_unregister_driver(); 713c52ca478SDave Jiang err_idxd_driver_register: 714c52ca478SDave Jiang idxd_unregister_bus_type(); 715c52ca478SDave Jiang return err; 716bfe1d560SDave Jiang } 717bfe1d560SDave Jiang module_init(idxd_init_module); 718bfe1d560SDave Jiang 719bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 720bfe1d560SDave Jiang { 721bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 72242d279f9SDave Jiang idxd_cdev_remove(); 723c52ca478SDave Jiang idxd_unregister_bus_type(); 724bfe1d560SDave Jiang } 725bfe1d560SDave Jiang module_exit(idxd_exit_module); 726