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), 502442b747SDave Jiang .cr_status_off = offsetof(struct dsa_completion_record, status), 512442b747SDave Jiang .cr_result_off = offsetof(struct dsa_completion_record, result), 52435b512dSDave Jiang }, 53435b512dSDave Jiang [IDXD_TYPE_IAX] = { 54435b512dSDave Jiang .name_prefix = "iax", 55435b512dSDave Jiang .type = IDXD_TYPE_IAX, 56435b512dSDave Jiang .compl_size = sizeof(struct iax_completion_record), 57435b512dSDave Jiang .align = 64, 58435b512dSDave Jiang .dev_type = &iax_device_type, 59c40bd7d9SDave Jiang .evl_cr_off = offsetof(struct iax_evl_entry, cr), 602442b747SDave Jiang .cr_status_off = offsetof(struct iax_completion_record, status), 612442b747SDave Jiang .cr_result_off = offsetof(struct iax_completion_record, error_code), 62435b512dSDave Jiang }, 63435b512dSDave Jiang }; 64435b512dSDave Jiang 65bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = { 66bfe1d560SDave Jiang /* DSA ver 1.0 platforms */ 67435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) }, 68f25b4638SDave Jiang 69f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 70435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) }, 71bfe1d560SDave Jiang { 0, } 72bfe1d560SDave Jiang }; 73bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 74bfe1d560SDave Jiang 75bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd) 76bfe1d560SDave Jiang { 77bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 78bfe1d560SDave Jiang struct device *dev = &pdev->dev; 79ec0d6423SDave Jiang struct idxd_irq_entry *ie; 80bfe1d560SDave Jiang int i, msixcnt; 81bfe1d560SDave Jiang int rc = 0; 82bfe1d560SDave Jiang 83bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev); 84bfe1d560SDave Jiang if (msixcnt < 0) { 85bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n"); 865fc8e85fSDave Jiang return -ENOSPC; 87bfe1d560SDave Jiang } 888b67426eSDave Jiang idxd->irq_cnt = msixcnt; 89bfe1d560SDave Jiang 905fc8e85fSDave Jiang rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); 915fc8e85fSDave Jiang if (rc != msixcnt) { 925fc8e85fSDave Jiang dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); 935fc8e85fSDave Jiang return -ENOSPC; 94bfe1d560SDave Jiang } 95bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 96bfe1d560SDave Jiang 97d5c10e0fSDave Jiang 98ec0d6423SDave Jiang ie = idxd_get_ie(idxd, 0); 99ec0d6423SDave Jiang ie->vector = pci_irq_vector(pdev, 0); 100ec0d6423SDave Jiang rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie); 101bfe1d560SDave Jiang if (rc < 0) { 102bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n"); 1035fc8e85fSDave Jiang goto err_misc_irq; 104bfe1d560SDave Jiang } 105403a2e23SDave Jiang dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector); 106bfe1d560SDave Jiang 107ec0d6423SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 108ec0d6423SDave Jiang int msix_idx = i + 1; 109bfe1d560SDave Jiang 110ec0d6423SDave Jiang ie = idxd_get_ie(idxd, msix_idx); 111ec0d6423SDave Jiang ie->id = msix_idx; 112ec0d6423SDave Jiang ie->int_handle = INVALID_INT_HANDLE; 113fffaed1eSJacob Pan ie->pasid = IOMMU_PASID_INVALID; 114403a2e23SDave Jiang 115ec0d6423SDave Jiang spin_lock_init(&ie->list_lock); 116ec0d6423SDave Jiang init_llist_head(&ie->pending_llist); 117ec0d6423SDave Jiang INIT_LIST_HEAD(&ie->work_list); 118bfe1d560SDave Jiang } 119bfe1d560SDave Jiang 120bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd); 121bfe1d560SDave Jiang return 0; 122bfe1d560SDave Jiang 1235fc8e85fSDave Jiang err_misc_irq: 124bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 1255fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 126bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n"); 127bfe1d560SDave Jiang return rc; 128bfe1d560SDave Jiang } 129bfe1d560SDave Jiang 130ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd) 131ddf742d4SDave Jiang { 132ddf742d4SDave Jiang struct pci_dev *pdev = idxd->pdev; 133ec0d6423SDave Jiang struct idxd_irq_entry *ie; 134403a2e23SDave Jiang int msixcnt; 135ddf742d4SDave Jiang 136403a2e23SDave Jiang msixcnt = pci_msix_vec_count(pdev); 137403a2e23SDave Jiang if (msixcnt <= 0) 138403a2e23SDave Jiang return; 139ddf742d4SDave Jiang 140403a2e23SDave Jiang ie = idxd_get_ie(idxd, 0); 141ddf742d4SDave Jiang idxd_mask_error_interrupts(idxd); 142403a2e23SDave Jiang free_irq(ie->vector, ie); 143ddf742d4SDave Jiang pci_free_irq_vectors(pdev); 144ddf742d4SDave Jiang } 145ddf742d4SDave Jiang 1467c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd) 1477c5dd23eSDave Jiang { 1487c5dd23eSDave Jiang struct device *dev = &idxd->pdev->dev; 1497c5dd23eSDave Jiang struct idxd_wq *wq; 150700af3a0SDave Jiang struct device *conf_dev; 1517c5dd23eSDave Jiang int i, rc; 1527c5dd23eSDave Jiang 1537c5dd23eSDave Jiang idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 1547c5dd23eSDave Jiang GFP_KERNEL, dev_to_node(dev)); 1557c5dd23eSDave Jiang if (!idxd->wqs) 1567c5dd23eSDave Jiang return -ENOMEM; 1577c5dd23eSDave Jiang 158de5819b9SJerry Snitselaar idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev)); 159de5819b9SJerry Snitselaar if (!idxd->wq_enable_map) { 160de5819b9SJerry Snitselaar kfree(idxd->wqs); 161de5819b9SJerry Snitselaar return -ENOMEM; 162de5819b9SJerry Snitselaar } 163de5819b9SJerry Snitselaar 1647c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 1657c5dd23eSDave Jiang wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); 1667c5dd23eSDave Jiang if (!wq) { 1677c5dd23eSDave Jiang rc = -ENOMEM; 1687c5dd23eSDave Jiang goto err; 1697c5dd23eSDave Jiang } 1707c5dd23eSDave Jiang 171700af3a0SDave Jiang idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ); 172700af3a0SDave Jiang conf_dev = wq_confdev(wq); 1737c5dd23eSDave Jiang wq->id = i; 1747c5dd23eSDave Jiang wq->idxd = idxd; 175700af3a0SDave Jiang device_initialize(wq_confdev(wq)); 176700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 177700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 178700af3a0SDave Jiang conf_dev->type = &idxd_wq_device_type; 179700af3a0SDave Jiang rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id); 1807c5dd23eSDave Jiang if (rc < 0) { 181700af3a0SDave Jiang put_device(conf_dev); 1827c5dd23eSDave Jiang goto err; 1837c5dd23eSDave Jiang } 1847c5dd23eSDave Jiang 1857c5dd23eSDave Jiang mutex_init(&wq->wq_lock); 18604922b74SDave Jiang init_waitqueue_head(&wq->err_queue); 18793a40a6dSDave Jiang init_completion(&wq->wq_dead); 18856fc39f5SDave Jiang init_completion(&wq->wq_resurrect); 18992452a72SDave Jiang wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER; 190e8dbd644SXiaochen Shen idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH); 1917930d855SDave Jiang wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES; 1927c5dd23eSDave Jiang wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); 1937c5dd23eSDave Jiang if (!wq->wqcfg) { 194700af3a0SDave Jiang put_device(conf_dev); 1957c5dd23eSDave Jiang rc = -ENOMEM; 1967c5dd23eSDave Jiang goto err; 1977c5dd23eSDave Jiang } 198b0325aefSDave Jiang 199b0325aefSDave Jiang if (idxd->hw.wq_cap.op_config) { 200b0325aefSDave Jiang wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL); 201b0325aefSDave Jiang if (!wq->opcap_bmap) { 202b0325aefSDave Jiang put_device(conf_dev); 203b0325aefSDave Jiang rc = -ENOMEM; 204b0325aefSDave Jiang goto err; 205b0325aefSDave Jiang } 206b0325aefSDave Jiang bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS); 207b0325aefSDave Jiang } 208b022f597SFenghua Yu mutex_init(&wq->uc_lock); 209b022f597SFenghua Yu xa_init(&wq->upasid_xa); 2107c5dd23eSDave Jiang idxd->wqs[i] = wq; 2117c5dd23eSDave Jiang } 2127c5dd23eSDave Jiang 2137c5dd23eSDave Jiang return 0; 2147c5dd23eSDave Jiang 2157c5dd23eSDave Jiang err: 216700af3a0SDave Jiang while (--i >= 0) { 217700af3a0SDave Jiang wq = idxd->wqs[i]; 218700af3a0SDave Jiang conf_dev = wq_confdev(wq); 219700af3a0SDave Jiang put_device(conf_dev); 220700af3a0SDave Jiang } 2217c5dd23eSDave Jiang return rc; 2227c5dd23eSDave Jiang } 2237c5dd23eSDave Jiang 22475b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd) 22575b91130SDave Jiang { 22675b91130SDave Jiang struct idxd_engine *engine; 22775b91130SDave Jiang struct device *dev = &idxd->pdev->dev; 228700af3a0SDave Jiang struct device *conf_dev; 22975b91130SDave Jiang int i, rc; 23075b91130SDave Jiang 23175b91130SDave Jiang idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), 23275b91130SDave Jiang GFP_KERNEL, dev_to_node(dev)); 23375b91130SDave Jiang if (!idxd->engines) 23475b91130SDave Jiang return -ENOMEM; 23575b91130SDave Jiang 23675b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) { 23775b91130SDave Jiang engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); 23875b91130SDave Jiang if (!engine) { 23975b91130SDave Jiang rc = -ENOMEM; 24075b91130SDave Jiang goto err; 24175b91130SDave Jiang } 24275b91130SDave Jiang 243700af3a0SDave Jiang idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE); 244700af3a0SDave Jiang conf_dev = engine_confdev(engine); 24575b91130SDave Jiang engine->id = i; 24675b91130SDave Jiang engine->idxd = idxd; 247700af3a0SDave Jiang device_initialize(conf_dev); 248700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 249700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 250700af3a0SDave Jiang conf_dev->type = &idxd_engine_device_type; 251700af3a0SDave Jiang rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id); 25275b91130SDave Jiang if (rc < 0) { 253700af3a0SDave Jiang put_device(conf_dev); 25475b91130SDave Jiang goto err; 25575b91130SDave Jiang } 25675b91130SDave Jiang 25775b91130SDave Jiang idxd->engines[i] = engine; 25875b91130SDave Jiang } 25975b91130SDave Jiang 26075b91130SDave Jiang return 0; 26175b91130SDave Jiang 26275b91130SDave Jiang err: 263700af3a0SDave Jiang while (--i >= 0) { 264700af3a0SDave Jiang engine = idxd->engines[i]; 265700af3a0SDave Jiang conf_dev = engine_confdev(engine); 266700af3a0SDave Jiang put_device(conf_dev); 267700af3a0SDave Jiang } 26875b91130SDave Jiang return rc; 26975b91130SDave Jiang } 27075b91130SDave Jiang 271defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd) 272defe49f9SDave Jiang { 273defe49f9SDave Jiang struct device *dev = &idxd->pdev->dev; 274700af3a0SDave Jiang struct device *conf_dev; 275defe49f9SDave Jiang struct idxd_group *group; 276defe49f9SDave Jiang int i, rc; 277defe49f9SDave Jiang 278defe49f9SDave Jiang idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), 279defe49f9SDave Jiang GFP_KERNEL, dev_to_node(dev)); 280defe49f9SDave Jiang if (!idxd->groups) 281defe49f9SDave Jiang return -ENOMEM; 282defe49f9SDave Jiang 283defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) { 284defe49f9SDave Jiang group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); 285defe49f9SDave Jiang if (!group) { 286defe49f9SDave Jiang rc = -ENOMEM; 287defe49f9SDave Jiang goto err; 288defe49f9SDave Jiang } 289defe49f9SDave Jiang 290700af3a0SDave Jiang idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP); 291700af3a0SDave Jiang conf_dev = group_confdev(group); 292defe49f9SDave Jiang group->id = i; 293defe49f9SDave Jiang group->idxd = idxd; 294700af3a0SDave Jiang device_initialize(conf_dev); 295700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 296700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 297700af3a0SDave Jiang conf_dev->type = &idxd_group_device_type; 298700af3a0SDave Jiang rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id); 299defe49f9SDave Jiang if (rc < 0) { 300700af3a0SDave Jiang put_device(conf_dev); 301defe49f9SDave Jiang goto err; 302defe49f9SDave Jiang } 303defe49f9SDave Jiang 304defe49f9SDave Jiang idxd->groups[i] = group; 3059735bde3SFenghua Yu if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) { 306ade8a86bSDave Jiang group->tc_a = 1; 307ade8a86bSDave Jiang group->tc_b = 1; 308ade8a86bSDave Jiang } else { 309defe49f9SDave Jiang group->tc_a = -1; 310defe49f9SDave Jiang group->tc_b = -1; 311defe49f9SDave Jiang } 312601bdadaSFenghua Yu /* 313601bdadaSFenghua Yu * The default value is the same as the value of 314601bdadaSFenghua Yu * total read buffers in GRPCAP. 315601bdadaSFenghua Yu */ 316601bdadaSFenghua Yu group->rdbufs_allowed = idxd->max_rdbufs; 317ade8a86bSDave Jiang } 318defe49f9SDave Jiang 319defe49f9SDave Jiang return 0; 320defe49f9SDave Jiang 321defe49f9SDave Jiang err: 322700af3a0SDave Jiang while (--i >= 0) { 323700af3a0SDave Jiang group = idxd->groups[i]; 324700af3a0SDave Jiang put_device(group_confdev(group)); 325700af3a0SDave Jiang } 326defe49f9SDave Jiang return rc; 327defe49f9SDave Jiang } 328defe49f9SDave Jiang 329ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd) 330ddf742d4SDave Jiang { 331ddf742d4SDave Jiang int i; 332ddf742d4SDave Jiang 333ddf742d4SDave Jiang for (i = 0; i < idxd->max_groups; i++) 334700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 335ddf742d4SDave Jiang for (i = 0; i < idxd->max_engines; i++) 336700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 337ddf742d4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) 338700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 339ddf742d4SDave Jiang destroy_workqueue(idxd->wq); 340ddf742d4SDave Jiang } 341ddf742d4SDave Jiang 3421649091fSDave Jiang static int idxd_init_evl(struct idxd_device *idxd) 3431649091fSDave Jiang { 3441649091fSDave Jiang struct device *dev = &idxd->pdev->dev; 3451649091fSDave Jiang struct idxd_evl *evl; 3461649091fSDave Jiang 3471649091fSDave Jiang if (idxd->hw.gen_cap.evl_support == 0) 3481649091fSDave Jiang return 0; 3491649091fSDave Jiang 3501649091fSDave Jiang evl = kzalloc_node(sizeof(*evl), GFP_KERNEL, dev_to_node(dev)); 3511649091fSDave Jiang if (!evl) 3521649091fSDave Jiang return -ENOMEM; 3531649091fSDave Jiang 354244da66cSDave Jiang spin_lock_init(&evl->lock); 3551649091fSDave Jiang evl->size = IDXD_EVL_SIZE_MIN; 356c2f156bfSDave Jiang 357c2f156bfSDave Jiang idxd->evl_cache = kmem_cache_create(dev_name(idxd_confdev(idxd)), 358c2f156bfSDave Jiang sizeof(struct idxd_evl_fault) + evl_ent_size(idxd), 359c2f156bfSDave Jiang 0, 0, NULL); 360c2f156bfSDave Jiang if (!idxd->evl_cache) { 361c2f156bfSDave Jiang kfree(evl); 362c2f156bfSDave Jiang return -ENOMEM; 363c2f156bfSDave Jiang } 364c2f156bfSDave Jiang 3651649091fSDave Jiang idxd->evl = evl; 3661649091fSDave Jiang return 0; 3671649091fSDave Jiang } 3681649091fSDave Jiang 369bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd) 370bfe1d560SDave Jiang { 371bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 372defe49f9SDave Jiang int rc, i; 373bfe1d560SDave Jiang 3740d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq); 3757c5dd23eSDave Jiang 3767c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd); 3777c5dd23eSDave Jiang if (rc < 0) 378eb15e715SDave Jiang goto err_wqs; 3797c5dd23eSDave Jiang 38075b91130SDave Jiang rc = idxd_setup_engines(idxd); 38175b91130SDave Jiang if (rc < 0) 38275b91130SDave Jiang goto err_engine; 38375b91130SDave Jiang 384defe49f9SDave Jiang rc = idxd_setup_groups(idxd); 385defe49f9SDave Jiang if (rc < 0) 386defe49f9SDave Jiang goto err_group; 387bfe1d560SDave Jiang 3880d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 3897c5dd23eSDave Jiang if (!idxd->wq) { 3907c5dd23eSDave Jiang rc = -ENOMEM; 391defe49f9SDave Jiang goto err_wkq_create; 3927c5dd23eSDave Jiang } 3930d5c10b4SDave Jiang 3941649091fSDave Jiang rc = idxd_init_evl(idxd); 3951649091fSDave Jiang if (rc < 0) 3961649091fSDave Jiang goto err_evl; 3971649091fSDave Jiang 398bfe1d560SDave Jiang return 0; 3997c5dd23eSDave Jiang 4001649091fSDave Jiang err_evl: 4011649091fSDave Jiang destroy_workqueue(idxd->wq); 402defe49f9SDave Jiang err_wkq_create: 403defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) 404700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 405defe49f9SDave Jiang err_group: 40675b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) 407700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 40875b91130SDave Jiang err_engine: 4097c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) 410700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 411eb15e715SDave Jiang err_wqs: 4127c5dd23eSDave Jiang return rc; 413bfe1d560SDave Jiang } 414bfe1d560SDave Jiang 415bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 416bfe1d560SDave Jiang { 417bfe1d560SDave Jiang union offsets_reg offsets; 418bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 419bfe1d560SDave Jiang 420bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 4212f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 4222f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 423bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 4242f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 4252f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 4262f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 4272f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 4282f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 429bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 430bfe1d560SDave Jiang } 431bfe1d560SDave Jiang 43234ca0066SDave Jiang void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count) 433a8563a33SDave Jiang { 434a8563a33SDave Jiang int i, j, nr; 435a8563a33SDave Jiang 436a8563a33SDave Jiang for (i = 0, nr = 0; i < count; i++) { 437a8563a33SDave Jiang for (j = 0; j < BITS_PER_LONG_LONG; j++) { 438a8563a33SDave Jiang if (val[i] & BIT(j)) 439a8563a33SDave Jiang set_bit(nr, bmap); 440a8563a33SDave Jiang nr++; 441a8563a33SDave Jiang } 442a8563a33SDave Jiang } 443a8563a33SDave Jiang } 444a8563a33SDave Jiang 445bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 446bfe1d560SDave Jiang { 447bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 448bfe1d560SDave Jiang int i; 449bfe1d560SDave Jiang 450bfe1d560SDave Jiang /* reading generic capabilities */ 451bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 452bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 453eb15e715SDave Jiang 454eb15e715SDave Jiang if (idxd->hw.gen_cap.cmd_cap) { 455eb15e715SDave Jiang idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET); 456eb15e715SDave Jiang dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap); 457eb15e715SDave Jiang } 458eb15e715SDave Jiang 4598b67426eSDave Jiang /* reading command capabilities */ 4608b67426eSDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) 4618b67426eSDave Jiang idxd->request_int_handles = true; 4628b67426eSDave Jiang 463bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 464bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 465e8dbd644SXiaochen Shen idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift); 466bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 467bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 468bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 469bfe1d560SDave Jiang 470bfe1d560SDave Jiang /* reading group capabilities */ 471bfe1d560SDave Jiang idxd->hw.group_cap.bits = 472bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 473bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 474bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 475bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 4767ed6f1b8SDave Jiang idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs; 4777ed6f1b8SDave Jiang dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs); 4787ed6f1b8SDave Jiang idxd->nr_rdbufs = idxd->max_rdbufs; 479bfe1d560SDave Jiang 480bfe1d560SDave Jiang /* read engine capabilities */ 481bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 482bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 483bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 484bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 485bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 486bfe1d560SDave Jiang 487bfe1d560SDave Jiang /* read workqueue capabilities */ 488bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 489bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 490bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 491bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 492bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 493bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 494d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 495d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 496bfe1d560SDave Jiang 497bfe1d560SDave Jiang /* reading operation capabilities */ 498bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 499bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 500bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 501bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 502bfe1d560SDave Jiang } 503a8563a33SDave Jiang multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4); 5049f0d99b3SDave Jiang 5059f0d99b3SDave Jiang /* read iaa cap */ 5069f0d99b3SDave Jiang if (idxd->data->type == IDXD_TYPE_IAX && idxd->hw.version >= DEVICE_VERSION_2) 5079f0d99b3SDave Jiang idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET); 508bfe1d560SDave Jiang } 509bfe1d560SDave Jiang 510435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) 511bfe1d560SDave Jiang { 512bfe1d560SDave Jiang struct device *dev = &pdev->dev; 513700af3a0SDave Jiang struct device *conf_dev; 514bfe1d560SDave Jiang struct idxd_device *idxd; 51547c16ac2SDave Jiang int rc; 516bfe1d560SDave Jiang 51747c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 518bfe1d560SDave Jiang if (!idxd) 519bfe1d560SDave Jiang return NULL; 520bfe1d560SDave Jiang 521700af3a0SDave Jiang conf_dev = idxd_confdev(idxd); 522bfe1d560SDave Jiang idxd->pdev = pdev; 523435b512dSDave Jiang idxd->data = data; 524700af3a0SDave Jiang idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type); 5254b73e4ebSDave Jiang idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); 52647c16ac2SDave Jiang if (idxd->id < 0) 52747c16ac2SDave Jiang return NULL; 52847c16ac2SDave Jiang 529a8563a33SDave Jiang idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev)); 530a8563a33SDave Jiang if (!idxd->opcap_bmap) { 531a8563a33SDave Jiang ida_free(&idxd_ida, idxd->id); 532a8563a33SDave Jiang return NULL; 533a8563a33SDave Jiang } 534a8563a33SDave Jiang 535700af3a0SDave Jiang device_initialize(conf_dev); 536700af3a0SDave Jiang conf_dev->parent = dev; 537700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 538700af3a0SDave Jiang conf_dev->type = idxd->data->dev_type; 539700af3a0SDave Jiang rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); 54047c16ac2SDave Jiang if (rc < 0) { 541700af3a0SDave Jiang put_device(conf_dev); 54247c16ac2SDave Jiang return NULL; 54347c16ac2SDave Jiang } 54447c16ac2SDave Jiang 545bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 54653b2ee7fSDave Jiang spin_lock_init(&idxd->cmd_lock); 547bfe1d560SDave Jiang 548bfe1d560SDave Jiang return idxd; 549bfe1d560SDave Jiang } 550bfe1d560SDave Jiang 5518e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 5528e50d392SDave Jiang { 553*f5ccf55eSJacob Pan struct pci_dev *pdev = idxd->pdev; 554*f5ccf55eSJacob Pan struct device *dev = &pdev->dev; 555*f5ccf55eSJacob Pan struct iommu_domain *domain; 556*f5ccf55eSJacob Pan ioasid_t pasid; 557*f5ccf55eSJacob Pan int ret; 558*f5ccf55eSJacob Pan 559*f5ccf55eSJacob Pan /* 560*f5ccf55eSJacob Pan * Attach a global PASID to the DMA domain so that we can use ENQCMDS 561*f5ccf55eSJacob Pan * to submit work on buffers mapped by DMA API. 562*f5ccf55eSJacob Pan */ 563*f5ccf55eSJacob Pan domain = iommu_get_domain_for_dev(dev); 564*f5ccf55eSJacob Pan if (!domain) 565*f5ccf55eSJacob Pan return -EPERM; 566*f5ccf55eSJacob Pan 567*f5ccf55eSJacob Pan pasid = iommu_alloc_global_pasid(dev); 568*f5ccf55eSJacob Pan if (pasid == IOMMU_PASID_INVALID) 569*f5ccf55eSJacob Pan return -ENOSPC; 570*f5ccf55eSJacob Pan 571*f5ccf55eSJacob Pan /* 572*f5ccf55eSJacob Pan * DMA domain is owned by the driver, it should support all valid 573*f5ccf55eSJacob Pan * types such as DMA-FQ, identity, etc. 574*f5ccf55eSJacob Pan */ 575*f5ccf55eSJacob Pan ret = iommu_attach_device_pasid(domain, dev, pasid); 576*f5ccf55eSJacob Pan if (ret) { 577*f5ccf55eSJacob Pan dev_err(dev, "failed to attach device pasid %d, domain type %d", 578*f5ccf55eSJacob Pan pasid, domain->type); 579*f5ccf55eSJacob Pan iommu_free_global_pasid(pasid); 580*f5ccf55eSJacob Pan return ret; 581*f5ccf55eSJacob Pan } 582*f5ccf55eSJacob Pan 583*f5ccf55eSJacob Pan /* Since we set user privilege for kernel DMA, enable completion IRQ */ 584*f5ccf55eSJacob Pan idxd_set_user_intr(idxd, 1); 585*f5ccf55eSJacob Pan idxd->pasid = pasid; 586*f5ccf55eSJacob Pan 587*f5ccf55eSJacob Pan return ret; 5888e50d392SDave Jiang } 5898e50d392SDave Jiang 5908e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 5918e50d392SDave Jiang { 592*f5ccf55eSJacob Pan struct pci_dev *pdev = idxd->pdev; 593*f5ccf55eSJacob Pan struct device *dev = &pdev->dev; 594*f5ccf55eSJacob Pan struct iommu_domain *domain; 5958e50d392SDave Jiang 596*f5ccf55eSJacob Pan domain = iommu_get_domain_for_dev(dev); 597*f5ccf55eSJacob Pan if (!domain) 598*f5ccf55eSJacob Pan return; 599*f5ccf55eSJacob Pan 600*f5ccf55eSJacob Pan iommu_detach_device_pasid(domain, dev, idxd->pasid); 601*f5ccf55eSJacob Pan iommu_free_global_pasid(idxd->pasid); 602*f5ccf55eSJacob Pan 603*f5ccf55eSJacob Pan idxd_set_user_intr(idxd, 0); 6048e50d392SDave Jiang idxd->sva = NULL; 605*f5ccf55eSJacob Pan idxd->pasid = IOMMU_PASID_INVALID; 6068e50d392SDave Jiang } 6078e50d392SDave Jiang 60884c9ef72SLu Baolu static int idxd_enable_sva(struct pci_dev *pdev) 60984c9ef72SLu Baolu { 61084c9ef72SLu Baolu int ret; 61184c9ef72SLu Baolu 61284c9ef72SLu Baolu ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 61384c9ef72SLu Baolu if (ret) 61484c9ef72SLu Baolu return ret; 61584c9ef72SLu Baolu 61684c9ef72SLu Baolu ret = iommu_dev_enable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 61784c9ef72SLu Baolu if (ret) 61884c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 61984c9ef72SLu Baolu 62084c9ef72SLu Baolu return ret; 62184c9ef72SLu Baolu } 62284c9ef72SLu Baolu 62384c9ef72SLu Baolu static void idxd_disable_sva(struct pci_dev *pdev) 62484c9ef72SLu Baolu { 62584c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 62684c9ef72SLu Baolu iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_IOPF); 62784c9ef72SLu Baolu } 62884c9ef72SLu Baolu 629bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 630bfe1d560SDave Jiang { 631bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 632bfe1d560SDave Jiang struct device *dev = &pdev->dev; 633bfe1d560SDave Jiang int rc; 634bfe1d560SDave Jiang 635bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 63689e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 63789e3becdSDave Jiang if (rc < 0) 63889e3becdSDave Jiang return rc; 63989e3becdSDave Jiang 640bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 641bfe1d560SDave Jiang 64203d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 64384c9ef72SLu Baolu if (idxd_enable_sva(pdev)) { 64442a1b738SDave Jiang dev_warn(dev, "Unable to turn on user SVA feature.\n"); 6458ffccd11SJerry Snitselaar } else { 64642a1b738SDave Jiang set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags); 64742a1b738SDave Jiang 648*f5ccf55eSJacob Pan rc = idxd_enable_system_pasid(idxd); 649*f5ccf55eSJacob Pan if (rc) 650*f5ccf55eSJacob Pan dev_warn(dev, "No in-kernel DMA with PASID. %d\n", rc); 65142a1b738SDave Jiang else 6528e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 6538ffccd11SJerry Snitselaar } 65403d939c7SDave Jiang } else if (!sva) { 65503d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n"); 6568e50d392SDave Jiang } 6578e50d392SDave Jiang 658bfe1d560SDave Jiang idxd_read_caps(idxd); 659bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 660bfe1d560SDave Jiang 661bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 662bfe1d560SDave Jiang if (rc) 6637c5dd23eSDave Jiang goto err; 664bfe1d560SDave Jiang 6658c66bbdcSDave Jiang /* If the configs are readonly, then load them from device */ 6668c66bbdcSDave Jiang if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { 6678c66bbdcSDave Jiang dev_dbg(dev, "Loading RO device config\n"); 6688c66bbdcSDave Jiang rc = idxd_device_load_config(idxd); 6698c66bbdcSDave Jiang if (rc < 0) 670ddf742d4SDave Jiang goto err_config; 6718c66bbdcSDave Jiang } 6728c66bbdcSDave Jiang 673bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 674bfe1d560SDave Jiang if (rc) 675ddf742d4SDave Jiang goto err_config; 676bfe1d560SDave Jiang 67742d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 67842d279f9SDave Jiang 6790bde4444STom Zanussi rc = perfmon_pmu_init(idxd); 6800bde4444STom Zanussi if (rc < 0) 6810bde4444STom Zanussi dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc); 6820bde4444STom Zanussi 683bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 684bfe1d560SDave Jiang return 0; 685bfe1d560SDave Jiang 686ddf742d4SDave Jiang err_config: 687ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 6887c5dd23eSDave Jiang err: 6898e50d392SDave Jiang if (device_pasid_enabled(idxd)) 6908e50d392SDave Jiang idxd_disable_system_pasid(idxd); 69142a1b738SDave Jiang if (device_user_pasid_enabled(idxd)) 69284c9ef72SLu Baolu idxd_disable_sva(pdev); 693bfe1d560SDave Jiang return rc; 694bfe1d560SDave Jiang } 695bfe1d560SDave Jiang 696ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd) 697ddf742d4SDave Jiang { 698ddf742d4SDave Jiang perfmon_pmu_remove(idxd); 699ddf742d4SDave Jiang idxd_cleanup_interrupts(idxd); 700ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 701ddf742d4SDave Jiang if (device_pasid_enabled(idxd)) 702ddf742d4SDave Jiang idxd_disable_system_pasid(idxd); 70342a1b738SDave Jiang if (device_user_pasid_enabled(idxd)) 70484c9ef72SLu Baolu idxd_disable_sva(idxd->pdev); 705ddf742d4SDave Jiang } 706ddf742d4SDave Jiang 707bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 708bfe1d560SDave Jiang { 709bfe1d560SDave Jiang struct device *dev = &pdev->dev; 710bfe1d560SDave Jiang struct idxd_device *idxd; 711435b512dSDave Jiang struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data; 712bfe1d560SDave Jiang int rc; 713bfe1d560SDave Jiang 714a39c7cd0SDave Jiang rc = pci_enable_device(pdev); 715bfe1d560SDave Jiang if (rc) 716bfe1d560SDave Jiang return rc; 717bfe1d560SDave Jiang 7188e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 719435b512dSDave Jiang idxd = idxd_alloc(pdev, data); 720a39c7cd0SDave Jiang if (!idxd) { 721a39c7cd0SDave Jiang rc = -ENOMEM; 722a39c7cd0SDave Jiang goto err_idxd_alloc; 723a39c7cd0SDave Jiang } 724bfe1d560SDave Jiang 7258e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 726a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 727a39c7cd0SDave Jiang if (!idxd->reg_base) { 728a39c7cd0SDave Jiang rc = -ENOMEM; 729a39c7cd0SDave Jiang goto err_iomap; 730a39c7cd0SDave Jiang } 731bfe1d560SDave Jiang 732bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 73353b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 734bfe1d560SDave Jiang if (rc) 735a39c7cd0SDave Jiang goto err; 736bfe1d560SDave Jiang 737bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 738bfe1d560SDave Jiang pci_set_master(pdev); 739bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 740bfe1d560SDave Jiang 741bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 742bfe1d560SDave Jiang rc = idxd_probe(idxd); 743bfe1d560SDave Jiang if (rc) { 744bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 745a39c7cd0SDave Jiang goto err; 746bfe1d560SDave Jiang } 747bfe1d560SDave Jiang 74847c16ac2SDave Jiang rc = idxd_register_devices(idxd); 749c52ca478SDave Jiang if (rc) { 750c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 751ddf742d4SDave Jiang goto err_dev_register; 752c52ca478SDave Jiang } 753c52ca478SDave Jiang 7545fbe6503SDave Jiang rc = idxd_device_init_debugfs(idxd); 7555fbe6503SDave Jiang if (rc) 7565fbe6503SDave Jiang dev_warn(dev, "IDXD debugfs failed to setup\n"); 7575fbe6503SDave Jiang 758bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 759bfe1d560SDave Jiang idxd->hw.version); 760bfe1d560SDave Jiang 761bfe1d560SDave Jiang return 0; 762a39c7cd0SDave Jiang 763ddf742d4SDave Jiang err_dev_register: 764ddf742d4SDave Jiang idxd_cleanup(idxd); 765a39c7cd0SDave Jiang err: 766a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 767a39c7cd0SDave Jiang err_iomap: 768700af3a0SDave Jiang put_device(idxd_confdev(idxd)); 769a39c7cd0SDave Jiang err_idxd_alloc: 770a39c7cd0SDave Jiang pci_disable_device(pdev); 771a39c7cd0SDave Jiang return rc; 772bfe1d560SDave Jiang } 773bfe1d560SDave Jiang 7745b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd) 7755b0c68c4SDave Jiang { 7765b0c68c4SDave Jiang struct idxd_wq *wq; 7775b0c68c4SDave Jiang int i; 7785b0c68c4SDave Jiang 7795b0c68c4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 7805b0c68c4SDave Jiang wq = idxd->wqs[i]; 7815b0c68c4SDave Jiang if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL) 7825b0c68c4SDave Jiang idxd_wq_quiesce(wq); 7835b0c68c4SDave Jiang } 7845b0c68c4SDave Jiang } 7855b0c68c4SDave Jiang 786bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 787bfe1d560SDave Jiang { 788bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 789bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 790403a2e23SDave Jiang int rc; 791bfe1d560SDave Jiang 792bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 793bfe1d560SDave Jiang if (rc) 794bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 795bfe1d560SDave Jiang 796403a2e23SDave Jiang irq_entry = &idxd->ie; 7975fc8e85fSDave Jiang synchronize_irq(irq_entry->vector); 798403a2e23SDave Jiang idxd_mask_error_interrupts(idxd); 79949c4959fSDave Jiang flush_workqueue(idxd->wq); 800bfe1d560SDave Jiang } 801bfe1d560SDave Jiang 802bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 803bfe1d560SDave Jiang { 804bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 80549c4959fSDave Jiang struct idxd_irq_entry *irq_entry; 806bfe1d560SDave Jiang 80798da0106SDave Jiang idxd_unregister_devices(idxd); 80898da0106SDave Jiang /* 80998da0106SDave Jiang * When ->release() is called for the idxd->conf_dev, it frees all the memory related 81098da0106SDave Jiang * to the idxd context. The driver still needs those bits in order to do the rest of 81198da0106SDave Jiang * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref 81298da0106SDave Jiang * on the device here to hold off the freeing while allowing the idxd sub-driver 81398da0106SDave Jiang * to unbind. 81498da0106SDave Jiang */ 81598da0106SDave Jiang get_device(idxd_confdev(idxd)); 81698da0106SDave Jiang device_unregister(idxd_confdev(idxd)); 817bfe1d560SDave Jiang idxd_shutdown(pdev); 8188e50d392SDave Jiang if (device_pasid_enabled(idxd)) 8198e50d392SDave Jiang idxd_disable_system_pasid(idxd); 8205fbe6503SDave Jiang idxd_device_remove_debugfs(idxd); 82149c4959fSDave Jiang 822403a2e23SDave Jiang irq_entry = idxd_get_ie(idxd, 0); 82349c4959fSDave Jiang free_irq(irq_entry->vector, irq_entry); 82449c4959fSDave Jiang pci_free_irq_vectors(pdev); 82549c4959fSDave Jiang pci_iounmap(pdev, idxd->reg_base); 82642a1b738SDave Jiang if (device_user_pasid_enabled(idxd)) 82784c9ef72SLu Baolu idxd_disable_sva(pdev); 82849c4959fSDave Jiang pci_disable_device(pdev); 82949c4959fSDave Jiang destroy_workqueue(idxd->wq); 83049c4959fSDave Jiang perfmon_pmu_remove(idxd); 83198da0106SDave Jiang put_device(idxd_confdev(idxd)); 832bfe1d560SDave Jiang } 833bfe1d560SDave Jiang 834bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 835bfe1d560SDave Jiang .name = DRV_NAME, 836bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 837bfe1d560SDave Jiang .probe = idxd_pci_probe, 838bfe1d560SDave Jiang .remove = idxd_remove, 839bfe1d560SDave Jiang .shutdown = idxd_shutdown, 840bfe1d560SDave Jiang }; 841bfe1d560SDave Jiang 842bfe1d560SDave Jiang static int __init idxd_init_module(void) 843bfe1d560SDave Jiang { 8444b73e4ebSDave Jiang int err; 845bfe1d560SDave Jiang 846bfe1d560SDave Jiang /* 8478e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 848bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 849bfe1d560SDave Jiang */ 85074b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) { 851bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 852bfe1d560SDave Jiang return -ENODEV; 853bfe1d560SDave Jiang } 854bfe1d560SDave Jiang 85574b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_ENQCMD)) 8568e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 8578e50d392SDave Jiang else 8588e50d392SDave Jiang support_enqcmd = true; 859bfe1d560SDave Jiang 8600bde4444STom Zanussi perfmon_init(); 8610bde4444STom Zanussi 862034b3290SDave Jiang err = idxd_driver_register(&idxd_drv); 863034b3290SDave Jiang if (err < 0) 864034b3290SDave Jiang goto err_idxd_driver_register; 865034b3290SDave Jiang 8660cda4f69SDave Jiang err = idxd_driver_register(&idxd_dmaengine_drv); 8670cda4f69SDave Jiang if (err < 0) 8680cda4f69SDave Jiang goto err_idxd_dmaengine_driver_register; 8690cda4f69SDave Jiang 870448c3de8SDave Jiang err = idxd_driver_register(&idxd_user_drv); 871448c3de8SDave Jiang if (err < 0) 872448c3de8SDave Jiang goto err_idxd_user_driver_register; 873448c3de8SDave Jiang 87442d279f9SDave Jiang err = idxd_cdev_register(); 87542d279f9SDave Jiang if (err) 87642d279f9SDave Jiang goto err_cdev_register; 87742d279f9SDave Jiang 8785fbe6503SDave Jiang err = idxd_init_debugfs(); 8795fbe6503SDave Jiang if (err) 8805fbe6503SDave Jiang goto err_debugfs; 8815fbe6503SDave Jiang 882c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 883c52ca478SDave Jiang if (err) 884c52ca478SDave Jiang goto err_pci_register; 885c52ca478SDave Jiang 886bfe1d560SDave Jiang return 0; 887c52ca478SDave Jiang 888c52ca478SDave Jiang err_pci_register: 8895fbe6503SDave Jiang idxd_remove_debugfs(); 8905fbe6503SDave Jiang err_debugfs: 89142d279f9SDave Jiang idxd_cdev_remove(); 89242d279f9SDave Jiang err_cdev_register: 893448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 894448c3de8SDave Jiang err_idxd_user_driver_register: 8950cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 8960cda4f69SDave Jiang err_idxd_dmaengine_driver_register: 897034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 898034b3290SDave Jiang err_idxd_driver_register: 899c52ca478SDave Jiang return err; 900bfe1d560SDave Jiang } 901bfe1d560SDave Jiang module_init(idxd_init_module); 902bfe1d560SDave Jiang 903bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 904bfe1d560SDave Jiang { 905448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 9060cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 907034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 908bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 90942d279f9SDave Jiang idxd_cdev_remove(); 9100bde4444STom Zanussi perfmon_exit(); 9115fbe6503SDave Jiang idxd_remove_debugfs(); 912bfe1d560SDave Jiang } 913bfe1d560SDave Jiang module_exit(idxd_exit_module); 914