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 171*ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd) 172*ddf742d4SDave Jiang { 173*ddf742d4SDave Jiang struct pci_dev *pdev = idxd->pdev; 174*ddf742d4SDave Jiang struct idxd_irq_entry *irq_entry; 175*ddf742d4SDave Jiang int i, msixcnt; 176*ddf742d4SDave Jiang 177*ddf742d4SDave Jiang msixcnt = pci_msix_vec_count(pdev); 178*ddf742d4SDave Jiang if (msixcnt <= 0) 179*ddf742d4SDave Jiang return; 180*ddf742d4SDave Jiang 181*ddf742d4SDave Jiang irq_entry = &idxd->irq_entries[0]; 182*ddf742d4SDave Jiang free_irq(irq_entry->vector, irq_entry); 183*ddf742d4SDave Jiang 184*ddf742d4SDave Jiang for (i = 1; i < msixcnt; i++) { 185*ddf742d4SDave Jiang 186*ddf742d4SDave Jiang irq_entry = &idxd->irq_entries[i]; 187*ddf742d4SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) 188*ddf742d4SDave Jiang idxd_device_release_int_handle(idxd, idxd->int_handles[i], 189*ddf742d4SDave Jiang IDXD_IRQ_MSIX); 190*ddf742d4SDave Jiang free_irq(irq_entry->vector, irq_entry); 191*ddf742d4SDave Jiang } 192*ddf742d4SDave Jiang 193*ddf742d4SDave Jiang idxd_mask_error_interrupts(idxd); 194*ddf742d4SDave Jiang pci_free_irq_vectors(pdev); 195*ddf742d4SDave Jiang } 196*ddf742d4SDave 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 333*ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd) 334*ddf742d4SDave Jiang { 335*ddf742d4SDave Jiang int i; 336*ddf742d4SDave Jiang 337*ddf742d4SDave Jiang for (i = 0; i < idxd->max_groups; i++) 338*ddf742d4SDave Jiang put_device(&idxd->groups[i]->conf_dev); 339*ddf742d4SDave Jiang for (i = 0; i < idxd->max_engines; i++) 340*ddf742d4SDave Jiang put_device(&idxd->engines[i]->conf_dev); 341*ddf742d4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) 342*ddf742d4SDave Jiang put_device(&idxd->wqs[i]->conf_dev); 343*ddf742d4SDave Jiang destroy_workqueue(idxd->wq); 344*ddf742d4SDave Jiang } 345*ddf742d4SDave 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)) { 354eb15e715SDave Jiang idxd->int_handles = devm_kcalloc(dev, idxd->max_wqs, sizeof(int), GFP_KERNEL); 355eb15e715SDave Jiang if (!idxd->int_handles) 356eb15e715SDave Jiang return -ENOMEM; 357eb15e715SDave Jiang } 358eb15e715SDave Jiang 3597c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd); 3607c5dd23eSDave Jiang if (rc < 0) 361eb15e715SDave Jiang goto err_wqs; 3627c5dd23eSDave Jiang 36375b91130SDave Jiang rc = idxd_setup_engines(idxd); 36475b91130SDave Jiang if (rc < 0) 36575b91130SDave Jiang goto err_engine; 36675b91130SDave Jiang 367defe49f9SDave Jiang rc = idxd_setup_groups(idxd); 368defe49f9SDave Jiang if (rc < 0) 369defe49f9SDave Jiang goto err_group; 370bfe1d560SDave Jiang 3710d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 3727c5dd23eSDave Jiang if (!idxd->wq) { 3737c5dd23eSDave Jiang rc = -ENOMEM; 374defe49f9SDave Jiang goto err_wkq_create; 3757c5dd23eSDave Jiang } 3760d5c10b4SDave Jiang 377bfe1d560SDave Jiang return 0; 3787c5dd23eSDave Jiang 379defe49f9SDave Jiang err_wkq_create: 380defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) 381defe49f9SDave Jiang put_device(&idxd->groups[i]->conf_dev); 382defe49f9SDave Jiang err_group: 38375b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) 38475b91130SDave Jiang put_device(&idxd->engines[i]->conf_dev); 38575b91130SDave Jiang err_engine: 3867c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) 3877c5dd23eSDave Jiang put_device(&idxd->wqs[i]->conf_dev); 388eb15e715SDave Jiang err_wqs: 389eb15e715SDave Jiang kfree(idxd->int_handles); 3907c5dd23eSDave Jiang return rc; 391bfe1d560SDave Jiang } 392bfe1d560SDave Jiang 393bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 394bfe1d560SDave Jiang { 395bfe1d560SDave Jiang union offsets_reg offsets; 396bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 397bfe1d560SDave Jiang 398bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 3992f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 4002f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 401bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 4022f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 4032f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 4042f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 4052f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 4062f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 407bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 408bfe1d560SDave Jiang } 409bfe1d560SDave Jiang 410bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 411bfe1d560SDave Jiang { 412bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 413bfe1d560SDave Jiang int i; 414bfe1d560SDave Jiang 415bfe1d560SDave Jiang /* reading generic capabilities */ 416bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 417bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 418eb15e715SDave Jiang 419eb15e715SDave Jiang if (idxd->hw.gen_cap.cmd_cap) { 420eb15e715SDave Jiang idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET); 421eb15e715SDave Jiang dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap); 422eb15e715SDave Jiang } 423eb15e715SDave Jiang 424bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 425bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 426bfe1d560SDave Jiang idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift; 427bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 428bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 429bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 430bfe1d560SDave Jiang 431bfe1d560SDave Jiang /* reading group capabilities */ 432bfe1d560SDave Jiang idxd->hw.group_cap.bits = 433bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 434bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 435bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 436bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 437bfe1d560SDave Jiang idxd->max_tokens = idxd->hw.group_cap.total_tokens; 438bfe1d560SDave Jiang dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens); 439c52ca478SDave Jiang idxd->nr_tokens = idxd->max_tokens; 440bfe1d560SDave Jiang 441bfe1d560SDave Jiang /* read engine capabilities */ 442bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 443bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 444bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 445bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 446bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 447bfe1d560SDave Jiang 448bfe1d560SDave Jiang /* read workqueue capabilities */ 449bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 450bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 451bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 452bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 453bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 454bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 455d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 456d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 457bfe1d560SDave Jiang 458bfe1d560SDave Jiang /* reading operation capabilities */ 459bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 460bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 461bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 462bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 463bfe1d560SDave Jiang } 464bfe1d560SDave Jiang } 465bfe1d560SDave Jiang 466435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) 467bfe1d560SDave Jiang { 468bfe1d560SDave Jiang struct device *dev = &pdev->dev; 469bfe1d560SDave Jiang struct idxd_device *idxd; 47047c16ac2SDave Jiang int rc; 471bfe1d560SDave Jiang 47247c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 473bfe1d560SDave Jiang if (!idxd) 474bfe1d560SDave Jiang return NULL; 475bfe1d560SDave Jiang 476bfe1d560SDave Jiang idxd->pdev = pdev; 477435b512dSDave Jiang idxd->data = data; 4784b73e4ebSDave Jiang idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); 47947c16ac2SDave Jiang if (idxd->id < 0) 48047c16ac2SDave Jiang return NULL; 48147c16ac2SDave Jiang 48247c16ac2SDave Jiang device_initialize(&idxd->conf_dev); 48347c16ac2SDave Jiang idxd->conf_dev.parent = dev; 4844b73e4ebSDave Jiang idxd->conf_dev.bus = &dsa_bus_type; 485435b512dSDave Jiang idxd->conf_dev.type = idxd->data->dev_type; 486435b512dSDave Jiang rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); 48747c16ac2SDave Jiang if (rc < 0) { 48847c16ac2SDave Jiang put_device(&idxd->conf_dev); 48947c16ac2SDave Jiang return NULL; 49047c16ac2SDave Jiang } 49147c16ac2SDave Jiang 492bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 49353b2ee7fSDave Jiang spin_lock_init(&idxd->cmd_lock); 494bfe1d560SDave Jiang 495bfe1d560SDave Jiang return idxd; 496bfe1d560SDave Jiang } 497bfe1d560SDave Jiang 4988e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 4998e50d392SDave Jiang { 5008e50d392SDave Jiang int flags; 5018e50d392SDave Jiang unsigned int pasid; 5028e50d392SDave Jiang struct iommu_sva *sva; 5038e50d392SDave Jiang 5048e50d392SDave Jiang flags = SVM_FLAG_SUPERVISOR_MODE; 5058e50d392SDave Jiang 5068e50d392SDave Jiang sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags); 5078e50d392SDave Jiang if (IS_ERR(sva)) { 5088e50d392SDave Jiang dev_warn(&idxd->pdev->dev, 5098e50d392SDave Jiang "iommu sva bind failed: %ld\n", PTR_ERR(sva)); 5108e50d392SDave Jiang return PTR_ERR(sva); 5118e50d392SDave Jiang } 5128e50d392SDave Jiang 5138e50d392SDave Jiang pasid = iommu_sva_get_pasid(sva); 5148e50d392SDave Jiang if (pasid == IOMMU_PASID_INVALID) { 5158e50d392SDave Jiang iommu_sva_unbind_device(sva); 5168e50d392SDave Jiang return -ENODEV; 5178e50d392SDave Jiang } 5188e50d392SDave Jiang 5198e50d392SDave Jiang idxd->sva = sva; 5208e50d392SDave Jiang idxd->pasid = pasid; 5218e50d392SDave Jiang dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid); 5228e50d392SDave Jiang return 0; 5238e50d392SDave Jiang } 5248e50d392SDave Jiang 5258e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 5268e50d392SDave Jiang { 5278e50d392SDave Jiang 5288e50d392SDave Jiang iommu_sva_unbind_device(idxd->sva); 5298e50d392SDave Jiang idxd->sva = NULL; 5308e50d392SDave Jiang } 5318e50d392SDave Jiang 532bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 533bfe1d560SDave Jiang { 534bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 535bfe1d560SDave Jiang struct device *dev = &pdev->dev; 536bfe1d560SDave Jiang int rc; 537bfe1d560SDave Jiang 538bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 53989e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 54089e3becdSDave Jiang if (rc < 0) 54189e3becdSDave Jiang return rc; 54289e3becdSDave Jiang 543bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 544bfe1d560SDave Jiang 54503d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 546cf5f86a7SDave Jiang rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA); 547cf5f86a7SDave Jiang if (rc == 0) { 5488e50d392SDave Jiang rc = idxd_enable_system_pasid(idxd); 549cf5f86a7SDave Jiang if (rc < 0) { 550cf5f86a7SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 5518e50d392SDave Jiang dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); 552cf5f86a7SDave Jiang } else { 5538e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 554cf5f86a7SDave Jiang } 555cf5f86a7SDave Jiang } else { 556cf5f86a7SDave Jiang dev_warn(dev, "Unable to turn on SVA feature.\n"); 557cf5f86a7SDave Jiang } 55803d939c7SDave Jiang } else if (!sva) { 55903d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n"); 5608e50d392SDave Jiang } 5618e50d392SDave Jiang 562bfe1d560SDave Jiang idxd_read_caps(idxd); 563bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 564bfe1d560SDave Jiang 565bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 566bfe1d560SDave Jiang if (rc) 5677c5dd23eSDave Jiang goto err; 568bfe1d560SDave Jiang 5698c66bbdcSDave Jiang /* If the configs are readonly, then load them from device */ 5708c66bbdcSDave Jiang if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { 5718c66bbdcSDave Jiang dev_dbg(dev, "Loading RO device config\n"); 5728c66bbdcSDave Jiang rc = idxd_device_load_config(idxd); 5738c66bbdcSDave Jiang if (rc < 0) 574*ddf742d4SDave Jiang goto err_config; 5758c66bbdcSDave Jiang } 5768c66bbdcSDave Jiang 577bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 578bfe1d560SDave Jiang if (rc) 579*ddf742d4SDave Jiang goto err_config; 580bfe1d560SDave Jiang 581bfe1d560SDave Jiang dev_dbg(dev, "IDXD interrupt setup complete.\n"); 582bfe1d560SDave Jiang 58342d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 58442d279f9SDave Jiang 5850bde4444STom Zanussi rc = perfmon_pmu_init(idxd); 5860bde4444STom Zanussi if (rc < 0) 5870bde4444STom Zanussi dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc); 5880bde4444STom Zanussi 589bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 590bfe1d560SDave Jiang return 0; 591bfe1d560SDave Jiang 592*ddf742d4SDave Jiang err_config: 593*ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 5947c5dd23eSDave Jiang err: 5958e50d392SDave Jiang if (device_pasid_enabled(idxd)) 5968e50d392SDave Jiang idxd_disable_system_pasid(idxd); 597cf5f86a7SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 598bfe1d560SDave Jiang return rc; 599bfe1d560SDave Jiang } 600bfe1d560SDave Jiang 601*ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd) 602*ddf742d4SDave Jiang { 603*ddf742d4SDave Jiang struct device *dev = &idxd->pdev->dev; 604*ddf742d4SDave Jiang 605*ddf742d4SDave Jiang perfmon_pmu_remove(idxd); 606*ddf742d4SDave Jiang idxd_cleanup_interrupts(idxd); 607*ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 608*ddf742d4SDave Jiang if (device_pasid_enabled(idxd)) 609*ddf742d4SDave Jiang idxd_disable_system_pasid(idxd); 610*ddf742d4SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 611*ddf742d4SDave Jiang } 612*ddf742d4SDave Jiang 613bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 614bfe1d560SDave Jiang { 615bfe1d560SDave Jiang struct device *dev = &pdev->dev; 616bfe1d560SDave Jiang struct idxd_device *idxd; 617435b512dSDave Jiang struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data; 618bfe1d560SDave Jiang int rc; 619bfe1d560SDave Jiang 620a39c7cd0SDave Jiang rc = pci_enable_device(pdev); 621bfe1d560SDave Jiang if (rc) 622bfe1d560SDave Jiang return rc; 623bfe1d560SDave Jiang 6248e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 625435b512dSDave Jiang idxd = idxd_alloc(pdev, data); 626a39c7cd0SDave Jiang if (!idxd) { 627a39c7cd0SDave Jiang rc = -ENOMEM; 628a39c7cd0SDave Jiang goto err_idxd_alloc; 629a39c7cd0SDave Jiang } 630bfe1d560SDave Jiang 6318e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 632a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 633a39c7cd0SDave Jiang if (!idxd->reg_base) { 634a39c7cd0SDave Jiang rc = -ENOMEM; 635a39c7cd0SDave Jiang goto err_iomap; 636a39c7cd0SDave Jiang } 637bfe1d560SDave Jiang 638bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 639bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 640bfe1d560SDave Jiang if (rc) 641bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 642bfe1d560SDave Jiang if (rc) 643a39c7cd0SDave Jiang goto err; 644bfe1d560SDave Jiang 645bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 646bfe1d560SDave Jiang if (rc) 647bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 648bfe1d560SDave Jiang if (rc) 649a39c7cd0SDave Jiang goto err; 650bfe1d560SDave Jiang 651bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 652bfe1d560SDave Jiang pci_set_master(pdev); 653bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 654bfe1d560SDave Jiang 655bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 656bfe1d560SDave Jiang rc = idxd_probe(idxd); 657bfe1d560SDave Jiang if (rc) { 658bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 659a39c7cd0SDave Jiang goto err; 660bfe1d560SDave Jiang } 661bfe1d560SDave Jiang 66247c16ac2SDave Jiang rc = idxd_register_devices(idxd); 663c52ca478SDave Jiang if (rc) { 664c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 665*ddf742d4SDave Jiang goto err_dev_register; 666c52ca478SDave Jiang } 667c52ca478SDave Jiang 668c52ca478SDave Jiang idxd->state = IDXD_DEV_CONF_READY; 669c52ca478SDave Jiang 670bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 671bfe1d560SDave Jiang idxd->hw.version); 672bfe1d560SDave Jiang 673bfe1d560SDave Jiang return 0; 674a39c7cd0SDave Jiang 675*ddf742d4SDave Jiang err_dev_register: 676*ddf742d4SDave Jiang idxd_cleanup(idxd); 677a39c7cd0SDave Jiang err: 678a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 679a39c7cd0SDave Jiang err_iomap: 68047c16ac2SDave Jiang put_device(&idxd->conf_dev); 681a39c7cd0SDave Jiang err_idxd_alloc: 682a39c7cd0SDave Jiang pci_disable_device(pdev); 683a39c7cd0SDave Jiang return rc; 684bfe1d560SDave Jiang } 685bfe1d560SDave Jiang 6868f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) 6878f47d1a5SDave Jiang { 6888f47d1a5SDave Jiang struct idxd_desc *desc, *itr; 6898f47d1a5SDave Jiang struct llist_node *head; 6908f47d1a5SDave Jiang 6918f47d1a5SDave Jiang head = llist_del_all(&ie->pending_llist); 6928f47d1a5SDave Jiang if (!head) 6938f47d1a5SDave Jiang return; 6948f47d1a5SDave Jiang 6958f47d1a5SDave Jiang llist_for_each_entry_safe(desc, itr, head, llnode) { 6968f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 6978f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 6988f47d1a5SDave Jiang } 6998f47d1a5SDave Jiang } 7008f47d1a5SDave Jiang 7018f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie) 7028f47d1a5SDave Jiang { 7038f47d1a5SDave Jiang struct idxd_desc *desc, *iter; 7048f47d1a5SDave Jiang 7058f47d1a5SDave Jiang list_for_each_entry_safe(desc, iter, &ie->work_list, list) { 7068f47d1a5SDave Jiang list_del(&desc->list); 7078f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 7088f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 7098f47d1a5SDave Jiang } 7108f47d1a5SDave Jiang } 7118f47d1a5SDave Jiang 7125b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd) 7135b0c68c4SDave Jiang { 7145b0c68c4SDave Jiang struct idxd_wq *wq; 7155b0c68c4SDave Jiang int i; 7165b0c68c4SDave Jiang 7175b0c68c4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 7185b0c68c4SDave Jiang wq = idxd->wqs[i]; 7195b0c68c4SDave Jiang if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL) 7205b0c68c4SDave Jiang idxd_wq_quiesce(wq); 7215b0c68c4SDave Jiang } 7225b0c68c4SDave Jiang } 7235b0c68c4SDave Jiang 724eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd) 725eb15e715SDave Jiang { 726eb15e715SDave Jiang struct device *dev = &idxd->pdev->dev; 727eb15e715SDave Jiang int i, rc; 728eb15e715SDave Jiang 729eb15e715SDave Jiang for (i = 0; i < idxd->num_wq_irqs; i++) { 730eb15e715SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) { 731eb15e715SDave Jiang rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i], 732eb15e715SDave Jiang IDXD_IRQ_MSIX); 733eb15e715SDave Jiang if (rc < 0) 734eb15e715SDave Jiang dev_warn(dev, "irq handle %d release failed\n", 735eb15e715SDave Jiang idxd->int_handles[i]); 736eb15e715SDave Jiang else 737eb15e715SDave Jiang dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]); 738eb15e715SDave Jiang } 739eb15e715SDave Jiang } 740eb15e715SDave Jiang } 741eb15e715SDave Jiang 742bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 743bfe1d560SDave Jiang { 744bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 745bfe1d560SDave Jiang int rc, i; 746bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 747bfe1d560SDave Jiang int msixcnt = pci_msix_vec_count(pdev); 748bfe1d560SDave Jiang 749bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 750bfe1d560SDave Jiang if (rc) 751bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 752bfe1d560SDave Jiang 753bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 754bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 755bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 756bfe1d560SDave Jiang 757bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 758bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 7595fc8e85fSDave Jiang synchronize_irq(irq_entry->vector); 7605fc8e85fSDave Jiang free_irq(irq_entry->vector, irq_entry); 761bfe1d560SDave Jiang if (i == 0) 762bfe1d560SDave Jiang continue; 7638f47d1a5SDave Jiang idxd_flush_pending_llist(irq_entry); 7648f47d1a5SDave Jiang idxd_flush_work_list(irq_entry); 765bfe1d560SDave Jiang } 7660d5c10b4SDave Jiang 7676df0e6c5SDave Jiang idxd_msix_perm_clear(idxd); 768eb15e715SDave Jiang idxd_release_int_handles(idxd); 7695fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 770a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 771a39c7cd0SDave Jiang pci_disable_device(pdev); 7720d5c10b4SDave Jiang destroy_workqueue(idxd->wq); 773bfe1d560SDave Jiang } 774bfe1d560SDave Jiang 775bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 776bfe1d560SDave Jiang { 777bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 778bfe1d560SDave Jiang 779bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 780bfe1d560SDave Jiang idxd_shutdown(pdev); 7818e50d392SDave Jiang if (device_pasid_enabled(idxd)) 7828e50d392SDave Jiang idxd_disable_system_pasid(idxd); 78347c16ac2SDave Jiang idxd_unregister_devices(idxd); 7840bde4444STom Zanussi perfmon_pmu_remove(idxd); 785cf5f86a7SDave Jiang iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 786bfe1d560SDave Jiang } 787bfe1d560SDave Jiang 788bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 789bfe1d560SDave Jiang .name = DRV_NAME, 790bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 791bfe1d560SDave Jiang .probe = idxd_pci_probe, 792bfe1d560SDave Jiang .remove = idxd_remove, 793bfe1d560SDave Jiang .shutdown = idxd_shutdown, 794bfe1d560SDave Jiang }; 795bfe1d560SDave Jiang 796bfe1d560SDave Jiang static int __init idxd_init_module(void) 797bfe1d560SDave Jiang { 7984b73e4ebSDave Jiang int err; 799bfe1d560SDave Jiang 800bfe1d560SDave Jiang /* 8018e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 802bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 803bfe1d560SDave Jiang */ 804bfe1d560SDave Jiang if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) { 805bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 806bfe1d560SDave Jiang return -ENODEV; 807bfe1d560SDave Jiang } 808bfe1d560SDave Jiang 8098e50d392SDave Jiang if (!boot_cpu_has(X86_FEATURE_ENQCMD)) 8108e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 8118e50d392SDave Jiang else 8128e50d392SDave Jiang support_enqcmd = true; 813bfe1d560SDave Jiang 8140bde4444STom Zanussi perfmon_init(); 8150bde4444STom Zanussi 816c52ca478SDave Jiang err = idxd_register_bus_type(); 817c52ca478SDave Jiang if (err < 0) 818bfe1d560SDave Jiang return err; 819bfe1d560SDave Jiang 820c52ca478SDave Jiang err = idxd_register_driver(); 821c52ca478SDave Jiang if (err < 0) 822c52ca478SDave Jiang goto err_idxd_driver_register; 823c52ca478SDave Jiang 82442d279f9SDave Jiang err = idxd_cdev_register(); 82542d279f9SDave Jiang if (err) 82642d279f9SDave Jiang goto err_cdev_register; 82742d279f9SDave Jiang 828c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 829c52ca478SDave Jiang if (err) 830c52ca478SDave Jiang goto err_pci_register; 831c52ca478SDave Jiang 832bfe1d560SDave Jiang return 0; 833c52ca478SDave Jiang 834c52ca478SDave Jiang err_pci_register: 83542d279f9SDave Jiang idxd_cdev_remove(); 83642d279f9SDave Jiang err_cdev_register: 837c52ca478SDave Jiang idxd_unregister_driver(); 838c52ca478SDave Jiang err_idxd_driver_register: 839c52ca478SDave Jiang idxd_unregister_bus_type(); 840c52ca478SDave Jiang return err; 841bfe1d560SDave Jiang } 842bfe1d560SDave Jiang module_init(idxd_init_module); 843bfe1d560SDave Jiang 844bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 845bfe1d560SDave Jiang { 846077cdb35SDave Jiang idxd_unregister_driver(); 847bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 84842d279f9SDave Jiang idxd_cdev_remove(); 849c52ca478SDave Jiang idxd_unregister_bus_type(); 8500bde4444STom Zanussi perfmon_exit(); 851bfe1d560SDave Jiang } 852bfe1d560SDave Jiang module_exit(idxd_exit_module); 853