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) }, 39*f25b4638SDave Jiang 40*f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 41*f25b4638SDave 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", 48*f25b4638SDave 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__); 3380d5c10b4SDave Jiang idxd_device_init_reset(idxd); 339bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 340bfe1d560SDave Jiang 3418e50d392SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM)) { 3428e50d392SDave Jiang rc = idxd_enable_system_pasid(idxd); 3438e50d392SDave Jiang if (rc < 0) 3448e50d392SDave Jiang dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); 3458e50d392SDave Jiang else 3468e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 3478e50d392SDave Jiang } 3488e50d392SDave Jiang 349bfe1d560SDave Jiang idxd_read_caps(idxd); 350bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 351bfe1d560SDave Jiang 352bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 353bfe1d560SDave Jiang if (rc) 354bfe1d560SDave Jiang goto err_setup; 355bfe1d560SDave Jiang 356bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 357bfe1d560SDave Jiang if (rc) 358bfe1d560SDave Jiang goto err_setup; 359bfe1d560SDave Jiang 360bfe1d560SDave Jiang dev_dbg(dev, "IDXD interrupt setup complete.\n"); 361bfe1d560SDave Jiang 362bfe1d560SDave Jiang mutex_lock(&idxd_idr_lock); 363bfe1d560SDave Jiang idxd->id = idr_alloc(&idxd_idrs[idxd->type], idxd, 0, 0, GFP_KERNEL); 364bfe1d560SDave Jiang mutex_unlock(&idxd_idr_lock); 365bfe1d560SDave Jiang if (idxd->id < 0) { 366bfe1d560SDave Jiang rc = -ENOMEM; 367bfe1d560SDave Jiang goto err_idr_fail; 368bfe1d560SDave Jiang } 369bfe1d560SDave Jiang 37042d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 37142d279f9SDave Jiang 372bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 373bfe1d560SDave Jiang return 0; 374bfe1d560SDave Jiang 375bfe1d560SDave Jiang err_idr_fail: 376bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 377bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 378bfe1d560SDave Jiang err_setup: 3798e50d392SDave Jiang if (device_pasid_enabled(idxd)) 3808e50d392SDave Jiang idxd_disable_system_pasid(idxd); 381bfe1d560SDave Jiang return rc; 382bfe1d560SDave Jiang } 383bfe1d560SDave Jiang 384*f25b4638SDave Jiang static void idxd_type_init(struct idxd_device *idxd) 385*f25b4638SDave Jiang { 386*f25b4638SDave Jiang if (idxd->type == IDXD_TYPE_DSA) 387*f25b4638SDave Jiang idxd->compl_size = sizeof(struct dsa_completion_record); 388*f25b4638SDave Jiang else if (idxd->type == IDXD_TYPE_IAX) 389*f25b4638SDave Jiang idxd->compl_size = sizeof(struct iax_completion_record); 390*f25b4638SDave Jiang } 391*f25b4638SDave Jiang 392bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 393bfe1d560SDave Jiang { 394bfe1d560SDave Jiang struct device *dev = &pdev->dev; 395bfe1d560SDave Jiang struct idxd_device *idxd; 396bfe1d560SDave Jiang int rc; 397bfe1d560SDave Jiang 398bfe1d560SDave Jiang rc = pcim_enable_device(pdev); 399bfe1d560SDave Jiang if (rc) 400bfe1d560SDave Jiang return rc; 401bfe1d560SDave Jiang 4028e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 4038e50d392SDave Jiang idxd = idxd_alloc(pdev); 4048e50d392SDave Jiang if (!idxd) 4058e50d392SDave Jiang return -ENOMEM; 406bfe1d560SDave Jiang 4078e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 4088e50d392SDave Jiang idxd->reg_base = pcim_iomap(pdev, IDXD_MMIO_BAR, 0); 4098e50d392SDave Jiang if (!idxd->reg_base) 410bfe1d560SDave Jiang return -ENOMEM; 411bfe1d560SDave Jiang 412bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 413bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 414bfe1d560SDave Jiang if (rc) 415bfe1d560SDave Jiang rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 416bfe1d560SDave Jiang if (rc) 417bfe1d560SDave Jiang return rc; 418bfe1d560SDave Jiang 419bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 420bfe1d560SDave Jiang if (rc) 421bfe1d560SDave Jiang rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); 422bfe1d560SDave Jiang if (rc) 423bfe1d560SDave Jiang return rc; 424bfe1d560SDave Jiang 425bfe1d560SDave Jiang idxd_set_type(idxd); 426bfe1d560SDave Jiang 427*f25b4638SDave Jiang idxd_type_init(idxd); 428*f25b4638SDave Jiang 429bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 430bfe1d560SDave Jiang pci_set_master(pdev); 431bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 432bfe1d560SDave Jiang 433bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 434bfe1d560SDave Jiang rc = idxd_probe(idxd); 435bfe1d560SDave Jiang if (rc) { 436bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 437bfe1d560SDave Jiang return -ENODEV; 438bfe1d560SDave Jiang } 439bfe1d560SDave Jiang 440c52ca478SDave Jiang rc = idxd_setup_sysfs(idxd); 441c52ca478SDave Jiang if (rc) { 442c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 443c52ca478SDave Jiang return -ENODEV; 444c52ca478SDave Jiang } 445c52ca478SDave Jiang 446c52ca478SDave Jiang idxd->state = IDXD_DEV_CONF_READY; 447c52ca478SDave Jiang 448bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 449bfe1d560SDave Jiang idxd->hw.version); 450bfe1d560SDave Jiang 451bfe1d560SDave Jiang return 0; 452bfe1d560SDave Jiang } 453bfe1d560SDave Jiang 4548f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) 4558f47d1a5SDave Jiang { 4568f47d1a5SDave Jiang struct idxd_desc *desc, *itr; 4578f47d1a5SDave Jiang struct llist_node *head; 4588f47d1a5SDave Jiang 4598f47d1a5SDave Jiang head = llist_del_all(&ie->pending_llist); 4608f47d1a5SDave Jiang if (!head) 4618f47d1a5SDave Jiang return; 4628f47d1a5SDave Jiang 4638f47d1a5SDave Jiang llist_for_each_entry_safe(desc, itr, head, llnode) { 4648f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 4658f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 4668f47d1a5SDave Jiang } 4678f47d1a5SDave Jiang } 4688f47d1a5SDave Jiang 4698f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie) 4708f47d1a5SDave Jiang { 4718f47d1a5SDave Jiang struct idxd_desc *desc, *iter; 4728f47d1a5SDave Jiang 4738f47d1a5SDave Jiang list_for_each_entry_safe(desc, iter, &ie->work_list, list) { 4748f47d1a5SDave Jiang list_del(&desc->list); 4758f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 4768f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 4778f47d1a5SDave Jiang } 4788f47d1a5SDave Jiang } 4798f47d1a5SDave Jiang 480bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 481bfe1d560SDave Jiang { 482bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 483bfe1d560SDave Jiang int rc, i; 484bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 485bfe1d560SDave Jiang int msixcnt = pci_msix_vec_count(pdev); 486bfe1d560SDave Jiang 487bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 488bfe1d560SDave Jiang if (rc) 489bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 490bfe1d560SDave Jiang 491bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 492bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 493bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 494bfe1d560SDave Jiang 495bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 496bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 497bfe1d560SDave Jiang synchronize_irq(idxd->msix_entries[i].vector); 498bfe1d560SDave Jiang if (i == 0) 499bfe1d560SDave Jiang continue; 5008f47d1a5SDave Jiang idxd_flush_pending_llist(irq_entry); 5018f47d1a5SDave Jiang idxd_flush_work_list(irq_entry); 502bfe1d560SDave Jiang } 5030d5c10b4SDave Jiang 5040d5c10b4SDave Jiang destroy_workqueue(idxd->wq); 505bfe1d560SDave Jiang } 506bfe1d560SDave Jiang 507bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 508bfe1d560SDave Jiang { 509bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 510bfe1d560SDave Jiang 511bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 512c52ca478SDave Jiang idxd_cleanup_sysfs(idxd); 513bfe1d560SDave Jiang idxd_shutdown(pdev); 5148e50d392SDave Jiang if (device_pasid_enabled(idxd)) 5158e50d392SDave Jiang idxd_disable_system_pasid(idxd); 516bfe1d560SDave Jiang mutex_lock(&idxd_idr_lock); 517bfe1d560SDave Jiang idr_remove(&idxd_idrs[idxd->type], idxd->id); 518bfe1d560SDave Jiang mutex_unlock(&idxd_idr_lock); 519bfe1d560SDave Jiang } 520bfe1d560SDave Jiang 521bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 522bfe1d560SDave Jiang .name = DRV_NAME, 523bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 524bfe1d560SDave Jiang .probe = idxd_pci_probe, 525bfe1d560SDave Jiang .remove = idxd_remove, 526bfe1d560SDave Jiang .shutdown = idxd_shutdown, 527bfe1d560SDave Jiang }; 528bfe1d560SDave Jiang 529bfe1d560SDave Jiang static int __init idxd_init_module(void) 530bfe1d560SDave Jiang { 531bfe1d560SDave Jiang int err, i; 532bfe1d560SDave Jiang 533bfe1d560SDave Jiang /* 5348e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 535bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 536bfe1d560SDave Jiang */ 537bfe1d560SDave Jiang if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) { 538bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 539bfe1d560SDave Jiang return -ENODEV; 540bfe1d560SDave Jiang } 541bfe1d560SDave Jiang 5428e50d392SDave Jiang if (!boot_cpu_has(X86_FEATURE_ENQCMD)) 5438e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 5448e50d392SDave Jiang else 5458e50d392SDave Jiang support_enqcmd = true; 546bfe1d560SDave Jiang 547bfe1d560SDave Jiang mutex_init(&idxd_idr_lock); 548bfe1d560SDave Jiang for (i = 0; i < IDXD_TYPE_MAX; i++) 549bfe1d560SDave Jiang idr_init(&idxd_idrs[i]); 550bfe1d560SDave Jiang 551c52ca478SDave Jiang err = idxd_register_bus_type(); 552c52ca478SDave Jiang if (err < 0) 553bfe1d560SDave Jiang return err; 554bfe1d560SDave Jiang 555c52ca478SDave Jiang err = idxd_register_driver(); 556c52ca478SDave Jiang if (err < 0) 557c52ca478SDave Jiang goto err_idxd_driver_register; 558c52ca478SDave Jiang 55942d279f9SDave Jiang err = idxd_cdev_register(); 56042d279f9SDave Jiang if (err) 56142d279f9SDave Jiang goto err_cdev_register; 56242d279f9SDave Jiang 563c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 564c52ca478SDave Jiang if (err) 565c52ca478SDave Jiang goto err_pci_register; 566c52ca478SDave Jiang 567bfe1d560SDave Jiang return 0; 568c52ca478SDave Jiang 569c52ca478SDave Jiang err_pci_register: 57042d279f9SDave Jiang idxd_cdev_remove(); 57142d279f9SDave Jiang err_cdev_register: 572c52ca478SDave Jiang idxd_unregister_driver(); 573c52ca478SDave Jiang err_idxd_driver_register: 574c52ca478SDave Jiang idxd_unregister_bus_type(); 575c52ca478SDave Jiang return err; 576bfe1d560SDave Jiang } 577bfe1d560SDave Jiang module_init(idxd_init_module); 578bfe1d560SDave Jiang 579bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 580bfe1d560SDave Jiang { 581bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 58242d279f9SDave Jiang idxd_cdev_remove(); 583c52ca478SDave Jiang idxd_unregister_bus_type(); 584bfe1d560SDave Jiang } 585bfe1d560SDave Jiang module_exit(idxd_exit_module); 586