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" 240bde4444STom Zanussi #include "perfmon.h" 25bfe1d560SDave Jiang 26bfe1d560SDave Jiang MODULE_VERSION(IDXD_DRIVER_VERSION); 27bfe1d560SDave Jiang MODULE_LICENSE("GPL v2"); 28bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation"); 29d9e5481fSDave Jiang MODULE_IMPORT_NS(IDXD); 30bfe1d560SDave Jiang 3103d939c7SDave Jiang static bool sva = true; 3203d939c7SDave Jiang module_param(sva, bool, 0644); 3303d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off"); 3403d939c7SDave Jiang 35ade8a86bSDave Jiang bool tc_override; 36ade8a86bSDave Jiang module_param(tc_override, bool, 0644); 37ade8a86bSDave Jiang MODULE_PARM_DESC(tc_override, "Override traffic class defaults"); 38ade8a86bSDave Jiang 39bfe1d560SDave Jiang #define DRV_NAME "idxd" 40bfe1d560SDave Jiang 418e50d392SDave Jiang bool support_enqcmd; 424b73e4ebSDave Jiang DEFINE_IDA(idxd_ida); 43bfe1d560SDave Jiang 44435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = { 45435b512dSDave Jiang [IDXD_TYPE_DSA] = { 46435b512dSDave Jiang .name_prefix = "dsa", 47435b512dSDave Jiang .type = IDXD_TYPE_DSA, 48435b512dSDave Jiang .compl_size = sizeof(struct dsa_completion_record), 49435b512dSDave Jiang .align = 32, 50435b512dSDave Jiang .dev_type = &dsa_device_type, 51435b512dSDave Jiang }, 52435b512dSDave Jiang [IDXD_TYPE_IAX] = { 53435b512dSDave Jiang .name_prefix = "iax", 54435b512dSDave Jiang .type = IDXD_TYPE_IAX, 55435b512dSDave Jiang .compl_size = sizeof(struct iax_completion_record), 56435b512dSDave Jiang .align = 64, 57435b512dSDave Jiang .dev_type = &iax_device_type, 58435b512dSDave Jiang }, 59435b512dSDave Jiang }; 60435b512dSDave Jiang 61bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = { 62bfe1d560SDave Jiang /* DSA ver 1.0 platforms */ 63435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) }, 64f25b4638SDave Jiang 65f25b4638SDave Jiang /* IAX ver 1.0 platforms */ 66435b512dSDave Jiang { PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) }, 67bfe1d560SDave Jiang { 0, } 68bfe1d560SDave Jiang }; 69bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl); 70bfe1d560SDave Jiang 71bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd) 72bfe1d560SDave Jiang { 73bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 74bfe1d560SDave Jiang struct device *dev = &pdev->dev; 75bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 76bfe1d560SDave Jiang int i, msixcnt; 77bfe1d560SDave Jiang int rc = 0; 78bfe1d560SDave Jiang 79bfe1d560SDave Jiang msixcnt = pci_msix_vec_count(pdev); 80bfe1d560SDave Jiang if (msixcnt < 0) { 81bfe1d560SDave Jiang dev_err(dev, "Not MSI-X interrupt capable.\n"); 825fc8e85fSDave Jiang return -ENOSPC; 83bfe1d560SDave Jiang } 84bfe1d560SDave Jiang 855fc8e85fSDave Jiang rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX); 865fc8e85fSDave Jiang if (rc != msixcnt) { 875fc8e85fSDave Jiang dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc); 885fc8e85fSDave Jiang return -ENOSPC; 89bfe1d560SDave Jiang } 90bfe1d560SDave Jiang dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt); 91bfe1d560SDave Jiang 92bfe1d560SDave Jiang /* 93bfe1d560SDave Jiang * We implement 1 completion list per MSI-X entry except for 94bfe1d560SDave Jiang * entry 0, which is for errors and others. 95bfe1d560SDave Jiang */ 9647c16ac2SDave Jiang idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry), 9747c16ac2SDave Jiang GFP_KERNEL, dev_to_node(dev)); 98bfe1d560SDave Jiang if (!idxd->irq_entries) { 99bfe1d560SDave Jiang rc = -ENOMEM; 1005fc8e85fSDave Jiang goto err_irq_entries; 101bfe1d560SDave Jiang } 102bfe1d560SDave Jiang 103bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 104bfe1d560SDave Jiang idxd->irq_entries[i].id = i; 105bfe1d560SDave Jiang idxd->irq_entries[i].idxd = idxd; 1065fc8e85fSDave Jiang idxd->irq_entries[i].vector = pci_irq_vector(pdev, i); 107e4f4d8cdSDave Jiang spin_lock_init(&idxd->irq_entries[i].list_lock); 108bfe1d560SDave Jiang } 109bfe1d560SDave Jiang 110d5c10e0fSDave Jiang idxd_msix_perm_setup(idxd); 111d5c10e0fSDave Jiang 112bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[0]; 113a1610461SDave Jiang rc = request_threaded_irq(irq_entry->vector, NULL, idxd_misc_thread, 1145fc8e85fSDave Jiang 0, "idxd-misc", irq_entry); 115bfe1d560SDave Jiang if (rc < 0) { 116bfe1d560SDave Jiang dev_err(dev, "Failed to allocate misc interrupt.\n"); 1175fc8e85fSDave Jiang goto err_misc_irq; 118bfe1d560SDave Jiang } 119bfe1d560SDave Jiang 1205fc8e85fSDave Jiang dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector); 121bfe1d560SDave Jiang 122bfe1d560SDave Jiang /* first MSI-X entry is not for wq interrupts */ 123bfe1d560SDave Jiang idxd->num_wq_irqs = msixcnt - 1; 124bfe1d560SDave Jiang 125bfe1d560SDave Jiang for (i = 1; i < msixcnt; i++) { 126bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 127bfe1d560SDave Jiang 128bfe1d560SDave Jiang init_llist_head(&idxd->irq_entries[i].pending_llist); 129bfe1d560SDave Jiang INIT_LIST_HEAD(&idxd->irq_entries[i].work_list); 130a1610461SDave Jiang rc = request_threaded_irq(irq_entry->vector, NULL, 1315fc8e85fSDave Jiang idxd_wq_thread, 0, "idxd-portal", irq_entry); 132bfe1d560SDave Jiang if (rc < 0) { 1335fc8e85fSDave Jiang dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector); 1345fc8e85fSDave Jiang goto err_wq_irqs; 135bfe1d560SDave Jiang } 136eb15e715SDave Jiang 1375fc8e85fSDave Jiang dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector); 138eb15e715SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) { 139eb15e715SDave Jiang /* 140eb15e715SDave Jiang * The MSIX vector enumeration starts at 1 with vector 0 being the 141eb15e715SDave Jiang * misc interrupt that handles non I/O completion events. The 142eb15e715SDave Jiang * interrupt handles are for IMS enumeration on guest. The misc 143eb15e715SDave Jiang * interrupt vector does not require a handle and therefore we start 144eb15e715SDave Jiang * the int_handles at index 0. Since 'i' starts at 1, the first 145eb15e715SDave Jiang * int_handles index will be 0. 146eb15e715SDave Jiang */ 147eb15e715SDave Jiang rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1], 148eb15e715SDave Jiang IDXD_IRQ_MSIX); 149eb15e715SDave Jiang if (rc < 0) { 150eb15e715SDave Jiang free_irq(irq_entry->vector, irq_entry); 151eb15e715SDave Jiang goto err_wq_irqs; 152eb15e715SDave Jiang } 153eb15e715SDave Jiang dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]); 154eb15e715SDave Jiang } 155bfe1d560SDave Jiang } 156bfe1d560SDave Jiang 157bfe1d560SDave Jiang idxd_unmask_error_interrupts(idxd); 158bfe1d560SDave Jiang return 0; 159bfe1d560SDave Jiang 1605fc8e85fSDave Jiang err_wq_irqs: 1615fc8e85fSDave Jiang while (--i >= 0) { 1625fc8e85fSDave Jiang irq_entry = &idxd->irq_entries[i]; 1635fc8e85fSDave Jiang free_irq(irq_entry->vector, irq_entry); 164eb15e715SDave Jiang if (i != 0) 165eb15e715SDave Jiang idxd_device_release_int_handle(idxd, 166eb15e715SDave Jiang idxd->int_handles[i], IDXD_IRQ_MSIX); 1675fc8e85fSDave Jiang } 1685fc8e85fSDave Jiang err_misc_irq: 169bfe1d560SDave Jiang /* Disable error interrupt generation */ 170bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 171d5c10e0fSDave Jiang idxd_msix_perm_clear(idxd); 1725fc8e85fSDave Jiang err_irq_entries: 1735fc8e85fSDave Jiang pci_free_irq_vectors(pdev); 174bfe1d560SDave Jiang dev_err(dev, "No usable interrupts\n"); 175bfe1d560SDave Jiang return rc; 176bfe1d560SDave Jiang } 177bfe1d560SDave Jiang 178ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd) 179ddf742d4SDave Jiang { 180ddf742d4SDave Jiang struct pci_dev *pdev = idxd->pdev; 181ddf742d4SDave Jiang struct idxd_irq_entry *irq_entry; 182ddf742d4SDave Jiang int i, msixcnt; 183ddf742d4SDave Jiang 184ddf742d4SDave Jiang msixcnt = pci_msix_vec_count(pdev); 185ddf742d4SDave Jiang if (msixcnt <= 0) 186ddf742d4SDave Jiang return; 187ddf742d4SDave Jiang 188ddf742d4SDave Jiang irq_entry = &idxd->irq_entries[0]; 189ddf742d4SDave Jiang free_irq(irq_entry->vector, irq_entry); 190ddf742d4SDave Jiang 191ddf742d4SDave Jiang for (i = 1; i < msixcnt; i++) { 192ddf742d4SDave Jiang 193ddf742d4SDave Jiang irq_entry = &idxd->irq_entries[i]; 194ddf742d4SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) 195ddf742d4SDave Jiang idxd_device_release_int_handle(idxd, idxd->int_handles[i], 196ddf742d4SDave Jiang IDXD_IRQ_MSIX); 197ddf742d4SDave Jiang free_irq(irq_entry->vector, irq_entry); 198ddf742d4SDave Jiang } 199ddf742d4SDave Jiang 200ddf742d4SDave Jiang idxd_mask_error_interrupts(idxd); 201ddf742d4SDave Jiang pci_free_irq_vectors(pdev); 202ddf742d4SDave Jiang } 203ddf742d4SDave Jiang 2047c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd) 2057c5dd23eSDave Jiang { 2067c5dd23eSDave Jiang struct device *dev = &idxd->pdev->dev; 2077c5dd23eSDave Jiang struct idxd_wq *wq; 208700af3a0SDave Jiang struct device *conf_dev; 2097c5dd23eSDave Jiang int i, rc; 2107c5dd23eSDave Jiang 2117c5dd23eSDave Jiang idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *), 2127c5dd23eSDave Jiang GFP_KERNEL, dev_to_node(dev)); 2137c5dd23eSDave Jiang if (!idxd->wqs) 2147c5dd23eSDave Jiang return -ENOMEM; 2157c5dd23eSDave Jiang 2167c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 2177c5dd23eSDave Jiang wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev)); 2187c5dd23eSDave Jiang if (!wq) { 2197c5dd23eSDave Jiang rc = -ENOMEM; 2207c5dd23eSDave Jiang goto err; 2217c5dd23eSDave Jiang } 2227c5dd23eSDave Jiang 223700af3a0SDave Jiang idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ); 224700af3a0SDave Jiang conf_dev = wq_confdev(wq); 2257c5dd23eSDave Jiang wq->id = i; 2267c5dd23eSDave Jiang wq->idxd = idxd; 227700af3a0SDave Jiang device_initialize(wq_confdev(wq)); 228700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 229700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 230700af3a0SDave Jiang conf_dev->type = &idxd_wq_device_type; 231700af3a0SDave Jiang rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id); 2327c5dd23eSDave Jiang if (rc < 0) { 233700af3a0SDave Jiang put_device(conf_dev); 2347c5dd23eSDave Jiang goto err; 2357c5dd23eSDave Jiang } 2367c5dd23eSDave Jiang 2377c5dd23eSDave Jiang mutex_init(&wq->wq_lock); 23804922b74SDave Jiang init_waitqueue_head(&wq->err_queue); 23993a40a6dSDave Jiang init_completion(&wq->wq_dead); 2407c5dd23eSDave Jiang wq->max_xfer_bytes = idxd->max_xfer_bytes; 2417c5dd23eSDave Jiang wq->max_batch_size = idxd->max_batch_size; 2427c5dd23eSDave Jiang wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); 2437c5dd23eSDave Jiang if (!wq->wqcfg) { 244700af3a0SDave Jiang put_device(conf_dev); 2457c5dd23eSDave Jiang rc = -ENOMEM; 2467c5dd23eSDave Jiang goto err; 2477c5dd23eSDave Jiang } 2487c5dd23eSDave Jiang idxd->wqs[i] = wq; 2497c5dd23eSDave Jiang } 2507c5dd23eSDave Jiang 2517c5dd23eSDave Jiang return 0; 2527c5dd23eSDave Jiang 2537c5dd23eSDave Jiang err: 254700af3a0SDave Jiang while (--i >= 0) { 255700af3a0SDave Jiang wq = idxd->wqs[i]; 256700af3a0SDave Jiang conf_dev = wq_confdev(wq); 257700af3a0SDave Jiang put_device(conf_dev); 258700af3a0SDave Jiang } 2597c5dd23eSDave Jiang return rc; 2607c5dd23eSDave Jiang } 2617c5dd23eSDave Jiang 26275b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd) 26375b91130SDave Jiang { 26475b91130SDave Jiang struct idxd_engine *engine; 26575b91130SDave Jiang struct device *dev = &idxd->pdev->dev; 266700af3a0SDave Jiang struct device *conf_dev; 26775b91130SDave Jiang int i, rc; 26875b91130SDave Jiang 26975b91130SDave Jiang idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *), 27075b91130SDave Jiang GFP_KERNEL, dev_to_node(dev)); 27175b91130SDave Jiang if (!idxd->engines) 27275b91130SDave Jiang return -ENOMEM; 27375b91130SDave Jiang 27475b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) { 27575b91130SDave Jiang engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev)); 27675b91130SDave Jiang if (!engine) { 27775b91130SDave Jiang rc = -ENOMEM; 27875b91130SDave Jiang goto err; 27975b91130SDave Jiang } 28075b91130SDave Jiang 281700af3a0SDave Jiang idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE); 282700af3a0SDave Jiang conf_dev = engine_confdev(engine); 28375b91130SDave Jiang engine->id = i; 28475b91130SDave Jiang engine->idxd = idxd; 285700af3a0SDave Jiang device_initialize(conf_dev); 286700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 287700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 288700af3a0SDave Jiang conf_dev->type = &idxd_engine_device_type; 289700af3a0SDave Jiang rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id); 29075b91130SDave Jiang if (rc < 0) { 291700af3a0SDave Jiang put_device(conf_dev); 29275b91130SDave Jiang goto err; 29375b91130SDave Jiang } 29475b91130SDave Jiang 29575b91130SDave Jiang idxd->engines[i] = engine; 29675b91130SDave Jiang } 29775b91130SDave Jiang 29875b91130SDave Jiang return 0; 29975b91130SDave Jiang 30075b91130SDave Jiang err: 301700af3a0SDave Jiang while (--i >= 0) { 302700af3a0SDave Jiang engine = idxd->engines[i]; 303700af3a0SDave Jiang conf_dev = engine_confdev(engine); 304700af3a0SDave Jiang put_device(conf_dev); 305700af3a0SDave Jiang } 30675b91130SDave Jiang return rc; 30775b91130SDave Jiang } 30875b91130SDave Jiang 309defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd) 310defe49f9SDave Jiang { 311defe49f9SDave Jiang struct device *dev = &idxd->pdev->dev; 312700af3a0SDave Jiang struct device *conf_dev; 313defe49f9SDave Jiang struct idxd_group *group; 314defe49f9SDave Jiang int i, rc; 315defe49f9SDave Jiang 316defe49f9SDave Jiang idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *), 317defe49f9SDave Jiang GFP_KERNEL, dev_to_node(dev)); 318defe49f9SDave Jiang if (!idxd->groups) 319defe49f9SDave Jiang return -ENOMEM; 320defe49f9SDave Jiang 321defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) { 322defe49f9SDave Jiang group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev)); 323defe49f9SDave Jiang if (!group) { 324defe49f9SDave Jiang rc = -ENOMEM; 325defe49f9SDave Jiang goto err; 326defe49f9SDave Jiang } 327defe49f9SDave Jiang 328700af3a0SDave Jiang idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP); 329700af3a0SDave Jiang conf_dev = group_confdev(group); 330defe49f9SDave Jiang group->id = i; 331defe49f9SDave Jiang group->idxd = idxd; 332700af3a0SDave Jiang device_initialize(conf_dev); 333700af3a0SDave Jiang conf_dev->parent = idxd_confdev(idxd); 334700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 335700af3a0SDave Jiang conf_dev->type = &idxd_group_device_type; 336700af3a0SDave Jiang rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id); 337defe49f9SDave Jiang if (rc < 0) { 338700af3a0SDave Jiang put_device(conf_dev); 339defe49f9SDave Jiang goto err; 340defe49f9SDave Jiang } 341defe49f9SDave Jiang 342defe49f9SDave Jiang idxd->groups[i] = group; 343ade8a86bSDave Jiang if (idxd->hw.version < DEVICE_VERSION_2 && !tc_override) { 344ade8a86bSDave Jiang group->tc_a = 1; 345ade8a86bSDave Jiang group->tc_b = 1; 346ade8a86bSDave Jiang } else { 347defe49f9SDave Jiang group->tc_a = -1; 348defe49f9SDave Jiang group->tc_b = -1; 349defe49f9SDave Jiang } 350ade8a86bSDave Jiang } 351defe49f9SDave Jiang 352defe49f9SDave Jiang return 0; 353defe49f9SDave Jiang 354defe49f9SDave Jiang err: 355700af3a0SDave Jiang while (--i >= 0) { 356700af3a0SDave Jiang group = idxd->groups[i]; 357700af3a0SDave Jiang put_device(group_confdev(group)); 358700af3a0SDave Jiang } 359defe49f9SDave Jiang return rc; 360defe49f9SDave Jiang } 361defe49f9SDave Jiang 362ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd) 363ddf742d4SDave Jiang { 364ddf742d4SDave Jiang int i; 365ddf742d4SDave Jiang 366ddf742d4SDave Jiang for (i = 0; i < idxd->max_groups; i++) 367700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 368ddf742d4SDave Jiang for (i = 0; i < idxd->max_engines; i++) 369700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 370ddf742d4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) 371700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 372ddf742d4SDave Jiang destroy_workqueue(idxd->wq); 373ddf742d4SDave Jiang } 374ddf742d4SDave Jiang 375bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd) 376bfe1d560SDave Jiang { 377bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 378defe49f9SDave Jiang int rc, i; 379bfe1d560SDave Jiang 3800d5c10b4SDave Jiang init_waitqueue_head(&idxd->cmd_waitq); 3817c5dd23eSDave Jiang 382eb15e715SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) { 38333f9f3c3SDave Jiang idxd->int_handles = kcalloc_node(idxd->max_wqs, sizeof(int), GFP_KERNEL, 38433f9f3c3SDave Jiang dev_to_node(dev)); 385eb15e715SDave Jiang if (!idxd->int_handles) 386eb15e715SDave Jiang return -ENOMEM; 387eb15e715SDave Jiang } 388eb15e715SDave Jiang 3897c5dd23eSDave Jiang rc = idxd_setup_wqs(idxd); 3907c5dd23eSDave Jiang if (rc < 0) 391eb15e715SDave Jiang goto err_wqs; 3927c5dd23eSDave Jiang 39375b91130SDave Jiang rc = idxd_setup_engines(idxd); 39475b91130SDave Jiang if (rc < 0) 39575b91130SDave Jiang goto err_engine; 39675b91130SDave Jiang 397defe49f9SDave Jiang rc = idxd_setup_groups(idxd); 398defe49f9SDave Jiang if (rc < 0) 399defe49f9SDave Jiang goto err_group; 400bfe1d560SDave Jiang 4010d5c10b4SDave Jiang idxd->wq = create_workqueue(dev_name(dev)); 4027c5dd23eSDave Jiang if (!idxd->wq) { 4037c5dd23eSDave Jiang rc = -ENOMEM; 404defe49f9SDave Jiang goto err_wkq_create; 4057c5dd23eSDave Jiang } 4060d5c10b4SDave Jiang 407bfe1d560SDave Jiang return 0; 4087c5dd23eSDave Jiang 409defe49f9SDave Jiang err_wkq_create: 410defe49f9SDave Jiang for (i = 0; i < idxd->max_groups; i++) 411700af3a0SDave Jiang put_device(group_confdev(idxd->groups[i])); 412defe49f9SDave Jiang err_group: 41375b91130SDave Jiang for (i = 0; i < idxd->max_engines; i++) 414700af3a0SDave Jiang put_device(engine_confdev(idxd->engines[i])); 41575b91130SDave Jiang err_engine: 4167c5dd23eSDave Jiang for (i = 0; i < idxd->max_wqs; i++) 417700af3a0SDave Jiang put_device(wq_confdev(idxd->wqs[i])); 418eb15e715SDave Jiang err_wqs: 419eb15e715SDave Jiang kfree(idxd->int_handles); 4207c5dd23eSDave Jiang return rc; 421bfe1d560SDave Jiang } 422bfe1d560SDave Jiang 423bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd) 424bfe1d560SDave Jiang { 425bfe1d560SDave Jiang union offsets_reg offsets; 426bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 427bfe1d560SDave Jiang 428bfe1d560SDave Jiang offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET); 4292f8417a9SDave Jiang offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64)); 4302f8417a9SDave Jiang idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT; 431bfe1d560SDave Jiang dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset); 4322f8417a9SDave Jiang idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT; 4332f8417a9SDave Jiang dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset); 4342f8417a9SDave Jiang idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT; 4352f8417a9SDave Jiang dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset); 4362f8417a9SDave Jiang idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT; 437bfe1d560SDave Jiang dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset); 438bfe1d560SDave Jiang } 439bfe1d560SDave Jiang 440bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd) 441bfe1d560SDave Jiang { 442bfe1d560SDave Jiang struct device *dev = &idxd->pdev->dev; 443bfe1d560SDave Jiang int i; 444bfe1d560SDave Jiang 445bfe1d560SDave Jiang /* reading generic capabilities */ 446bfe1d560SDave Jiang idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET); 447bfe1d560SDave Jiang dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits); 448eb15e715SDave Jiang 449eb15e715SDave Jiang if (idxd->hw.gen_cap.cmd_cap) { 450eb15e715SDave Jiang idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET); 451eb15e715SDave Jiang dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap); 452eb15e715SDave Jiang } 453eb15e715SDave Jiang 454bfe1d560SDave Jiang idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift; 455bfe1d560SDave Jiang dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes); 456bfe1d560SDave Jiang idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift; 457bfe1d560SDave Jiang dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size); 458bfe1d560SDave Jiang if (idxd->hw.gen_cap.config_en) 459bfe1d560SDave Jiang set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags); 460bfe1d560SDave Jiang 461bfe1d560SDave Jiang /* reading group capabilities */ 462bfe1d560SDave Jiang idxd->hw.group_cap.bits = 463bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET); 464bfe1d560SDave Jiang dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits); 465bfe1d560SDave Jiang idxd->max_groups = idxd->hw.group_cap.num_groups; 466bfe1d560SDave Jiang dev_dbg(dev, "max groups: %u\n", idxd->max_groups); 467bfe1d560SDave Jiang idxd->max_tokens = idxd->hw.group_cap.total_tokens; 468bfe1d560SDave Jiang dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens); 469c52ca478SDave Jiang idxd->nr_tokens = idxd->max_tokens; 470bfe1d560SDave Jiang 471bfe1d560SDave Jiang /* read engine capabilities */ 472bfe1d560SDave Jiang idxd->hw.engine_cap.bits = 473bfe1d560SDave Jiang ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET); 474bfe1d560SDave Jiang dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits); 475bfe1d560SDave Jiang idxd->max_engines = idxd->hw.engine_cap.num_engines; 476bfe1d560SDave Jiang dev_dbg(dev, "max engines: %u\n", idxd->max_engines); 477bfe1d560SDave Jiang 478bfe1d560SDave Jiang /* read workqueue capabilities */ 479bfe1d560SDave Jiang idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET); 480bfe1d560SDave Jiang dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits); 481bfe1d560SDave Jiang idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size; 482bfe1d560SDave Jiang dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size); 483bfe1d560SDave Jiang idxd->max_wqs = idxd->hw.wq_cap.num_wqs; 484bfe1d560SDave Jiang dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs); 485d98793b5SDave Jiang idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN); 486d98793b5SDave Jiang dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size); 487bfe1d560SDave Jiang 488bfe1d560SDave Jiang /* reading operation capabilities */ 489bfe1d560SDave Jiang for (i = 0; i < 4; i++) { 490bfe1d560SDave Jiang idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base + 491bfe1d560SDave Jiang IDXD_OPCAP_OFFSET + i * sizeof(u64)); 492bfe1d560SDave Jiang dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]); 493bfe1d560SDave Jiang } 494bfe1d560SDave Jiang } 495bfe1d560SDave Jiang 496435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) 497bfe1d560SDave Jiang { 498bfe1d560SDave Jiang struct device *dev = &pdev->dev; 499700af3a0SDave Jiang struct device *conf_dev; 500bfe1d560SDave Jiang struct idxd_device *idxd; 50147c16ac2SDave Jiang int rc; 502bfe1d560SDave Jiang 50347c16ac2SDave Jiang idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev)); 504bfe1d560SDave Jiang if (!idxd) 505bfe1d560SDave Jiang return NULL; 506bfe1d560SDave Jiang 507700af3a0SDave Jiang conf_dev = idxd_confdev(idxd); 508bfe1d560SDave Jiang idxd->pdev = pdev; 509435b512dSDave Jiang idxd->data = data; 510700af3a0SDave Jiang idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type); 5114b73e4ebSDave Jiang idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); 51247c16ac2SDave Jiang if (idxd->id < 0) 51347c16ac2SDave Jiang return NULL; 51447c16ac2SDave Jiang 515700af3a0SDave Jiang device_initialize(conf_dev); 516700af3a0SDave Jiang conf_dev->parent = dev; 517700af3a0SDave Jiang conf_dev->bus = &dsa_bus_type; 518700af3a0SDave Jiang conf_dev->type = idxd->data->dev_type; 519700af3a0SDave Jiang rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); 52047c16ac2SDave Jiang if (rc < 0) { 521700af3a0SDave Jiang put_device(conf_dev); 52247c16ac2SDave Jiang return NULL; 52347c16ac2SDave Jiang } 52447c16ac2SDave Jiang 525bfe1d560SDave Jiang spin_lock_init(&idxd->dev_lock); 52653b2ee7fSDave Jiang spin_lock_init(&idxd->cmd_lock); 527bfe1d560SDave Jiang 528bfe1d560SDave Jiang return idxd; 529bfe1d560SDave Jiang } 530bfe1d560SDave Jiang 5318e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd) 5328e50d392SDave Jiang { 5338e50d392SDave Jiang int flags; 5348e50d392SDave Jiang unsigned int pasid; 5358e50d392SDave Jiang struct iommu_sva *sva; 5368e50d392SDave Jiang 5378e50d392SDave Jiang flags = SVM_FLAG_SUPERVISOR_MODE; 5388e50d392SDave Jiang 5398e50d392SDave Jiang sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags); 5408e50d392SDave Jiang if (IS_ERR(sva)) { 5418e50d392SDave Jiang dev_warn(&idxd->pdev->dev, 5428e50d392SDave Jiang "iommu sva bind failed: %ld\n", PTR_ERR(sva)); 5438e50d392SDave Jiang return PTR_ERR(sva); 5448e50d392SDave Jiang } 5458e50d392SDave Jiang 5468e50d392SDave Jiang pasid = iommu_sva_get_pasid(sva); 5478e50d392SDave Jiang if (pasid == IOMMU_PASID_INVALID) { 5488e50d392SDave Jiang iommu_sva_unbind_device(sva); 5498e50d392SDave Jiang return -ENODEV; 5508e50d392SDave Jiang } 5518e50d392SDave Jiang 5528e50d392SDave Jiang idxd->sva = sva; 5538e50d392SDave Jiang idxd->pasid = pasid; 5548e50d392SDave Jiang dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid); 5558e50d392SDave Jiang return 0; 5568e50d392SDave Jiang } 5578e50d392SDave Jiang 5588e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd) 5598e50d392SDave Jiang { 5608e50d392SDave Jiang 5618e50d392SDave Jiang iommu_sva_unbind_device(idxd->sva); 5628e50d392SDave Jiang idxd->sva = NULL; 5638e50d392SDave Jiang } 5648e50d392SDave Jiang 565bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd) 566bfe1d560SDave Jiang { 567bfe1d560SDave Jiang struct pci_dev *pdev = idxd->pdev; 568bfe1d560SDave Jiang struct device *dev = &pdev->dev; 569bfe1d560SDave Jiang int rc; 570bfe1d560SDave Jiang 571bfe1d560SDave Jiang dev_dbg(dev, "%s entered and resetting device\n", __func__); 57289e3becdSDave Jiang rc = idxd_device_init_reset(idxd); 57389e3becdSDave Jiang if (rc < 0) 57489e3becdSDave Jiang return rc; 57589e3becdSDave Jiang 576bfe1d560SDave Jiang dev_dbg(dev, "IDXD reset complete\n"); 577bfe1d560SDave Jiang 57803d939c7SDave Jiang if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) { 579cf5f86a7SDave Jiang rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA); 580cf5f86a7SDave Jiang if (rc == 0) { 5818e50d392SDave Jiang rc = idxd_enable_system_pasid(idxd); 582cf5f86a7SDave Jiang if (rc < 0) { 583cf5f86a7SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 5848e50d392SDave Jiang dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc); 585cf5f86a7SDave Jiang } else { 5868e50d392SDave Jiang set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags); 587cf5f86a7SDave Jiang } 588cf5f86a7SDave Jiang } else { 589cf5f86a7SDave Jiang dev_warn(dev, "Unable to turn on SVA feature.\n"); 590cf5f86a7SDave Jiang } 59103d939c7SDave Jiang } else if (!sva) { 59203d939c7SDave Jiang dev_warn(dev, "User forced SVA off via module param.\n"); 5938e50d392SDave Jiang } 5948e50d392SDave Jiang 595bfe1d560SDave Jiang idxd_read_caps(idxd); 596bfe1d560SDave Jiang idxd_read_table_offsets(idxd); 597bfe1d560SDave Jiang 598bfe1d560SDave Jiang rc = idxd_setup_internals(idxd); 599bfe1d560SDave Jiang if (rc) 6007c5dd23eSDave Jiang goto err; 601bfe1d560SDave Jiang 6028c66bbdcSDave Jiang /* If the configs are readonly, then load them from device */ 6038c66bbdcSDave Jiang if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) { 6048c66bbdcSDave Jiang dev_dbg(dev, "Loading RO device config\n"); 6058c66bbdcSDave Jiang rc = idxd_device_load_config(idxd); 6068c66bbdcSDave Jiang if (rc < 0) 607ddf742d4SDave Jiang goto err_config; 6088c66bbdcSDave Jiang } 6098c66bbdcSDave Jiang 610bfe1d560SDave Jiang rc = idxd_setup_interrupts(idxd); 611bfe1d560SDave Jiang if (rc) 612ddf742d4SDave Jiang goto err_config; 613bfe1d560SDave Jiang 614bfe1d560SDave Jiang dev_dbg(dev, "IDXD interrupt setup complete.\n"); 615bfe1d560SDave Jiang 61642d279f9SDave Jiang idxd->major = idxd_cdev_get_major(idxd); 61742d279f9SDave Jiang 6180bde4444STom Zanussi rc = perfmon_pmu_init(idxd); 6190bde4444STom Zanussi if (rc < 0) 6200bde4444STom Zanussi dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc); 6210bde4444STom Zanussi 622bfe1d560SDave Jiang dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id); 623bfe1d560SDave Jiang return 0; 624bfe1d560SDave Jiang 625ddf742d4SDave Jiang err_config: 626ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 6277c5dd23eSDave Jiang err: 6288e50d392SDave Jiang if (device_pasid_enabled(idxd)) 6298e50d392SDave Jiang idxd_disable_system_pasid(idxd); 630cf5f86a7SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 631bfe1d560SDave Jiang return rc; 632bfe1d560SDave Jiang } 633bfe1d560SDave Jiang 634ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd) 635ddf742d4SDave Jiang { 636ddf742d4SDave Jiang struct device *dev = &idxd->pdev->dev; 637ddf742d4SDave Jiang 638ddf742d4SDave Jiang perfmon_pmu_remove(idxd); 639ddf742d4SDave Jiang idxd_cleanup_interrupts(idxd); 640ddf742d4SDave Jiang idxd_cleanup_internals(idxd); 641ddf742d4SDave Jiang if (device_pasid_enabled(idxd)) 642ddf742d4SDave Jiang idxd_disable_system_pasid(idxd); 643ddf742d4SDave Jiang iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA); 644ddf742d4SDave Jiang } 645ddf742d4SDave Jiang 646bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 647bfe1d560SDave Jiang { 648bfe1d560SDave Jiang struct device *dev = &pdev->dev; 649bfe1d560SDave Jiang struct idxd_device *idxd; 650435b512dSDave Jiang struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data; 651bfe1d560SDave Jiang int rc; 652bfe1d560SDave Jiang 653a39c7cd0SDave Jiang rc = pci_enable_device(pdev); 654bfe1d560SDave Jiang if (rc) 655bfe1d560SDave Jiang return rc; 656bfe1d560SDave Jiang 6578e50d392SDave Jiang dev_dbg(dev, "Alloc IDXD context\n"); 658435b512dSDave Jiang idxd = idxd_alloc(pdev, data); 659a39c7cd0SDave Jiang if (!idxd) { 660a39c7cd0SDave Jiang rc = -ENOMEM; 661a39c7cd0SDave Jiang goto err_idxd_alloc; 662a39c7cd0SDave Jiang } 663bfe1d560SDave Jiang 6648e50d392SDave Jiang dev_dbg(dev, "Mapping BARs\n"); 665a39c7cd0SDave Jiang idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0); 666a39c7cd0SDave Jiang if (!idxd->reg_base) { 667a39c7cd0SDave Jiang rc = -ENOMEM; 668a39c7cd0SDave Jiang goto err_iomap; 669a39c7cd0SDave Jiang } 670bfe1d560SDave Jiang 671bfe1d560SDave Jiang dev_dbg(dev, "Set DMA masks\n"); 67253b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 673bfe1d560SDave Jiang if (rc) 67453b50458SChristophe JAILLET rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 675bfe1d560SDave Jiang if (rc) 676a39c7cd0SDave Jiang goto err; 677bfe1d560SDave Jiang 678bfe1d560SDave Jiang dev_dbg(dev, "Set PCI master\n"); 679bfe1d560SDave Jiang pci_set_master(pdev); 680bfe1d560SDave Jiang pci_set_drvdata(pdev, idxd); 681bfe1d560SDave Jiang 682bfe1d560SDave Jiang idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET); 683bfe1d560SDave Jiang rc = idxd_probe(idxd); 684bfe1d560SDave Jiang if (rc) { 685bfe1d560SDave Jiang dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n"); 686a39c7cd0SDave Jiang goto err; 687bfe1d560SDave Jiang } 688bfe1d560SDave Jiang 68947c16ac2SDave Jiang rc = idxd_register_devices(idxd); 690c52ca478SDave Jiang if (rc) { 691c52ca478SDave Jiang dev_err(dev, "IDXD sysfs setup failed\n"); 692ddf742d4SDave Jiang goto err_dev_register; 693c52ca478SDave Jiang } 694c52ca478SDave Jiang 695bfe1d560SDave Jiang dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n", 696bfe1d560SDave Jiang idxd->hw.version); 697bfe1d560SDave Jiang 698bfe1d560SDave Jiang return 0; 699a39c7cd0SDave Jiang 700ddf742d4SDave Jiang err_dev_register: 701ddf742d4SDave Jiang idxd_cleanup(idxd); 702a39c7cd0SDave Jiang err: 703a39c7cd0SDave Jiang pci_iounmap(pdev, idxd->reg_base); 704a39c7cd0SDave Jiang err_iomap: 705700af3a0SDave Jiang put_device(idxd_confdev(idxd)); 706a39c7cd0SDave Jiang err_idxd_alloc: 707a39c7cd0SDave Jiang pci_disable_device(pdev); 708a39c7cd0SDave Jiang return rc; 709bfe1d560SDave Jiang } 710bfe1d560SDave Jiang 7118f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie) 7128f47d1a5SDave Jiang { 7138f47d1a5SDave Jiang struct idxd_desc *desc, *itr; 7148f47d1a5SDave Jiang struct llist_node *head; 7158f47d1a5SDave Jiang 7168f47d1a5SDave Jiang head = llist_del_all(&ie->pending_llist); 7178f47d1a5SDave Jiang if (!head) 7188f47d1a5SDave Jiang return; 7198f47d1a5SDave Jiang 7208f47d1a5SDave Jiang llist_for_each_entry_safe(desc, itr, head, llnode) { 7218f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 7228f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 7238f47d1a5SDave Jiang } 7248f47d1a5SDave Jiang } 7258f47d1a5SDave Jiang 7268f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie) 7278f47d1a5SDave Jiang { 7288f47d1a5SDave Jiang struct idxd_desc *desc, *iter; 7298f47d1a5SDave Jiang 7308f47d1a5SDave Jiang list_for_each_entry_safe(desc, iter, &ie->work_list, list) { 7318f47d1a5SDave Jiang list_del(&desc->list); 7328f47d1a5SDave Jiang idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT); 7338f47d1a5SDave Jiang idxd_free_desc(desc->wq, desc); 7348f47d1a5SDave Jiang } 7358f47d1a5SDave Jiang } 7368f47d1a5SDave Jiang 7375b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd) 7385b0c68c4SDave Jiang { 7395b0c68c4SDave Jiang struct idxd_wq *wq; 7405b0c68c4SDave Jiang int i; 7415b0c68c4SDave Jiang 7425b0c68c4SDave Jiang for (i = 0; i < idxd->max_wqs; i++) { 7435b0c68c4SDave Jiang wq = idxd->wqs[i]; 7445b0c68c4SDave Jiang if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL) 7455b0c68c4SDave Jiang idxd_wq_quiesce(wq); 7465b0c68c4SDave Jiang } 7475b0c68c4SDave Jiang } 7485b0c68c4SDave Jiang 749eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd) 750eb15e715SDave Jiang { 751eb15e715SDave Jiang struct device *dev = &idxd->pdev->dev; 752eb15e715SDave Jiang int i, rc; 753eb15e715SDave Jiang 754eb15e715SDave Jiang for (i = 0; i < idxd->num_wq_irqs; i++) { 755eb15e715SDave Jiang if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) { 756eb15e715SDave Jiang rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i], 757eb15e715SDave Jiang IDXD_IRQ_MSIX); 758eb15e715SDave Jiang if (rc < 0) 759eb15e715SDave Jiang dev_warn(dev, "irq handle %d release failed\n", 760eb15e715SDave Jiang idxd->int_handles[i]); 761eb15e715SDave Jiang else 762eb15e715SDave Jiang dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]); 763eb15e715SDave Jiang } 764eb15e715SDave Jiang } 765eb15e715SDave Jiang } 766eb15e715SDave Jiang 767bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev) 768bfe1d560SDave Jiang { 769bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 770bfe1d560SDave Jiang int rc, i; 771bfe1d560SDave Jiang struct idxd_irq_entry *irq_entry; 772bfe1d560SDave Jiang int msixcnt = pci_msix_vec_count(pdev); 773bfe1d560SDave Jiang 774bfe1d560SDave Jiang rc = idxd_device_disable(idxd); 775bfe1d560SDave Jiang if (rc) 776bfe1d560SDave Jiang dev_err(&pdev->dev, "Disabling device failed\n"); 777bfe1d560SDave Jiang 778bfe1d560SDave Jiang dev_dbg(&pdev->dev, "%s called\n", __func__); 779bfe1d560SDave Jiang idxd_mask_msix_vectors(idxd); 780bfe1d560SDave Jiang idxd_mask_error_interrupts(idxd); 781bfe1d560SDave Jiang 782bfe1d560SDave Jiang for (i = 0; i < msixcnt; i++) { 783bfe1d560SDave Jiang irq_entry = &idxd->irq_entries[i]; 7845fc8e85fSDave Jiang synchronize_irq(irq_entry->vector); 785bfe1d560SDave Jiang if (i == 0) 786bfe1d560SDave Jiang continue; 7878f47d1a5SDave Jiang idxd_flush_pending_llist(irq_entry); 7888f47d1a5SDave Jiang idxd_flush_work_list(irq_entry); 789bfe1d560SDave Jiang } 79049c4959fSDave Jiang flush_workqueue(idxd->wq); 791bfe1d560SDave Jiang } 792bfe1d560SDave Jiang 793bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev) 794bfe1d560SDave Jiang { 795bfe1d560SDave Jiang struct idxd_device *idxd = pci_get_drvdata(pdev); 79649c4959fSDave Jiang struct idxd_irq_entry *irq_entry; 79749c4959fSDave Jiang int msixcnt = pci_msix_vec_count(pdev); 79849c4959fSDave Jiang int i; 799bfe1d560SDave Jiang 800*98da0106SDave Jiang idxd_unregister_devices(idxd); 801*98da0106SDave Jiang /* 802*98da0106SDave Jiang * When ->release() is called for the idxd->conf_dev, it frees all the memory related 803*98da0106SDave Jiang * to the idxd context. The driver still needs those bits in order to do the rest of 804*98da0106SDave Jiang * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref 805*98da0106SDave Jiang * on the device here to hold off the freeing while allowing the idxd sub-driver 806*98da0106SDave Jiang * to unbind. 807*98da0106SDave Jiang */ 808*98da0106SDave Jiang get_device(idxd_confdev(idxd)); 809*98da0106SDave Jiang device_unregister(idxd_confdev(idxd)); 810bfe1d560SDave Jiang idxd_shutdown(pdev); 8118e50d392SDave Jiang if (device_pasid_enabled(idxd)) 8128e50d392SDave Jiang idxd_disable_system_pasid(idxd); 81349c4959fSDave Jiang 81449c4959fSDave Jiang for (i = 0; i < msixcnt; i++) { 81549c4959fSDave Jiang irq_entry = &idxd->irq_entries[i]; 81649c4959fSDave Jiang free_irq(irq_entry->vector, irq_entry); 81749c4959fSDave Jiang } 81849c4959fSDave Jiang idxd_msix_perm_clear(idxd); 81949c4959fSDave Jiang idxd_release_int_handles(idxd); 82049c4959fSDave Jiang pci_free_irq_vectors(pdev); 82149c4959fSDave Jiang pci_iounmap(pdev, idxd->reg_base); 822cf5f86a7SDave Jiang iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA); 82349c4959fSDave Jiang pci_disable_device(pdev); 82449c4959fSDave Jiang destroy_workqueue(idxd->wq); 82549c4959fSDave Jiang perfmon_pmu_remove(idxd); 826*98da0106SDave Jiang put_device(idxd_confdev(idxd)); 827bfe1d560SDave Jiang } 828bfe1d560SDave Jiang 829bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = { 830bfe1d560SDave Jiang .name = DRV_NAME, 831bfe1d560SDave Jiang .id_table = idxd_pci_tbl, 832bfe1d560SDave Jiang .probe = idxd_pci_probe, 833bfe1d560SDave Jiang .remove = idxd_remove, 834bfe1d560SDave Jiang .shutdown = idxd_shutdown, 835bfe1d560SDave Jiang }; 836bfe1d560SDave Jiang 837bfe1d560SDave Jiang static int __init idxd_init_module(void) 838bfe1d560SDave Jiang { 8394b73e4ebSDave Jiang int err; 840bfe1d560SDave Jiang 841bfe1d560SDave Jiang /* 8428e50d392SDave Jiang * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in 843bfe1d560SDave Jiang * enumerating the device. We can not utilize it. 844bfe1d560SDave Jiang */ 84574b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) { 846bfe1d560SDave Jiang pr_warn("idxd driver failed to load without MOVDIR64B.\n"); 847bfe1d560SDave Jiang return -ENODEV; 848bfe1d560SDave Jiang } 849bfe1d560SDave Jiang 85074b2fc88SBorislav Petkov if (!cpu_feature_enabled(X86_FEATURE_ENQCMD)) 8518e50d392SDave Jiang pr_warn("Platform does not have ENQCMD(S) support.\n"); 8528e50d392SDave Jiang else 8538e50d392SDave Jiang support_enqcmd = true; 854bfe1d560SDave Jiang 8550bde4444STom Zanussi perfmon_init(); 8560bde4444STom Zanussi 857034b3290SDave Jiang err = idxd_driver_register(&idxd_drv); 858034b3290SDave Jiang if (err < 0) 859034b3290SDave Jiang goto err_idxd_driver_register; 860034b3290SDave Jiang 8610cda4f69SDave Jiang err = idxd_driver_register(&idxd_dmaengine_drv); 8620cda4f69SDave Jiang if (err < 0) 8630cda4f69SDave Jiang goto err_idxd_dmaengine_driver_register; 8640cda4f69SDave Jiang 865448c3de8SDave Jiang err = idxd_driver_register(&idxd_user_drv); 866448c3de8SDave Jiang if (err < 0) 867448c3de8SDave Jiang goto err_idxd_user_driver_register; 868448c3de8SDave Jiang 86942d279f9SDave Jiang err = idxd_cdev_register(); 87042d279f9SDave Jiang if (err) 87142d279f9SDave Jiang goto err_cdev_register; 87242d279f9SDave Jiang 873c52ca478SDave Jiang err = pci_register_driver(&idxd_pci_driver); 874c52ca478SDave Jiang if (err) 875c52ca478SDave Jiang goto err_pci_register; 876c52ca478SDave Jiang 877bfe1d560SDave Jiang return 0; 878c52ca478SDave Jiang 879c52ca478SDave Jiang err_pci_register: 88042d279f9SDave Jiang idxd_cdev_remove(); 88142d279f9SDave Jiang err_cdev_register: 882448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 883448c3de8SDave Jiang err_idxd_user_driver_register: 8840cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 8850cda4f69SDave Jiang err_idxd_dmaengine_driver_register: 886034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 887034b3290SDave Jiang err_idxd_driver_register: 888c52ca478SDave Jiang return err; 889bfe1d560SDave Jiang } 890bfe1d560SDave Jiang module_init(idxd_init_module); 891bfe1d560SDave Jiang 892bfe1d560SDave Jiang static void __exit idxd_exit_module(void) 893bfe1d560SDave Jiang { 894448c3de8SDave Jiang idxd_driver_unregister(&idxd_user_drv); 8950cda4f69SDave Jiang idxd_driver_unregister(&idxd_dmaengine_drv); 896034b3290SDave Jiang idxd_driver_unregister(&idxd_drv); 897bfe1d560SDave Jiang pci_unregister_driver(&idxd_pci_driver); 89842d279f9SDave Jiang idxd_cdev_remove(); 8990bde4444STom Zanussi perfmon_exit(); 900bfe1d560SDave Jiang } 901bfe1d560SDave Jiang module_exit(idxd_exit_module); 902