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); 256e2fb806SJeff Johnson MODULE_DESCRIPTION("Intel Data Streaming Accelerator and In-Memory Analytics Accelerator common driver"); 26bfe1d560SDave Jiang MODULE_LICENSE("GPL v2"); 27bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation"); 28d9e5481fSDave Jiang MODULE_IMPORT_NS(IDXD); 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 34ade8a86bSDave Jiang bool tc_override; 35ade8a86bSDave Jiang module_param(tc_override, bool, 0644); 36ade8a86bSDave Jiang MODULE_PARM_DESC(tc_override, "Override traffic class defaults"); 37ade8a86bSDave Jiang 38bfe1d560SDave Jiang #define DRV_NAME "idxd" 39bfe1d560SDave Jiang 408e50d392SDave Jiang bool support_enqcmd; 414b73e4ebSDave Jiang DEFINE_IDA(idxd_ida); 42bfe1d560SDave Jiang 43435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = { 44435b512dSDave Jiang [IDXD_TYPE_DSA] = { 45435b512dSDave Jiang .name_prefix = "dsa", 46435b512dSDave Jiang .type = IDXD_TYPE_DSA, 47435b512dSDave Jiang .compl_size = sizeof(struct dsa_completion_record), 48435b512dSDave Jiang .align = 32, 49435b512dSDave Jiang .dev_type = &dsa_device_type, 50c40bd7d9SDave Jiang .evl_cr_off = offsetof(struct dsa_evl_entry, cr), 51e11452ebSArjan van de Ven .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */ 522442b747SDave Jiang .cr_status_off = offsetof(struct dsa_completion_record, status), 532442b747SDave Jiang .cr_result_off = offsetof(struct dsa_completion_record, result), 54435b512dSDave Jiang }, 55435b512dSDave Jiang [IDXD_TYPE_IAX] = { 56435b512dSDave Jiang .name_prefix = "iax", 57435b512dSDave Jiang .type = IDXD_TYPE_IAX, 58435b512dSDave Jiang .compl_size = sizeof(struct iax_completion_record), 59435b512dSDave Jiang .align = 64, 60435b512dSDave Jiang .dev_type = &iax_device_type, 61c40bd7d9SDave Jiang .evl_cr_off = offsetof(struct iax_evl_entry, cr), 62e11452ebSArjan van de Ven .user_submission_safe = false, /* See INTEL-SA-01084 security advisory */ 632442b747SDave Jiang .cr_status_off = offsetof(struct iax_completion_record, status), 642442b747SDave Jiang .cr_result_off = offsetof(struct iax_completion_record, error_code), 65979f6dedSTom Zanussi .load_device_defaults = idxd_load_iaa_device_defaults, 66435b512dSDave Jiang }, 67435b512dSDave Jiang }; 68435b512dSDave Jiang 69bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = { 70bfe1d560SDave Jiang /* DSA ver 1.0 platforms */ 71435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) }, 72*f91f2a98SFenghua Yu /* DSA on GNR-D platforms */ 73*f91f2a98SFenghua Yu { PCI_DEVICE_DATA(INTEL, DSA_GNRD, &idxd_driver_data[IDXD_TYPE_DSA]) }, 74f25b4638SDave Jiang 75f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 76435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) }, 77bfe1d560SDave Jiang { 0, } 78bfe1d560SDave Jiang }; 79bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 80bfe1d560SDave Jiang 81bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd) 82bfe1d560SDave Jiang { 83bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 84bfe1d560SDave Jiang struct device *dev = &pdev->dev; 85ec0d6423SDave Jiang struct idxd_irq_entry *ie; 86bfe1d560SDave Jiang int i, msixcnt; 87bfe1d560SDave Jiang int rc = 0; 88bfe1d560SDave Jiang 89bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev); 90bfe1d560SDave Jiang if (msixcnt < 0) { 91bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n"); 925fc8e85fSDave Jiang return -ENOSPC; 93bfe1d560SDave Jiang } 948b67426eSDave Jiang idxd->irq_cnt = msixcnt; 95bfe1d560SDave Jiang 965fc8e85fSDave Jiang rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); 975fc8e85fSDave Jiang if (rc != msixcnt) { 985fc8e85fSDave Jiang dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); 995fc8e85fSDave Jiang return -ENOSPC; 100bfe1d560SDave Jiang } 101bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 102bfe1d560SDave Jiang 103d5c10e0fSDave Jiang 104ec0d6423SDave Jiang ie = idxd_get_ie(idxd, 0); 105ec0d6423SDave Jiang ie->vector = pci_irq_vector(pdev, 0); 106ec0d6423SDave Jiang rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie); 107bfe1d560SDave Jiang if (rc < 0) { 108bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n"); 1095fc8e85fSDave Jiang goto err_misc_irq; 110bfe1d560SDave Jiang } 111403a2e23SDave Jiang dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector); 112bfe1d560SDave Jiang 113ec0d6423SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 114ec0d6423SDave Jiang int msix_idx = i + 1; 115bfe1d560SDave Jiang 116ec0d6423SDave Jiang ie = idxd_get_ie(idxd, msix_idx); 117ec0d6423SDave Jiang ie->id = msix_idx; 118ec0d6423SDave Jiang ie->int_handle = INVALID_INT_HANDLE; 119fffaed1eSJacob Pan ie->pasid = IOMMU_PASID_INVALID; 120403a2e23SDave Jiang 121ec0d6423SDave Jiang spin_lock_init(&ie->list_lock); 122ec0d6423SDave Jiang init_llist_head(&ie->pending_llist); 123ec0d6423SDave Jiang INIT_LIST_HEAD(&ie->work_list); 124bfe1d560SDave Jiang } 125bfe1d560SDave Jiang 126bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd); 127bfe1d560SDave Jiang return 0; 128bfe1d560SDave Jiang 1295fc8e85fSDave Jiang err_misc_irq: 130bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 1315fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 132bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n"); 133bfe1d560SDave Jiang return rc; 134bfe1d560SDave Jiang } 135bfe1d560SDave Jiang 136ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd) 137ddf742d4SDave Jiang { 138ddf742d4SDave Jiang struct pci_dev *pdev = idxd->pdev; 139ec0d6423SDave Jiang struct idxd_irq_entry *ie; 140403a2e23SDave Jiang int msixcnt; 141ddf742d4SDave Jiang 142403a2e23SDave Jiang msixcnt = pci_msix_vec_count(pdev); 143403a2e23SDave Jiang if (msixcnt <= 0) 144403a2e23SDave Jiang return; 145ddf742d4SDave Jiang 146403a2e23SDave Jiang ie = idxd_get_ie(idxd, 0); 147ddf742d4SDave Jiang idxd_mask_error_interrupts(idxd); 148403a2e23SDave Jiang free_irq(ie->vector, ie); 149ddf742d4SDave Jiang pci_free_irq_vectors(pdev); 150ddf742d4SDave Jiang } 151ddf742d4SDave Jiang 1527c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd) 1537c5dd23eSDave Jiang { 1547c5dd23eSDave Jiang struct device *dev = &idxd->pdev->dev; 1557c5dd23eSDave Jiang struct idxd_wq *wq; 156700af3a0SDave Jiang struct device *conf_dev; 1577c5dd23eSDave Jiang int i, rc; 1587c5dd23eSDave Jiang 1597c5dd23eSDave Jiang idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 1607c5dd23eSDave Jiang GFP_KERNEL, dev_to_node(dev)); 1617c5dd23eSDave Jiang if (!idxd->wqs) 1627c5dd23eSDave Jiang return -ENOMEM; 1637c5dd23eSDave Jiang 164de5819b9SJerry Snitselaar idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev)); 165de5819b9SJerry Snitselaar if (!idxd->wq_enable_map) { 166de5819b9SJerry Snitselaar kfree(idxd->wqs); 167de5819b9SJerry Snitselaar return -ENOMEM; 168de5819b9SJerry Snitselaar } 169de5819b9SJerry Snitselaar 1707c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 1717c5dd23eSDave Jiang wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); 1727c5dd23eSDave Jiang if (!wq) { 1737c5dd23eSDave Jiang rc = -ENOMEM; 1747c5dd23eSDave Jiang goto err; 1757c5dd23eSDave Jiang } 1767c5dd23eSDave Jiang 177700af3a0SDave Jiang idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ); 178700af3a0SDave Jiang conf_dev = wq_confdev(wq); 1797c5dd23eSDave Jiang wq->id = i; 1807c5dd23eSDave Jiang wq->idxd = idxd; 181700af3a0SDave Jiang device_initialize(wq_confdev(wq)); 182700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 183700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 184700af3a0SDave Jiang conf_dev->type = &idxd_wq_device_type; 185700af3a0SDave Jiang rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id); 1867c5dd23eSDave Jiang if (rc < 0) { 187700af3a0SDave Jiang put_device(conf_dev); 1887c5dd23eSDave Jiang goto err; 1897c5dd23eSDave Jiang } 1907c5dd23eSDave Jiang 1917c5dd23eSDave Jiang mutex_init(&wq->wq_lock); 19204922b74SDave Jiang init_waitqueue_head(&wq->err_queue); 19393a40a6dSDave Jiang init_completion(&wq->wq_dead); 19456fc39f5SDave Jiang init_completion(&wq->wq_resurrect); 19592452a72SDave Jiang wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER; 196e8dbd644SXiaochen Shen idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH); 1977930d855SDave Jiang wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES; 1987c5dd23eSDave Jiang wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); 1997c5dd23eSDave Jiang if (!wq->wqcfg) { 200700af3a0SDave Jiang put_device(conf_dev); 2017c5dd23eSDave Jiang rc = -ENOMEM; 2027c5dd23eSDave Jiang goto err; 2037c5dd23eSDave Jiang } 204b0325aefSDave Jiang 205b0325aefSDave Jiang if (idxd->hw.wq_cap.op_config) { 206b0325aefSDave Jiang wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL); 207b0325aefSDave Jiang if (!wq->opcap_bmap) { 208b0325aefSDave Jiang put_device(conf_dev); 209b0325aefSDave Jiang rc = -ENOMEM; 210b0325aefSDave Jiang goto err; 211b0325aefSDave Jiang } 212b0325aefSDave Jiang bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS); 213b0325aefSDave Jiang } 214b022f597SFenghua Yu mutex_init(&wq->uc_lock); 215b022f597SFenghua Yu xa_init(&wq->upasid_xa); 2167c5dd23eSDave Jiang idxd->wqs[i] = wq; 2177c5dd23eSDave Jiang } 2187c5dd23eSDave Jiang 2197c5dd23eSDave Jiang return 0; 2207c5dd23eSDave Jiang 2217c5dd23eSDave Jiang err: 222700af3a0SDave Jiang while (--i >= 0) { 223700af3a0SDave Jiang wq = idxd->wqs[i]; 224700af3a0SDave Jiang conf_dev = wq_confdev(wq); 225700af3a0SDave Jiang put_device(conf_dev); 226700af3a0SDave Jiang } 2277c5dd23eSDave Jiang return rc; 2287c5dd23eSDave Jiang } 2297c5dd23eSDave Jiang 23075b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd) 23175b91130SDave Jiang { 23275b91130SDave Jiang struct idxd_engine *engine; 23375b91130SDave Jiang struct device *dev = &idxd->pdev->dev; 234700af3a0SDave Jiang struct device *conf_dev; 23575b91130SDave Jiang int i, rc; 23675b91130SDave Jiang 23775b91130SDave Jiang idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), 23875b91130SDave Jiang GFP_KERNEL, dev_to_node(dev)); 23975b91130SDave Jiang if (!idxd->engines) 24075b91130SDave Jiang return -ENOMEM; 24175b91130SDave Jiang 24275b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) { 24375b91130SDave Jiang engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); 24475b91130SDave Jiang if (!engine) { 24575b91130SDave Jiang rc = -ENOMEM; 24675b91130SDave Jiang goto err; 24775b91130SDave Jiang } 24875b91130SDave Jiang 249700af3a0SDave Jiang idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE); 250700af3a0SDave Jiang conf_dev = engine_confdev(engine); 25175b91130SDave Jiang engine->id = i; 25275b91130SDave Jiang engine->idxd = idxd; 253700af3a0SDave Jiang device_initialize(conf_dev); 254700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 255700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 256700af3a0SDave Jiang conf_dev->type = &idxd_engine_device_type; 257700af3a0SDave Jiang rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id); 25875b91130SDave Jiang if (rc < 0) { 259700af3a0SDave Jiang put_device(conf_dev); 26075b91130SDave Jiang goto err; 26175b91130SDave Jiang } 26275b91130SDave Jiang 26375b91130SDave Jiang idxd->engines[i] = engine; 26475b91130SDave Jiang } 26575b91130SDave Jiang 26675b91130SDave Jiang return 0; 26775b91130SDave Jiang 26875b91130SDave Jiang err: 269700af3a0SDave Jiang while (--i >= 0) { 270700af3a0SDave Jiang engine = idxd->engines[i]; 271700af3a0SDave Jiang conf_dev = engine_confdev(engine); 272700af3a0SDave Jiang put_device(conf_dev); 273700af3a0SDave Jiang } 27475b91130SDave Jiang return rc; 27575b91130SDave Jiang } 27675b91130SDave Jiang 277defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd) 278defe49f9SDave Jiang { 279defe49f9SDave Jiang struct device *dev = &idxd->pdev->dev; 280700af3a0SDave Jiang struct device *conf_dev; 281defe49f9SDave Jiang struct idxd_group *group; 282defe49f9SDave Jiang int i, rc; 283defe49f9SDave Jiang 284defe49f9SDave Jiang idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), 285defe49f9SDave Jiang GFP_KERNEL, dev_to_node(dev)); 286defe49f9SDave Jiang if (!idxd->groups) 287defe49f9SDave Jiang return -ENOMEM; 288defe49f9SDave Jiang 289defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) { 290defe49f9SDave Jiang group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); 291defe49f9SDave Jiang if (!group) { 292defe49f9SDave Jiang rc = -ENOMEM; 293defe49f9SDave Jiang goto err; 294defe49f9SDave Jiang } 295defe49f9SDave Jiang 296700af3a0SDave Jiang idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP); 297700af3a0SDave Jiang conf_dev = group_confdev(group); 298defe49f9SDave Jiang group->id = i; 299defe49f9SDave Jiang group->idxd = idxd; 300700af3a0SDave Jiang device_initialize(conf_dev); 301700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 302700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 303700af3a0SDave Jiang conf_dev->type = &idxd_group_device_type; 304700af3a0SDave Jiang rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id); 305defe49f9SDave Jiang if (rc < 0) { 306700af3a0SDave Jiang put_device(conf_dev); 307defe49f9SDave Jiang goto err; 308defe49f9SDave Jiang } 309defe49f9SDave Jiang 310defe49f9SDave Jiang idxd->groups[i] = group; 3119735bde3SFenghua Yu if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) { 312ade8a86bSDave Jiang group->tc_a = 1; 313ade8a86bSDave Jiang group->tc_b = 1; 314ade8a86bSDave Jiang } else { 315defe49f9SDave Jiang group->tc_a = -1; 316defe49f9SDave Jiang group->tc_b = -1; 317defe49f9SDave Jiang } 318601bdadaSFenghua Yu /* 319601bdadaSFenghua Yu * The default value is the same as the value of 320601bdadaSFenghua Yu * total read buffers in GRPCAP. 321601bdadaSFenghua Yu */ 322601bdadaSFenghua Yu group->rdbufs_allowed = idxd->max_rdbufs; 323ade8a86bSDave Jiang } 324defe49f9SDave Jiang 325defe49f9SDave Jiang return 0; 326defe49f9SDave Jiang 327defe49f9SDave Jiang err: 328700af3a0SDave Jiang while (--i >= 0) { 329700af3a0SDave Jiang group = idxd->groups[i]; 330700af3a0SDave Jiang put_device(group_confdev(group)); 331700af3a0SDave Jiang } 332defe49f9SDave Jiang return rc; 333defe49f9SDave Jiang } 334defe49f9SDave Jiang 335ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd) 336ddf742d4SDave Jiang { 337ddf742d4SDave Jiang int i; 338ddf742d4SDave Jiang 339ddf742d4SDave Jiang for (i = 0; i < idxd->max_groups; i++) 340700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 341ddf742d4SDave Jiang for (i = 0; i < idxd->max_engines; i++) 342700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 343ddf742d4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) 344700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 345ddf742d4SDave Jiang destroy_workqueue(idxd->wq); 346ddf742d4SDave Jiang } 347ddf742d4SDave Jiang 3481649091fSDave Jiang static int idxd_init_evl(struct idxd_device *idxd) 3491649091fSDave Jiang { 3501649091fSDave Jiang struct device *dev = &idxd->pdev->dev; 351d3ea125dSFenghua Yu unsigned int evl_cache_size; 3521649091fSDave Jiang struct idxd_evl *evl; 353d3ea125dSFenghua Yu const char *idxd_name; 3541649091fSDave Jiang 3551649091fSDave Jiang if (idxd->hw.gen_cap.evl_support == 0) 3561649091fSDave Jiang return 0; 3571649091fSDave Jiang 3581649091fSDave Jiang evl = kzalloc_node(sizeof(*evl), GFP_KERNEL, dev_to_node(dev)); 3591649091fSDave Jiang if (!evl) 3601649091fSDave Jiang return -ENOMEM; 3611649091fSDave Jiang 362d5638de8SRex Zhang mutex_init(&evl->lock); 3631649091fSDave Jiang evl->size = IDXD_EVL_SIZE_MIN; 364c2f156bfSDave Jiang 365d3ea125dSFenghua Yu idxd_name = dev_name(idxd_confdev(idxd)); 366d3ea125dSFenghua Yu evl_cache_size = sizeof(struct idxd_evl_fault) + evl_ent_size(idxd); 367d3ea125dSFenghua Yu /* 368d3ea125dSFenghua Yu * Since completion record in evl_cache will be copied to user 369d3ea125dSFenghua Yu * when handling completion record page fault, need to create 370d3ea125dSFenghua Yu * the cache suitable for user copy. 371d3ea125dSFenghua Yu */ 372d3ea125dSFenghua Yu idxd->evl_cache = kmem_cache_create_usercopy(idxd_name, evl_cache_size, 373d3ea125dSFenghua Yu 0, 0, 0, evl_cache_size, 374d3ea125dSFenghua Yu NULL); 375c2f156bfSDave Jiang if (!idxd->evl_cache) { 376c2f156bfSDave Jiang kfree(evl); 377c2f156bfSDave Jiang return -ENOMEM; 378c2f156bfSDave Jiang } 379c2f156bfSDave Jiang 3801649091fSDave Jiang idxd->evl = evl; 3811649091fSDave Jiang return 0; 3821649091fSDave Jiang } 3831649091fSDave Jiang 384bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd) 385bfe1d560SDave Jiang { 386bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 387defe49f9SDave Jiang int rc, i; 388bfe1d560SDave Jiang 3890d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq); 3907c5dd23eSDave Jiang 3917c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd); 3927c5dd23eSDave Jiang if (rc < 0) 393eb15e715SDave Jiang goto err_wqs; 3947c5dd23eSDave Jiang 39575b91130SDave Jiang rc = idxd_setup_engines(idxd); 39675b91130SDave Jiang if (rc < 0) 39775b91130SDave Jiang goto err_engine; 39875b91130SDave Jiang 399defe49f9SDave Jiang rc = idxd_setup_groups(idxd); 400defe49f9SDave Jiang if (rc < 0) 401defe49f9SDave Jiang goto err_group; 402bfe1d560SDave Jiang 4030d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 4047c5dd23eSDave Jiang if (!idxd->wq) { 4057c5dd23eSDave Jiang rc = -ENOMEM; 406defe49f9SDave Jiang goto err_wkq_create; 4077c5dd23eSDave Jiang } 4080d5c10b4SDave Jiang 4091649091fSDave Jiang rc = idxd_init_evl(idxd); 4101649091fSDave Jiang if (rc < 0) 4111649091fSDave Jiang goto err_evl; 4121649091fSDave Jiang 413bfe1d560SDave Jiang return 0; 4147c5dd23eSDave Jiang 4151649091fSDave Jiang err_evl: 4161649091fSDave Jiang destroy_workqueue(idxd->wq); 417defe49f9SDave Jiang err_wkq_create: 418defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) 419700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 420defe49f9SDave Jiang err_group: 42175b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) 422700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 42375b91130SDave Jiang err_engine: 4247c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) 425700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 426eb15e715SDave Jiang err_wqs: 4277c5dd23eSDave Jiang return rc; 428bfe1d560SDave Jiang } 429bfe1d560SDave Jiang 430bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 431bfe1d560SDave Jiang { 432bfe1d560SDave Jiang union offsets_reg offsets; 433bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 434bfe1d560SDave Jiang 435bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 4362f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 4372f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 438bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 4392f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 4402f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 4412f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 4422f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 4432f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 444bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 445bfe1d560SDave Jiang } 446bfe1d560SDave Jiang 44734ca0066SDave Jiang void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count) 448a8563a33SDave Jiang { 449a8563a33SDave Jiang int i, j, nr; 450a8563a33SDave Jiang 451a8563a33SDave Jiang for (i = 0, nr = 0; i < count; i++) { 452a8563a33SDave Jiang for (j = 0; j < BITS_PER_LONG_LONG; j++) { 453a8563a33SDave Jiang if (val[i] & BIT(j)) 454a8563a33SDave Jiang set_bit(nr, bmap); 455a8563a33SDave Jiang nr++; 456a8563a33SDave Jiang } 457a8563a33SDave Jiang } 458a8563a33SDave Jiang } 459a8563a33SDave Jiang 460bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 461bfe1d560SDave Jiang { 462bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 463bfe1d560SDave Jiang int i; 464bfe1d560SDave Jiang 465bfe1d560SDave Jiang /* reading generic capabilities */ 466bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 467bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 468eb15e715SDave Jiang 469eb15e715SDave Jiang if (idxd->hw.gen_cap.cmd_cap) { 470eb15e715SDave Jiang idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET); 471eb15e715SDave Jiang dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap); 472eb15e715SDave Jiang } 473eb15e715SDave Jiang 4748b67426eSDave Jiang /* reading command capabilities */ 4758b67426eSDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) 4768b67426eSDave Jiang idxd->request_int_handles = true; 4778b67426eSDave Jiang 478bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 479bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 480e8dbd644SXiaochen Shen idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift); 481bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 482bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 483bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 484bfe1d560SDave Jiang 485bfe1d560SDave Jiang /* reading group capabilities */ 486bfe1d560SDave Jiang idxd->hw.group_cap.bits = 487bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 488bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 489bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 490bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 4917ed6f1b8SDave Jiang idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs; 4927ed6f1b8SDave Jiang dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs); 4937ed6f1b8SDave Jiang idxd->nr_rdbufs = idxd->max_rdbufs; 494bfe1d560SDave Jiang 495bfe1d560SDave Jiang /* read engine capabilities */ 496bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 497bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 498bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 499bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 500bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 501bfe1d560SDave Jiang 502bfe1d560SDave Jiang /* read workqueue capabilities */ 503bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 504bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 505bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 506bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 507bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 508bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 509d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 510d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 511bfe1d560SDave Jiang 512bfe1d560SDave Jiang /* reading operation capabilities */ 513bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 514bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 515bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 516bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 517bfe1d560SDave Jiang } 518a8563a33SDave Jiang multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4); 5199f0d99b3SDave Jiang 5209f0d99b3SDave Jiang /* read iaa cap */ 5219f0d99b3SDave Jiang if (idxd->data->type == IDXD_TYPE_IAX && idxd->hw.version >= DEVICE_VERSION_2) 5229f0d99b3SDave Jiang idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET); 523bfe1d560SDave Jiang } 524bfe1d560SDave Jiang 525435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) 526bfe1d560SDave Jiang { 527bfe1d560SDave Jiang struct device *dev = &pdev->dev; 528700af3a0SDave Jiang struct device *conf_dev; 529bfe1d560SDave Jiang struct idxd_device *idxd; 53047c16ac2SDave Jiang int rc; 531bfe1d560SDave Jiang 53247c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 533bfe1d560SDave Jiang if (!idxd) 534bfe1d560SDave Jiang return NULL; 535bfe1d560SDave Jiang 536700af3a0SDave Jiang conf_dev = idxd_confdev(idxd); 537bfe1d560SDave Jiang idxd->pdev = pdev; 538435b512dSDave Jiang idxd->data = data; 539700af3a0SDave Jiang idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type); 5404b73e4ebSDave Jiang idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); 54147c16ac2SDave Jiang if (idxd->id < 0) 54247c16ac2SDave Jiang return NULL; 54347c16ac2SDave Jiang 544a8563a33SDave Jiang idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev)); 545a8563a33SDave Jiang if (!idxd->opcap_bmap) { 546a8563a33SDave Jiang ida_free(&idxd_ida, idxd->id); 547a8563a33SDave Jiang return NULL; 548a8563a33SDave Jiang } 549a8563a33SDave Jiang 550700af3a0SDave Jiang device_initialize(conf_dev); 551700af3a0SDave Jiang conf_dev->parent = dev; 552700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 553700af3a0SDave Jiang conf_dev->type = idxd->data->dev_type; 554700af3a0SDave Jiang rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); 55547c16ac2SDave Jiang if (rc < 0) { 556700af3a0SDave Jiang put_device(conf_dev); 55747c16ac2SDave Jiang return NULL; 55847c16ac2SDave Jiang } 55947c16ac2SDave Jiang 560bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 56153b2ee7fSDave Jiang spin_lock_init(&idxd->cmd_lock); 562bfe1d560SDave Jiang 563bfe1d560SDave Jiang return idxd; 564bfe1d560SDave Jiang } 565bfe1d560SDave Jiang 5668e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 5678e50d392SDave Jiang { 568f5ccf55eSJacob Pan struct pci_dev *pdev = idxd->pdev; 569f5ccf55eSJacob Pan struct device *dev = &pdev->dev; 570f5ccf55eSJacob Pan struct iommu_domain *domain; 571f5ccf55eSJacob Pan ioasid_t pasid; 572f5ccf55eSJacob Pan int ret; 573f5ccf55eSJacob Pan 574f5ccf55eSJacob Pan /* 575f5ccf55eSJacob Pan * Attach a global PASID to the DMA domain so that we can use ENQCMDS 576f5ccf55eSJacob Pan * to submit work on buffers mapped by DMA API. 577f5ccf55eSJacob Pan */ 578f5ccf55eSJacob Pan domain = iommu_get_domain_for_dev(dev); 579f5ccf55eSJacob Pan if (!domain) 580f5ccf55eSJacob Pan return -EPERM; 581f5ccf55eSJacob Pan 582f5ccf55eSJacob Pan pasid = iommu_alloc_global_pasid(dev); 583f5ccf55eSJacob Pan if (pasid == IOMMU_PASID_INVALID) 584f5ccf55eSJacob Pan return -ENOSPC; 585f5ccf55eSJacob Pan 586f5ccf55eSJacob Pan /* 587f5ccf55eSJacob Pan * DMA domain is owned by the driver, it should support all valid 588f5ccf55eSJacob Pan * types such as DMA-FQ, identity, etc. 589f5ccf55eSJacob Pan */ 59014678219SLu Baolu ret = iommu_attach_device_pasid(domain, dev, pasid, NULL); 591f5ccf55eSJacob Pan if (ret) { 592f5ccf55eSJacob Pan dev_err(dev, "failed to attach device pasid %d, domain type %d", 593f5ccf55eSJacob Pan pasid, domain->type); 594f5ccf55eSJacob Pan iommu_free_global_pasid(pasid); 595f5ccf55eSJacob Pan return ret; 596f5ccf55eSJacob Pan } 597f5ccf55eSJacob Pan 598f5ccf55eSJacob Pan /* Since we set user privilege for kernel DMA, enable completion IRQ */ 599f5ccf55eSJacob Pan idxd_set_user_intr(idxd, 1); 600f5ccf55eSJacob Pan idxd->pasid = pasid; 601f5ccf55eSJacob Pan 602f5ccf55eSJacob Pan return ret; 6038e50d392SDave Jiang } 6048e50d392SDave Jiang 6058e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 6068e50d392SDave Jiang { 607f5ccf55eSJacob Pan struct pci_dev *pdev = idxd->pdev; 608f5ccf55eSJacob Pan struct device *dev = &pdev->dev; 609f5ccf55eSJacob Pan struct iommu_domain *domain; 6108e50d392SDave Jiang 611f5ccf55eSJacob Pan domain = iommu_get_domain_for_dev(dev); 612f5ccf55eSJacob Pan if (!domain) 613f5ccf55eSJacob Pan return; 614f5ccf55eSJacob Pan 615f5ccf55eSJacob Pan iommu_detach_device_pasid(domain, dev, idxd->pasid); 616f5ccf55eSJacob Pan iommu_free_global_pasid(idxd->pasid); 617f5ccf55eSJacob Pan 618f5ccf55eSJacob Pan idxd_set_user_intr(idxd, 0); 6198e50d392SDave Jiang idxd->sva = NULL; 620f5ccf55eSJacob Pan idxd->pasid = IOMMU_PASID_INVALID; 6218e50d392SDave Jiang } 6228e50d392SDave Jiang 62384c9ef72SLu Baolu static int idxd_enable_sva(struct pci_dev *pdev) 62484c9ef72SLu Baolu { 62584c9ef72SLu Baolu int ret; 62684c9ef72SLu Baolu 62784c9ef72SLu Baolu ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 62884c9ef72SLu Baolu if (ret) 62984c9ef72SLu Baolu return ret; 63084c9ef72SLu Baolu 63184c9ef72SLu Baolu ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 63284c9ef72SLu Baolu if (ret) 63384c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 63484c9ef72SLu Baolu 63584c9ef72SLu Baolu return ret; 63684c9ef72SLu Baolu } 63784c9ef72SLu Baolu 63884c9ef72SLu Baolu static void idxd_disable_sva(struct pci_dev *pdev) 63984c9ef72SLu Baolu { 64084c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 64184c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 64284c9ef72SLu Baolu } 64384c9ef72SLu Baolu 644bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 645bfe1d560SDave Jiang { 646bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 647bfe1d560SDave Jiang struct device *dev = &pdev->dev; 648bfe1d560SDave Jiang int rc; 649bfe1d560SDave Jiang 650bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 65189e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 65289e3becdSDave Jiang if (rc < 0) 65389e3becdSDave Jiang return rc; 65489e3becdSDave Jiang 655bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 656bfe1d560SDave Jiang 65703d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 65884c9ef72SLu Baolu if (idxd_enable_sva(pdev)) { 65942a1b738SDave Jiang dev_warn(dev, "Unable to turn on user SVA feature.\n"); 6608ffccd11SJerry Snitselaar } else { 66142a1b738SDave Jiang set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags); 66242a1b738SDave Jiang 663f5ccf55eSJacob Pan rc = idxd_enable_system_pasid(idxd); 664f5ccf55eSJacob Pan if (rc) 665f5ccf55eSJacob Pan dev_warn(dev, "No in-kernel DMA with PASID. %d\n", rc); 66642a1b738SDave Jiang else 6678e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 6688ffccd11SJerry Snitselaar } 66903d939c7SDave Jiang } else if (!sva) { 67003d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n"); 6718e50d392SDave Jiang } 6728e50d392SDave Jiang 673bfe1d560SDave Jiang idxd_read_caps(idxd); 674bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 675bfe1d560SDave Jiang 676bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 677bfe1d560SDave Jiang if (rc) 6787c5dd23eSDave Jiang goto err; 679bfe1d560SDave Jiang 6808c66bbdcSDave Jiang /* If the configs are readonly, then load them from device */ 6818c66bbdcSDave Jiang if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { 6828c66bbdcSDave Jiang dev_dbg(dev, "Loading RO device config\n"); 6838c66bbdcSDave Jiang rc = idxd_device_load_config(idxd); 6848c66bbdcSDave Jiang if (rc < 0) 685ddf742d4SDave Jiang goto err_config; 6868c66bbdcSDave Jiang } 6878c66bbdcSDave Jiang 688bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 689bfe1d560SDave Jiang if (rc) 690ddf742d4SDave Jiang goto err_config; 691bfe1d560SDave Jiang 69242d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 69342d279f9SDave Jiang 6940bde4444STom Zanussi rc = perfmon_pmu_init(idxd); 6950bde4444STom Zanussi if (rc < 0) 6960bde4444STom Zanussi dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc); 6970bde4444STom Zanussi 698bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 699bfe1d560SDave Jiang return 0; 700bfe1d560SDave Jiang 701ddf742d4SDave Jiang err_config: 702ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 7037c5dd23eSDave Jiang err: 7048e50d392SDave Jiang if (device_pasid_enabled(idxd)) 7058e50d392SDave Jiang idxd_disable_system_pasid(idxd); 70642a1b738SDave Jiang if (device_user_pasid_enabled(idxd)) 70784c9ef72SLu Baolu idxd_disable_sva(pdev); 708bfe1d560SDave Jiang return rc; 709bfe1d560SDave Jiang } 710bfe1d560SDave Jiang 711ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd) 712ddf742d4SDave Jiang { 713ddf742d4SDave Jiang perfmon_pmu_remove(idxd); 714ddf742d4SDave Jiang idxd_cleanup_interrupts(idxd); 715ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 716ddf742d4SDave Jiang if (device_pasid_enabled(idxd)) 717ddf742d4SDave Jiang idxd_disable_system_pasid(idxd); 71842a1b738SDave Jiang if (device_user_pasid_enabled(idxd)) 71984c9ef72SLu Baolu idxd_disable_sva(idxd->pdev); 720ddf742d4SDave Jiang } 721ddf742d4SDave Jiang 722bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 723bfe1d560SDave Jiang { 724bfe1d560SDave Jiang struct device *dev = &pdev->dev; 725bfe1d560SDave Jiang struct idxd_device *idxd; 726435b512dSDave Jiang struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data; 727bfe1d560SDave Jiang int rc; 728bfe1d560SDave Jiang 729a39c7cd0SDave Jiang rc = pci_enable_device(pdev); 730bfe1d560SDave Jiang if (rc) 731bfe1d560SDave Jiang return rc; 732bfe1d560SDave Jiang 7338e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 734435b512dSDave Jiang idxd = idxd_alloc(pdev, data); 735a39c7cd0SDave Jiang if (!idxd) { 736a39c7cd0SDave Jiang rc = -ENOMEM; 737a39c7cd0SDave Jiang goto err_idxd_alloc; 738a39c7cd0SDave Jiang } 739bfe1d560SDave Jiang 7408e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 741a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 742a39c7cd0SDave Jiang if (!idxd->reg_base) { 743a39c7cd0SDave Jiang rc = -ENOMEM; 744a39c7cd0SDave Jiang goto err_iomap; 745a39c7cd0SDave Jiang } 746bfe1d560SDave Jiang 747bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 74853b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 749bfe1d560SDave Jiang if (rc) 750a39c7cd0SDave Jiang goto err; 751bfe1d560SDave Jiang 752bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 753bfe1d560SDave Jiang pci_set_master(pdev); 754bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 755bfe1d560SDave Jiang 756bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 757bfe1d560SDave Jiang rc = idxd_probe(idxd); 758bfe1d560SDave Jiang if (rc) { 759bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 760a39c7cd0SDave Jiang goto err; 761bfe1d560SDave Jiang } 762bfe1d560SDave Jiang 763979f6dedSTom Zanussi if (data->load_device_defaults) { 764979f6dedSTom Zanussi rc = data->load_device_defaults(idxd); 765979f6dedSTom Zanussi if (rc) 766979f6dedSTom Zanussi dev_warn(dev, "IDXD loading device defaults failed\n"); 767979f6dedSTom Zanussi } 768979f6dedSTom Zanussi 76947c16ac2SDave Jiang rc = idxd_register_devices(idxd); 770c52ca478SDave Jiang if (rc) { 771c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 772ddf742d4SDave Jiang goto err_dev_register; 773c52ca478SDave Jiang } 774c52ca478SDave Jiang 7755fbe6503SDave Jiang rc = idxd_device_init_debugfs(idxd); 7765fbe6503SDave Jiang if (rc) 7775fbe6503SDave Jiang dev_warn(dev, "IDXD debugfs failed to setup\n"); 7785fbe6503SDave Jiang 779bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 780bfe1d560SDave Jiang idxd->hw.version); 781bfe1d560SDave Jiang 782e11452ebSArjan van de Ven idxd->user_submission_safe = data->user_submission_safe; 783e11452ebSArjan van de Ven 784bfe1d560SDave Jiang return 0; 785a39c7cd0SDave Jiang 786ddf742d4SDave Jiang err_dev_register: 787ddf742d4SDave Jiang idxd_cleanup(idxd); 788a39c7cd0SDave Jiang err: 789a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 790a39c7cd0SDave Jiang err_iomap: 791700af3a0SDave Jiang put_device(idxd_confdev(idxd)); 792a39c7cd0SDave Jiang err_idxd_alloc: 793a39c7cd0SDave Jiang pci_disable_device(pdev); 794a39c7cd0SDave Jiang return rc; 795bfe1d560SDave Jiang } 796bfe1d560SDave Jiang 7975b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd) 7985b0c68c4SDave Jiang { 7995b0c68c4SDave Jiang struct idxd_wq *wq; 8005b0c68c4SDave Jiang int i; 8015b0c68c4SDave Jiang 8025b0c68c4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 8035b0c68c4SDave Jiang wq = idxd->wqs[i]; 8045b0c68c4SDave Jiang if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL) 8055b0c68c4SDave Jiang idxd_wq_quiesce(wq); 8065b0c68c4SDave Jiang } 8075b0c68c4SDave Jiang } 8085b0c68c4SDave Jiang 809bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 810bfe1d560SDave Jiang { 811bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 812bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 813403a2e23SDave Jiang int rc; 814bfe1d560SDave Jiang 815bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 816bfe1d560SDave Jiang if (rc) 817bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 818bfe1d560SDave Jiang 819403a2e23SDave Jiang irq_entry = &idxd->ie; 8205fc8e85fSDave Jiang synchronize_irq(irq_entry->vector); 821403a2e23SDave Jiang idxd_mask_error_interrupts(idxd); 82249c4959fSDave Jiang flush_workqueue(idxd->wq); 823bfe1d560SDave Jiang } 824bfe1d560SDave Jiang 825bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 826bfe1d560SDave Jiang { 827bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 82849c4959fSDave Jiang struct idxd_irq_entry *irq_entry; 829bfe1d560SDave Jiang 83098da0106SDave Jiang idxd_unregister_devices(idxd); 83198da0106SDave Jiang /* 83298da0106SDave Jiang * When ->release() is called for the idxd->conf_dev, it frees all the memory related 83398da0106SDave Jiang * to the idxd context. The driver still needs those bits in order to do the rest of 83498da0106SDave Jiang * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref 83598da0106SDave Jiang * on the device here to hold off the freeing while allowing the idxd sub-driver 83698da0106SDave Jiang * to unbind. 83798da0106SDave Jiang */ 83898da0106SDave Jiang get_device(idxd_confdev(idxd)); 83998da0106SDave Jiang device_unregister(idxd_confdev(idxd)); 840bfe1d560SDave Jiang idxd_shutdown(pdev); 8418e50d392SDave Jiang if (device_pasid_enabled(idxd)) 8428e50d392SDave Jiang idxd_disable_system_pasid(idxd); 8435fbe6503SDave Jiang idxd_device_remove_debugfs(idxd); 84449c4959fSDave Jiang 845403a2e23SDave Jiang irq_entry = idxd_get_ie(idxd, 0); 84649c4959fSDave Jiang free_irq(irq_entry->vector, irq_entry); 84749c4959fSDave Jiang pci_free_irq_vectors(pdev); 84849c4959fSDave Jiang pci_iounmap(pdev, idxd->reg_base); 84942a1b738SDave Jiang if (device_user_pasid_enabled(idxd)) 85084c9ef72SLu Baolu idxd_disable_sva(pdev); 85149c4959fSDave Jiang pci_disable_device(pdev); 85249c4959fSDave Jiang destroy_workqueue(idxd->wq); 85349c4959fSDave Jiang perfmon_pmu_remove(idxd); 85498da0106SDave Jiang put_device(idxd_confdev(idxd)); 855bfe1d560SDave Jiang } 856bfe1d560SDave Jiang 857bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 858bfe1d560SDave Jiang .name = DRV_NAME, 859bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 860bfe1d560SDave Jiang .probe = idxd_pci_probe, 861bfe1d560SDave Jiang .remove = idxd_remove, 862bfe1d560SDave Jiang .shutdown = idxd_shutdown, 863bfe1d560SDave Jiang }; 864bfe1d560SDave Jiang 865bfe1d560SDave Jiang static int __init idxd_init_module(void) 866bfe1d560SDave Jiang { 8674b73e4ebSDave Jiang int err; 868bfe1d560SDave Jiang 869bfe1d560SDave Jiang /* 8708e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 871bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 872bfe1d560SDave Jiang */ 87374b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) { 874bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 875bfe1d560SDave Jiang return -ENODEV; 876bfe1d560SDave Jiang } 877bfe1d560SDave Jiang 87874b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_ENQCMD)) 8798e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 8808e50d392SDave Jiang else 8818e50d392SDave Jiang support_enqcmd = true; 882bfe1d560SDave Jiang 8830bde4444STom Zanussi perfmon_init(); 8840bde4444STom Zanussi 885034b3290SDave Jiang err = idxd_driver_register(&idxd_drv); 886034b3290SDave Jiang if (err < 0) 887034b3290SDave Jiang goto err_idxd_driver_register; 888034b3290SDave Jiang 8890cda4f69SDave Jiang err = idxd_driver_register(&idxd_dmaengine_drv); 8900cda4f69SDave Jiang if (err < 0) 8910cda4f69SDave Jiang goto err_idxd_dmaengine_driver_register; 8920cda4f69SDave Jiang 893448c3de8SDave Jiang err = idxd_driver_register(&idxd_user_drv); 894448c3de8SDave Jiang if (err < 0) 895448c3de8SDave Jiang goto err_idxd_user_driver_register; 896448c3de8SDave Jiang 89742d279f9SDave Jiang err = idxd_cdev_register(); 89842d279f9SDave Jiang if (err) 89942d279f9SDave Jiang goto err_cdev_register; 90042d279f9SDave Jiang 9015fbe6503SDave Jiang err = idxd_init_debugfs(); 9025fbe6503SDave Jiang if (err) 9035fbe6503SDave Jiang goto err_debugfs; 9045fbe6503SDave Jiang 905c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 906c52ca478SDave Jiang if (err) 907c52ca478SDave Jiang goto err_pci_register; 908c52ca478SDave Jiang 909bfe1d560SDave Jiang return 0; 910c52ca478SDave Jiang 911c52ca478SDave Jiang err_pci_register: 9125fbe6503SDave Jiang idxd_remove_debugfs(); 9135fbe6503SDave Jiang err_debugfs: 91442d279f9SDave Jiang idxd_cdev_remove(); 91542d279f9SDave Jiang err_cdev_register: 916448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 917448c3de8SDave Jiang err_idxd_user_driver_register: 9180cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 9190cda4f69SDave Jiang err_idxd_dmaengine_driver_register: 920034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 921034b3290SDave Jiang err_idxd_driver_register: 922c52ca478SDave Jiang return err; 923bfe1d560SDave Jiang } 924bfe1d560SDave Jiang module_init(idxd_init_module); 925bfe1d560SDave Jiang 926bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 927bfe1d560SDave Jiang { 928448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 9290cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 930034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 931bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 93242d279f9SDave Jiang idxd_cdev_remove(); 9330bde4444STom Zanussi perfmon_exit(); 9345fbe6503SDave Jiang idxd_remove_debugfs(); 935bfe1d560SDave Jiang } 936bfe1d560SDave Jiang module_exit(idxd_exit_module); 937