1bfe1d560SDave Jiang // SPDX-License-Identifier: GPL-2.0 2bfe1d560SDave Jiang /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */ 3bfe1d560SDave Jiang #include <linux/init.h> 4bfe1d560SDave Jiang #include <linux/kernel.h> 5bfe1d560SDave Jiang #include <linux/module.h> 6bfe1d560SDave Jiang #include <linux/slab.h> 7bfe1d560SDave Jiang #include <linux/pci.h> 8bfe1d560SDave Jiang #include <linux/interrupt.h> 9bfe1d560SDave Jiang #include <linux/delay.h> 10bfe1d560SDave Jiang #include <linux/dma-mapping.h> 11bfe1d560SDave Jiang #include <linux/workqueue.h> 12bfe1d560SDave Jiang #include <linux/aer.h> 13bfe1d560SDave Jiang #include <linux/fs.h> 14bfe1d560SDave Jiang #include <linux/io-64-nonatomic-lo-hi.h> 15bfe1d560SDave Jiang #include <linux/device.h> 16bfe1d560SDave Jiang #include <linux/idr.h> 178e50d392SDave Jiang #include <linux/intel-svm.h> 188e50d392SDave Jiang #include <linux/iommu.h> 19bfe1d560SDave Jiang #include <uapi/linux/idxd.h> 208f47d1a5SDave Jiang #include <linux/dmaengine.h> 218f47d1a5SDave Jiang #include "../dmaengine.h" 22bfe1d560SDave Jiang #include "registers.h" 23bfe1d560SDave Jiang #include "idxd.h" 24bfe1d560SDave Jiang 25bfe1d560SDave Jiang MODULE_VERSION(IDXD_DRIVER_VERSION); 26bfe1d560SDave Jiang MODULE_LICENSE("GPL v2"); 27bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation"); 28bfe1d560SDave Jiang 29bfe1d560SDave Jiang #define DRV_NAME "idxd" 30bfe1d560SDave Jiang 318e50d392SDave Jiang bool support_enqcmd; 328e50d392SDave Jiang 33bfe1d560SDave Jiang static struct idr idxd_idrs[IDXD_TYPE_MAX]; 34bfe1d560SDave Jiang static struct mutex idxd_idr_lock; 35bfe1d560SDave Jiang 36bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = { 37bfe1d560SDave Jiang /* DSA ver 1.0 platforms */ 38bfe1d560SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_DSA_SPR0) }, 39f25b4638SDave Jiang 40f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 41f25b4638SDave Jiang { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IAX_SPR0) }, 42bfe1d560SDave Jiang { 0, } 43bfe1d560SDave Jiang }; 44bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 45bfe1d560SDave Jiang 46bfe1d560SDave Jiang static char *idxd_name[] = { 47bfe1d560SDave Jiang "dsa", 48f25b4638SDave Jiang "iax" 49bfe1d560SDave Jiang }; 50bfe1d560SDave Jiang 51bfe1d560SDave Jiang const char *idxd_get_dev_name(struct idxd_device *idxd) 52bfe1d560SDave Jiang { 53bfe1d560SDave Jiang return idxd_name[idxd->type]; 54bfe1d560SDave Jiang } 55bfe1d560SDave Jiang 56bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd) 57bfe1d560SDave Jiang { 58bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 59bfe1d560SDave Jiang struct device *dev = &pdev->dev; 60bfe1d560SDave Jiang struct msix_entry *msix; 61bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 62bfe1d560SDave Jiang int i, msixcnt; 63bfe1d560SDave Jiang int rc = 0; 648e50d392SDave Jiang union msix_perm mperm; 65bfe1d560SDave Jiang 66bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev); 67bfe1d560SDave Jiang if (msixcnt < 0) { 68bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n"); 69bfe1d560SDave Jiang goto err_no_irq; 70bfe1d560SDave Jiang } 71bfe1d560SDave Jiang 72bfe1d560SDave Jiang idxd->msix_entries = devm_kzalloc(dev, sizeof(struct msix_entry) * 73bfe1d560SDave Jiang msixcnt, GFP_KERNEL); 74bfe1d560SDave Jiang if (!idxd->msix_entries) { 75bfe1d560SDave Jiang rc = -ENOMEM; 76bfe1d560SDave Jiang goto err_no_irq; 77bfe1d560SDave Jiang } 78bfe1d560SDave Jiang 79bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) 80bfe1d560SDave Jiang idxd->msix_entries[i].entry = i; 81bfe1d560SDave Jiang 82bfe1d560SDave Jiang rc = pci_enable_msix_exact(pdev, idxd->msix_entries, msixcnt); 83bfe1d560SDave Jiang if (rc) { 84bfe1d560SDave Jiang dev_err(dev, "Failed enabling %d MSIX entries.\n", msixcnt); 85bfe1d560SDave Jiang goto err_no_irq; 86bfe1d560SDave Jiang } 87bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 88bfe1d560SDave Jiang 89bfe1d560SDave Jiang /* 90bfe1d560SDave Jiang * We implement 1 completion list per MSI-X entry except for 91bfe1d560SDave Jiang * entry 0, which is for errors and others. 92bfe1d560SDave Jiang */ 93bfe1d560SDave Jiang idxd->irq_entries = devm_kcalloc(dev, msixcnt, 94bfe1d560SDave Jiang sizeof(struct idxd_irq_entry), 95bfe1d560SDave Jiang GFP_KERNEL); 96bfe1d560SDave Jiang if (!idxd->irq_entries) { 97bfe1d560SDave Jiang rc = -ENOMEM; 98bfe1d560SDave Jiang goto err_no_irq; 99bfe1d560SDave Jiang } 100bfe1d560SDave Jiang 101bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 102bfe1d560SDave Jiang idxd->irq_entries[i].id = i; 103bfe1d560SDave Jiang idxd->irq_entries[i].idxd = idxd; 104e4f4d8cdSDave Jiang spin_lock_init(&idxd->irq_entries[i].list_lock); 105bfe1d560SDave Jiang } 106bfe1d560SDave Jiang 107bfe1d560SDave Jiang msix = &idxd->msix_entries[0]; 108bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[0]; 109bfe1d560SDave Jiang rc = devm_request_threaded_irq(dev, msix->vector, idxd_irq_handler, 110bfe1d560SDave Jiang idxd_misc_thread, 0, "idxd-misc", 111bfe1d560SDave Jiang irq_entry); 112bfe1d560SDave Jiang if (rc < 0) { 113bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n"); 114bfe1d560SDave Jiang goto err_no_irq; 115bfe1d560SDave Jiang } 116bfe1d560SDave Jiang 117bfe1d560SDave Jiang dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", 118bfe1d560SDave Jiang msix->vector); 119bfe1d560SDave Jiang 120bfe1d560SDave Jiang /* first MSI-X entry is not for wq interrupts */ 121bfe1d560SDave Jiang idxd->num_wq_irqs = msixcnt - 1; 122bfe1d560SDave Jiang 123bfe1d560SDave Jiang for (i = 1; i < msixcnt; i++) { 124bfe1d560SDave Jiang msix = &idxd->msix_entries[i]; 125bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 126bfe1d560SDave Jiang 127bfe1d560SDave Jiang init_llist_head(&idxd->irq_entries[i].pending_llist); 128bfe1d560SDave Jiang INIT_LIST_HEAD(&idxd->irq_entries[i].work_list); 129bfe1d560SDave Jiang rc = devm_request_threaded_irq(dev, msix->vector, 130bfe1d560SDave Jiang idxd_irq_handler, 131bfe1d560SDave Jiang idxd_wq_thread, 0, 132bfe1d560SDave Jiang "idxd-portal", irq_entry); 133bfe1d560SDave Jiang if (rc < 0) { 134bfe1d560SDave Jiang dev_err(dev, "Failed to allocate irq %d.\n", 135bfe1d560SDave Jiang msix->vector); 136bfe1d560SDave Jiang goto err_no_irq; 137bfe1d560SDave Jiang } 138bfe1d560SDave Jiang dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", 139bfe1d560SDave Jiang i, msix->vector); 140bfe1d560SDave Jiang } 141bfe1d560SDave Jiang 142bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd); 143bfe1d560SDave Jiang 1448e50d392SDave Jiang /* Setup MSIX permission table */ 1458e50d392SDave Jiang mperm.bits = 0; 1468e50d392SDave Jiang mperm.pasid = idxd->pasid; 1478e50d392SDave Jiang mperm.pasid_en = device_pasid_enabled(idxd); 1488e50d392SDave Jiang for (i = 1; i < msixcnt; i++) 1498e50d392SDave Jiang iowrite32(mperm.bits, idxd->reg_base + idxd->msix_perm_offset + i * 8); 1508e50d392SDave Jiang 151bfe1d560SDave Jiang return 0; 152bfe1d560SDave Jiang 153bfe1d560SDave Jiang err_no_irq: 154bfe1d560SDave Jiang /* Disable error interrupt generation */ 155bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 156bfe1d560SDave Jiang pci_disable_msix(pdev); 157bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n"); 158bfe1d560SDave Jiang return rc; 159bfe1d560SDave Jiang } 160bfe1d560SDave Jiang 161bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd) 162bfe1d560SDave Jiang { 163bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 164bfe1d560SDave Jiang int i; 165bfe1d560SDave Jiang 1660d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq); 167bfe1d560SDave Jiang idxd->groups = devm_kcalloc(dev, idxd->max_groups, 168bfe1d560SDave Jiang sizeof(struct idxd_group), GFP_KERNEL); 169bfe1d560SDave Jiang if (!idxd->groups) 170bfe1d560SDave Jiang return -ENOMEM; 171bfe1d560SDave Jiang 172bfe1d560SDave Jiang for (i = 0; i < idxd->max_groups; i++) { 173bfe1d560SDave Jiang idxd->groups[i].idxd = idxd; 174bfe1d560SDave Jiang idxd->groups[i].id = i; 175bfe1d560SDave Jiang idxd->groups[i].tc_a = -1; 176bfe1d560SDave Jiang idxd->groups[i].tc_b = -1; 177bfe1d560SDave Jiang } 178bfe1d560SDave Jiang 179bfe1d560SDave Jiang idxd->wqs = devm_kcalloc(dev, idxd->max_wqs, sizeof(struct idxd_wq), 180bfe1d560SDave Jiang GFP_KERNEL); 181bfe1d560SDave Jiang if (!idxd->wqs) 182bfe1d560SDave Jiang return -ENOMEM; 183bfe1d560SDave Jiang 184bfe1d560SDave Jiang idxd->engines = devm_kcalloc(dev, idxd->max_engines, 185bfe1d560SDave Jiang sizeof(struct idxd_engine), GFP_KERNEL); 186bfe1d560SDave Jiang if (!idxd->engines) 187bfe1d560SDave Jiang return -ENOMEM; 188bfe1d560SDave Jiang 189bfe1d560SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 190bfe1d560SDave Jiang struct idxd_wq *wq = &idxd->wqs[i]; 191bfe1d560SDave Jiang 192bfe1d560SDave Jiang wq->id = i; 193bfe1d560SDave Jiang wq->idxd = idxd; 194bfe1d560SDave Jiang mutex_init(&wq->wq_lock); 19542d279f9SDave Jiang wq->idxd_cdev.minor = -1; 196d7aad555SDave Jiang wq->max_xfer_bytes = idxd->max_xfer_bytes; 197e7184b15SDave Jiang wq->max_batch_size = idxd->max_batch_size; 198d98793b5SDave Jiang wq->wqcfg = devm_kzalloc(dev, idxd->wqcfg_size, GFP_KERNEL); 199d98793b5SDave Jiang if (!wq->wqcfg) 200d98793b5SDave Jiang return -ENOMEM; 201bfe1d560SDave Jiang } 202bfe1d560SDave Jiang 203bfe1d560SDave Jiang for (i = 0; i < idxd->max_engines; i++) { 204bfe1d560SDave Jiang idxd->engines[i].idxd = idxd; 205bfe1d560SDave Jiang idxd->engines[i].id = i; 206bfe1d560SDave Jiang } 207bfe1d560SDave Jiang 2080d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 2090d5c10b4SDave Jiang if (!idxd->wq) 2100d5c10b4SDave Jiang return -ENOMEM; 2110d5c10b4SDave Jiang 212bfe1d560SDave Jiang return 0; 213bfe1d560SDave Jiang } 214bfe1d560SDave Jiang 215bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 216bfe1d560SDave Jiang { 217bfe1d560SDave Jiang union offsets_reg offsets; 218bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 219bfe1d560SDave Jiang 220bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 2212f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 2222f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 223bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 2242f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 2252f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 2262f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 2272f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 2282f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 229bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 230bfe1d560SDave Jiang } 231bfe1d560SDave Jiang 232bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 233bfe1d560SDave Jiang { 234bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 235bfe1d560SDave Jiang int i; 236bfe1d560SDave Jiang 237bfe1d560SDave Jiang /* reading generic capabilities */ 238bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 239bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 240bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 241bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 242bfe1d560SDave Jiang idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift; 243bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 244bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 245bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 246bfe1d560SDave Jiang 247bfe1d560SDave Jiang /* reading group capabilities */ 248bfe1d560SDave Jiang idxd->hw.group_cap.bits = 249bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 250bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 251bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 252bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 253bfe1d560SDave Jiang idxd->max_tokens = idxd->hw.group_cap.total_tokens; 254bfe1d560SDave Jiang dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens); 255c52ca478SDave Jiang idxd->nr_tokens = idxd->max_tokens; 256bfe1d560SDave Jiang 257bfe1d560SDave Jiang /* read engine capabilities */ 258bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 259bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 260bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 261bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 262bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 263bfe1d560SDave Jiang 264bfe1d560SDave Jiang /* read workqueue capabilities */ 265bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 266bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 267bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 268bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 269bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 270bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 271d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 272d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 273bfe1d560SDave Jiang 274bfe1d560SDave Jiang /* reading operation capabilities */ 275bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 276bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 277bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 278bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 279bfe1d560SDave Jiang } 280bfe1d560SDave Jiang } 281bfe1d560SDave Jiang 2828e50d392SDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev) 283bfe1d560SDave Jiang { 284bfe1d560SDave Jiang struct device *dev = &pdev->dev; 285bfe1d560SDave Jiang struct idxd_device *idxd; 286bfe1d560SDave Jiang 287bfe1d560SDave Jiang idxd = devm_kzalloc(dev, sizeof(struct idxd_device), GFP_KERNEL); 288bfe1d560SDave Jiang if (!idxd) 289bfe1d560SDave Jiang return NULL; 290bfe1d560SDave Jiang 291bfe1d560SDave Jiang idxd->pdev = pdev; 292bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 293bfe1d560SDave Jiang 294bfe1d560SDave Jiang return idxd; 295bfe1d560SDave Jiang } 296bfe1d560SDave Jiang 2978e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 2988e50d392SDave Jiang { 2998e50d392SDave Jiang int flags; 3008e50d392SDave Jiang unsigned int pasid; 3018e50d392SDave Jiang struct iommu_sva *sva; 3028e50d392SDave Jiang 3038e50d392SDave Jiang flags = SVM_FLAG_SUPERVISOR_MODE; 3048e50d392SDave Jiang 3058e50d392SDave Jiang sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags); 3068e50d392SDave Jiang if (IS_ERR(sva)) { 3078e50d392SDave Jiang dev_warn(&idxd->pdev->dev, 3088e50d392SDave Jiang "iommu sva bind failed: %ld\n", PTR_ERR(sva)); 3098e50d392SDave Jiang return PTR_ERR(sva); 3108e50d392SDave Jiang } 3118e50d392SDave Jiang 3128e50d392SDave Jiang pasid = iommu_sva_get_pasid(sva); 3138e50d392SDave Jiang if (pasid == IOMMU_PASID_INVALID) { 3148e50d392SDave Jiang iommu_sva_unbind_device(sva); 3158e50d392SDave Jiang return -ENODEV; 3168e50d392SDave Jiang } 3178e50d392SDave Jiang 3188e50d392SDave Jiang idxd->sva = sva; 3198e50d392SDave Jiang idxd->pasid = pasid; 3208e50d392SDave Jiang dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid); 3218e50d392SDave Jiang return 0; 3228e50d392SDave Jiang } 3238e50d392SDave Jiang 3248e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 3258e50d392SDave Jiang { 3268e50d392SDave Jiang 3278e50d392SDave Jiang iommu_sva_unbind_device(idxd->sva); 3288e50d392SDave Jiang idxd->sva = NULL; 3298e50d392SDave Jiang } 3308e50d392SDave Jiang 331bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 332bfe1d560SDave Jiang { 333bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 334bfe1d560SDave Jiang struct device *dev = &pdev->dev; 335bfe1d560SDave Jiang int rc; 336bfe1d560SDave Jiang 337bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 338*89e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 339*89e3becdSDave Jiang if (rc < 0) 340*89e3becdSDave Jiang return rc; 341*89e3becdSDave Jiang 342bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 343bfe1d560SDave Jiang 3448e50d392SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM)) { 3458e50d392SDave Jiang rc = idxd_enable_system_pasid(idxd); 3468e50d392SDave Jiang if (rc < 0) 3478e50d392SDave Jiang dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); 3488e50d392SDave Jiang else 3498e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 3508e50d392SDave Jiang } 3518e50d392SDave Jiang 352bfe1d560SDave Jiang idxd_read_caps(idxd); 353bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 354bfe1d560SDave Jiang 355bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 356bfe1d560SDave Jiang if (rc) 357bfe1d560SDave Jiang goto err_setup; 358bfe1d560SDave Jiang 359bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 360bfe1d560SDave Jiang if (rc) 361bfe1d560SDave Jiang goto err_setup; 362bfe1d560SDave Jiang 363bfe1d560SDave Jiang dev_dbg(dev, "IDXD interrupt setup complete.\n"); 364bfe1d560SDave Jiang 365bfe1d560SDave Jiang mutex_lock(&idxd_idr_lock); 366bfe1d560SDave Jiang idxd->id = idr_alloc(&idxd_idrs[idxd->type], idxd, 0, 0, GFP_KERNEL); 367bfe1d560SDave Jiang mutex_unlock(&idxd_idr_lock); 368bfe1d560SDave Jiang if (idxd->id < 0) { 369bfe1d560SDave Jiang rc = -ENOMEM; 370bfe1d560SDave Jiang goto err_idr_fail; 371bfe1d560SDave Jiang } 372bfe1d560SDave Jiang 37342d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 37442d279f9SDave Jiang 375bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 376bfe1d560SDave Jiang return 0; 377bfe1d560SDave Jiang 378bfe1d560SDave Jiang err_idr_fail: 379bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 380bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 381bfe1d560SDave Jiang err_setup: 3828e50d392SDave Jiang if (device_pasid_enabled(idxd)) 3838e50d392SDave Jiang idxd_disable_system_pasid(idxd); 384bfe1d560SDave Jiang return rc; 385bfe1d560SDave Jiang } 386bfe1d560SDave Jiang 387f25b4638SDave Jiang static void idxd_type_init(struct idxd_device *idxd) 388f25b4638SDave Jiang { 389f25b4638SDave Jiang if (idxd->type == IDXD_TYPE_DSA) 390f25b4638SDave Jiang idxd->compl_size = sizeof(struct dsa_completion_record); 391f25b4638SDave Jiang else if (idxd->type == IDXD_TYPE_IAX) 392f25b4638SDave Jiang idxd->compl_size = sizeof(struct iax_completion_record); 393f25b4638SDave Jiang } 394f25b4638SDave Jiang 395bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 396bfe1d560SDave Jiang { 397bfe1d560SDave Jiang struct device *dev = &pdev->dev; 398bfe1d560SDave Jiang struct idxd_device *idxd; 399bfe1d560SDave Jiang int rc; 400bfe1d560SDave Jiang 401bfe1d560SDave Jiang rc = pcim_enable_device(pdev); 402bfe1d560SDave Jiang if (rc) 403bfe1d560SDave Jiang return rc; 404bfe1d560SDave Jiang 4058e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 4068e50d392SDave Jiang idxd = idxd_alloc(pdev); 4078e50d392SDave Jiang if (!idxd) 4088e50d392SDave Jiang return -ENOMEM; 409bfe1d560SDave Jiang 4108e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 4118e50d392SDave Jiang idxd->reg_base = pcim_iomap(pdev, IDXD_MMIO_BAR, 0); 4128e50d392SDave Jiang if (!idxd->reg_base) 413bfe1d560SDave Jiang return -ENOMEM; 414bfe1d560SDave Jiang 415bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 416bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 417bfe1d560SDave Jiang if (rc) 418bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 419bfe1d560SDave Jiang if (rc) 420bfe1d560SDave Jiang return rc; 421bfe1d560SDave Jiang 422bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 423bfe1d560SDave Jiang if (rc) 424bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 425bfe1d560SDave Jiang if (rc) 426bfe1d560SDave Jiang return rc; 427bfe1d560SDave Jiang 428bfe1d560SDave Jiang idxd_set_type(idxd); 429bfe1d560SDave Jiang 430f25b4638SDave Jiang idxd_type_init(idxd); 431f25b4638SDave Jiang 432bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 433bfe1d560SDave Jiang pci_set_master(pdev); 434bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 435bfe1d560SDave Jiang 436bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 437bfe1d560SDave Jiang rc = idxd_probe(idxd); 438bfe1d560SDave Jiang if (rc) { 439bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 440bfe1d560SDave Jiang return -ENODEV; 441bfe1d560SDave Jiang } 442bfe1d560SDave Jiang 443c52ca478SDave Jiang rc = idxd_setup_sysfs(idxd); 444c52ca478SDave Jiang if (rc) { 445c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 446c52ca478SDave Jiang return -ENODEV; 447c52ca478SDave Jiang } 448c52ca478SDave Jiang 449c52ca478SDave Jiang idxd->state = IDXD_DEV_CONF_READY; 450c52ca478SDave Jiang 451bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 452bfe1d560SDave Jiang idxd->hw.version); 453bfe1d560SDave Jiang 454bfe1d560SDave Jiang return 0; 455bfe1d560SDave Jiang } 456bfe1d560SDave Jiang 4578f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) 4588f47d1a5SDave Jiang { 4598f47d1a5SDave Jiang struct idxd_desc *desc, *itr; 4608f47d1a5SDave Jiang struct llist_node *head; 4618f47d1a5SDave Jiang 4628f47d1a5SDave Jiang head = llist_del_all(&ie->pending_llist); 4638f47d1a5SDave Jiang if (!head) 4648f47d1a5SDave Jiang return; 4658f47d1a5SDave Jiang 4668f47d1a5SDave Jiang llist_for_each_entry_safe(desc, itr, head, llnode) { 4678f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 4688f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 4698f47d1a5SDave Jiang } 4708f47d1a5SDave Jiang } 4718f47d1a5SDave Jiang 4728f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie) 4738f47d1a5SDave Jiang { 4748f47d1a5SDave Jiang struct idxd_desc *desc, *iter; 4758f47d1a5SDave Jiang 4768f47d1a5SDave Jiang list_for_each_entry_safe(desc, iter, &ie->work_list, list) { 4778f47d1a5SDave Jiang list_del(&desc->list); 4788f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 4798f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 4808f47d1a5SDave Jiang } 4818f47d1a5SDave Jiang } 4828f47d1a5SDave Jiang 483bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 484bfe1d560SDave Jiang { 485bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 486bfe1d560SDave Jiang int rc, i; 487bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 488bfe1d560SDave Jiang int msixcnt = pci_msix_vec_count(pdev); 489bfe1d560SDave Jiang 490bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 491bfe1d560SDave Jiang if (rc) 492bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 493bfe1d560SDave Jiang 494bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 495bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 496bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 497bfe1d560SDave Jiang 498bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 499bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 500bfe1d560SDave Jiang synchronize_irq(idxd->msix_entries[i].vector); 501bfe1d560SDave Jiang if (i == 0) 502bfe1d560SDave Jiang continue; 5038f47d1a5SDave Jiang idxd_flush_pending_llist(irq_entry); 5048f47d1a5SDave Jiang idxd_flush_work_list(irq_entry); 505bfe1d560SDave Jiang } 5060d5c10b4SDave Jiang 5070d5c10b4SDave Jiang destroy_workqueue(idxd->wq); 508bfe1d560SDave Jiang } 509bfe1d560SDave Jiang 510bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 511bfe1d560SDave Jiang { 512bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 513bfe1d560SDave Jiang 514bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 515c52ca478SDave Jiang idxd_cleanup_sysfs(idxd); 516bfe1d560SDave Jiang idxd_shutdown(pdev); 5178e50d392SDave Jiang if (device_pasid_enabled(idxd)) 5188e50d392SDave Jiang idxd_disable_system_pasid(idxd); 519bfe1d560SDave Jiang mutex_lock(&idxd_idr_lock); 520bfe1d560SDave Jiang idr_remove(&idxd_idrs[idxd->type], idxd->id); 521bfe1d560SDave Jiang mutex_unlock(&idxd_idr_lock); 522bfe1d560SDave Jiang } 523bfe1d560SDave Jiang 524bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 525bfe1d560SDave Jiang .name = DRV_NAME, 526bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 527bfe1d560SDave Jiang .probe = idxd_pci_probe, 528bfe1d560SDave Jiang .remove = idxd_remove, 529bfe1d560SDave Jiang .shutdown = idxd_shutdown, 530bfe1d560SDave Jiang }; 531bfe1d560SDave Jiang 532bfe1d560SDave Jiang static int __init idxd_init_module(void) 533bfe1d560SDave Jiang { 534bfe1d560SDave Jiang int err, i; 535bfe1d560SDave Jiang 536bfe1d560SDave Jiang /* 5378e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 538bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 539bfe1d560SDave Jiang */ 540bfe1d560SDave Jiang if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) { 541bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 542bfe1d560SDave Jiang return -ENODEV; 543bfe1d560SDave Jiang } 544bfe1d560SDave Jiang 5458e50d392SDave Jiang if (!boot_cpu_has(X86_FEATURE_ENQCMD)) 5468e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 5478e50d392SDave Jiang else 5488e50d392SDave Jiang support_enqcmd = true; 549bfe1d560SDave Jiang 550bfe1d560SDave Jiang mutex_init(&idxd_idr_lock); 551bfe1d560SDave Jiang for (i = 0; i < IDXD_TYPE_MAX; i++) 552bfe1d560SDave Jiang idr_init(&idxd_idrs[i]); 553bfe1d560SDave Jiang 554c52ca478SDave Jiang err = idxd_register_bus_type(); 555c52ca478SDave Jiang if (err < 0) 556bfe1d560SDave Jiang return err; 557bfe1d560SDave Jiang 558c52ca478SDave Jiang err = idxd_register_driver(); 559c52ca478SDave Jiang if (err < 0) 560c52ca478SDave Jiang goto err_idxd_driver_register; 561c52ca478SDave Jiang 56242d279f9SDave Jiang err = idxd_cdev_register(); 56342d279f9SDave Jiang if (err) 56442d279f9SDave Jiang goto err_cdev_register; 56542d279f9SDave Jiang 566c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 567c52ca478SDave Jiang if (err) 568c52ca478SDave Jiang goto err_pci_register; 569c52ca478SDave Jiang 570bfe1d560SDave Jiang return 0; 571c52ca478SDave Jiang 572c52ca478SDave Jiang err_pci_register: 57342d279f9SDave Jiang idxd_cdev_remove(); 57442d279f9SDave Jiang err_cdev_register: 575c52ca478SDave Jiang idxd_unregister_driver(); 576c52ca478SDave Jiang err_idxd_driver_register: 577c52ca478SDave Jiang idxd_unregister_bus_type(); 578c52ca478SDave Jiang return err; 579bfe1d560SDave Jiang } 580bfe1d560SDave Jiang module_init(idxd_init_module); 581bfe1d560SDave Jiang 582bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 583bfe1d560SDave Jiang { 584bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 58542d279f9SDave Jiang idxd_cdev_remove(); 586c52ca478SDave Jiang idxd_unregister_bus_type(); 587bfe1d560SDave Jiang } 588bfe1d560SDave Jiang module_exit(idxd_exit_module); 589