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" 240bde4444STom Zanussi #include "perfmon.h" 25bfe1d560SDave Jiang 26bfe1d560SDave Jiang MODULE_VERSION(IDXD_DRIVER_VERSION); 27bfe1d560SDave Jiang MODULE_LICENSE("GPL v2"); 28bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation"); 29bfe1d560SDave Jiang 3003d939c7SDave Jiang static bool sva = true; 3103d939c7SDave Jiang module_param(sva, bool, 0644); 3203d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off"); 3303d939c7SDave Jiang 34bfe1d560SDave Jiang #define DRV_NAME "idxd" 35bfe1d560SDave Jiang 368e50d392SDave Jiang bool support_enqcmd; 374b73e4ebSDave Jiang DEFINE_IDA(idxd_ida); 38bfe1d560SDave Jiang 39435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = { 40435b512dSDave Jiang [IDXD_TYPE_DSA] = { 41435b512dSDave Jiang .name_prefix = "dsa", 42435b512dSDave Jiang .type = IDXD_TYPE_DSA, 43435b512dSDave Jiang .compl_size = sizeof(struct dsa_completion_record), 44435b512dSDave Jiang .align = 32, 45435b512dSDave Jiang .dev_type = &dsa_device_type, 46435b512dSDave Jiang }, 47435b512dSDave Jiang [IDXD_TYPE_IAX] = { 48435b512dSDave Jiang .name_prefix = "iax", 49435b512dSDave Jiang .type = IDXD_TYPE_IAX, 50435b512dSDave Jiang .compl_size = sizeof(struct iax_completion_record), 51435b512dSDave Jiang .align = 64, 52435b512dSDave Jiang .dev_type = &iax_device_type, 53435b512dSDave Jiang }, 54435b512dSDave Jiang }; 55435b512dSDave Jiang 56bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = { 57bfe1d560SDave Jiang /* DSA ver 1.0 platforms */ 58435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) }, 59f25b4638SDave Jiang 60f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 61435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) }, 62bfe1d560SDave Jiang { 0, } 63bfe1d560SDave Jiang }; 64bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 65bfe1d560SDave Jiang 66bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd) 67bfe1d560SDave Jiang { 68bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 69bfe1d560SDave Jiang struct device *dev = &pdev->dev; 70bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 71bfe1d560SDave Jiang int i, msixcnt; 72bfe1d560SDave Jiang int rc = 0; 73bfe1d560SDave Jiang 74bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev); 75bfe1d560SDave Jiang if (msixcnt < 0) { 76bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n"); 775fc8e85fSDave Jiang return -ENOSPC; 78bfe1d560SDave Jiang } 79bfe1d560SDave Jiang 805fc8e85fSDave Jiang rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); 815fc8e85fSDave Jiang if (rc != msixcnt) { 825fc8e85fSDave Jiang dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); 835fc8e85fSDave Jiang return -ENOSPC; 84bfe1d560SDave Jiang } 85bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 86bfe1d560SDave Jiang 87bfe1d560SDave Jiang /* 88bfe1d560SDave Jiang * We implement 1 completion list per MSI-X entry except for 89bfe1d560SDave Jiang * entry 0, which is for errors and others. 90bfe1d560SDave Jiang */ 9147c16ac2SDave Jiang idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry), 9247c16ac2SDave Jiang GFP_KERNEL, dev_to_node(dev)); 93bfe1d560SDave Jiang if (!idxd->irq_entries) { 94bfe1d560SDave Jiang rc = -ENOMEM; 955fc8e85fSDave Jiang goto err_irq_entries; 96bfe1d560SDave Jiang } 97bfe1d560SDave Jiang 98bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 99bfe1d560SDave Jiang idxd->irq_entries[i].id = i; 100bfe1d560SDave Jiang idxd->irq_entries[i].idxd = idxd; 1015fc8e85fSDave Jiang idxd->irq_entries[i].vector = pci_irq_vector(pdev, i); 102e4f4d8cdSDave Jiang spin_lock_init(&idxd->irq_entries[i].list_lock); 103bfe1d560SDave Jiang } 104bfe1d560SDave Jiang 105bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[0]; 106a1610461SDave Jiang rc = request_threaded_irq(irq_entry->vector, NULL, idxd_misc_thread, 1075fc8e85fSDave Jiang 0, "idxd-misc", irq_entry); 108bfe1d560SDave Jiang if (rc < 0) { 109bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n"); 1105fc8e85fSDave Jiang goto err_misc_irq; 111bfe1d560SDave Jiang } 112bfe1d560SDave Jiang 1135fc8e85fSDave Jiang dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector); 114bfe1d560SDave Jiang 115bfe1d560SDave Jiang /* first MSI-X entry is not for wq interrupts */ 116bfe1d560SDave Jiang idxd->num_wq_irqs = msixcnt - 1; 117bfe1d560SDave Jiang 118bfe1d560SDave Jiang for (i = 1; i < msixcnt; i++) { 119bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 120bfe1d560SDave Jiang 121bfe1d560SDave Jiang init_llist_head(&idxd->irq_entries[i].pending_llist); 122bfe1d560SDave Jiang INIT_LIST_HEAD(&idxd->irq_entries[i].work_list); 123a1610461SDave Jiang rc = request_threaded_irq(irq_entry->vector, NULL, 1245fc8e85fSDave Jiang idxd_wq_thread, 0, "idxd-portal", irq_entry); 125bfe1d560SDave Jiang if (rc < 0) { 1265fc8e85fSDave Jiang dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector); 1275fc8e85fSDave Jiang goto err_wq_irqs; 128bfe1d560SDave Jiang } 129eb15e715SDave Jiang 1305fc8e85fSDave Jiang dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector); 131eb15e715SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) { 132eb15e715SDave Jiang /* 133eb15e715SDave Jiang * The MSIX vector enumeration starts at 1 with vector 0 being the 134eb15e715SDave Jiang * misc interrupt that handles non I/O completion events. The 135eb15e715SDave Jiang * interrupt handles are for IMS enumeration on guest. The misc 136eb15e715SDave Jiang * interrupt vector does not require a handle and therefore we start 137eb15e715SDave Jiang * the int_handles at index 0. Since 'i' starts at 1, the first 138eb15e715SDave Jiang * int_handles index will be 0. 139eb15e715SDave Jiang */ 140eb15e715SDave Jiang rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1], 141eb15e715SDave Jiang IDXD_IRQ_MSIX); 142eb15e715SDave Jiang if (rc < 0) { 143eb15e715SDave Jiang free_irq(irq_entry->vector, irq_entry); 144eb15e715SDave Jiang goto err_wq_irqs; 145eb15e715SDave Jiang } 146eb15e715SDave Jiang dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]); 147eb15e715SDave Jiang } 148bfe1d560SDave Jiang } 149bfe1d560SDave Jiang 150bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd); 1516df0e6c5SDave Jiang idxd_msix_perm_setup(idxd); 152bfe1d560SDave Jiang return 0; 153bfe1d560SDave Jiang 1545fc8e85fSDave Jiang err_wq_irqs: 1555fc8e85fSDave Jiang while (--i >= 0) { 1565fc8e85fSDave Jiang irq_entry = &idxd->irq_entries[i]; 1575fc8e85fSDave Jiang free_irq(irq_entry->vector, irq_entry); 158eb15e715SDave Jiang if (i != 0) 159eb15e715SDave Jiang idxd_device_release_int_handle(idxd, 160eb15e715SDave Jiang idxd->int_handles[i], IDXD_IRQ_MSIX); 1615fc8e85fSDave Jiang } 1625fc8e85fSDave Jiang err_misc_irq: 163bfe1d560SDave Jiang /* Disable error interrupt generation */ 164bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 1655fc8e85fSDave Jiang err_irq_entries: 1665fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 167bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n"); 168bfe1d560SDave Jiang return rc; 169bfe1d560SDave Jiang } 170bfe1d560SDave Jiang 171ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd) 172ddf742d4SDave Jiang { 173ddf742d4SDave Jiang struct pci_dev *pdev = idxd->pdev; 174ddf742d4SDave Jiang struct idxd_irq_entry *irq_entry; 175ddf742d4SDave Jiang int i, msixcnt; 176ddf742d4SDave Jiang 177ddf742d4SDave Jiang msixcnt = pci_msix_vec_count(pdev); 178ddf742d4SDave Jiang if (msixcnt <= 0) 179ddf742d4SDave Jiang return; 180ddf742d4SDave Jiang 181ddf742d4SDave Jiang irq_entry = &idxd->irq_entries[0]; 182ddf742d4SDave Jiang free_irq(irq_entry->vector, irq_entry); 183ddf742d4SDave Jiang 184ddf742d4SDave Jiang for (i = 1; i < msixcnt; i++) { 185ddf742d4SDave Jiang 186ddf742d4SDave Jiang irq_entry = &idxd->irq_entries[i]; 187ddf742d4SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) 188ddf742d4SDave Jiang idxd_device_release_int_handle(idxd, idxd->int_handles[i], 189ddf742d4SDave Jiang IDXD_IRQ_MSIX); 190ddf742d4SDave Jiang free_irq(irq_entry->vector, irq_entry); 191ddf742d4SDave Jiang } 192ddf742d4SDave Jiang 193ddf742d4SDave Jiang idxd_mask_error_interrupts(idxd); 194ddf742d4SDave Jiang pci_free_irq_vectors(pdev); 195ddf742d4SDave Jiang } 196ddf742d4SDave Jiang 1977c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd) 1987c5dd23eSDave Jiang { 1997c5dd23eSDave Jiang struct device *dev = &idxd->pdev->dev; 2007c5dd23eSDave Jiang struct idxd_wq *wq; 2017c5dd23eSDave Jiang int i, rc; 2027c5dd23eSDave Jiang 2037c5dd23eSDave Jiang idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 2047c5dd23eSDave Jiang GFP_KERNEL, dev_to_node(dev)); 2057c5dd23eSDave Jiang if (!idxd->wqs) 2067c5dd23eSDave Jiang return -ENOMEM; 2077c5dd23eSDave Jiang 2087c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 2097c5dd23eSDave Jiang wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); 2107c5dd23eSDave Jiang if (!wq) { 2117c5dd23eSDave Jiang rc = -ENOMEM; 2127c5dd23eSDave Jiang goto err; 2137c5dd23eSDave Jiang } 2147c5dd23eSDave Jiang 2157c5dd23eSDave Jiang wq->id = i; 2167c5dd23eSDave Jiang wq->idxd = idxd; 2177c5dd23eSDave Jiang device_initialize(&wq->conf_dev); 2187c5dd23eSDave Jiang wq->conf_dev.parent = &idxd->conf_dev; 2194b73e4ebSDave Jiang wq->conf_dev.bus = &dsa_bus_type; 2207c5dd23eSDave Jiang wq->conf_dev.type = &idxd_wq_device_type; 2217c5dd23eSDave Jiang rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id); 2227c5dd23eSDave Jiang if (rc < 0) { 2237c5dd23eSDave Jiang put_device(&wq->conf_dev); 2247c5dd23eSDave Jiang goto err; 2257c5dd23eSDave Jiang } 2267c5dd23eSDave Jiang 2277c5dd23eSDave Jiang mutex_init(&wq->wq_lock); 22804922b74SDave Jiang init_waitqueue_head(&wq->err_queue); 22993a40a6dSDave Jiang init_completion(&wq->wq_dead); 2307c5dd23eSDave Jiang wq->max_xfer_bytes = idxd->max_xfer_bytes; 2317c5dd23eSDave Jiang wq->max_batch_size = idxd->max_batch_size; 2327c5dd23eSDave Jiang wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); 2337c5dd23eSDave Jiang if (!wq->wqcfg) { 2347c5dd23eSDave Jiang put_device(&wq->conf_dev); 2357c5dd23eSDave Jiang rc = -ENOMEM; 2367c5dd23eSDave Jiang goto err; 2377c5dd23eSDave Jiang } 2387c5dd23eSDave Jiang idxd->wqs[i] = wq; 2397c5dd23eSDave Jiang } 2407c5dd23eSDave Jiang 2417c5dd23eSDave Jiang return 0; 2427c5dd23eSDave Jiang 2437c5dd23eSDave Jiang err: 2447c5dd23eSDave Jiang while (--i >= 0) 2457c5dd23eSDave Jiang put_device(&idxd->wqs[i]->conf_dev); 2467c5dd23eSDave Jiang return rc; 2477c5dd23eSDave Jiang } 2487c5dd23eSDave Jiang 24975b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd) 25075b91130SDave Jiang { 25175b91130SDave Jiang struct idxd_engine *engine; 25275b91130SDave Jiang struct device *dev = &idxd->pdev->dev; 25375b91130SDave Jiang int i, rc; 25475b91130SDave Jiang 25575b91130SDave Jiang idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), 25675b91130SDave Jiang GFP_KERNEL, dev_to_node(dev)); 25775b91130SDave Jiang if (!idxd->engines) 25875b91130SDave Jiang return -ENOMEM; 25975b91130SDave Jiang 26075b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) { 26175b91130SDave Jiang engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); 26275b91130SDave Jiang if (!engine) { 26375b91130SDave Jiang rc = -ENOMEM; 26475b91130SDave Jiang goto err; 26575b91130SDave Jiang } 26675b91130SDave Jiang 26775b91130SDave Jiang engine->id = i; 26875b91130SDave Jiang engine->idxd = idxd; 26975b91130SDave Jiang device_initialize(&engine->conf_dev); 27075b91130SDave Jiang engine->conf_dev.parent = &idxd->conf_dev; 2711c4841ccSDave Jiang engine->conf_dev.bus = &dsa_bus_type; 27275b91130SDave Jiang engine->conf_dev.type = &idxd_engine_device_type; 27375b91130SDave Jiang rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id); 27475b91130SDave Jiang if (rc < 0) { 27575b91130SDave Jiang put_device(&engine->conf_dev); 27675b91130SDave Jiang goto err; 27775b91130SDave Jiang } 27875b91130SDave Jiang 27975b91130SDave Jiang idxd->engines[i] = engine; 28075b91130SDave Jiang } 28175b91130SDave Jiang 28275b91130SDave Jiang return 0; 28375b91130SDave Jiang 28475b91130SDave Jiang err: 28575b91130SDave Jiang while (--i >= 0) 28675b91130SDave Jiang put_device(&idxd->engines[i]->conf_dev); 28775b91130SDave Jiang return rc; 28875b91130SDave Jiang } 28975b91130SDave Jiang 290defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd) 291defe49f9SDave Jiang { 292defe49f9SDave Jiang struct device *dev = &idxd->pdev->dev; 293defe49f9SDave Jiang struct idxd_group *group; 294defe49f9SDave Jiang int i, rc; 295defe49f9SDave Jiang 296defe49f9SDave Jiang idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), 297defe49f9SDave Jiang GFP_KERNEL, dev_to_node(dev)); 298defe49f9SDave Jiang if (!idxd->groups) 299defe49f9SDave Jiang return -ENOMEM; 300defe49f9SDave Jiang 301defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) { 302defe49f9SDave Jiang group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); 303defe49f9SDave Jiang if (!group) { 304defe49f9SDave Jiang rc = -ENOMEM; 305defe49f9SDave Jiang goto err; 306defe49f9SDave Jiang } 307defe49f9SDave Jiang 308defe49f9SDave Jiang group->id = i; 309defe49f9SDave Jiang group->idxd = idxd; 310defe49f9SDave Jiang device_initialize(&group->conf_dev); 311defe49f9SDave Jiang group->conf_dev.parent = &idxd->conf_dev; 3124b73e4ebSDave Jiang group->conf_dev.bus = &dsa_bus_type; 313defe49f9SDave Jiang group->conf_dev.type = &idxd_group_device_type; 314defe49f9SDave Jiang rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id); 315defe49f9SDave Jiang if (rc < 0) { 316defe49f9SDave Jiang put_device(&group->conf_dev); 317defe49f9SDave Jiang goto err; 318defe49f9SDave Jiang } 319defe49f9SDave Jiang 320defe49f9SDave Jiang idxd->groups[i] = group; 321defe49f9SDave Jiang group->tc_a = -1; 322defe49f9SDave Jiang group->tc_b = -1; 323defe49f9SDave Jiang } 324defe49f9SDave Jiang 325defe49f9SDave Jiang return 0; 326defe49f9SDave Jiang 327defe49f9SDave Jiang err: 328defe49f9SDave Jiang while (--i >= 0) 329defe49f9SDave Jiang put_device(&idxd->groups[i]->conf_dev); 330defe49f9SDave Jiang return rc; 331defe49f9SDave Jiang } 332defe49f9SDave Jiang 333ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd) 334ddf742d4SDave Jiang { 335ddf742d4SDave Jiang int i; 336ddf742d4SDave Jiang 337ddf742d4SDave Jiang for (i = 0; i < idxd->max_groups; i++) 338ddf742d4SDave Jiang put_device(&idxd->groups[i]->conf_dev); 339ddf742d4SDave Jiang for (i = 0; i < idxd->max_engines; i++) 340ddf742d4SDave Jiang put_device(&idxd->engines[i]->conf_dev); 341ddf742d4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) 342ddf742d4SDave Jiang put_device(&idxd->wqs[i]->conf_dev); 343ddf742d4SDave Jiang destroy_workqueue(idxd->wq); 344ddf742d4SDave Jiang } 345ddf742d4SDave Jiang 346bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd) 347bfe1d560SDave Jiang { 348bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 349defe49f9SDave Jiang int rc, i; 350bfe1d560SDave Jiang 3510d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq); 3527c5dd23eSDave Jiang 353eb15e715SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) { 35433f9f3c3SDave Jiang idxd->int_handles = kcalloc_node(idxd->max_wqs, sizeof(int), GFP_KERNEL, 35533f9f3c3SDave Jiang dev_to_node(dev)); 356eb15e715SDave Jiang if (!idxd->int_handles) 357eb15e715SDave Jiang return -ENOMEM; 358eb15e715SDave Jiang } 359eb15e715SDave Jiang 3607c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd); 3617c5dd23eSDave Jiang if (rc < 0) 362eb15e715SDave Jiang goto err_wqs; 3637c5dd23eSDave Jiang 36475b91130SDave Jiang rc = idxd_setup_engines(idxd); 36575b91130SDave Jiang if (rc < 0) 36675b91130SDave Jiang goto err_engine; 36775b91130SDave Jiang 368defe49f9SDave Jiang rc = idxd_setup_groups(idxd); 369defe49f9SDave Jiang if (rc < 0) 370defe49f9SDave Jiang goto err_group; 371bfe1d560SDave Jiang 3720d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 3737c5dd23eSDave Jiang if (!idxd->wq) { 3747c5dd23eSDave Jiang rc = -ENOMEM; 375defe49f9SDave Jiang goto err_wkq_create; 3767c5dd23eSDave Jiang } 3770d5c10b4SDave Jiang 378bfe1d560SDave Jiang return 0; 3797c5dd23eSDave Jiang 380defe49f9SDave Jiang err_wkq_create: 381defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) 382defe49f9SDave Jiang put_device(&idxd->groups[i]->conf_dev); 383defe49f9SDave Jiang err_group: 38475b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) 38575b91130SDave Jiang put_device(&idxd->engines[i]->conf_dev); 38675b91130SDave Jiang err_engine: 3877c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) 3887c5dd23eSDave Jiang put_device(&idxd->wqs[i]->conf_dev); 389eb15e715SDave Jiang err_wqs: 390eb15e715SDave Jiang kfree(idxd->int_handles); 3917c5dd23eSDave Jiang return rc; 392bfe1d560SDave Jiang } 393bfe1d560SDave Jiang 394bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 395bfe1d560SDave Jiang { 396bfe1d560SDave Jiang union offsets_reg offsets; 397bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 398bfe1d560SDave Jiang 399bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 4002f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 4012f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 402bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 4032f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 4042f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 4052f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 4062f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 4072f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 408bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 409bfe1d560SDave Jiang } 410bfe1d560SDave Jiang 411bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 412bfe1d560SDave Jiang { 413bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 414bfe1d560SDave Jiang int i; 415bfe1d560SDave Jiang 416bfe1d560SDave Jiang /* reading generic capabilities */ 417bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 418bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 419eb15e715SDave Jiang 420eb15e715SDave Jiang if (idxd->hw.gen_cap.cmd_cap) { 421eb15e715SDave Jiang idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET); 422eb15e715SDave Jiang dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap); 423eb15e715SDave Jiang } 424eb15e715SDave Jiang 425bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 426bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 427bfe1d560SDave Jiang idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift; 428bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 429bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 430bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 431bfe1d560SDave Jiang 432bfe1d560SDave Jiang /* reading group capabilities */ 433bfe1d560SDave Jiang idxd->hw.group_cap.bits = 434bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 435bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 436bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 437bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 438bfe1d560SDave Jiang idxd->max_tokens = idxd->hw.group_cap.total_tokens; 439bfe1d560SDave Jiang dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens); 440c52ca478SDave Jiang idxd->nr_tokens = idxd->max_tokens; 441bfe1d560SDave Jiang 442bfe1d560SDave Jiang /* read engine capabilities */ 443bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 444bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 445bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 446bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 447bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 448bfe1d560SDave Jiang 449bfe1d560SDave Jiang /* read workqueue capabilities */ 450bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 451bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 452bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 453bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 454bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 455bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 456d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 457d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 458bfe1d560SDave Jiang 459bfe1d560SDave Jiang /* reading operation capabilities */ 460bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 461bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 462bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 463bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 464bfe1d560SDave Jiang } 465bfe1d560SDave Jiang } 466bfe1d560SDave Jiang 467435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) 468bfe1d560SDave Jiang { 469bfe1d560SDave Jiang struct device *dev = &pdev->dev; 470bfe1d560SDave Jiang struct idxd_device *idxd; 47147c16ac2SDave Jiang int rc; 472bfe1d560SDave Jiang 47347c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 474bfe1d560SDave Jiang if (!idxd) 475bfe1d560SDave Jiang return NULL; 476bfe1d560SDave Jiang 477bfe1d560SDave Jiang idxd->pdev = pdev; 478435b512dSDave Jiang idxd->data = data; 4794b73e4ebSDave Jiang idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); 48047c16ac2SDave Jiang if (idxd->id < 0) 48147c16ac2SDave Jiang return NULL; 48247c16ac2SDave Jiang 48347c16ac2SDave Jiang device_initialize(&idxd->conf_dev); 48447c16ac2SDave Jiang idxd->conf_dev.parent = dev; 4854b73e4ebSDave Jiang idxd->conf_dev.bus = &dsa_bus_type; 486435b512dSDave Jiang idxd->conf_dev.type = idxd->data->dev_type; 487435b512dSDave Jiang rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); 48847c16ac2SDave Jiang if (rc < 0) { 48947c16ac2SDave Jiang put_device(&idxd->conf_dev); 49047c16ac2SDave Jiang return NULL; 49147c16ac2SDave Jiang } 49247c16ac2SDave Jiang 493bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 49453b2ee7fSDave Jiang spin_lock_init(&idxd->cmd_lock); 495bfe1d560SDave Jiang 496bfe1d560SDave Jiang return idxd; 497bfe1d560SDave Jiang } 498bfe1d560SDave Jiang 4998e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 5008e50d392SDave Jiang { 5018e50d392SDave Jiang int flags; 5028e50d392SDave Jiang unsigned int pasid; 5038e50d392SDave Jiang struct iommu_sva *sva; 5048e50d392SDave Jiang 5058e50d392SDave Jiang flags = SVM_FLAG_SUPERVISOR_MODE; 5068e50d392SDave Jiang 5078e50d392SDave Jiang sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags); 5088e50d392SDave Jiang if (IS_ERR(sva)) { 5098e50d392SDave Jiang dev_warn(&idxd->pdev->dev, 5108e50d392SDave Jiang "iommu sva bind failed: %ld\n", PTR_ERR(sva)); 5118e50d392SDave Jiang return PTR_ERR(sva); 5128e50d392SDave Jiang } 5138e50d392SDave Jiang 5148e50d392SDave Jiang pasid = iommu_sva_get_pasid(sva); 5158e50d392SDave Jiang if (pasid == IOMMU_PASID_INVALID) { 5168e50d392SDave Jiang iommu_sva_unbind_device(sva); 5178e50d392SDave Jiang return -ENODEV; 5188e50d392SDave Jiang } 5198e50d392SDave Jiang 5208e50d392SDave Jiang idxd->sva = sva; 5218e50d392SDave Jiang idxd->pasid = pasid; 5228e50d392SDave Jiang dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid); 5238e50d392SDave Jiang return 0; 5248e50d392SDave Jiang } 5258e50d392SDave Jiang 5268e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 5278e50d392SDave Jiang { 5288e50d392SDave Jiang 5298e50d392SDave Jiang iommu_sva_unbind_device(idxd->sva); 5308e50d392SDave Jiang idxd->sva = NULL; 5318e50d392SDave Jiang } 5328e50d392SDave Jiang 533bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 534bfe1d560SDave Jiang { 535bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 536bfe1d560SDave Jiang struct device *dev = &pdev->dev; 537bfe1d560SDave Jiang int rc; 538bfe1d560SDave Jiang 539bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 54089e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 54189e3becdSDave Jiang if (rc < 0) 54289e3becdSDave Jiang return rc; 54389e3becdSDave Jiang 544bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 545bfe1d560SDave Jiang 54603d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 547cf5f86a7SDave Jiang rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA); 548cf5f86a7SDave Jiang if (rc == 0) { 5498e50d392SDave Jiang rc = idxd_enable_system_pasid(idxd); 550cf5f86a7SDave Jiang if (rc < 0) { 551cf5f86a7SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 5528e50d392SDave Jiang dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); 553cf5f86a7SDave Jiang } else { 5548e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 555cf5f86a7SDave Jiang } 556cf5f86a7SDave Jiang } else { 557cf5f86a7SDave Jiang dev_warn(dev, "Unable to turn on SVA feature.\n"); 558cf5f86a7SDave Jiang } 55903d939c7SDave Jiang } else if (!sva) { 56003d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n"); 5618e50d392SDave Jiang } 5628e50d392SDave Jiang 563bfe1d560SDave Jiang idxd_read_caps(idxd); 564bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 565bfe1d560SDave Jiang 566bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 567bfe1d560SDave Jiang if (rc) 5687c5dd23eSDave Jiang goto err; 569bfe1d560SDave Jiang 5708c66bbdcSDave Jiang /* If the configs are readonly, then load them from device */ 5718c66bbdcSDave Jiang if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { 5728c66bbdcSDave Jiang dev_dbg(dev, "Loading RO device config\n"); 5738c66bbdcSDave Jiang rc = idxd_device_load_config(idxd); 5748c66bbdcSDave Jiang if (rc < 0) 575ddf742d4SDave Jiang goto err_config; 5768c66bbdcSDave Jiang } 5778c66bbdcSDave Jiang 578bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 579bfe1d560SDave Jiang if (rc) 580ddf742d4SDave Jiang goto err_config; 581bfe1d560SDave Jiang 582bfe1d560SDave Jiang dev_dbg(dev, "IDXD interrupt setup complete.\n"); 583bfe1d560SDave Jiang 58442d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 58542d279f9SDave Jiang 5860bde4444STom Zanussi rc = perfmon_pmu_init(idxd); 5870bde4444STom Zanussi if (rc < 0) 5880bde4444STom Zanussi dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc); 5890bde4444STom Zanussi 590bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 591bfe1d560SDave Jiang return 0; 592bfe1d560SDave Jiang 593ddf742d4SDave Jiang err_config: 594ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 5957c5dd23eSDave Jiang err: 5968e50d392SDave Jiang if (device_pasid_enabled(idxd)) 5978e50d392SDave Jiang idxd_disable_system_pasid(idxd); 598cf5f86a7SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 599bfe1d560SDave Jiang return rc; 600bfe1d560SDave Jiang } 601bfe1d560SDave Jiang 602ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd) 603ddf742d4SDave Jiang { 604ddf742d4SDave Jiang struct device *dev = &idxd->pdev->dev; 605ddf742d4SDave Jiang 606ddf742d4SDave Jiang perfmon_pmu_remove(idxd); 607ddf742d4SDave Jiang idxd_cleanup_interrupts(idxd); 608ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 609ddf742d4SDave Jiang if (device_pasid_enabled(idxd)) 610ddf742d4SDave Jiang idxd_disable_system_pasid(idxd); 611ddf742d4SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 612ddf742d4SDave Jiang } 613ddf742d4SDave Jiang 614bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 615bfe1d560SDave Jiang { 616bfe1d560SDave Jiang struct device *dev = &pdev->dev; 617bfe1d560SDave Jiang struct idxd_device *idxd; 618435b512dSDave Jiang struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data; 619bfe1d560SDave Jiang int rc; 620bfe1d560SDave Jiang 621a39c7cd0SDave Jiang rc = pci_enable_device(pdev); 622bfe1d560SDave Jiang if (rc) 623bfe1d560SDave Jiang return rc; 624bfe1d560SDave Jiang 6258e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 626435b512dSDave Jiang idxd = idxd_alloc(pdev, data); 627a39c7cd0SDave Jiang if (!idxd) { 628a39c7cd0SDave Jiang rc = -ENOMEM; 629a39c7cd0SDave Jiang goto err_idxd_alloc; 630a39c7cd0SDave Jiang } 631bfe1d560SDave Jiang 6328e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 633a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 634a39c7cd0SDave Jiang if (!idxd->reg_base) { 635a39c7cd0SDave Jiang rc = -ENOMEM; 636a39c7cd0SDave Jiang goto err_iomap; 637a39c7cd0SDave Jiang } 638bfe1d560SDave Jiang 639bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 640*53b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 641bfe1d560SDave Jiang if (rc) 642*53b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 643bfe1d560SDave Jiang if (rc) 644a39c7cd0SDave Jiang goto err; 645bfe1d560SDave Jiang 646bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 647bfe1d560SDave Jiang pci_set_master(pdev); 648bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 649bfe1d560SDave Jiang 650bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 651bfe1d560SDave Jiang rc = idxd_probe(idxd); 652bfe1d560SDave Jiang if (rc) { 653bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 654a39c7cd0SDave Jiang goto err; 655bfe1d560SDave Jiang } 656bfe1d560SDave Jiang 65747c16ac2SDave Jiang rc = idxd_register_devices(idxd); 658c52ca478SDave Jiang if (rc) { 659c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 660ddf742d4SDave Jiang goto err_dev_register; 661c52ca478SDave Jiang } 662c52ca478SDave Jiang 663c52ca478SDave Jiang idxd->state = IDXD_DEV_CONF_READY; 664c52ca478SDave Jiang 665bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 666bfe1d560SDave Jiang idxd->hw.version); 667bfe1d560SDave Jiang 668bfe1d560SDave Jiang return 0; 669a39c7cd0SDave Jiang 670ddf742d4SDave Jiang err_dev_register: 671ddf742d4SDave Jiang idxd_cleanup(idxd); 672a39c7cd0SDave Jiang err: 673a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 674a39c7cd0SDave Jiang err_iomap: 67547c16ac2SDave Jiang put_device(&idxd->conf_dev); 676a39c7cd0SDave Jiang err_idxd_alloc: 677a39c7cd0SDave Jiang pci_disable_device(pdev); 678a39c7cd0SDave Jiang return rc; 679bfe1d560SDave Jiang } 680bfe1d560SDave Jiang 6818f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) 6828f47d1a5SDave Jiang { 6838f47d1a5SDave Jiang struct idxd_desc *desc, *itr; 6848f47d1a5SDave Jiang struct llist_node *head; 6858f47d1a5SDave Jiang 6868f47d1a5SDave Jiang head = llist_del_all(&ie->pending_llist); 6878f47d1a5SDave Jiang if (!head) 6888f47d1a5SDave Jiang return; 6898f47d1a5SDave Jiang 6908f47d1a5SDave Jiang llist_for_each_entry_safe(desc, itr, head, llnode) { 6918f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 6928f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 6938f47d1a5SDave Jiang } 6948f47d1a5SDave Jiang } 6958f47d1a5SDave Jiang 6968f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie) 6978f47d1a5SDave Jiang { 6988f47d1a5SDave Jiang struct idxd_desc *desc, *iter; 6998f47d1a5SDave Jiang 7008f47d1a5SDave Jiang list_for_each_entry_safe(desc, iter, &ie->work_list, list) { 7018f47d1a5SDave Jiang list_del(&desc->list); 7028f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 7038f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 7048f47d1a5SDave Jiang } 7058f47d1a5SDave Jiang } 7068f47d1a5SDave Jiang 7075b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd) 7085b0c68c4SDave Jiang { 7095b0c68c4SDave Jiang struct idxd_wq *wq; 7105b0c68c4SDave Jiang int i; 7115b0c68c4SDave Jiang 7125b0c68c4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 7135b0c68c4SDave Jiang wq = idxd->wqs[i]; 7145b0c68c4SDave Jiang if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL) 7155b0c68c4SDave Jiang idxd_wq_quiesce(wq); 7165b0c68c4SDave Jiang } 7175b0c68c4SDave Jiang } 7185b0c68c4SDave Jiang 719eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd) 720eb15e715SDave Jiang { 721eb15e715SDave Jiang struct device *dev = &idxd->pdev->dev; 722eb15e715SDave Jiang int i, rc; 723eb15e715SDave Jiang 724eb15e715SDave Jiang for (i = 0; i < idxd->num_wq_irqs; i++) { 725eb15e715SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) { 726eb15e715SDave Jiang rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i], 727eb15e715SDave Jiang IDXD_IRQ_MSIX); 728eb15e715SDave Jiang if (rc < 0) 729eb15e715SDave Jiang dev_warn(dev, "irq handle %d release failed\n", 730eb15e715SDave Jiang idxd->int_handles[i]); 731eb15e715SDave Jiang else 732eb15e715SDave Jiang dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]); 733eb15e715SDave Jiang } 734eb15e715SDave Jiang } 735eb15e715SDave Jiang } 736eb15e715SDave Jiang 737bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 738bfe1d560SDave Jiang { 739bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 740bfe1d560SDave Jiang int rc, i; 741bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 742bfe1d560SDave Jiang int msixcnt = pci_msix_vec_count(pdev); 743bfe1d560SDave Jiang 744bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 745bfe1d560SDave Jiang if (rc) 746bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 747bfe1d560SDave Jiang 748bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 749bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 750bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 751bfe1d560SDave Jiang 752bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 753bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 7545fc8e85fSDave Jiang synchronize_irq(irq_entry->vector); 7555fc8e85fSDave Jiang free_irq(irq_entry->vector, irq_entry); 756bfe1d560SDave Jiang if (i == 0) 757bfe1d560SDave Jiang continue; 7588f47d1a5SDave Jiang idxd_flush_pending_llist(irq_entry); 7598f47d1a5SDave Jiang idxd_flush_work_list(irq_entry); 760bfe1d560SDave Jiang } 7610d5c10b4SDave Jiang 7626df0e6c5SDave Jiang idxd_msix_perm_clear(idxd); 763eb15e715SDave Jiang idxd_release_int_handles(idxd); 7645fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 765a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 766a39c7cd0SDave Jiang pci_disable_device(pdev); 7670d5c10b4SDave Jiang destroy_workqueue(idxd->wq); 768bfe1d560SDave Jiang } 769bfe1d560SDave Jiang 770bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 771bfe1d560SDave Jiang { 772bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 773bfe1d560SDave Jiang 774bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 775bfe1d560SDave Jiang idxd_shutdown(pdev); 7768e50d392SDave Jiang if (device_pasid_enabled(idxd)) 7778e50d392SDave Jiang idxd_disable_system_pasid(idxd); 77847c16ac2SDave Jiang idxd_unregister_devices(idxd); 7790bde4444STom Zanussi perfmon_pmu_remove(idxd); 780cf5f86a7SDave Jiang iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 781bfe1d560SDave Jiang } 782bfe1d560SDave Jiang 783bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 784bfe1d560SDave Jiang .name = DRV_NAME, 785bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 786bfe1d560SDave Jiang .probe = idxd_pci_probe, 787bfe1d560SDave Jiang .remove = idxd_remove, 788bfe1d560SDave Jiang .shutdown = idxd_shutdown, 789bfe1d560SDave Jiang }; 790bfe1d560SDave Jiang 791bfe1d560SDave Jiang static int __init idxd_init_module(void) 792bfe1d560SDave Jiang { 7934b73e4ebSDave Jiang int err; 794bfe1d560SDave Jiang 795bfe1d560SDave Jiang /* 7968e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 797bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 798bfe1d560SDave Jiang */ 79974b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) { 800bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 801bfe1d560SDave Jiang return -ENODEV; 802bfe1d560SDave Jiang } 803bfe1d560SDave Jiang 80474b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_ENQCMD)) 8058e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 8068e50d392SDave Jiang else 8078e50d392SDave Jiang support_enqcmd = true; 808bfe1d560SDave Jiang 8090bde4444STom Zanussi perfmon_init(); 8100bde4444STom Zanussi 811c52ca478SDave Jiang err = idxd_register_bus_type(); 812c52ca478SDave Jiang if (err < 0) 813bfe1d560SDave Jiang return err; 814bfe1d560SDave Jiang 815c52ca478SDave Jiang err = idxd_register_driver(); 816c52ca478SDave Jiang if (err < 0) 817c52ca478SDave Jiang goto err_idxd_driver_register; 818c52ca478SDave Jiang 81942d279f9SDave Jiang err = idxd_cdev_register(); 82042d279f9SDave Jiang if (err) 82142d279f9SDave Jiang goto err_cdev_register; 82242d279f9SDave Jiang 823c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 824c52ca478SDave Jiang if (err) 825c52ca478SDave Jiang goto err_pci_register; 826c52ca478SDave Jiang 827bfe1d560SDave Jiang return 0; 828c52ca478SDave Jiang 829c52ca478SDave Jiang err_pci_register: 83042d279f9SDave Jiang idxd_cdev_remove(); 83142d279f9SDave Jiang err_cdev_register: 832c52ca478SDave Jiang idxd_unregister_driver(); 833c52ca478SDave Jiang err_idxd_driver_register: 834c52ca478SDave Jiang idxd_unregister_bus_type(); 835c52ca478SDave Jiang return err; 836bfe1d560SDave Jiang } 837bfe1d560SDave Jiang module_init(idxd_init_module); 838bfe1d560SDave Jiang 839bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 840bfe1d560SDave Jiang { 841077cdb35SDave Jiang idxd_unregister_driver(); 842bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 84342d279f9SDave Jiang idxd_cdev_remove(); 844c52ca478SDave Jiang idxd_unregister_bus_type(); 8450bde4444STom Zanussi perfmon_exit(); 846bfe1d560SDave Jiang } 847bfe1d560SDave Jiang module_exit(idxd_exit_module); 848