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"); 29d9e5481fSDave Jiang MODULE_IMPORT_NS(IDXD); 30bfe1d560SDave Jiang 3103d939c7SDave Jiang static bool sva = true; 3203d939c7SDave Jiang module_param(sva, bool, 0644); 3303d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off"); 3403d939c7SDave Jiang 35ade8a86bSDave Jiang bool tc_override; 36ade8a86bSDave Jiang module_param(tc_override, bool, 0644); 37ade8a86bSDave Jiang MODULE_PARM_DESC(tc_override, "Override traffic class defaults"); 38ade8a86bSDave Jiang 39bfe1d560SDave Jiang #define DRV_NAME "idxd" 40bfe1d560SDave Jiang 418e50d392SDave Jiang bool support_enqcmd; 424b73e4ebSDave Jiang DEFINE_IDA(idxd_ida); 43bfe1d560SDave Jiang 44435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = { 45435b512dSDave Jiang [IDXD_TYPE_DSA] = { 46435b512dSDave Jiang .name_prefix = "dsa", 47435b512dSDave Jiang .type = IDXD_TYPE_DSA, 48435b512dSDave Jiang .compl_size = sizeof(struct dsa_completion_record), 49435b512dSDave Jiang .align = 32, 50435b512dSDave Jiang .dev_type = &dsa_device_type, 51435b512dSDave Jiang }, 52435b512dSDave Jiang [IDXD_TYPE_IAX] = { 53435b512dSDave Jiang .name_prefix = "iax", 54435b512dSDave Jiang .type = IDXD_TYPE_IAX, 55435b512dSDave Jiang .compl_size = sizeof(struct iax_completion_record), 56435b512dSDave Jiang .align = 64, 57435b512dSDave Jiang .dev_type = &iax_device_type, 58435b512dSDave Jiang }, 59435b512dSDave Jiang }; 60435b512dSDave Jiang 61bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = { 62bfe1d560SDave Jiang /* DSA ver 1.0 platforms */ 63435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) }, 64f25b4638SDave Jiang 65f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 66435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) }, 67bfe1d560SDave Jiang { 0, } 68bfe1d560SDave Jiang }; 69bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 70bfe1d560SDave Jiang 71bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd) 72bfe1d560SDave Jiang { 73bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 74bfe1d560SDave Jiang struct device *dev = &pdev->dev; 75*ec0d6423SDave Jiang struct idxd_irq_entry *ie; 76bfe1d560SDave Jiang int i, msixcnt; 77bfe1d560SDave Jiang int rc = 0; 78bfe1d560SDave Jiang 79bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev); 80bfe1d560SDave Jiang if (msixcnt < 0) { 81bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n"); 825fc8e85fSDave Jiang return -ENOSPC; 83bfe1d560SDave Jiang } 848b67426eSDave Jiang idxd->irq_cnt = msixcnt; 85bfe1d560SDave Jiang 865fc8e85fSDave Jiang rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); 875fc8e85fSDave Jiang if (rc != msixcnt) { 885fc8e85fSDave Jiang dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); 895fc8e85fSDave Jiang return -ENOSPC; 90bfe1d560SDave Jiang } 91bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 92bfe1d560SDave Jiang 93d5c10e0fSDave Jiang idxd_msix_perm_setup(idxd); 94d5c10e0fSDave Jiang 95*ec0d6423SDave Jiang ie = idxd_get_ie(idxd, 0); 96*ec0d6423SDave Jiang ie->vector = pci_irq_vector(pdev, 0); 97*ec0d6423SDave Jiang rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie); 98bfe1d560SDave Jiang if (rc < 0) { 99bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n"); 1005fc8e85fSDave Jiang goto err_misc_irq; 101bfe1d560SDave Jiang } 102bfe1d560SDave Jiang 103*ec0d6423SDave Jiang dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", ie->vector); 104bfe1d560SDave Jiang 105*ec0d6423SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 106*ec0d6423SDave Jiang int msix_idx = i + 1; 107bfe1d560SDave Jiang 108*ec0d6423SDave Jiang ie = idxd_get_ie(idxd, msix_idx); 109bfe1d560SDave Jiang 110*ec0d6423SDave Jiang /* MSIX vector 0 special, wq irq entry starts at 1 */ 111*ec0d6423SDave Jiang ie->id = msix_idx; 112*ec0d6423SDave Jiang ie->vector = pci_irq_vector(pdev, msix_idx); 113*ec0d6423SDave Jiang ie->int_handle = INVALID_INT_HANDLE; 114*ec0d6423SDave Jiang if (device_pasid_enabled(idxd) && i > 0) 115*ec0d6423SDave Jiang ie->pasid = idxd->pasid; 116*ec0d6423SDave Jiang else 117*ec0d6423SDave Jiang ie->pasid = INVALID_IOASID; 118*ec0d6423SDave Jiang spin_lock_init(&ie->list_lock); 119*ec0d6423SDave Jiang init_llist_head(&ie->pending_llist); 120*ec0d6423SDave Jiang INIT_LIST_HEAD(&ie->work_list); 121*ec0d6423SDave Jiang 122*ec0d6423SDave Jiang rc = request_threaded_irq(ie->vector, NULL, idxd_wq_thread, 0, "idxd-portal", ie); 123bfe1d560SDave Jiang if (rc < 0) { 124*ec0d6423SDave Jiang dev_err(dev, "Failed to allocate irq %d.\n", ie->vector); 1255fc8e85fSDave Jiang goto err_wq_irqs; 126bfe1d560SDave Jiang } 127eb15e715SDave Jiang 128*ec0d6423SDave Jiang dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, ie->vector); 1298b67426eSDave Jiang if (idxd->request_int_handles) { 130*ec0d6423SDave Jiang rc = idxd_device_request_int_handle(idxd, i, &ie->int_handle, 131eb15e715SDave Jiang IDXD_IRQ_MSIX); 132eb15e715SDave Jiang if (rc < 0) { 133*ec0d6423SDave Jiang free_irq(ie->vector, ie); 134eb15e715SDave Jiang goto err_wq_irqs; 135eb15e715SDave Jiang } 136*ec0d6423SDave Jiang dev_dbg(dev, "int handle requested: %u\n", ie->int_handle); 137*ec0d6423SDave Jiang } else { 138*ec0d6423SDave Jiang ie->int_handle = msix_idx; 139eb15e715SDave Jiang } 140*ec0d6423SDave Jiang 141bfe1d560SDave Jiang } 142bfe1d560SDave Jiang 143bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd); 144bfe1d560SDave Jiang return 0; 145bfe1d560SDave Jiang 1465fc8e85fSDave Jiang err_wq_irqs: 1475fc8e85fSDave Jiang while (--i >= 0) { 148*ec0d6423SDave Jiang ie = &idxd->wqs[i]->ie; 149*ec0d6423SDave Jiang free_irq(ie->vector, ie); 150*ec0d6423SDave Jiang if (ie->int_handle != INVALID_INT_HANDLE) { 151*ec0d6423SDave Jiang idxd_device_release_int_handle(idxd, ie->int_handle, IDXD_IRQ_MSIX); 152*ec0d6423SDave Jiang ie->int_handle = INVALID_INT_HANDLE; 153*ec0d6423SDave Jiang ie->pasid = INVALID_IOASID; 1548b67426eSDave Jiang } 155*ec0d6423SDave Jiang ie->vector = -1; 1565fc8e85fSDave Jiang } 1575fc8e85fSDave Jiang err_misc_irq: 158bfe1d560SDave Jiang /* Disable error interrupt generation */ 159bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 160d5c10e0fSDave Jiang idxd_msix_perm_clear(idxd); 1615fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 162bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n"); 163bfe1d560SDave Jiang return rc; 164bfe1d560SDave Jiang } 165bfe1d560SDave Jiang 166ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd) 167ddf742d4SDave Jiang { 168ddf742d4SDave Jiang struct pci_dev *pdev = idxd->pdev; 169*ec0d6423SDave Jiang struct idxd_irq_entry *ie; 1708b67426eSDave Jiang int i; 171ddf742d4SDave Jiang 1728b67426eSDave Jiang for (i = 0; i < idxd->irq_cnt; i++) { 173*ec0d6423SDave Jiang ie = idxd_get_ie(idxd, i); 174*ec0d6423SDave Jiang if (ie->int_handle != INVALID_INT_HANDLE) { 175*ec0d6423SDave Jiang idxd_device_release_int_handle(idxd, ie->int_handle, IDXD_IRQ_MSIX); 176*ec0d6423SDave Jiang ie->int_handle = INVALID_INT_HANDLE; 177*ec0d6423SDave Jiang ie->pasid = INVALID_IOASID; 1788b67426eSDave Jiang } 179*ec0d6423SDave Jiang free_irq(ie->vector, ie); 180*ec0d6423SDave Jiang ie->vector = -1; 181ddf742d4SDave Jiang } 182ddf742d4SDave Jiang 183ddf742d4SDave Jiang idxd_mask_error_interrupts(idxd); 184ddf742d4SDave Jiang pci_free_irq_vectors(pdev); 185ddf742d4SDave Jiang } 186ddf742d4SDave Jiang 1877c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd) 1887c5dd23eSDave Jiang { 1897c5dd23eSDave Jiang struct device *dev = &idxd->pdev->dev; 1907c5dd23eSDave Jiang struct idxd_wq *wq; 191700af3a0SDave Jiang struct device *conf_dev; 1927c5dd23eSDave Jiang int i, rc; 1937c5dd23eSDave Jiang 1947c5dd23eSDave Jiang idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 1957c5dd23eSDave Jiang GFP_KERNEL, dev_to_node(dev)); 1967c5dd23eSDave Jiang if (!idxd->wqs) 1977c5dd23eSDave Jiang return -ENOMEM; 1987c5dd23eSDave Jiang 1997c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 2007c5dd23eSDave Jiang wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); 2017c5dd23eSDave Jiang if (!wq) { 2027c5dd23eSDave Jiang rc = -ENOMEM; 2037c5dd23eSDave Jiang goto err; 2047c5dd23eSDave Jiang } 2057c5dd23eSDave Jiang 206700af3a0SDave Jiang idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ); 207700af3a0SDave Jiang conf_dev = wq_confdev(wq); 2087c5dd23eSDave Jiang wq->id = i; 2097c5dd23eSDave Jiang wq->idxd = idxd; 210700af3a0SDave Jiang device_initialize(wq_confdev(wq)); 211700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 212700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 213700af3a0SDave Jiang conf_dev->type = &idxd_wq_device_type; 214700af3a0SDave Jiang rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id); 2157c5dd23eSDave Jiang if (rc < 0) { 216700af3a0SDave Jiang put_device(conf_dev); 2177c5dd23eSDave Jiang goto err; 2187c5dd23eSDave Jiang } 2197c5dd23eSDave Jiang 2207c5dd23eSDave Jiang mutex_init(&wq->wq_lock); 22104922b74SDave Jiang init_waitqueue_head(&wq->err_queue); 22293a40a6dSDave Jiang init_completion(&wq->wq_dead); 22356fc39f5SDave Jiang init_completion(&wq->wq_resurrect); 22492452a72SDave Jiang wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER; 22592452a72SDave Jiang wq->max_batch_size = WQ_DEFAULT_MAX_BATCH; 2267930d855SDave Jiang wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES; 2277c5dd23eSDave Jiang wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); 2287c5dd23eSDave Jiang if (!wq->wqcfg) { 229700af3a0SDave Jiang put_device(conf_dev); 2307c5dd23eSDave Jiang rc = -ENOMEM; 2317c5dd23eSDave Jiang goto err; 2327c5dd23eSDave Jiang } 2337c5dd23eSDave Jiang idxd->wqs[i] = wq; 2347c5dd23eSDave Jiang } 2357c5dd23eSDave Jiang 2367c5dd23eSDave Jiang return 0; 2377c5dd23eSDave Jiang 2387c5dd23eSDave Jiang err: 239700af3a0SDave Jiang while (--i >= 0) { 240700af3a0SDave Jiang wq = idxd->wqs[i]; 241700af3a0SDave Jiang conf_dev = wq_confdev(wq); 242700af3a0SDave Jiang put_device(conf_dev); 243700af3a0SDave Jiang } 2447c5dd23eSDave Jiang return rc; 2457c5dd23eSDave Jiang } 2467c5dd23eSDave Jiang 24775b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd) 24875b91130SDave Jiang { 24975b91130SDave Jiang struct idxd_engine *engine; 25075b91130SDave Jiang struct device *dev = &idxd->pdev->dev; 251700af3a0SDave Jiang struct device *conf_dev; 25275b91130SDave Jiang int i, rc; 25375b91130SDave Jiang 25475b91130SDave Jiang idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), 25575b91130SDave Jiang GFP_KERNEL, dev_to_node(dev)); 25675b91130SDave Jiang if (!idxd->engines) 25775b91130SDave Jiang return -ENOMEM; 25875b91130SDave Jiang 25975b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) { 26075b91130SDave Jiang engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); 26175b91130SDave Jiang if (!engine) { 26275b91130SDave Jiang rc = -ENOMEM; 26375b91130SDave Jiang goto err; 26475b91130SDave Jiang } 26575b91130SDave Jiang 266700af3a0SDave Jiang idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE); 267700af3a0SDave Jiang conf_dev = engine_confdev(engine); 26875b91130SDave Jiang engine->id = i; 26975b91130SDave Jiang engine->idxd = idxd; 270700af3a0SDave Jiang device_initialize(conf_dev); 271700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 272700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 273700af3a0SDave Jiang conf_dev->type = &idxd_engine_device_type; 274700af3a0SDave Jiang rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id); 27575b91130SDave Jiang if (rc < 0) { 276700af3a0SDave Jiang put_device(conf_dev); 27775b91130SDave Jiang goto err; 27875b91130SDave Jiang } 27975b91130SDave Jiang 28075b91130SDave Jiang idxd->engines[i] = engine; 28175b91130SDave Jiang } 28275b91130SDave Jiang 28375b91130SDave Jiang return 0; 28475b91130SDave Jiang 28575b91130SDave Jiang err: 286700af3a0SDave Jiang while (--i >= 0) { 287700af3a0SDave Jiang engine = idxd->engines[i]; 288700af3a0SDave Jiang conf_dev = engine_confdev(engine); 289700af3a0SDave Jiang put_device(conf_dev); 290700af3a0SDave Jiang } 29175b91130SDave Jiang return rc; 29275b91130SDave Jiang } 29375b91130SDave Jiang 294defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd) 295defe49f9SDave Jiang { 296defe49f9SDave Jiang struct device *dev = &idxd->pdev->dev; 297700af3a0SDave Jiang struct device *conf_dev; 298defe49f9SDave Jiang struct idxd_group *group; 299defe49f9SDave Jiang int i, rc; 300defe49f9SDave Jiang 301defe49f9SDave Jiang idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), 302defe49f9SDave Jiang GFP_KERNEL, dev_to_node(dev)); 303defe49f9SDave Jiang if (!idxd->groups) 304defe49f9SDave Jiang return -ENOMEM; 305defe49f9SDave Jiang 306defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) { 307defe49f9SDave Jiang group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); 308defe49f9SDave Jiang if (!group) { 309defe49f9SDave Jiang rc = -ENOMEM; 310defe49f9SDave Jiang goto err; 311defe49f9SDave Jiang } 312defe49f9SDave Jiang 313700af3a0SDave Jiang idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP); 314700af3a0SDave Jiang conf_dev = group_confdev(group); 315defe49f9SDave Jiang group->id = i; 316defe49f9SDave Jiang group->idxd = idxd; 317700af3a0SDave Jiang device_initialize(conf_dev); 318700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 319700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 320700af3a0SDave Jiang conf_dev->type = &idxd_group_device_type; 321700af3a0SDave Jiang rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id); 322defe49f9SDave Jiang if (rc < 0) { 323700af3a0SDave Jiang put_device(conf_dev); 324defe49f9SDave Jiang goto err; 325defe49f9SDave Jiang } 326defe49f9SDave Jiang 327defe49f9SDave Jiang idxd->groups[i] = group; 328ade8a86bSDave Jiang if (idxd->hw.version < DEVICE_VERSION_2 && !tc_override) { 329ade8a86bSDave Jiang group->tc_a = 1; 330ade8a86bSDave Jiang group->tc_b = 1; 331ade8a86bSDave Jiang } else { 332defe49f9SDave Jiang group->tc_a = -1; 333defe49f9SDave Jiang group->tc_b = -1; 334defe49f9SDave Jiang } 335ade8a86bSDave Jiang } 336defe49f9SDave Jiang 337defe49f9SDave Jiang return 0; 338defe49f9SDave Jiang 339defe49f9SDave Jiang err: 340700af3a0SDave Jiang while (--i >= 0) { 341700af3a0SDave Jiang group = idxd->groups[i]; 342700af3a0SDave Jiang put_device(group_confdev(group)); 343700af3a0SDave Jiang } 344defe49f9SDave Jiang return rc; 345defe49f9SDave Jiang } 346defe49f9SDave Jiang 347ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd) 348ddf742d4SDave Jiang { 349ddf742d4SDave Jiang int i; 350ddf742d4SDave Jiang 351ddf742d4SDave Jiang for (i = 0; i < idxd->max_groups; i++) 352700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 353ddf742d4SDave Jiang for (i = 0; i < idxd->max_engines; i++) 354700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 355ddf742d4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) 356700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 357ddf742d4SDave Jiang destroy_workqueue(idxd->wq); 358ddf742d4SDave Jiang } 359ddf742d4SDave Jiang 360bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd) 361bfe1d560SDave Jiang { 362bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 363defe49f9SDave Jiang int rc, i; 364bfe1d560SDave Jiang 3650d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq); 3667c5dd23eSDave Jiang 3677c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd); 3687c5dd23eSDave Jiang if (rc < 0) 369eb15e715SDave Jiang goto err_wqs; 3707c5dd23eSDave Jiang 37175b91130SDave Jiang rc = idxd_setup_engines(idxd); 37275b91130SDave Jiang if (rc < 0) 37375b91130SDave Jiang goto err_engine; 37475b91130SDave Jiang 375defe49f9SDave Jiang rc = idxd_setup_groups(idxd); 376defe49f9SDave Jiang if (rc < 0) 377defe49f9SDave Jiang goto err_group; 378bfe1d560SDave Jiang 3790d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 3807c5dd23eSDave Jiang if (!idxd->wq) { 3817c5dd23eSDave Jiang rc = -ENOMEM; 382defe49f9SDave Jiang goto err_wkq_create; 3837c5dd23eSDave Jiang } 3840d5c10b4SDave Jiang 385bfe1d560SDave Jiang return 0; 3867c5dd23eSDave Jiang 387defe49f9SDave Jiang err_wkq_create: 388defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) 389700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 390defe49f9SDave Jiang err_group: 39175b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) 392700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 39375b91130SDave Jiang err_engine: 3947c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) 395700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 396eb15e715SDave Jiang err_wqs: 3977c5dd23eSDave Jiang return rc; 398bfe1d560SDave Jiang } 399bfe1d560SDave Jiang 400bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 401bfe1d560SDave Jiang { 402bfe1d560SDave Jiang union offsets_reg offsets; 403bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 404bfe1d560SDave Jiang 405bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 4062f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 4072f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 408bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 4092f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 4102f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 4112f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 4122f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 4132f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 414bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 415bfe1d560SDave Jiang } 416bfe1d560SDave Jiang 417bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 418bfe1d560SDave Jiang { 419bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 420bfe1d560SDave Jiang int i; 421bfe1d560SDave Jiang 422bfe1d560SDave Jiang /* reading generic capabilities */ 423bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 424bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 425eb15e715SDave Jiang 426eb15e715SDave Jiang if (idxd->hw.gen_cap.cmd_cap) { 427eb15e715SDave Jiang idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET); 428eb15e715SDave Jiang dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap); 429eb15e715SDave Jiang } 430eb15e715SDave Jiang 4318b67426eSDave Jiang /* reading command capabilities */ 4328b67426eSDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) 4338b67426eSDave Jiang idxd->request_int_handles = true; 4348b67426eSDave Jiang 435bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 436bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 437bfe1d560SDave Jiang idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift; 438bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 439bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 440bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 441bfe1d560SDave Jiang 442bfe1d560SDave Jiang /* reading group capabilities */ 443bfe1d560SDave Jiang idxd->hw.group_cap.bits = 444bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 445bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 446bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 447bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 448bfe1d560SDave Jiang idxd->max_tokens = idxd->hw.group_cap.total_tokens; 449bfe1d560SDave Jiang dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens); 450c52ca478SDave Jiang idxd->nr_tokens = idxd->max_tokens; 451bfe1d560SDave Jiang 452bfe1d560SDave Jiang /* read engine capabilities */ 453bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 454bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 455bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 456bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 457bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 458bfe1d560SDave Jiang 459bfe1d560SDave Jiang /* read workqueue capabilities */ 460bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 461bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 462bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 463bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 464bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 465bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 466d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 467d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 468bfe1d560SDave Jiang 469bfe1d560SDave Jiang /* reading operation capabilities */ 470bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 471bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 472bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 473bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 474bfe1d560SDave Jiang } 475bfe1d560SDave Jiang } 476bfe1d560SDave Jiang 477435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) 478bfe1d560SDave Jiang { 479bfe1d560SDave Jiang struct device *dev = &pdev->dev; 480700af3a0SDave Jiang struct device *conf_dev; 481bfe1d560SDave Jiang struct idxd_device *idxd; 48247c16ac2SDave Jiang int rc; 483bfe1d560SDave Jiang 48447c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 485bfe1d560SDave Jiang if (!idxd) 486bfe1d560SDave Jiang return NULL; 487bfe1d560SDave Jiang 488700af3a0SDave Jiang conf_dev = idxd_confdev(idxd); 489bfe1d560SDave Jiang idxd->pdev = pdev; 490435b512dSDave Jiang idxd->data = data; 491700af3a0SDave Jiang idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type); 4924b73e4ebSDave Jiang idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); 49347c16ac2SDave Jiang if (idxd->id < 0) 49447c16ac2SDave Jiang return NULL; 49547c16ac2SDave Jiang 496700af3a0SDave Jiang device_initialize(conf_dev); 497700af3a0SDave Jiang conf_dev->parent = dev; 498700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 499700af3a0SDave Jiang conf_dev->type = idxd->data->dev_type; 500700af3a0SDave Jiang rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); 50147c16ac2SDave Jiang if (rc < 0) { 502700af3a0SDave Jiang put_device(conf_dev); 50347c16ac2SDave Jiang return NULL; 50447c16ac2SDave Jiang } 50547c16ac2SDave Jiang 506bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 50753b2ee7fSDave Jiang spin_lock_init(&idxd->cmd_lock); 508bfe1d560SDave Jiang 509bfe1d560SDave Jiang return idxd; 510bfe1d560SDave Jiang } 511bfe1d560SDave Jiang 5128e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 5138e50d392SDave Jiang { 5148e50d392SDave Jiang int flags; 5158e50d392SDave Jiang unsigned int pasid; 5168e50d392SDave Jiang struct iommu_sva *sva; 5178e50d392SDave Jiang 5188e50d392SDave Jiang flags = SVM_FLAG_SUPERVISOR_MODE; 5198e50d392SDave Jiang 5208e50d392SDave Jiang sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags); 5218e50d392SDave Jiang if (IS_ERR(sva)) { 5228e50d392SDave Jiang dev_warn(&idxd->pdev->dev, 5238e50d392SDave Jiang "iommu sva bind failed: %ld\n", PTR_ERR(sva)); 5248e50d392SDave Jiang return PTR_ERR(sva); 5258e50d392SDave Jiang } 5268e50d392SDave Jiang 5278e50d392SDave Jiang pasid = iommu_sva_get_pasid(sva); 5288e50d392SDave Jiang if (pasid == IOMMU_PASID_INVALID) { 5298e50d392SDave Jiang iommu_sva_unbind_device(sva); 5308e50d392SDave Jiang return -ENODEV; 5318e50d392SDave Jiang } 5328e50d392SDave Jiang 5338e50d392SDave Jiang idxd->sva = sva; 5348e50d392SDave Jiang idxd->pasid = pasid; 5358e50d392SDave Jiang dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid); 5368e50d392SDave Jiang return 0; 5378e50d392SDave Jiang } 5388e50d392SDave Jiang 5398e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 5408e50d392SDave Jiang { 5418e50d392SDave Jiang 5428e50d392SDave Jiang iommu_sva_unbind_device(idxd->sva); 5438e50d392SDave Jiang idxd->sva = NULL; 5448e50d392SDave Jiang } 5458e50d392SDave Jiang 546bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 547bfe1d560SDave Jiang { 548bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 549bfe1d560SDave Jiang struct device *dev = &pdev->dev; 550bfe1d560SDave Jiang int rc; 551bfe1d560SDave Jiang 552bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 55389e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 55489e3becdSDave Jiang if (rc < 0) 55589e3becdSDave Jiang return rc; 55689e3becdSDave Jiang 557bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 558bfe1d560SDave Jiang 55903d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 560cf5f86a7SDave Jiang rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA); 561cf5f86a7SDave Jiang if (rc == 0) { 5628e50d392SDave Jiang rc = idxd_enable_system_pasid(idxd); 563cf5f86a7SDave Jiang if (rc < 0) { 564cf5f86a7SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 5658e50d392SDave Jiang dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); 566cf5f86a7SDave Jiang } else { 5678e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 568cf5f86a7SDave Jiang } 569cf5f86a7SDave Jiang } else { 570cf5f86a7SDave Jiang dev_warn(dev, "Unable to turn on SVA feature.\n"); 571cf5f86a7SDave Jiang } 57203d939c7SDave Jiang } else if (!sva) { 57303d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n"); 5748e50d392SDave Jiang } 5758e50d392SDave Jiang 576bfe1d560SDave Jiang idxd_read_caps(idxd); 577bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 578bfe1d560SDave Jiang 579bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 580bfe1d560SDave Jiang if (rc) 5817c5dd23eSDave Jiang goto err; 582bfe1d560SDave Jiang 5838c66bbdcSDave Jiang /* If the configs are readonly, then load them from device */ 5848c66bbdcSDave Jiang if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { 5858c66bbdcSDave Jiang dev_dbg(dev, "Loading RO device config\n"); 5868c66bbdcSDave Jiang rc = idxd_device_load_config(idxd); 5878c66bbdcSDave Jiang if (rc < 0) 588ddf742d4SDave Jiang goto err_config; 5898c66bbdcSDave Jiang } 5908c66bbdcSDave Jiang 591bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 592bfe1d560SDave Jiang if (rc) 593ddf742d4SDave Jiang goto err_config; 594bfe1d560SDave Jiang 595bfe1d560SDave Jiang dev_dbg(dev, "IDXD interrupt setup complete.\n"); 596bfe1d560SDave Jiang 59742d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 59842d279f9SDave Jiang 5990bde4444STom Zanussi rc = perfmon_pmu_init(idxd); 6000bde4444STom Zanussi if (rc < 0) 6010bde4444STom Zanussi dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc); 6020bde4444STom Zanussi 603bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 604bfe1d560SDave Jiang return 0; 605bfe1d560SDave Jiang 606ddf742d4SDave Jiang err_config: 607ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 6087c5dd23eSDave Jiang err: 6098e50d392SDave Jiang if (device_pasid_enabled(idxd)) 6108e50d392SDave Jiang idxd_disable_system_pasid(idxd); 611cf5f86a7SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 612bfe1d560SDave Jiang return rc; 613bfe1d560SDave Jiang } 614bfe1d560SDave Jiang 615ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd) 616ddf742d4SDave Jiang { 617ddf742d4SDave Jiang struct device *dev = &idxd->pdev->dev; 618ddf742d4SDave Jiang 619ddf742d4SDave Jiang perfmon_pmu_remove(idxd); 620ddf742d4SDave Jiang idxd_cleanup_interrupts(idxd); 621ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 622ddf742d4SDave Jiang if (device_pasid_enabled(idxd)) 623ddf742d4SDave Jiang idxd_disable_system_pasid(idxd); 624ddf742d4SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 625ddf742d4SDave Jiang } 626ddf742d4SDave Jiang 627bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 628bfe1d560SDave Jiang { 629bfe1d560SDave Jiang struct device *dev = &pdev->dev; 630bfe1d560SDave Jiang struct idxd_device *idxd; 631435b512dSDave Jiang struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data; 632bfe1d560SDave Jiang int rc; 633bfe1d560SDave Jiang 634a39c7cd0SDave Jiang rc = pci_enable_device(pdev); 635bfe1d560SDave Jiang if (rc) 636bfe1d560SDave Jiang return rc; 637bfe1d560SDave Jiang 6388e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 639435b512dSDave Jiang idxd = idxd_alloc(pdev, data); 640a39c7cd0SDave Jiang if (!idxd) { 641a39c7cd0SDave Jiang rc = -ENOMEM; 642a39c7cd0SDave Jiang goto err_idxd_alloc; 643a39c7cd0SDave Jiang } 644bfe1d560SDave Jiang 6458e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 646a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 647a39c7cd0SDave Jiang if (!idxd->reg_base) { 648a39c7cd0SDave Jiang rc = -ENOMEM; 649a39c7cd0SDave Jiang goto err_iomap; 650a39c7cd0SDave Jiang } 651bfe1d560SDave Jiang 652bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 65353b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 654bfe1d560SDave Jiang if (rc) 65553b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 656bfe1d560SDave Jiang if (rc) 657a39c7cd0SDave Jiang goto err; 658bfe1d560SDave Jiang 659bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 660bfe1d560SDave Jiang pci_set_master(pdev); 661bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 662bfe1d560SDave Jiang 663bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 664bfe1d560SDave Jiang rc = idxd_probe(idxd); 665bfe1d560SDave Jiang if (rc) { 666bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 667a39c7cd0SDave Jiang goto err; 668bfe1d560SDave Jiang } 669bfe1d560SDave Jiang 67047c16ac2SDave Jiang rc = idxd_register_devices(idxd); 671c52ca478SDave Jiang if (rc) { 672c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 673ddf742d4SDave Jiang goto err_dev_register; 674c52ca478SDave Jiang } 675c52ca478SDave Jiang 676bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 677bfe1d560SDave Jiang idxd->hw.version); 678bfe1d560SDave Jiang 679bfe1d560SDave Jiang return 0; 680a39c7cd0SDave Jiang 681ddf742d4SDave Jiang err_dev_register: 682ddf742d4SDave Jiang idxd_cleanup(idxd); 683a39c7cd0SDave Jiang err: 684a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 685a39c7cd0SDave Jiang err_iomap: 686700af3a0SDave Jiang put_device(idxd_confdev(idxd)); 687a39c7cd0SDave Jiang err_idxd_alloc: 688a39c7cd0SDave Jiang pci_disable_device(pdev); 689a39c7cd0SDave Jiang return rc; 690bfe1d560SDave Jiang } 691bfe1d560SDave Jiang 6928f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) 6938f47d1a5SDave Jiang { 6948f47d1a5SDave Jiang struct idxd_desc *desc, *itr; 6958f47d1a5SDave Jiang struct llist_node *head; 6968f47d1a5SDave Jiang 6978f47d1a5SDave Jiang head = llist_del_all(&ie->pending_llist); 6988f47d1a5SDave Jiang if (!head) 6998f47d1a5SDave Jiang return; 7008f47d1a5SDave Jiang 7015d78abb6SDave Jiang llist_for_each_entry_safe(desc, itr, head, llnode) 7025d78abb6SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true); 7038f47d1a5SDave Jiang } 7048f47d1a5SDave Jiang 7058f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie) 7068f47d1a5SDave Jiang { 7078f47d1a5SDave Jiang struct idxd_desc *desc, *iter; 7088f47d1a5SDave Jiang 7098f47d1a5SDave Jiang list_for_each_entry_safe(desc, iter, &ie->work_list, list) { 7108f47d1a5SDave Jiang list_del(&desc->list); 7115d78abb6SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true); 7128f47d1a5SDave Jiang } 7138f47d1a5SDave Jiang } 7148f47d1a5SDave Jiang 7155b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd) 7165b0c68c4SDave Jiang { 7175b0c68c4SDave Jiang struct idxd_wq *wq; 7185b0c68c4SDave Jiang int i; 7195b0c68c4SDave Jiang 7205b0c68c4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 7215b0c68c4SDave Jiang wq = idxd->wqs[i]; 7225b0c68c4SDave Jiang if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL) 7235b0c68c4SDave Jiang idxd_wq_quiesce(wq); 7245b0c68c4SDave Jiang } 7255b0c68c4SDave Jiang } 7265b0c68c4SDave Jiang 727eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd) 728eb15e715SDave Jiang { 729eb15e715SDave Jiang struct device *dev = &idxd->pdev->dev; 730eb15e715SDave Jiang int i, rc; 731eb15e715SDave Jiang 7328b67426eSDave Jiang for (i = 1; i < idxd->irq_cnt; i++) { 733*ec0d6423SDave Jiang struct idxd_irq_entry *ie = idxd_get_ie(idxd, i); 7348b67426eSDave Jiang 7358b67426eSDave Jiang if (ie->int_handle != INVALID_INT_HANDLE) { 7368b67426eSDave Jiang rc = idxd_device_release_int_handle(idxd, ie->int_handle, IDXD_IRQ_MSIX); 737eb15e715SDave Jiang if (rc < 0) 7388b67426eSDave Jiang dev_warn(dev, "irq handle %d release failed\n", ie->int_handle); 739eb15e715SDave Jiang else 7408b67426eSDave Jiang dev_dbg(dev, "int handle released: %u\n", ie->int_handle); 741eb15e715SDave Jiang } 742eb15e715SDave Jiang } 743eb15e715SDave Jiang } 744eb15e715SDave Jiang 745bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 746bfe1d560SDave Jiang { 747bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 748bfe1d560SDave Jiang int rc, i; 749bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 750bfe1d560SDave Jiang int msixcnt = pci_msix_vec_count(pdev); 751bfe1d560SDave Jiang 752bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 753bfe1d560SDave Jiang if (rc) 754bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 755bfe1d560SDave Jiang 756bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 757bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 758bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 759bfe1d560SDave Jiang 760bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 761*ec0d6423SDave Jiang irq_entry = idxd_get_ie(idxd, i); 7625fc8e85fSDave Jiang synchronize_irq(irq_entry->vector); 763bfe1d560SDave Jiang if (i == 0) 764bfe1d560SDave Jiang continue; 7658f47d1a5SDave Jiang idxd_flush_pending_llist(irq_entry); 7668f47d1a5SDave Jiang idxd_flush_work_list(irq_entry); 767bfe1d560SDave Jiang } 76849c4959fSDave Jiang flush_workqueue(idxd->wq); 769bfe1d560SDave Jiang } 770bfe1d560SDave Jiang 771bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 772bfe1d560SDave Jiang { 773bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 77449c4959fSDave Jiang struct idxd_irq_entry *irq_entry; 77549c4959fSDave Jiang int msixcnt = pci_msix_vec_count(pdev); 77649c4959fSDave Jiang int i; 777bfe1d560SDave Jiang 77898da0106SDave Jiang idxd_unregister_devices(idxd); 77998da0106SDave Jiang /* 78098da0106SDave Jiang * When ->release() is called for the idxd->conf_dev, it frees all the memory related 78198da0106SDave Jiang * to the idxd context. The driver still needs those bits in order to do the rest of 78298da0106SDave Jiang * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref 78398da0106SDave Jiang * on the device here to hold off the freeing while allowing the idxd sub-driver 78498da0106SDave Jiang * to unbind. 78598da0106SDave Jiang */ 78698da0106SDave Jiang get_device(idxd_confdev(idxd)); 78798da0106SDave Jiang device_unregister(idxd_confdev(idxd)); 788bfe1d560SDave Jiang idxd_shutdown(pdev); 7898e50d392SDave Jiang if (device_pasid_enabled(idxd)) 7908e50d392SDave Jiang idxd_disable_system_pasid(idxd); 79149c4959fSDave Jiang 79249c4959fSDave Jiang for (i = 0; i < msixcnt; i++) { 793*ec0d6423SDave Jiang irq_entry = idxd_get_ie(idxd, i); 79449c4959fSDave Jiang free_irq(irq_entry->vector, irq_entry); 79549c4959fSDave Jiang } 79649c4959fSDave Jiang idxd_msix_perm_clear(idxd); 79749c4959fSDave Jiang idxd_release_int_handles(idxd); 79849c4959fSDave Jiang pci_free_irq_vectors(pdev); 79949c4959fSDave Jiang pci_iounmap(pdev, idxd->reg_base); 800cf5f86a7SDave Jiang iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 80149c4959fSDave Jiang pci_disable_device(pdev); 80249c4959fSDave Jiang destroy_workqueue(idxd->wq); 80349c4959fSDave Jiang perfmon_pmu_remove(idxd); 80498da0106SDave Jiang put_device(idxd_confdev(idxd)); 805bfe1d560SDave Jiang } 806bfe1d560SDave Jiang 807bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 808bfe1d560SDave Jiang .name = DRV_NAME, 809bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 810bfe1d560SDave Jiang .probe = idxd_pci_probe, 811bfe1d560SDave Jiang .remove = idxd_remove, 812bfe1d560SDave Jiang .shutdown = idxd_shutdown, 813bfe1d560SDave Jiang }; 814bfe1d560SDave Jiang 815bfe1d560SDave Jiang static int __init idxd_init_module(void) 816bfe1d560SDave Jiang { 8174b73e4ebSDave Jiang int err; 818bfe1d560SDave Jiang 819bfe1d560SDave Jiang /* 8208e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 821bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 822bfe1d560SDave Jiang */ 82374b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) { 824bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 825bfe1d560SDave Jiang return -ENODEV; 826bfe1d560SDave Jiang } 827bfe1d560SDave Jiang 82874b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_ENQCMD)) 8298e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 8308e50d392SDave Jiang else 8318e50d392SDave Jiang support_enqcmd = true; 832bfe1d560SDave Jiang 8330bde4444STom Zanussi perfmon_init(); 8340bde4444STom Zanussi 835034b3290SDave Jiang err = idxd_driver_register(&idxd_drv); 836034b3290SDave Jiang if (err < 0) 837034b3290SDave Jiang goto err_idxd_driver_register; 838034b3290SDave Jiang 8390cda4f69SDave Jiang err = idxd_driver_register(&idxd_dmaengine_drv); 8400cda4f69SDave Jiang if (err < 0) 8410cda4f69SDave Jiang goto err_idxd_dmaengine_driver_register; 8420cda4f69SDave Jiang 843448c3de8SDave Jiang err = idxd_driver_register(&idxd_user_drv); 844448c3de8SDave Jiang if (err < 0) 845448c3de8SDave Jiang goto err_idxd_user_driver_register; 846448c3de8SDave Jiang 84742d279f9SDave Jiang err = idxd_cdev_register(); 84842d279f9SDave Jiang if (err) 84942d279f9SDave Jiang goto err_cdev_register; 85042d279f9SDave Jiang 851c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 852c52ca478SDave Jiang if (err) 853c52ca478SDave Jiang goto err_pci_register; 854c52ca478SDave Jiang 855bfe1d560SDave Jiang return 0; 856c52ca478SDave Jiang 857c52ca478SDave Jiang err_pci_register: 85842d279f9SDave Jiang idxd_cdev_remove(); 85942d279f9SDave Jiang err_cdev_register: 860448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 861448c3de8SDave Jiang err_idxd_user_driver_register: 8620cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 8630cda4f69SDave Jiang err_idxd_dmaengine_driver_register: 864034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 865034b3290SDave Jiang err_idxd_driver_register: 866c52ca478SDave Jiang return err; 867bfe1d560SDave Jiang } 868bfe1d560SDave Jiang module_init(idxd_init_module); 869bfe1d560SDave Jiang 870bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 871bfe1d560SDave Jiang { 872448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 8730cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 874034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 875bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 87642d279f9SDave Jiang idxd_cdev_remove(); 8770bde4444STom Zanussi perfmon_exit(); 878bfe1d560SDave Jiang } 879bfe1d560SDave Jiang module_exit(idxd_exit_module); 880