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/fs.h> 13bfe1d560SDave Jiang #include <linux/io-64-nonatomic-lo-hi.h> 14bfe1d560SDave Jiang #include <linux/device.h> 15bfe1d560SDave Jiang #include <linux/idr.h> 168e50d392SDave Jiang #include <linux/iommu.h> 17bfe1d560SDave Jiang #include <uapi/linux/idxd.h> 188f47d1a5SDave Jiang #include <linux/dmaengine.h> 198f47d1a5SDave Jiang #include "../dmaengine.h" 20bfe1d560SDave Jiang #include "registers.h" 21bfe1d560SDave Jiang #include "idxd.h" 220bde4444STom Zanussi #include "perfmon.h" 23bfe1d560SDave Jiang 24bfe1d560SDave Jiang MODULE_VERSION(IDXD_DRIVER_VERSION); 25bfe1d560SDave Jiang MODULE_LICENSE("GPL v2"); 26bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation"); 27d9e5481fSDave Jiang MODULE_IMPORT_NS(IDXD); 28bfe1d560SDave Jiang 2903d939c7SDave Jiang static bool sva = true; 3003d939c7SDave Jiang module_param(sva, bool, 0644); 3103d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off"); 3203d939c7SDave Jiang 33ade8a86bSDave Jiang bool tc_override; 34ade8a86bSDave Jiang module_param(tc_override, bool, 0644); 35ade8a86bSDave Jiang MODULE_PARM_DESC(tc_override, "Override traffic class defaults"); 36ade8a86bSDave Jiang 37bfe1d560SDave Jiang #define DRV_NAME "idxd" 38bfe1d560SDave Jiang 398e50d392SDave Jiang bool support_enqcmd; 404b73e4ebSDave Jiang DEFINE_IDA(idxd_ida); 41bfe1d560SDave Jiang 42435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = { 43435b512dSDave Jiang [IDXD_TYPE_DSA] = { 44435b512dSDave Jiang .name_prefix = "dsa", 45435b512dSDave Jiang .type = IDXD_TYPE_DSA, 46435b512dSDave Jiang .compl_size = sizeof(struct dsa_completion_record), 47435b512dSDave Jiang .align = 32, 48435b512dSDave Jiang .dev_type = &dsa_device_type, 49c40bd7d9SDave Jiang .evl_cr_off = offsetof(struct dsa_evl_entry, cr), 50e11452ebSArjan van de Ven .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */ 512442b747SDave Jiang .cr_status_off = offsetof(struct dsa_completion_record, status), 522442b747SDave Jiang .cr_result_off = offsetof(struct dsa_completion_record, result), 53435b512dSDave Jiang }, 54435b512dSDave Jiang [IDXD_TYPE_IAX] = { 55435b512dSDave Jiang .name_prefix = "iax", 56435b512dSDave Jiang .type = IDXD_TYPE_IAX, 57435b512dSDave Jiang .compl_size = sizeof(struct iax_completion_record), 58435b512dSDave Jiang .align = 64, 59435b512dSDave Jiang .dev_type = &iax_device_type, 60c40bd7d9SDave Jiang .evl_cr_off = offsetof(struct iax_evl_entry, cr), 61e11452ebSArjan van de Ven .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */ 622442b747SDave Jiang .cr_status_off = offsetof(struct iax_completion_record, status), 632442b747SDave Jiang .cr_result_off = offsetof(struct iax_completion_record, error_code), 64979f6dedSTom Zanussi .load_device_defaults = idxd_load_iaa_device_defaults, 65435b512dSDave Jiang }, 66435b512dSDave Jiang }; 67435b512dSDave Jiang 68bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = { 69bfe1d560SDave Jiang /* DSA ver 1.0 platforms */ 70435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) }, 71f25b4638SDave Jiang 72f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 73435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) }, 74bfe1d560SDave Jiang { 0, } 75bfe1d560SDave Jiang }; 76bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 77bfe1d560SDave Jiang 78bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd) 79bfe1d560SDave Jiang { 80bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 81bfe1d560SDave Jiang struct device *dev = &pdev->dev; 82ec0d6423SDave Jiang struct idxd_irq_entry *ie; 83bfe1d560SDave Jiang int i, msixcnt; 84bfe1d560SDave Jiang int rc = 0; 85bfe1d560SDave Jiang 86bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev); 87bfe1d560SDave Jiang if (msixcnt < 0) { 88bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n"); 895fc8e85fSDave Jiang return -ENOSPC; 90bfe1d560SDave Jiang } 918b67426eSDave Jiang idxd->irq_cnt = msixcnt; 92bfe1d560SDave Jiang 935fc8e85fSDave Jiang rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); 945fc8e85fSDave Jiang if (rc != msixcnt) { 955fc8e85fSDave Jiang dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); 965fc8e85fSDave Jiang return -ENOSPC; 97bfe1d560SDave Jiang } 98bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 99bfe1d560SDave Jiang 100d5c10e0fSDave Jiang 101ec0d6423SDave Jiang ie = idxd_get_ie(idxd, 0); 102ec0d6423SDave Jiang ie->vector = pci_irq_vector(pdev, 0); 103ec0d6423SDave Jiang rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie); 104bfe1d560SDave Jiang if (rc < 0) { 105bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n"); 1065fc8e85fSDave Jiang goto err_misc_irq; 107bfe1d560SDave Jiang } 108403a2e23SDave Jiang dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector); 109bfe1d560SDave Jiang 110ec0d6423SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 111ec0d6423SDave Jiang int msix_idx = i + 1; 112bfe1d560SDave Jiang 113ec0d6423SDave Jiang ie = idxd_get_ie(idxd, msix_idx); 114ec0d6423SDave Jiang ie->id = msix_idx; 115ec0d6423SDave Jiang ie->int_handle = INVALID_INT_HANDLE; 116fffaed1eSJacob Pan ie->pasid = IOMMU_PASID_INVALID; 117403a2e23SDave Jiang 118ec0d6423SDave Jiang spin_lock_init(&ie->list_lock); 119ec0d6423SDave Jiang init_llist_head(&ie->pending_llist); 120ec0d6423SDave Jiang INIT_LIST_HEAD(&ie->work_list); 121bfe1d560SDave Jiang } 122bfe1d560SDave Jiang 123bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd); 124bfe1d560SDave Jiang return 0; 125bfe1d560SDave Jiang 1265fc8e85fSDave Jiang err_misc_irq: 127bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 1285fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 129bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n"); 130bfe1d560SDave Jiang return rc; 131bfe1d560SDave Jiang } 132bfe1d560SDave Jiang 133ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd) 134ddf742d4SDave Jiang { 135ddf742d4SDave Jiang struct pci_dev *pdev = idxd->pdev; 136ec0d6423SDave Jiang struct idxd_irq_entry *ie; 137403a2e23SDave Jiang int msixcnt; 138ddf742d4SDave Jiang 139403a2e23SDave Jiang msixcnt = pci_msix_vec_count(pdev); 140403a2e23SDave Jiang if (msixcnt <= 0) 141403a2e23SDave Jiang return; 142ddf742d4SDave Jiang 143403a2e23SDave Jiang ie = idxd_get_ie(idxd, 0); 144ddf742d4SDave Jiang idxd_mask_error_interrupts(idxd); 145403a2e23SDave Jiang free_irq(ie->vector, ie); 146ddf742d4SDave Jiang pci_free_irq_vectors(pdev); 147ddf742d4SDave Jiang } 148ddf742d4SDave Jiang 1497c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd) 1507c5dd23eSDave Jiang { 1517c5dd23eSDave Jiang struct device *dev = &idxd->pdev->dev; 1527c5dd23eSDave Jiang struct idxd_wq *wq; 153700af3a0SDave Jiang struct device *conf_dev; 1547c5dd23eSDave Jiang int i, rc; 1557c5dd23eSDave Jiang 1567c5dd23eSDave Jiang idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 1577c5dd23eSDave Jiang GFP_KERNEL, dev_to_node(dev)); 1587c5dd23eSDave Jiang if (!idxd->wqs) 1597c5dd23eSDave Jiang return -ENOMEM; 1607c5dd23eSDave Jiang 161de5819b9SJerry Snitselaar idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev)); 162de5819b9SJerry Snitselaar if (!idxd->wq_enable_map) { 163de5819b9SJerry Snitselaar kfree(idxd->wqs); 164de5819b9SJerry Snitselaar return -ENOMEM; 165de5819b9SJerry Snitselaar } 166de5819b9SJerry Snitselaar 1677c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 1687c5dd23eSDave Jiang wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); 1697c5dd23eSDave Jiang if (!wq) { 1707c5dd23eSDave Jiang rc = -ENOMEM; 1717c5dd23eSDave Jiang goto err; 1727c5dd23eSDave Jiang } 1737c5dd23eSDave Jiang 174700af3a0SDave Jiang idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ); 175700af3a0SDave Jiang conf_dev = wq_confdev(wq); 1767c5dd23eSDave Jiang wq->id = i; 1777c5dd23eSDave Jiang wq->idxd = idxd; 178700af3a0SDave Jiang device_initialize(wq_confdev(wq)); 179700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 180700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 181700af3a0SDave Jiang conf_dev->type = &idxd_wq_device_type; 182700af3a0SDave Jiang rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id); 1837c5dd23eSDave Jiang if (rc < 0) { 184700af3a0SDave Jiang put_device(conf_dev); 1857c5dd23eSDave Jiang goto err; 1867c5dd23eSDave Jiang } 1877c5dd23eSDave Jiang 1887c5dd23eSDave Jiang mutex_init(&wq->wq_lock); 18904922b74SDave Jiang init_waitqueue_head(&wq->err_queue); 19093a40a6dSDave Jiang init_completion(&wq->wq_dead); 19156fc39f5SDave Jiang init_completion(&wq->wq_resurrect); 19292452a72SDave Jiang wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER; 193e8dbd644SXiaochen Shen idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH); 1947930d855SDave Jiang wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES; 1957c5dd23eSDave Jiang wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); 1967c5dd23eSDave Jiang if (!wq->wqcfg) { 197700af3a0SDave Jiang put_device(conf_dev); 1987c5dd23eSDave Jiang rc = -ENOMEM; 1997c5dd23eSDave Jiang goto err; 2007c5dd23eSDave Jiang } 201b0325aefSDave Jiang 202b0325aefSDave Jiang if (idxd->hw.wq_cap.op_config) { 203b0325aefSDave Jiang wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL); 204b0325aefSDave Jiang if (!wq->opcap_bmap) { 205b0325aefSDave Jiang put_device(conf_dev); 206b0325aefSDave Jiang rc = -ENOMEM; 207b0325aefSDave Jiang goto err; 208b0325aefSDave Jiang } 209b0325aefSDave Jiang bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS); 210b0325aefSDave Jiang } 211b022f597SFenghua Yu mutex_init(&wq->uc_lock); 212b022f597SFenghua Yu xa_init(&wq->upasid_xa); 2137c5dd23eSDave Jiang idxd->wqs[i] = wq; 2147c5dd23eSDave Jiang } 2157c5dd23eSDave Jiang 2167c5dd23eSDave Jiang return 0; 2177c5dd23eSDave Jiang 2187c5dd23eSDave Jiang err: 219700af3a0SDave Jiang while (--i >= 0) { 220700af3a0SDave Jiang wq = idxd->wqs[i]; 221700af3a0SDave Jiang conf_dev = wq_confdev(wq); 222700af3a0SDave Jiang put_device(conf_dev); 223700af3a0SDave Jiang } 2247c5dd23eSDave Jiang return rc; 2257c5dd23eSDave Jiang } 2267c5dd23eSDave Jiang 22775b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd) 22875b91130SDave Jiang { 22975b91130SDave Jiang struct idxd_engine *engine; 23075b91130SDave Jiang struct device *dev = &idxd->pdev->dev; 231700af3a0SDave Jiang struct device *conf_dev; 23275b91130SDave Jiang int i, rc; 23375b91130SDave Jiang 23475b91130SDave Jiang idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), 23575b91130SDave Jiang GFP_KERNEL, dev_to_node(dev)); 23675b91130SDave Jiang if (!idxd->engines) 23775b91130SDave Jiang return -ENOMEM; 23875b91130SDave Jiang 23975b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) { 24075b91130SDave Jiang engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); 24175b91130SDave Jiang if (!engine) { 24275b91130SDave Jiang rc = -ENOMEM; 24375b91130SDave Jiang goto err; 24475b91130SDave Jiang } 24575b91130SDave Jiang 246700af3a0SDave Jiang idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE); 247700af3a0SDave Jiang conf_dev = engine_confdev(engine); 24875b91130SDave Jiang engine->id = i; 24975b91130SDave Jiang engine->idxd = idxd; 250700af3a0SDave Jiang device_initialize(conf_dev); 251700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 252700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 253700af3a0SDave Jiang conf_dev->type = &idxd_engine_device_type; 254700af3a0SDave Jiang rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id); 25575b91130SDave Jiang if (rc < 0) { 256700af3a0SDave Jiang put_device(conf_dev); 25775b91130SDave Jiang goto err; 25875b91130SDave Jiang } 25975b91130SDave Jiang 26075b91130SDave Jiang idxd->engines[i] = engine; 26175b91130SDave Jiang } 26275b91130SDave Jiang 26375b91130SDave Jiang return 0; 26475b91130SDave Jiang 26575b91130SDave Jiang err: 266700af3a0SDave Jiang while (--i >= 0) { 267700af3a0SDave Jiang engine = idxd->engines[i]; 268700af3a0SDave Jiang conf_dev = engine_confdev(engine); 269700af3a0SDave Jiang put_device(conf_dev); 270700af3a0SDave Jiang } 27175b91130SDave Jiang return rc; 27275b91130SDave Jiang } 27375b91130SDave Jiang 274defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd) 275defe49f9SDave Jiang { 276defe49f9SDave Jiang struct device *dev = &idxd->pdev->dev; 277700af3a0SDave Jiang struct device *conf_dev; 278defe49f9SDave Jiang struct idxd_group *group; 279defe49f9SDave Jiang int i, rc; 280defe49f9SDave Jiang 281defe49f9SDave Jiang idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), 282defe49f9SDave Jiang GFP_KERNEL, dev_to_node(dev)); 283defe49f9SDave Jiang if (!idxd->groups) 284defe49f9SDave Jiang return -ENOMEM; 285defe49f9SDave Jiang 286defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) { 287defe49f9SDave Jiang group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); 288defe49f9SDave Jiang if (!group) { 289defe49f9SDave Jiang rc = -ENOMEM; 290defe49f9SDave Jiang goto err; 291defe49f9SDave Jiang } 292defe49f9SDave Jiang 293700af3a0SDave Jiang idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP); 294700af3a0SDave Jiang conf_dev = group_confdev(group); 295defe49f9SDave Jiang group->id = i; 296defe49f9SDave Jiang group->idxd = idxd; 297700af3a0SDave Jiang device_initialize(conf_dev); 298700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 299700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 300700af3a0SDave Jiang conf_dev->type = &idxd_group_device_type; 301700af3a0SDave Jiang rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id); 302defe49f9SDave Jiang if (rc < 0) { 303700af3a0SDave Jiang put_device(conf_dev); 304defe49f9SDave Jiang goto err; 305defe49f9SDave Jiang } 306defe49f9SDave Jiang 307defe49f9SDave Jiang idxd->groups[i] = group; 3089735bde3SFenghua Yu if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) { 309ade8a86bSDave Jiang group->tc_a = 1; 310ade8a86bSDave Jiang group->tc_b = 1; 311ade8a86bSDave Jiang } else { 312defe49f9SDave Jiang group->tc_a = -1; 313defe49f9SDave Jiang group->tc_b = -1; 314defe49f9SDave Jiang } 315601bdadaSFenghua Yu /* 316601bdadaSFenghua Yu * The default value is the same as the value of 317601bdadaSFenghua Yu * total read buffers in GRPCAP. 318601bdadaSFenghua Yu */ 319601bdadaSFenghua Yu group->rdbufs_allowed = idxd->max_rdbufs; 320ade8a86bSDave Jiang } 321defe49f9SDave Jiang 322defe49f9SDave Jiang return 0; 323defe49f9SDave Jiang 324defe49f9SDave Jiang err: 325700af3a0SDave Jiang while (--i >= 0) { 326700af3a0SDave Jiang group = idxd->groups[i]; 327700af3a0SDave Jiang put_device(group_confdev(group)); 328700af3a0SDave Jiang } 329defe49f9SDave Jiang return rc; 330defe49f9SDave Jiang } 331defe49f9SDave Jiang 332ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd) 333ddf742d4SDave Jiang { 334ddf742d4SDave Jiang int i; 335ddf742d4SDave Jiang 336ddf742d4SDave Jiang for (i = 0; i < idxd->max_groups; i++) 337700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 338ddf742d4SDave Jiang for (i = 0; i < idxd->max_engines; i++) 339700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 340ddf742d4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) 341700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 342ddf742d4SDave Jiang destroy_workqueue(idxd->wq); 343ddf742d4SDave Jiang } 344ddf742d4SDave Jiang 3451649091fSDave Jiang static int idxd_init_evl(struct idxd_device *idxd) 3461649091fSDave Jiang { 3471649091fSDave Jiang struct device *dev = &idxd->pdev->dev; 348d3ea125dSFenghua Yu unsigned int evl_cache_size; 3491649091fSDave Jiang struct idxd_evl *evl; 350d3ea125dSFenghua Yu const char *idxd_name; 3511649091fSDave Jiang 3521649091fSDave Jiang if (idxd->hw.gen_cap.evl_support == 0) 3531649091fSDave Jiang return 0; 3541649091fSDave Jiang 3551649091fSDave Jiang evl = kzalloc_node(sizeof(*evl), GFP_KERNEL, dev_to_node(dev)); 3561649091fSDave Jiang if (!evl) 3571649091fSDave Jiang return -ENOMEM; 3581649091fSDave Jiang 359d5638de8SRex Zhang mutex_init(&evl->lock); 3601649091fSDave Jiang evl->size = IDXD_EVL_SIZE_MIN; 361c2f156bfSDave Jiang 362d3ea125dSFenghua Yu idxd_name = dev_name(idxd_confdev(idxd)); 363d3ea125dSFenghua Yu evl_cache_size = sizeof(struct idxd_evl_fault) + evl_ent_size(idxd); 364d3ea125dSFenghua Yu /* 365d3ea125dSFenghua Yu * Since completion record in evl_cache will be copied to user 366d3ea125dSFenghua Yu * when handling completion record page fault, need to create 367d3ea125dSFenghua Yu * the cache suitable for user copy. 368d3ea125dSFenghua Yu */ 369d3ea125dSFenghua Yu idxd->evl_cache = kmem_cache_create_usercopy(idxd_name, evl_cache_size, 370d3ea125dSFenghua Yu 0, 0, 0, evl_cache_size, 371d3ea125dSFenghua Yu NULL); 372c2f156bfSDave Jiang if (!idxd->evl_cache) { 373c2f156bfSDave Jiang kfree(evl); 374c2f156bfSDave Jiang return -ENOMEM; 375c2f156bfSDave Jiang } 376c2f156bfSDave Jiang 3771649091fSDave Jiang idxd->evl = evl; 3781649091fSDave Jiang return 0; 3791649091fSDave Jiang } 3801649091fSDave Jiang 381bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd) 382bfe1d560SDave Jiang { 383bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 384defe49f9SDave Jiang int rc, i; 385bfe1d560SDave Jiang 3860d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq); 3877c5dd23eSDave Jiang 3887c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd); 3897c5dd23eSDave Jiang if (rc < 0) 390eb15e715SDave Jiang goto err_wqs; 3917c5dd23eSDave Jiang 39275b91130SDave Jiang rc = idxd_setup_engines(idxd); 39375b91130SDave Jiang if (rc < 0) 39475b91130SDave Jiang goto err_engine; 39575b91130SDave Jiang 396defe49f9SDave Jiang rc = idxd_setup_groups(idxd); 397defe49f9SDave Jiang if (rc < 0) 398defe49f9SDave Jiang goto err_group; 399bfe1d560SDave Jiang 4000d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 4017c5dd23eSDave Jiang if (!idxd->wq) { 4027c5dd23eSDave Jiang rc = -ENOMEM; 403defe49f9SDave Jiang goto err_wkq_create; 4047c5dd23eSDave Jiang } 4050d5c10b4SDave Jiang 4061649091fSDave Jiang rc = idxd_init_evl(idxd); 4071649091fSDave Jiang if (rc < 0) 4081649091fSDave Jiang goto err_evl; 4091649091fSDave Jiang 410bfe1d560SDave Jiang return 0; 4117c5dd23eSDave Jiang 4121649091fSDave Jiang err_evl: 4131649091fSDave Jiang destroy_workqueue(idxd->wq); 414defe49f9SDave Jiang err_wkq_create: 415defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) 416700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 417defe49f9SDave Jiang err_group: 41875b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) 419700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 42075b91130SDave Jiang err_engine: 4217c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) 422700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 423eb15e715SDave Jiang err_wqs: 4247c5dd23eSDave Jiang return rc; 425bfe1d560SDave Jiang } 426bfe1d560SDave Jiang 427bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 428bfe1d560SDave Jiang { 429bfe1d560SDave Jiang union offsets_reg offsets; 430bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 431bfe1d560SDave Jiang 432bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 4332f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 4342f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 435bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 4362f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 4372f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 4382f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 4392f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 4402f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 441bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 442bfe1d560SDave Jiang } 443bfe1d560SDave Jiang 44434ca0066SDave Jiang void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count) 445a8563a33SDave Jiang { 446a8563a33SDave Jiang int i, j, nr; 447a8563a33SDave Jiang 448a8563a33SDave Jiang for (i = 0, nr = 0; i < count; i++) { 449a8563a33SDave Jiang for (j = 0; j < BITS_PER_LONG_LONG; j++) { 450a8563a33SDave Jiang if (val[i] & BIT(j)) 451a8563a33SDave Jiang set_bit(nr, bmap); 452a8563a33SDave Jiang nr++; 453a8563a33SDave Jiang } 454a8563a33SDave Jiang } 455a8563a33SDave Jiang } 456a8563a33SDave Jiang 457bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 458bfe1d560SDave Jiang { 459bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 460bfe1d560SDave Jiang int i; 461bfe1d560SDave Jiang 462bfe1d560SDave Jiang /* reading generic capabilities */ 463bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 464bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 465eb15e715SDave Jiang 466eb15e715SDave Jiang if (idxd->hw.gen_cap.cmd_cap) { 467eb15e715SDave Jiang idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET); 468eb15e715SDave Jiang dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap); 469eb15e715SDave Jiang } 470eb15e715SDave Jiang 4718b67426eSDave Jiang /* reading command capabilities */ 4728b67426eSDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) 4738b67426eSDave Jiang idxd->request_int_handles = true; 4748b67426eSDave Jiang 475bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 476bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 477e8dbd644SXiaochen Shen idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift); 478bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 479bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 480bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 481bfe1d560SDave Jiang 482bfe1d560SDave Jiang /* reading group capabilities */ 483bfe1d560SDave Jiang idxd->hw.group_cap.bits = 484bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 485bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 486bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 487bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 4887ed6f1b8SDave Jiang idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs; 4897ed6f1b8SDave Jiang dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs); 4907ed6f1b8SDave Jiang idxd->nr_rdbufs = idxd->max_rdbufs; 491bfe1d560SDave Jiang 492bfe1d560SDave Jiang /* read engine capabilities */ 493bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 494bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 495bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 496bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 497bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 498bfe1d560SDave Jiang 499bfe1d560SDave Jiang /* read workqueue capabilities */ 500bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 501bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 502bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 503bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 504bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 505bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 506d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 507d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 508bfe1d560SDave Jiang 509bfe1d560SDave Jiang /* reading operation capabilities */ 510bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 511bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 512bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 513bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 514bfe1d560SDave Jiang } 515a8563a33SDave Jiang multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4); 5169f0d99b3SDave Jiang 5179f0d99b3SDave Jiang /* read iaa cap */ 5189f0d99b3SDave Jiang if (idxd->data->type == IDXD_TYPE_IAX && idxd->hw.version >= DEVICE_VERSION_2) 5199f0d99b3SDave Jiang idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET); 520bfe1d560SDave Jiang } 521bfe1d560SDave Jiang 522435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) 523bfe1d560SDave Jiang { 524bfe1d560SDave Jiang struct device *dev = &pdev->dev; 525700af3a0SDave Jiang struct device *conf_dev; 526bfe1d560SDave Jiang struct idxd_device *idxd; 52747c16ac2SDave Jiang int rc; 528bfe1d560SDave Jiang 52947c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 530bfe1d560SDave Jiang if (!idxd) 531bfe1d560SDave Jiang return NULL; 532bfe1d560SDave Jiang 533700af3a0SDave Jiang conf_dev = idxd_confdev(idxd); 534bfe1d560SDave Jiang idxd->pdev = pdev; 535435b512dSDave Jiang idxd->data = data; 536700af3a0SDave Jiang idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type); 5374b73e4ebSDave Jiang idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); 53847c16ac2SDave Jiang if (idxd->id < 0) 53947c16ac2SDave Jiang return NULL; 54047c16ac2SDave Jiang 541a8563a33SDave Jiang idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev)); 542a8563a33SDave Jiang if (!idxd->opcap_bmap) { 543a8563a33SDave Jiang ida_free(&idxd_ida, idxd->id); 544a8563a33SDave Jiang return NULL; 545a8563a33SDave Jiang } 546a8563a33SDave Jiang 547700af3a0SDave Jiang device_initialize(conf_dev); 548700af3a0SDave Jiang conf_dev->parent = dev; 549700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 550700af3a0SDave Jiang conf_dev->type = idxd->data->dev_type; 551700af3a0SDave Jiang rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); 55247c16ac2SDave Jiang if (rc < 0) { 553700af3a0SDave Jiang put_device(conf_dev); 55447c16ac2SDave Jiang return NULL; 55547c16ac2SDave Jiang } 55647c16ac2SDave Jiang 557bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 55853b2ee7fSDave Jiang spin_lock_init(&idxd->cmd_lock); 559bfe1d560SDave Jiang 560bfe1d560SDave Jiang return idxd; 561bfe1d560SDave Jiang } 562bfe1d560SDave Jiang 5638e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 5648e50d392SDave Jiang { 565f5ccf55eSJacob Pan struct pci_dev *pdev = idxd->pdev; 566f5ccf55eSJacob Pan struct device *dev = &pdev->dev; 567f5ccf55eSJacob Pan struct iommu_domain *domain; 568f5ccf55eSJacob Pan ioasid_t pasid; 569f5ccf55eSJacob Pan int ret; 570f5ccf55eSJacob Pan 571f5ccf55eSJacob Pan /* 572f5ccf55eSJacob Pan * Attach a global PASID to the DMA domain so that we can use ENQCMDS 573f5ccf55eSJacob Pan * to submit work on buffers mapped by DMA API. 574f5ccf55eSJacob Pan */ 575f5ccf55eSJacob Pan domain = iommu_get_domain_for_dev(dev); 576f5ccf55eSJacob Pan if (!domain) 577f5ccf55eSJacob Pan return -EPERM; 578f5ccf55eSJacob Pan 579f5ccf55eSJacob Pan pasid = iommu_alloc_global_pasid(dev); 580f5ccf55eSJacob Pan if (pasid == IOMMU_PASID_INVALID) 581f5ccf55eSJacob Pan return -ENOSPC; 582f5ccf55eSJacob Pan 583f5ccf55eSJacob Pan /* 584f5ccf55eSJacob Pan * DMA domain is owned by the driver, it should support all valid 585f5ccf55eSJacob Pan * types such as DMA-FQ, identity, etc. 586f5ccf55eSJacob Pan */ 587*14678219SLu Baolu ret = iommu_attach_device_pasid(domain, dev, pasid, NULL); 588f5ccf55eSJacob Pan if (ret) { 589f5ccf55eSJacob Pan dev_err(dev, "failed to attach device pasid %d, domain type %d", 590f5ccf55eSJacob Pan pasid, domain->type); 591f5ccf55eSJacob Pan iommu_free_global_pasid(pasid); 592f5ccf55eSJacob Pan return ret; 593f5ccf55eSJacob Pan } 594f5ccf55eSJacob Pan 595f5ccf55eSJacob Pan /* Since we set user privilege for kernel DMA, enable completion IRQ */ 596f5ccf55eSJacob Pan idxd_set_user_intr(idxd, 1); 597f5ccf55eSJacob Pan idxd->pasid = pasid; 598f5ccf55eSJacob Pan 599f5ccf55eSJacob Pan return ret; 6008e50d392SDave Jiang } 6018e50d392SDave Jiang 6028e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 6038e50d392SDave Jiang { 604f5ccf55eSJacob Pan struct pci_dev *pdev = idxd->pdev; 605f5ccf55eSJacob Pan struct device *dev = &pdev->dev; 606f5ccf55eSJacob Pan struct iommu_domain *domain; 6078e50d392SDave Jiang 608f5ccf55eSJacob Pan domain = iommu_get_domain_for_dev(dev); 609f5ccf55eSJacob Pan if (!domain) 610f5ccf55eSJacob Pan return; 611f5ccf55eSJacob Pan 612f5ccf55eSJacob Pan iommu_detach_device_pasid(domain, dev, idxd->pasid); 613f5ccf55eSJacob Pan iommu_free_global_pasid(idxd->pasid); 614f5ccf55eSJacob Pan 615f5ccf55eSJacob Pan idxd_set_user_intr(idxd, 0); 6168e50d392SDave Jiang idxd->sva = NULL; 617f5ccf55eSJacob Pan idxd->pasid = IOMMU_PASID_INVALID; 6188e50d392SDave Jiang } 6198e50d392SDave Jiang 62084c9ef72SLu Baolu static int idxd_enable_sva(struct pci_dev *pdev) 62184c9ef72SLu Baolu { 62284c9ef72SLu Baolu int ret; 62384c9ef72SLu Baolu 62484c9ef72SLu Baolu ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 62584c9ef72SLu Baolu if (ret) 62684c9ef72SLu Baolu return ret; 62784c9ef72SLu Baolu 62884c9ef72SLu Baolu ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 62984c9ef72SLu Baolu if (ret) 63084c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 63184c9ef72SLu Baolu 63284c9ef72SLu Baolu return ret; 63384c9ef72SLu Baolu } 63484c9ef72SLu Baolu 63584c9ef72SLu Baolu static void idxd_disable_sva(struct pci_dev *pdev) 63684c9ef72SLu Baolu { 63784c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 63884c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 63984c9ef72SLu Baolu } 64084c9ef72SLu Baolu 641bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 642bfe1d560SDave Jiang { 643bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 644bfe1d560SDave Jiang struct device *dev = &pdev->dev; 645bfe1d560SDave Jiang int rc; 646bfe1d560SDave Jiang 647bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 64889e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 64989e3becdSDave Jiang if (rc < 0) 65089e3becdSDave Jiang return rc; 65189e3becdSDave Jiang 652bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 653bfe1d560SDave Jiang 65403d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 65584c9ef72SLu Baolu if (idxd_enable_sva(pdev)) { 65642a1b738SDave Jiang dev_warn(dev, "Unable to turn on user SVA feature.\n"); 6578ffccd11SJerry Snitselaar } else { 65842a1b738SDave Jiang set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags); 65942a1b738SDave Jiang 660f5ccf55eSJacob Pan rc = idxd_enable_system_pasid(idxd); 661f5ccf55eSJacob Pan if (rc) 662f5ccf55eSJacob Pan dev_warn(dev, "No in-kernel DMA with PASID. %d\n", rc); 66342a1b738SDave Jiang else 6648e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 6658ffccd11SJerry Snitselaar } 66603d939c7SDave Jiang } else if (!sva) { 66703d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n"); 6688e50d392SDave Jiang } 6698e50d392SDave Jiang 670bfe1d560SDave Jiang idxd_read_caps(idxd); 671bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 672bfe1d560SDave Jiang 673bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 674bfe1d560SDave Jiang if (rc) 6757c5dd23eSDave Jiang goto err; 676bfe1d560SDave Jiang 6778c66bbdcSDave Jiang /* If the configs are readonly, then load them from device */ 6788c66bbdcSDave Jiang if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { 6798c66bbdcSDave Jiang dev_dbg(dev, "Loading RO device config\n"); 6808c66bbdcSDave Jiang rc = idxd_device_load_config(idxd); 6818c66bbdcSDave Jiang if (rc < 0) 682ddf742d4SDave Jiang goto err_config; 6838c66bbdcSDave Jiang } 6848c66bbdcSDave Jiang 685bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 686bfe1d560SDave Jiang if (rc) 687ddf742d4SDave Jiang goto err_config; 688bfe1d560SDave Jiang 68942d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 69042d279f9SDave Jiang 6910bde4444STom Zanussi rc = perfmon_pmu_init(idxd); 6920bde4444STom Zanussi if (rc < 0) 6930bde4444STom Zanussi dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc); 6940bde4444STom Zanussi 695bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 696bfe1d560SDave Jiang return 0; 697bfe1d560SDave Jiang 698ddf742d4SDave Jiang err_config: 699ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 7007c5dd23eSDave Jiang err: 7018e50d392SDave Jiang if (device_pasid_enabled(idxd)) 7028e50d392SDave Jiang idxd_disable_system_pasid(idxd); 70342a1b738SDave Jiang if (device_user_pasid_enabled(idxd)) 70484c9ef72SLu Baolu idxd_disable_sva(pdev); 705bfe1d560SDave Jiang return rc; 706bfe1d560SDave Jiang } 707bfe1d560SDave Jiang 708ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd) 709ddf742d4SDave Jiang { 710ddf742d4SDave Jiang perfmon_pmu_remove(idxd); 711ddf742d4SDave Jiang idxd_cleanup_interrupts(idxd); 712ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 713ddf742d4SDave Jiang if (device_pasid_enabled(idxd)) 714ddf742d4SDave Jiang idxd_disable_system_pasid(idxd); 71542a1b738SDave Jiang if (device_user_pasid_enabled(idxd)) 71684c9ef72SLu Baolu idxd_disable_sva(idxd->pdev); 717ddf742d4SDave Jiang } 718ddf742d4SDave Jiang 719bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 720bfe1d560SDave Jiang { 721bfe1d560SDave Jiang struct device *dev = &pdev->dev; 722bfe1d560SDave Jiang struct idxd_device *idxd; 723435b512dSDave Jiang struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data; 724bfe1d560SDave Jiang int rc; 725bfe1d560SDave Jiang 726a39c7cd0SDave Jiang rc = pci_enable_device(pdev); 727bfe1d560SDave Jiang if (rc) 728bfe1d560SDave Jiang return rc; 729bfe1d560SDave Jiang 7308e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 731435b512dSDave Jiang idxd = idxd_alloc(pdev, data); 732a39c7cd0SDave Jiang if (!idxd) { 733a39c7cd0SDave Jiang rc = -ENOMEM; 734a39c7cd0SDave Jiang goto err_idxd_alloc; 735a39c7cd0SDave Jiang } 736bfe1d560SDave Jiang 7378e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 738a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 739a39c7cd0SDave Jiang if (!idxd->reg_base) { 740a39c7cd0SDave Jiang rc = -ENOMEM; 741a39c7cd0SDave Jiang goto err_iomap; 742a39c7cd0SDave Jiang } 743bfe1d560SDave Jiang 744bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 74553b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 746bfe1d560SDave Jiang if (rc) 747a39c7cd0SDave Jiang goto err; 748bfe1d560SDave Jiang 749bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 750bfe1d560SDave Jiang pci_set_master(pdev); 751bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 752bfe1d560SDave Jiang 753bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 754bfe1d560SDave Jiang rc = idxd_probe(idxd); 755bfe1d560SDave Jiang if (rc) { 756bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 757a39c7cd0SDave Jiang goto err; 758bfe1d560SDave Jiang } 759bfe1d560SDave Jiang 760979f6dedSTom Zanussi if (data->load_device_defaults) { 761979f6dedSTom Zanussi rc = data->load_device_defaults(idxd); 762979f6dedSTom Zanussi if (rc) 763979f6dedSTom Zanussi dev_warn(dev, "IDXD loading device defaults failed\n"); 764979f6dedSTom Zanussi } 765979f6dedSTom Zanussi 76647c16ac2SDave Jiang rc = idxd_register_devices(idxd); 767c52ca478SDave Jiang if (rc) { 768c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 769ddf742d4SDave Jiang goto err_dev_register; 770c52ca478SDave Jiang } 771c52ca478SDave Jiang 7725fbe6503SDave Jiang rc = idxd_device_init_debugfs(idxd); 7735fbe6503SDave Jiang if (rc) 7745fbe6503SDave Jiang dev_warn(dev, "IDXD debugfs failed to setup\n"); 7755fbe6503SDave Jiang 776bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 777bfe1d560SDave Jiang idxd->hw.version); 778bfe1d560SDave Jiang 779e11452ebSArjan van de Ven idxd->user_submission_safe = data->user_submission_safe; 780e11452ebSArjan van de Ven 781bfe1d560SDave Jiang return 0; 782a39c7cd0SDave Jiang 783ddf742d4SDave Jiang err_dev_register: 784ddf742d4SDave Jiang idxd_cleanup(idxd); 785a39c7cd0SDave Jiang err: 786a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 787a39c7cd0SDave Jiang err_iomap: 788700af3a0SDave Jiang put_device(idxd_confdev(idxd)); 789a39c7cd0SDave Jiang err_idxd_alloc: 790a39c7cd0SDave Jiang pci_disable_device(pdev); 791a39c7cd0SDave Jiang return rc; 792bfe1d560SDave Jiang } 793bfe1d560SDave Jiang 7945b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd) 7955b0c68c4SDave Jiang { 7965b0c68c4SDave Jiang struct idxd_wq *wq; 7975b0c68c4SDave Jiang int i; 7985b0c68c4SDave Jiang 7995b0c68c4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 8005b0c68c4SDave Jiang wq = idxd->wqs[i]; 8015b0c68c4SDave Jiang if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL) 8025b0c68c4SDave Jiang idxd_wq_quiesce(wq); 8035b0c68c4SDave Jiang } 8045b0c68c4SDave Jiang } 8055b0c68c4SDave Jiang 806bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 807bfe1d560SDave Jiang { 808bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 809bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 810403a2e23SDave Jiang int rc; 811bfe1d560SDave Jiang 812bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 813bfe1d560SDave Jiang if (rc) 814bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 815bfe1d560SDave Jiang 816403a2e23SDave Jiang irq_entry = &idxd->ie; 8175fc8e85fSDave Jiang synchronize_irq(irq_entry->vector); 818403a2e23SDave Jiang idxd_mask_error_interrupts(idxd); 81949c4959fSDave Jiang flush_workqueue(idxd->wq); 820bfe1d560SDave Jiang } 821bfe1d560SDave Jiang 822bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 823bfe1d560SDave Jiang { 824bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 82549c4959fSDave Jiang struct idxd_irq_entry *irq_entry; 826bfe1d560SDave Jiang 82798da0106SDave Jiang idxd_unregister_devices(idxd); 82898da0106SDave Jiang /* 82998da0106SDave Jiang * When ->release() is called for the idxd->conf_dev, it frees all the memory related 83098da0106SDave Jiang * to the idxd context. The driver still needs those bits in order to do the rest of 83198da0106SDave Jiang * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref 83298da0106SDave Jiang * on the device here to hold off the freeing while allowing the idxd sub-driver 83398da0106SDave Jiang * to unbind. 83498da0106SDave Jiang */ 83598da0106SDave Jiang get_device(idxd_confdev(idxd)); 83698da0106SDave Jiang device_unregister(idxd_confdev(idxd)); 837bfe1d560SDave Jiang idxd_shutdown(pdev); 8388e50d392SDave Jiang if (device_pasid_enabled(idxd)) 8398e50d392SDave Jiang idxd_disable_system_pasid(idxd); 8405fbe6503SDave Jiang idxd_device_remove_debugfs(idxd); 84149c4959fSDave Jiang 842403a2e23SDave Jiang irq_entry = idxd_get_ie(idxd, 0); 84349c4959fSDave Jiang free_irq(irq_entry->vector, irq_entry); 84449c4959fSDave Jiang pci_free_irq_vectors(pdev); 84549c4959fSDave Jiang pci_iounmap(pdev, idxd->reg_base); 84642a1b738SDave Jiang if (device_user_pasid_enabled(idxd)) 84784c9ef72SLu Baolu idxd_disable_sva(pdev); 84849c4959fSDave Jiang pci_disable_device(pdev); 84949c4959fSDave Jiang destroy_workqueue(idxd->wq); 85049c4959fSDave Jiang perfmon_pmu_remove(idxd); 85198da0106SDave Jiang put_device(idxd_confdev(idxd)); 852bfe1d560SDave Jiang } 853bfe1d560SDave Jiang 854bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 855bfe1d560SDave Jiang .name = DRV_NAME, 856bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 857bfe1d560SDave Jiang .probe = idxd_pci_probe, 858bfe1d560SDave Jiang .remove = idxd_remove, 859bfe1d560SDave Jiang .shutdown = idxd_shutdown, 860bfe1d560SDave Jiang }; 861bfe1d560SDave Jiang 862bfe1d560SDave Jiang static int __init idxd_init_module(void) 863bfe1d560SDave Jiang { 8644b73e4ebSDave Jiang int err; 865bfe1d560SDave Jiang 866bfe1d560SDave Jiang /* 8678e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 868bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 869bfe1d560SDave Jiang */ 87074b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) { 871bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 872bfe1d560SDave Jiang return -ENODEV; 873bfe1d560SDave Jiang } 874bfe1d560SDave Jiang 87574b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_ENQCMD)) 8768e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 8778e50d392SDave Jiang else 8788e50d392SDave Jiang support_enqcmd = true; 879bfe1d560SDave Jiang 8800bde4444STom Zanussi perfmon_init(); 8810bde4444STom Zanussi 882034b3290SDave Jiang err = idxd_driver_register(&idxd_drv); 883034b3290SDave Jiang if (err < 0) 884034b3290SDave Jiang goto err_idxd_driver_register; 885034b3290SDave Jiang 8860cda4f69SDave Jiang err = idxd_driver_register(&idxd_dmaengine_drv); 8870cda4f69SDave Jiang if (err < 0) 8880cda4f69SDave Jiang goto err_idxd_dmaengine_driver_register; 8890cda4f69SDave Jiang 890448c3de8SDave Jiang err = idxd_driver_register(&idxd_user_drv); 891448c3de8SDave Jiang if (err < 0) 892448c3de8SDave Jiang goto err_idxd_user_driver_register; 893448c3de8SDave Jiang 89442d279f9SDave Jiang err = idxd_cdev_register(); 89542d279f9SDave Jiang if (err) 89642d279f9SDave Jiang goto err_cdev_register; 89742d279f9SDave Jiang 8985fbe6503SDave Jiang err = idxd_init_debugfs(); 8995fbe6503SDave Jiang if (err) 9005fbe6503SDave Jiang goto err_debugfs; 9015fbe6503SDave Jiang 902c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 903c52ca478SDave Jiang if (err) 904c52ca478SDave Jiang goto err_pci_register; 905c52ca478SDave Jiang 906bfe1d560SDave Jiang return 0; 907c52ca478SDave Jiang 908c52ca478SDave Jiang err_pci_register: 9095fbe6503SDave Jiang idxd_remove_debugfs(); 9105fbe6503SDave Jiang err_debugfs: 91142d279f9SDave Jiang idxd_cdev_remove(); 91242d279f9SDave Jiang err_cdev_register: 913448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 914448c3de8SDave Jiang err_idxd_user_driver_register: 9150cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 9160cda4f69SDave Jiang err_idxd_dmaengine_driver_register: 917034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 918034b3290SDave Jiang err_idxd_driver_register: 919c52ca478SDave Jiang return err; 920bfe1d560SDave Jiang } 921bfe1d560SDave Jiang module_init(idxd_init_module); 922bfe1d560SDave Jiang 923bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 924bfe1d560SDave Jiang { 925448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 9260cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 927034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 928bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 92942d279f9SDave Jiang idxd_cdev_remove(); 9300bde4444STom Zanussi perfmon_exit(); 9315fbe6503SDave Jiang idxd_remove_debugfs(); 932bfe1d560SDave Jiang } 933bfe1d560SDave Jiang module_exit(idxd_exit_module); 934