xref: /linux/drivers/dma/idxd/init.c (revision 33f9f3c33e9336e5f49501c9632584c3d1f4f3a5)
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");
29bfe1d560SDave Jiang 
3003d939c7SDave Jiang static bool sva = true;
3103d939c7SDave Jiang module_param(sva, bool, 0644);
3203d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
3303d939c7SDave Jiang 
34bfe1d560SDave Jiang #define DRV_NAME "idxd"
35bfe1d560SDave Jiang 
368e50d392SDave Jiang bool support_enqcmd;
374b73e4ebSDave Jiang DEFINE_IDA(idxd_ida);
38bfe1d560SDave Jiang 
39435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = {
40435b512dSDave Jiang 	[IDXD_TYPE_DSA] = {
41435b512dSDave Jiang 		.name_prefix = "dsa",
42435b512dSDave Jiang 		.type = IDXD_TYPE_DSA,
43435b512dSDave Jiang 		.compl_size = sizeof(struct dsa_completion_record),
44435b512dSDave Jiang 		.align = 32,
45435b512dSDave Jiang 		.dev_type = &dsa_device_type,
46435b512dSDave Jiang 	},
47435b512dSDave Jiang 	[IDXD_TYPE_IAX] = {
48435b512dSDave Jiang 		.name_prefix = "iax",
49435b512dSDave Jiang 		.type = IDXD_TYPE_IAX,
50435b512dSDave Jiang 		.compl_size = sizeof(struct iax_completion_record),
51435b512dSDave Jiang 		.align = 64,
52435b512dSDave Jiang 		.dev_type = &iax_device_type,
53435b512dSDave Jiang 	},
54435b512dSDave Jiang };
55435b512dSDave Jiang 
56bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = {
57bfe1d560SDave Jiang 	/* DSA ver 1.0 platforms */
58435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
59f25b4638SDave Jiang 
60f25b4638SDave Jiang 	/* IAX ver 1.0 platforms */
61435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
62bfe1d560SDave Jiang 	{ 0, }
63bfe1d560SDave Jiang };
64bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
65bfe1d560SDave Jiang 
66bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd)
67bfe1d560SDave Jiang {
68bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
69bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
70bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
71bfe1d560SDave Jiang 	int i, msixcnt;
72bfe1d560SDave Jiang 	int rc = 0;
73bfe1d560SDave Jiang 
74bfe1d560SDave Jiang 	msixcnt = pci_msix_vec_count(pdev);
75bfe1d560SDave Jiang 	if (msixcnt < 0) {
76bfe1d560SDave Jiang 		dev_err(dev, "Not MSI-X interrupt capable.\n");
775fc8e85fSDave Jiang 		return -ENOSPC;
78bfe1d560SDave Jiang 	}
79bfe1d560SDave Jiang 
805fc8e85fSDave Jiang 	rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
815fc8e85fSDave Jiang 	if (rc != msixcnt) {
825fc8e85fSDave Jiang 		dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
835fc8e85fSDave Jiang 		return -ENOSPC;
84bfe1d560SDave Jiang 	}
85bfe1d560SDave Jiang 	dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
86bfe1d560SDave Jiang 
87bfe1d560SDave Jiang 	/*
88bfe1d560SDave Jiang 	 * We implement 1 completion list per MSI-X entry except for
89bfe1d560SDave Jiang 	 * entry 0, which is for errors and others.
90bfe1d560SDave Jiang 	 */
9147c16ac2SDave Jiang 	idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
9247c16ac2SDave Jiang 					 GFP_KERNEL, dev_to_node(dev));
93bfe1d560SDave Jiang 	if (!idxd->irq_entries) {
94bfe1d560SDave Jiang 		rc = -ENOMEM;
955fc8e85fSDave Jiang 		goto err_irq_entries;
96bfe1d560SDave Jiang 	}
97bfe1d560SDave Jiang 
98bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
99bfe1d560SDave Jiang 		idxd->irq_entries[i].id = i;
100bfe1d560SDave Jiang 		idxd->irq_entries[i].idxd = idxd;
1015fc8e85fSDave Jiang 		idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
102e4f4d8cdSDave Jiang 		spin_lock_init(&idxd->irq_entries[i].list_lock);
103bfe1d560SDave Jiang 	}
104bfe1d560SDave Jiang 
105bfe1d560SDave Jiang 	irq_entry = &idxd->irq_entries[0];
106a1610461SDave Jiang 	rc = request_threaded_irq(irq_entry->vector, NULL, idxd_misc_thread,
1075fc8e85fSDave Jiang 				  0, "idxd-misc", irq_entry);
108bfe1d560SDave Jiang 	if (rc < 0) {
109bfe1d560SDave Jiang 		dev_err(dev, "Failed to allocate misc interrupt.\n");
1105fc8e85fSDave Jiang 		goto err_misc_irq;
111bfe1d560SDave Jiang 	}
112bfe1d560SDave Jiang 
1135fc8e85fSDave Jiang 	dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
114bfe1d560SDave Jiang 
115bfe1d560SDave Jiang 	/* first MSI-X entry is not for wq interrupts */
116bfe1d560SDave Jiang 	idxd->num_wq_irqs = msixcnt - 1;
117bfe1d560SDave Jiang 
118bfe1d560SDave Jiang 	for (i = 1; i < msixcnt; i++) {
119bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
120bfe1d560SDave Jiang 
121bfe1d560SDave Jiang 		init_llist_head(&idxd->irq_entries[i].pending_llist);
122bfe1d560SDave Jiang 		INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
123a1610461SDave Jiang 		rc = request_threaded_irq(irq_entry->vector, NULL,
1245fc8e85fSDave Jiang 					  idxd_wq_thread, 0, "idxd-portal", irq_entry);
125bfe1d560SDave Jiang 		if (rc < 0) {
1265fc8e85fSDave Jiang 			dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
1275fc8e85fSDave Jiang 			goto err_wq_irqs;
128bfe1d560SDave Jiang 		}
129eb15e715SDave Jiang 
1305fc8e85fSDave Jiang 		dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
131eb15e715SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
132eb15e715SDave Jiang 			/*
133eb15e715SDave Jiang 			 * The MSIX vector enumeration starts at 1 with vector 0 being the
134eb15e715SDave Jiang 			 * misc interrupt that handles non I/O completion events. The
135eb15e715SDave Jiang 			 * interrupt handles are for IMS enumeration on guest. The misc
136eb15e715SDave Jiang 			 * interrupt vector does not require a handle and therefore we start
137eb15e715SDave Jiang 			 * the int_handles at index 0. Since 'i' starts at 1, the first
138eb15e715SDave Jiang 			 * int_handles index will be 0.
139eb15e715SDave Jiang 			 */
140eb15e715SDave Jiang 			rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1],
141eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
142eb15e715SDave Jiang 			if (rc < 0) {
143eb15e715SDave Jiang 				free_irq(irq_entry->vector, irq_entry);
144eb15e715SDave Jiang 				goto err_wq_irqs;
145eb15e715SDave Jiang 			}
146eb15e715SDave Jiang 			dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]);
147eb15e715SDave Jiang 		}
148bfe1d560SDave Jiang 	}
149bfe1d560SDave Jiang 
150bfe1d560SDave Jiang 	idxd_unmask_error_interrupts(idxd);
1516df0e6c5SDave Jiang 	idxd_msix_perm_setup(idxd);
152bfe1d560SDave Jiang 	return 0;
153bfe1d560SDave Jiang 
1545fc8e85fSDave Jiang  err_wq_irqs:
1555fc8e85fSDave Jiang 	while (--i >= 0) {
1565fc8e85fSDave Jiang 		irq_entry = &idxd->irq_entries[i];
1575fc8e85fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
158eb15e715SDave Jiang 		if (i != 0)
159eb15e715SDave Jiang 			idxd_device_release_int_handle(idxd,
160eb15e715SDave Jiang 						       idxd->int_handles[i], IDXD_IRQ_MSIX);
1615fc8e85fSDave Jiang 	}
1625fc8e85fSDave Jiang  err_misc_irq:
163bfe1d560SDave Jiang 	/* Disable error interrupt generation */
164bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
1655fc8e85fSDave Jiang  err_irq_entries:
1665fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
167bfe1d560SDave Jiang 	dev_err(dev, "No usable interrupts\n");
168bfe1d560SDave Jiang 	return rc;
169bfe1d560SDave Jiang }
170bfe1d560SDave Jiang 
1717c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd)
1727c5dd23eSDave Jiang {
1737c5dd23eSDave Jiang 	struct device *dev = &idxd->pdev->dev;
1747c5dd23eSDave Jiang 	struct idxd_wq *wq;
1757c5dd23eSDave Jiang 	int i, rc;
1767c5dd23eSDave Jiang 
1777c5dd23eSDave Jiang 	idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
1787c5dd23eSDave Jiang 				 GFP_KERNEL, dev_to_node(dev));
1797c5dd23eSDave Jiang 	if (!idxd->wqs)
1807c5dd23eSDave Jiang 		return -ENOMEM;
1817c5dd23eSDave Jiang 
1827c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
1837c5dd23eSDave Jiang 		wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
1847c5dd23eSDave Jiang 		if (!wq) {
1857c5dd23eSDave Jiang 			rc = -ENOMEM;
1867c5dd23eSDave Jiang 			goto err;
1877c5dd23eSDave Jiang 		}
1887c5dd23eSDave Jiang 
1897c5dd23eSDave Jiang 		wq->id = i;
1907c5dd23eSDave Jiang 		wq->idxd = idxd;
1917c5dd23eSDave Jiang 		device_initialize(&wq->conf_dev);
1927c5dd23eSDave Jiang 		wq->conf_dev.parent = &idxd->conf_dev;
1934b73e4ebSDave Jiang 		wq->conf_dev.bus = &dsa_bus_type;
1947c5dd23eSDave Jiang 		wq->conf_dev.type = &idxd_wq_device_type;
1957c5dd23eSDave Jiang 		rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id);
1967c5dd23eSDave Jiang 		if (rc < 0) {
1977c5dd23eSDave Jiang 			put_device(&wq->conf_dev);
1987c5dd23eSDave Jiang 			goto err;
1997c5dd23eSDave Jiang 		}
2007c5dd23eSDave Jiang 
2017c5dd23eSDave Jiang 		mutex_init(&wq->wq_lock);
20204922b74SDave Jiang 		init_waitqueue_head(&wq->err_queue);
20393a40a6dSDave Jiang 		init_completion(&wq->wq_dead);
2047c5dd23eSDave Jiang 		wq->max_xfer_bytes = idxd->max_xfer_bytes;
2057c5dd23eSDave Jiang 		wq->max_batch_size = idxd->max_batch_size;
2067c5dd23eSDave Jiang 		wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
2077c5dd23eSDave Jiang 		if (!wq->wqcfg) {
2087c5dd23eSDave Jiang 			put_device(&wq->conf_dev);
2097c5dd23eSDave Jiang 			rc = -ENOMEM;
2107c5dd23eSDave Jiang 			goto err;
2117c5dd23eSDave Jiang 		}
2127c5dd23eSDave Jiang 		idxd->wqs[i] = wq;
2137c5dd23eSDave Jiang 	}
2147c5dd23eSDave Jiang 
2157c5dd23eSDave Jiang 	return 0;
2167c5dd23eSDave Jiang 
2177c5dd23eSDave Jiang  err:
2187c5dd23eSDave Jiang 	while (--i >= 0)
2197c5dd23eSDave Jiang 		put_device(&idxd->wqs[i]->conf_dev);
2207c5dd23eSDave Jiang 	return rc;
2217c5dd23eSDave Jiang }
2227c5dd23eSDave Jiang 
22375b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd)
22475b91130SDave Jiang {
22575b91130SDave Jiang 	struct idxd_engine *engine;
22675b91130SDave Jiang 	struct device *dev = &idxd->pdev->dev;
22775b91130SDave Jiang 	int i, rc;
22875b91130SDave Jiang 
22975b91130SDave Jiang 	idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
23075b91130SDave Jiang 				     GFP_KERNEL, dev_to_node(dev));
23175b91130SDave Jiang 	if (!idxd->engines)
23275b91130SDave Jiang 		return -ENOMEM;
23375b91130SDave Jiang 
23475b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++) {
23575b91130SDave Jiang 		engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
23675b91130SDave Jiang 		if (!engine) {
23775b91130SDave Jiang 			rc = -ENOMEM;
23875b91130SDave Jiang 			goto err;
23975b91130SDave Jiang 		}
24075b91130SDave Jiang 
24175b91130SDave Jiang 		engine->id = i;
24275b91130SDave Jiang 		engine->idxd = idxd;
24375b91130SDave Jiang 		device_initialize(&engine->conf_dev);
24475b91130SDave Jiang 		engine->conf_dev.parent = &idxd->conf_dev;
24575b91130SDave Jiang 		engine->conf_dev.type = &idxd_engine_device_type;
24675b91130SDave Jiang 		rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id);
24775b91130SDave Jiang 		if (rc < 0) {
24875b91130SDave Jiang 			put_device(&engine->conf_dev);
24975b91130SDave Jiang 			goto err;
25075b91130SDave Jiang 		}
25175b91130SDave Jiang 
25275b91130SDave Jiang 		idxd->engines[i] = engine;
25375b91130SDave Jiang 	}
25475b91130SDave Jiang 
25575b91130SDave Jiang 	return 0;
25675b91130SDave Jiang 
25775b91130SDave Jiang  err:
25875b91130SDave Jiang 	while (--i >= 0)
25975b91130SDave Jiang 		put_device(&idxd->engines[i]->conf_dev);
26075b91130SDave Jiang 	return rc;
26175b91130SDave Jiang }
26275b91130SDave Jiang 
263defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd)
264defe49f9SDave Jiang {
265defe49f9SDave Jiang 	struct device *dev = &idxd->pdev->dev;
266defe49f9SDave Jiang 	struct idxd_group *group;
267defe49f9SDave Jiang 	int i, rc;
268defe49f9SDave Jiang 
269defe49f9SDave Jiang 	idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
270defe49f9SDave Jiang 				    GFP_KERNEL, dev_to_node(dev));
271defe49f9SDave Jiang 	if (!idxd->groups)
272defe49f9SDave Jiang 		return -ENOMEM;
273defe49f9SDave Jiang 
274defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++) {
275defe49f9SDave Jiang 		group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
276defe49f9SDave Jiang 		if (!group) {
277defe49f9SDave Jiang 			rc = -ENOMEM;
278defe49f9SDave Jiang 			goto err;
279defe49f9SDave Jiang 		}
280defe49f9SDave Jiang 
281defe49f9SDave Jiang 		group->id = i;
282defe49f9SDave Jiang 		group->idxd = idxd;
283defe49f9SDave Jiang 		device_initialize(&group->conf_dev);
284defe49f9SDave Jiang 		group->conf_dev.parent = &idxd->conf_dev;
2854b73e4ebSDave Jiang 		group->conf_dev.bus = &dsa_bus_type;
286defe49f9SDave Jiang 		group->conf_dev.type = &idxd_group_device_type;
287defe49f9SDave Jiang 		rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id);
288defe49f9SDave Jiang 		if (rc < 0) {
289defe49f9SDave Jiang 			put_device(&group->conf_dev);
290defe49f9SDave Jiang 			goto err;
291defe49f9SDave Jiang 		}
292defe49f9SDave Jiang 
293defe49f9SDave Jiang 		idxd->groups[i] = group;
294defe49f9SDave Jiang 		group->tc_a = -1;
295defe49f9SDave Jiang 		group->tc_b = -1;
296defe49f9SDave Jiang 	}
297defe49f9SDave Jiang 
298defe49f9SDave Jiang 	return 0;
299defe49f9SDave Jiang 
300defe49f9SDave Jiang  err:
301defe49f9SDave Jiang 	while (--i >= 0)
302defe49f9SDave Jiang 		put_device(&idxd->groups[i]->conf_dev);
303defe49f9SDave Jiang 	return rc;
304defe49f9SDave Jiang }
305defe49f9SDave Jiang 
306bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd)
307bfe1d560SDave Jiang {
308bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
309defe49f9SDave Jiang 	int rc, i;
310bfe1d560SDave Jiang 
3110d5c10b4SDave Jiang 	init_waitqueue_head(&idxd->cmd_waitq);
3127c5dd23eSDave Jiang 
313eb15e715SDave Jiang 	if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
314*33f9f3c3SDave Jiang 		idxd->int_handles = kcalloc_node(idxd->max_wqs, sizeof(int), GFP_KERNEL,
315*33f9f3c3SDave Jiang 						 dev_to_node(dev));
316eb15e715SDave Jiang 		if (!idxd->int_handles)
317eb15e715SDave Jiang 			return -ENOMEM;
318eb15e715SDave Jiang 	}
319eb15e715SDave Jiang 
3207c5dd23eSDave Jiang 	rc = idxd_setup_wqs(idxd);
3217c5dd23eSDave Jiang 	if (rc < 0)
322eb15e715SDave Jiang 		goto err_wqs;
3237c5dd23eSDave Jiang 
32475b91130SDave Jiang 	rc = idxd_setup_engines(idxd);
32575b91130SDave Jiang 	if (rc < 0)
32675b91130SDave Jiang 		goto err_engine;
32775b91130SDave Jiang 
328defe49f9SDave Jiang 	rc = idxd_setup_groups(idxd);
329defe49f9SDave Jiang 	if (rc < 0)
330defe49f9SDave Jiang 		goto err_group;
331bfe1d560SDave Jiang 
3320d5c10b4SDave Jiang 	idxd->wq = create_workqueue(dev_name(dev));
3337c5dd23eSDave Jiang 	if (!idxd->wq) {
3347c5dd23eSDave Jiang 		rc = -ENOMEM;
335defe49f9SDave Jiang 		goto err_wkq_create;
3367c5dd23eSDave Jiang 	}
3370d5c10b4SDave Jiang 
338bfe1d560SDave Jiang 	return 0;
3397c5dd23eSDave Jiang 
340defe49f9SDave Jiang  err_wkq_create:
341defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
342defe49f9SDave Jiang 		put_device(&idxd->groups[i]->conf_dev);
343defe49f9SDave Jiang  err_group:
34475b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
34575b91130SDave Jiang 		put_device(&idxd->engines[i]->conf_dev);
34675b91130SDave Jiang  err_engine:
3477c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
3487c5dd23eSDave Jiang 		put_device(&idxd->wqs[i]->conf_dev);
349eb15e715SDave Jiang  err_wqs:
350eb15e715SDave Jiang 	kfree(idxd->int_handles);
3517c5dd23eSDave Jiang 	return rc;
352bfe1d560SDave Jiang }
353bfe1d560SDave Jiang 
354bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd)
355bfe1d560SDave Jiang {
356bfe1d560SDave Jiang 	union offsets_reg offsets;
357bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
358bfe1d560SDave Jiang 
359bfe1d560SDave Jiang 	offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
3602f8417a9SDave Jiang 	offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
3612f8417a9SDave Jiang 	idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
362bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
3632f8417a9SDave Jiang 	idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
3642f8417a9SDave Jiang 	dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
3652f8417a9SDave Jiang 	idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
3662f8417a9SDave Jiang 	dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
3672f8417a9SDave Jiang 	idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
368bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
369bfe1d560SDave Jiang }
370bfe1d560SDave Jiang 
371bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd)
372bfe1d560SDave Jiang {
373bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
374bfe1d560SDave Jiang 	int i;
375bfe1d560SDave Jiang 
376bfe1d560SDave Jiang 	/* reading generic capabilities */
377bfe1d560SDave Jiang 	idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
378bfe1d560SDave Jiang 	dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
379eb15e715SDave Jiang 
380eb15e715SDave Jiang 	if (idxd->hw.gen_cap.cmd_cap) {
381eb15e715SDave Jiang 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
382eb15e715SDave Jiang 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
383eb15e715SDave Jiang 	}
384eb15e715SDave Jiang 
385bfe1d560SDave Jiang 	idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
386bfe1d560SDave Jiang 	dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
387bfe1d560SDave Jiang 	idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
388bfe1d560SDave Jiang 	dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
389bfe1d560SDave Jiang 	if (idxd->hw.gen_cap.config_en)
390bfe1d560SDave Jiang 		set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
391bfe1d560SDave Jiang 
392bfe1d560SDave Jiang 	/* reading group capabilities */
393bfe1d560SDave Jiang 	idxd->hw.group_cap.bits =
394bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
395bfe1d560SDave Jiang 	dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
396bfe1d560SDave Jiang 	idxd->max_groups = idxd->hw.group_cap.num_groups;
397bfe1d560SDave Jiang 	dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
398bfe1d560SDave Jiang 	idxd->max_tokens = idxd->hw.group_cap.total_tokens;
399bfe1d560SDave Jiang 	dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
400c52ca478SDave Jiang 	idxd->nr_tokens = idxd->max_tokens;
401bfe1d560SDave Jiang 
402bfe1d560SDave Jiang 	/* read engine capabilities */
403bfe1d560SDave Jiang 	idxd->hw.engine_cap.bits =
404bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
405bfe1d560SDave Jiang 	dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
406bfe1d560SDave Jiang 	idxd->max_engines = idxd->hw.engine_cap.num_engines;
407bfe1d560SDave Jiang 	dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
408bfe1d560SDave Jiang 
409bfe1d560SDave Jiang 	/* read workqueue capabilities */
410bfe1d560SDave Jiang 	idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
411bfe1d560SDave Jiang 	dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
412bfe1d560SDave Jiang 	idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
413bfe1d560SDave Jiang 	dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
414bfe1d560SDave Jiang 	idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
415bfe1d560SDave Jiang 	dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
416d98793b5SDave Jiang 	idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
417d98793b5SDave Jiang 	dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
418bfe1d560SDave Jiang 
419bfe1d560SDave Jiang 	/* reading operation capabilities */
420bfe1d560SDave Jiang 	for (i = 0; i < 4; i++) {
421bfe1d560SDave Jiang 		idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
422bfe1d560SDave Jiang 				IDXD_OPCAP_OFFSET + i * sizeof(u64));
423bfe1d560SDave Jiang 		dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
424bfe1d560SDave Jiang 	}
425bfe1d560SDave Jiang }
426bfe1d560SDave Jiang 
427435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
428bfe1d560SDave Jiang {
429bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
430bfe1d560SDave Jiang 	struct idxd_device *idxd;
43147c16ac2SDave Jiang 	int rc;
432bfe1d560SDave Jiang 
43347c16ac2SDave Jiang 	idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
434bfe1d560SDave Jiang 	if (!idxd)
435bfe1d560SDave Jiang 		return NULL;
436bfe1d560SDave Jiang 
437bfe1d560SDave Jiang 	idxd->pdev = pdev;
438435b512dSDave Jiang 	idxd->data = data;
4394b73e4ebSDave Jiang 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
44047c16ac2SDave Jiang 	if (idxd->id < 0)
44147c16ac2SDave Jiang 		return NULL;
44247c16ac2SDave Jiang 
44347c16ac2SDave Jiang 	device_initialize(&idxd->conf_dev);
44447c16ac2SDave Jiang 	idxd->conf_dev.parent = dev;
4454b73e4ebSDave Jiang 	idxd->conf_dev.bus = &dsa_bus_type;
446435b512dSDave Jiang 	idxd->conf_dev.type = idxd->data->dev_type;
447435b512dSDave Jiang 	rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
44847c16ac2SDave Jiang 	if (rc < 0) {
44947c16ac2SDave Jiang 		put_device(&idxd->conf_dev);
45047c16ac2SDave Jiang 		return NULL;
45147c16ac2SDave Jiang 	}
45247c16ac2SDave Jiang 
453bfe1d560SDave Jiang 	spin_lock_init(&idxd->dev_lock);
45453b2ee7fSDave Jiang 	spin_lock_init(&idxd->cmd_lock);
455bfe1d560SDave Jiang 
456bfe1d560SDave Jiang 	return idxd;
457bfe1d560SDave Jiang }
458bfe1d560SDave Jiang 
4598e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd)
4608e50d392SDave Jiang {
4618e50d392SDave Jiang 	int flags;
4628e50d392SDave Jiang 	unsigned int pasid;
4638e50d392SDave Jiang 	struct iommu_sva *sva;
4648e50d392SDave Jiang 
4658e50d392SDave Jiang 	flags = SVM_FLAG_SUPERVISOR_MODE;
4668e50d392SDave Jiang 
4678e50d392SDave Jiang 	sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
4688e50d392SDave Jiang 	if (IS_ERR(sva)) {
4698e50d392SDave Jiang 		dev_warn(&idxd->pdev->dev,
4708e50d392SDave Jiang 			 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
4718e50d392SDave Jiang 		return PTR_ERR(sva);
4728e50d392SDave Jiang 	}
4738e50d392SDave Jiang 
4748e50d392SDave Jiang 	pasid = iommu_sva_get_pasid(sva);
4758e50d392SDave Jiang 	if (pasid == IOMMU_PASID_INVALID) {
4768e50d392SDave Jiang 		iommu_sva_unbind_device(sva);
4778e50d392SDave Jiang 		return -ENODEV;
4788e50d392SDave Jiang 	}
4798e50d392SDave Jiang 
4808e50d392SDave Jiang 	idxd->sva = sva;
4818e50d392SDave Jiang 	idxd->pasid = pasid;
4828e50d392SDave Jiang 	dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
4838e50d392SDave Jiang 	return 0;
4848e50d392SDave Jiang }
4858e50d392SDave Jiang 
4868e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd)
4878e50d392SDave Jiang {
4888e50d392SDave Jiang 
4898e50d392SDave Jiang 	iommu_sva_unbind_device(idxd->sva);
4908e50d392SDave Jiang 	idxd->sva = NULL;
4918e50d392SDave Jiang }
4928e50d392SDave Jiang 
493bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd)
494bfe1d560SDave Jiang {
495bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
496bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
497bfe1d560SDave Jiang 	int rc;
498bfe1d560SDave Jiang 
499bfe1d560SDave Jiang 	dev_dbg(dev, "%s entered and resetting device\n", __func__);
50089e3becdSDave Jiang 	rc = idxd_device_init_reset(idxd);
50189e3becdSDave Jiang 	if (rc < 0)
50289e3becdSDave Jiang 		return rc;
50389e3becdSDave Jiang 
504bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD reset complete\n");
505bfe1d560SDave Jiang 
50603d939c7SDave Jiang 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
507cf5f86a7SDave Jiang 		rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA);
508cf5f86a7SDave Jiang 		if (rc == 0) {
5098e50d392SDave Jiang 			rc = idxd_enable_system_pasid(idxd);
510cf5f86a7SDave Jiang 			if (rc < 0) {
511cf5f86a7SDave Jiang 				iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
5128e50d392SDave Jiang 				dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
513cf5f86a7SDave Jiang 			} else {
5148e50d392SDave Jiang 				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
515cf5f86a7SDave Jiang 			}
516cf5f86a7SDave Jiang 		} else {
517cf5f86a7SDave Jiang 			dev_warn(dev, "Unable to turn on SVA feature.\n");
518cf5f86a7SDave Jiang 		}
51903d939c7SDave Jiang 	} else if (!sva) {
52003d939c7SDave Jiang 		dev_warn(dev, "User forced SVA off via module param.\n");
5218e50d392SDave Jiang 	}
5228e50d392SDave Jiang 
523bfe1d560SDave Jiang 	idxd_read_caps(idxd);
524bfe1d560SDave Jiang 	idxd_read_table_offsets(idxd);
525bfe1d560SDave Jiang 
526bfe1d560SDave Jiang 	rc = idxd_setup_internals(idxd);
527bfe1d560SDave Jiang 	if (rc)
5287c5dd23eSDave Jiang 		goto err;
529bfe1d560SDave Jiang 
5308c66bbdcSDave Jiang 	/* If the configs are readonly, then load them from device */
5318c66bbdcSDave Jiang 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
5328c66bbdcSDave Jiang 		dev_dbg(dev, "Loading RO device config\n");
5338c66bbdcSDave Jiang 		rc = idxd_device_load_config(idxd);
5348c66bbdcSDave Jiang 		if (rc < 0)
5358c66bbdcSDave Jiang 			goto err;
5368c66bbdcSDave Jiang 	}
5378c66bbdcSDave Jiang 
538bfe1d560SDave Jiang 	rc = idxd_setup_interrupts(idxd);
539bfe1d560SDave Jiang 	if (rc)
5407c5dd23eSDave Jiang 		goto err;
541bfe1d560SDave Jiang 
542bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD interrupt setup complete.\n");
543bfe1d560SDave Jiang 
54442d279f9SDave Jiang 	idxd->major = idxd_cdev_get_major(idxd);
54542d279f9SDave Jiang 
5460bde4444STom Zanussi 	rc = perfmon_pmu_init(idxd);
5470bde4444STom Zanussi 	if (rc < 0)
5480bde4444STom Zanussi 		dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
5490bde4444STom Zanussi 
550bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
551bfe1d560SDave Jiang 	return 0;
552bfe1d560SDave Jiang 
5537c5dd23eSDave Jiang  err:
5548e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
5558e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
556cf5f86a7SDave Jiang 	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
557bfe1d560SDave Jiang 	return rc;
558bfe1d560SDave Jiang }
559bfe1d560SDave Jiang 
560bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
561bfe1d560SDave Jiang {
562bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
563bfe1d560SDave Jiang 	struct idxd_device *idxd;
564435b512dSDave Jiang 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
565bfe1d560SDave Jiang 	int rc;
566bfe1d560SDave Jiang 
567a39c7cd0SDave Jiang 	rc = pci_enable_device(pdev);
568bfe1d560SDave Jiang 	if (rc)
569bfe1d560SDave Jiang 		return rc;
570bfe1d560SDave Jiang 
5718e50d392SDave Jiang 	dev_dbg(dev, "Alloc IDXD context\n");
572435b512dSDave Jiang 	idxd = idxd_alloc(pdev, data);
573a39c7cd0SDave Jiang 	if (!idxd) {
574a39c7cd0SDave Jiang 		rc = -ENOMEM;
575a39c7cd0SDave Jiang 		goto err_idxd_alloc;
576a39c7cd0SDave Jiang 	}
577bfe1d560SDave Jiang 
5788e50d392SDave Jiang 	dev_dbg(dev, "Mapping BARs\n");
579a39c7cd0SDave Jiang 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
580a39c7cd0SDave Jiang 	if (!idxd->reg_base) {
581a39c7cd0SDave Jiang 		rc = -ENOMEM;
582a39c7cd0SDave Jiang 		goto err_iomap;
583a39c7cd0SDave Jiang 	}
584bfe1d560SDave Jiang 
585bfe1d560SDave Jiang 	dev_dbg(dev, "Set DMA masks\n");
586bfe1d560SDave Jiang 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
587bfe1d560SDave Jiang 	if (rc)
588bfe1d560SDave Jiang 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
589bfe1d560SDave Jiang 	if (rc)
590a39c7cd0SDave Jiang 		goto err;
591bfe1d560SDave Jiang 
592bfe1d560SDave Jiang 	rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
593bfe1d560SDave Jiang 	if (rc)
594bfe1d560SDave Jiang 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
595bfe1d560SDave Jiang 	if (rc)
596a39c7cd0SDave Jiang 		goto err;
597bfe1d560SDave Jiang 
598bfe1d560SDave Jiang 	dev_dbg(dev, "Set PCI master\n");
599bfe1d560SDave Jiang 	pci_set_master(pdev);
600bfe1d560SDave Jiang 	pci_set_drvdata(pdev, idxd);
601bfe1d560SDave Jiang 
602bfe1d560SDave Jiang 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
603bfe1d560SDave Jiang 	rc = idxd_probe(idxd);
604bfe1d560SDave Jiang 	if (rc) {
605bfe1d560SDave Jiang 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
606a39c7cd0SDave Jiang 		goto err;
607bfe1d560SDave Jiang 	}
608bfe1d560SDave Jiang 
60947c16ac2SDave Jiang 	rc = idxd_register_devices(idxd);
610c52ca478SDave Jiang 	if (rc) {
611c52ca478SDave Jiang 		dev_err(dev, "IDXD sysfs setup failed\n");
612a39c7cd0SDave Jiang 		goto err;
613c52ca478SDave Jiang 	}
614c52ca478SDave Jiang 
615c52ca478SDave Jiang 	idxd->state = IDXD_DEV_CONF_READY;
616c52ca478SDave Jiang 
617bfe1d560SDave Jiang 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
618bfe1d560SDave Jiang 		 idxd->hw.version);
619bfe1d560SDave Jiang 
620bfe1d560SDave Jiang 	return 0;
621a39c7cd0SDave Jiang 
622a39c7cd0SDave Jiang  err:
623a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
624a39c7cd0SDave Jiang  err_iomap:
62547c16ac2SDave Jiang 	put_device(&idxd->conf_dev);
626a39c7cd0SDave Jiang  err_idxd_alloc:
627a39c7cd0SDave Jiang 	pci_disable_device(pdev);
628a39c7cd0SDave Jiang 	return rc;
629bfe1d560SDave Jiang }
630bfe1d560SDave Jiang 
6318f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
6328f47d1a5SDave Jiang {
6338f47d1a5SDave Jiang 	struct idxd_desc *desc, *itr;
6348f47d1a5SDave Jiang 	struct llist_node *head;
6358f47d1a5SDave Jiang 
6368f47d1a5SDave Jiang 	head = llist_del_all(&ie->pending_llist);
6378f47d1a5SDave Jiang 	if (!head)
6388f47d1a5SDave Jiang 		return;
6398f47d1a5SDave Jiang 
6408f47d1a5SDave Jiang 	llist_for_each_entry_safe(desc, itr, head, llnode) {
6418f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
6428f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
6438f47d1a5SDave Jiang 	}
6448f47d1a5SDave Jiang }
6458f47d1a5SDave Jiang 
6468f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie)
6478f47d1a5SDave Jiang {
6488f47d1a5SDave Jiang 	struct idxd_desc *desc, *iter;
6498f47d1a5SDave Jiang 
6508f47d1a5SDave Jiang 	list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
6518f47d1a5SDave Jiang 		list_del(&desc->list);
6528f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
6538f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
6548f47d1a5SDave Jiang 	}
6558f47d1a5SDave Jiang }
6568f47d1a5SDave Jiang 
6575b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd)
6585b0c68c4SDave Jiang {
6595b0c68c4SDave Jiang 	struct idxd_wq *wq;
6605b0c68c4SDave Jiang 	int i;
6615b0c68c4SDave Jiang 
6625b0c68c4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
6635b0c68c4SDave Jiang 		wq = idxd->wqs[i];
6645b0c68c4SDave Jiang 		if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
6655b0c68c4SDave Jiang 			idxd_wq_quiesce(wq);
6665b0c68c4SDave Jiang 	}
6675b0c68c4SDave Jiang }
6685b0c68c4SDave Jiang 
669eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd)
670eb15e715SDave Jiang {
671eb15e715SDave Jiang 	struct device *dev = &idxd->pdev->dev;
672eb15e715SDave Jiang 	int i, rc;
673eb15e715SDave Jiang 
674eb15e715SDave Jiang 	for (i = 0; i < idxd->num_wq_irqs; i++) {
675eb15e715SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) {
676eb15e715SDave Jiang 			rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i],
677eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
678eb15e715SDave Jiang 			if (rc < 0)
679eb15e715SDave Jiang 				dev_warn(dev, "irq handle %d release failed\n",
680eb15e715SDave Jiang 					 idxd->int_handles[i]);
681eb15e715SDave Jiang 			else
682eb15e715SDave Jiang 				dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]);
683eb15e715SDave Jiang 		}
684eb15e715SDave Jiang 	}
685eb15e715SDave Jiang }
686eb15e715SDave Jiang 
687bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
688bfe1d560SDave Jiang {
689bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
690bfe1d560SDave Jiang 	int rc, i;
691bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
692bfe1d560SDave Jiang 	int msixcnt = pci_msix_vec_count(pdev);
693bfe1d560SDave Jiang 
694bfe1d560SDave Jiang 	rc = idxd_device_disable(idxd);
695bfe1d560SDave Jiang 	if (rc)
696bfe1d560SDave Jiang 		dev_err(&pdev->dev, "Disabling device failed\n");
697bfe1d560SDave Jiang 
698bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
699bfe1d560SDave Jiang 	idxd_mask_msix_vectors(idxd);
700bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
701bfe1d560SDave Jiang 
702bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
703bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
7045fc8e85fSDave Jiang 		synchronize_irq(irq_entry->vector);
7055fc8e85fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
706bfe1d560SDave Jiang 		if (i == 0)
707bfe1d560SDave Jiang 			continue;
7088f47d1a5SDave Jiang 		idxd_flush_pending_llist(irq_entry);
7098f47d1a5SDave Jiang 		idxd_flush_work_list(irq_entry);
710bfe1d560SDave Jiang 	}
7110d5c10b4SDave Jiang 
7126df0e6c5SDave Jiang 	idxd_msix_perm_clear(idxd);
713eb15e715SDave Jiang 	idxd_release_int_handles(idxd);
7145fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
715a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
716a39c7cd0SDave Jiang 	pci_disable_device(pdev);
7170d5c10b4SDave Jiang 	destroy_workqueue(idxd->wq);
718bfe1d560SDave Jiang }
719bfe1d560SDave Jiang 
720bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
721bfe1d560SDave Jiang {
722bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
723bfe1d560SDave Jiang 
724bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
725bfe1d560SDave Jiang 	idxd_shutdown(pdev);
7268e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
7278e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
72847c16ac2SDave Jiang 	idxd_unregister_devices(idxd);
7290bde4444STom Zanussi 	perfmon_pmu_remove(idxd);
730cf5f86a7SDave Jiang 	iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
731bfe1d560SDave Jiang }
732bfe1d560SDave Jiang 
733bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
734bfe1d560SDave Jiang 	.name		= DRV_NAME,
735bfe1d560SDave Jiang 	.id_table	= idxd_pci_tbl,
736bfe1d560SDave Jiang 	.probe		= idxd_pci_probe,
737bfe1d560SDave Jiang 	.remove		= idxd_remove,
738bfe1d560SDave Jiang 	.shutdown	= idxd_shutdown,
739bfe1d560SDave Jiang };
740bfe1d560SDave Jiang 
741bfe1d560SDave Jiang static int __init idxd_init_module(void)
742bfe1d560SDave Jiang {
7434b73e4ebSDave Jiang 	int err;
744bfe1d560SDave Jiang 
745bfe1d560SDave Jiang 	/*
7468e50d392SDave Jiang 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
747bfe1d560SDave Jiang 	 * enumerating the device. We can not utilize it.
748bfe1d560SDave Jiang 	 */
749bfe1d560SDave Jiang 	if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) {
750bfe1d560SDave Jiang 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
751bfe1d560SDave Jiang 		return -ENODEV;
752bfe1d560SDave Jiang 	}
753bfe1d560SDave Jiang 
7548e50d392SDave Jiang 	if (!boot_cpu_has(X86_FEATURE_ENQCMD))
7558e50d392SDave Jiang 		pr_warn("Platform does not have ENQCMD(S) support.\n");
7568e50d392SDave Jiang 	else
7578e50d392SDave Jiang 		support_enqcmd = true;
758bfe1d560SDave Jiang 
7590bde4444STom Zanussi 	perfmon_init();
7600bde4444STom Zanussi 
761c52ca478SDave Jiang 	err = idxd_register_bus_type();
762c52ca478SDave Jiang 	if (err < 0)
763bfe1d560SDave Jiang 		return err;
764bfe1d560SDave Jiang 
765c52ca478SDave Jiang 	err = idxd_register_driver();
766c52ca478SDave Jiang 	if (err < 0)
767c52ca478SDave Jiang 		goto err_idxd_driver_register;
768c52ca478SDave Jiang 
76942d279f9SDave Jiang 	err = idxd_cdev_register();
77042d279f9SDave Jiang 	if (err)
77142d279f9SDave Jiang 		goto err_cdev_register;
77242d279f9SDave Jiang 
773c52ca478SDave Jiang 	err = pci_register_driver(&idxd_pci_driver);
774c52ca478SDave Jiang 	if (err)
775c52ca478SDave Jiang 		goto err_pci_register;
776c52ca478SDave Jiang 
777bfe1d560SDave Jiang 	return 0;
778c52ca478SDave Jiang 
779c52ca478SDave Jiang err_pci_register:
78042d279f9SDave Jiang 	idxd_cdev_remove();
78142d279f9SDave Jiang err_cdev_register:
782c52ca478SDave Jiang 	idxd_unregister_driver();
783c52ca478SDave Jiang err_idxd_driver_register:
784c52ca478SDave Jiang 	idxd_unregister_bus_type();
785c52ca478SDave Jiang 	return err;
786bfe1d560SDave Jiang }
787bfe1d560SDave Jiang module_init(idxd_init_module);
788bfe1d560SDave Jiang 
789bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
790bfe1d560SDave Jiang {
791bfe1d560SDave Jiang 	pci_unregister_driver(&idxd_pci_driver);
79242d279f9SDave Jiang 	idxd_cdev_remove();
793c52ca478SDave Jiang 	idxd_unregister_bus_type();
7940bde4444STom Zanussi 	perfmon_exit();
795bfe1d560SDave Jiang }
796bfe1d560SDave Jiang module_exit(idxd_exit_module);
797