xref: /linux/drivers/dma/idxd/init.c (revision 5b0c68c473a131c2acb21abad44b0047b200e185)
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 
2903d939c7SDave Jiang static bool sva = true;
3003d939c7SDave Jiang module_param(sva, bool, 0644);
3103d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
3203d939c7SDave Jiang 
33bfe1d560SDave Jiang #define DRV_NAME "idxd"
34bfe1d560SDave Jiang 
358e50d392SDave Jiang bool support_enqcmd;
364b73e4ebSDave Jiang DEFINE_IDA(idxd_ida);
37bfe1d560SDave Jiang 
38435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = {
39435b512dSDave Jiang 	[IDXD_TYPE_DSA] = {
40435b512dSDave Jiang 		.name_prefix = "dsa",
41435b512dSDave Jiang 		.type = IDXD_TYPE_DSA,
42435b512dSDave Jiang 		.compl_size = sizeof(struct dsa_completion_record),
43435b512dSDave Jiang 		.align = 32,
44435b512dSDave Jiang 		.dev_type = &dsa_device_type,
45435b512dSDave Jiang 	},
46435b512dSDave Jiang 	[IDXD_TYPE_IAX] = {
47435b512dSDave Jiang 		.name_prefix = "iax",
48435b512dSDave Jiang 		.type = IDXD_TYPE_IAX,
49435b512dSDave Jiang 		.compl_size = sizeof(struct iax_completion_record),
50435b512dSDave Jiang 		.align = 64,
51435b512dSDave Jiang 		.dev_type = &iax_device_type,
52435b512dSDave Jiang 	},
53435b512dSDave Jiang };
54435b512dSDave Jiang 
55bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = {
56bfe1d560SDave Jiang 	/* DSA ver 1.0 platforms */
57435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
58f25b4638SDave Jiang 
59f25b4638SDave Jiang 	/* IAX ver 1.0 platforms */
60435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
61bfe1d560SDave Jiang 	{ 0, }
62bfe1d560SDave Jiang };
63bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
64bfe1d560SDave Jiang 
65bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd)
66bfe1d560SDave Jiang {
67bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
68bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
69bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
70bfe1d560SDave Jiang 	int i, msixcnt;
71bfe1d560SDave Jiang 	int rc = 0;
72bfe1d560SDave Jiang 
73bfe1d560SDave Jiang 	msixcnt = pci_msix_vec_count(pdev);
74bfe1d560SDave Jiang 	if (msixcnt < 0) {
75bfe1d560SDave Jiang 		dev_err(dev, "Not MSI-X interrupt capable.\n");
765fc8e85fSDave Jiang 		return -ENOSPC;
77bfe1d560SDave Jiang 	}
78bfe1d560SDave Jiang 
795fc8e85fSDave Jiang 	rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
805fc8e85fSDave Jiang 	if (rc != msixcnt) {
815fc8e85fSDave Jiang 		dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
825fc8e85fSDave Jiang 		return -ENOSPC;
83bfe1d560SDave Jiang 	}
84bfe1d560SDave Jiang 	dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
85bfe1d560SDave Jiang 
86bfe1d560SDave Jiang 	/*
87bfe1d560SDave Jiang 	 * We implement 1 completion list per MSI-X entry except for
88bfe1d560SDave Jiang 	 * entry 0, which is for errors and others.
89bfe1d560SDave Jiang 	 */
9047c16ac2SDave Jiang 	idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
9147c16ac2SDave Jiang 					 GFP_KERNEL, dev_to_node(dev));
92bfe1d560SDave Jiang 	if (!idxd->irq_entries) {
93bfe1d560SDave Jiang 		rc = -ENOMEM;
945fc8e85fSDave Jiang 		goto err_irq_entries;
95bfe1d560SDave Jiang 	}
96bfe1d560SDave Jiang 
97bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
98bfe1d560SDave Jiang 		idxd->irq_entries[i].id = i;
99bfe1d560SDave Jiang 		idxd->irq_entries[i].idxd = idxd;
1005fc8e85fSDave Jiang 		idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
101e4f4d8cdSDave Jiang 		spin_lock_init(&idxd->irq_entries[i].list_lock);
102bfe1d560SDave Jiang 	}
103bfe1d560SDave Jiang 
104bfe1d560SDave Jiang 	irq_entry = &idxd->irq_entries[0];
1055fc8e85fSDave Jiang 	rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler, idxd_misc_thread,
1065fc8e85fSDave Jiang 				  0, "idxd-misc", irq_entry);
107bfe1d560SDave Jiang 	if (rc < 0) {
108bfe1d560SDave Jiang 		dev_err(dev, "Failed to allocate misc interrupt.\n");
1095fc8e85fSDave Jiang 		goto err_misc_irq;
110bfe1d560SDave Jiang 	}
111bfe1d560SDave Jiang 
1125fc8e85fSDave Jiang 	dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
113bfe1d560SDave Jiang 
114bfe1d560SDave Jiang 	/* first MSI-X entry is not for wq interrupts */
115bfe1d560SDave Jiang 	idxd->num_wq_irqs = msixcnt - 1;
116bfe1d560SDave Jiang 
117bfe1d560SDave Jiang 	for (i = 1; i < msixcnt; i++) {
118bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
119bfe1d560SDave Jiang 
120bfe1d560SDave Jiang 		init_llist_head(&idxd->irq_entries[i].pending_llist);
121bfe1d560SDave Jiang 		INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
1225fc8e85fSDave Jiang 		rc = request_threaded_irq(irq_entry->vector, idxd_irq_handler,
1235fc8e85fSDave Jiang 					  idxd_wq_thread, 0, "idxd-portal", irq_entry);
124bfe1d560SDave Jiang 		if (rc < 0) {
1255fc8e85fSDave Jiang 			dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
1265fc8e85fSDave Jiang 			goto err_wq_irqs;
127bfe1d560SDave Jiang 		}
128eb15e715SDave Jiang 
1295fc8e85fSDave Jiang 		dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
130eb15e715SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
131eb15e715SDave Jiang 			/*
132eb15e715SDave Jiang 			 * The MSIX vector enumeration starts at 1 with vector 0 being the
133eb15e715SDave Jiang 			 * misc interrupt that handles non I/O completion events. The
134eb15e715SDave Jiang 			 * interrupt handles are for IMS enumeration on guest. The misc
135eb15e715SDave Jiang 			 * interrupt vector does not require a handle and therefore we start
136eb15e715SDave Jiang 			 * the int_handles at index 0. Since 'i' starts at 1, the first
137eb15e715SDave Jiang 			 * int_handles index will be 0.
138eb15e715SDave Jiang 			 */
139eb15e715SDave Jiang 			rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1],
140eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
141eb15e715SDave Jiang 			if (rc < 0) {
142eb15e715SDave Jiang 				free_irq(irq_entry->vector, irq_entry);
143eb15e715SDave Jiang 				goto err_wq_irqs;
144eb15e715SDave Jiang 			}
145eb15e715SDave Jiang 			dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]);
146eb15e715SDave Jiang 		}
147bfe1d560SDave Jiang 	}
148bfe1d560SDave Jiang 
149bfe1d560SDave Jiang 	idxd_unmask_error_interrupts(idxd);
1506df0e6c5SDave Jiang 	idxd_msix_perm_setup(idxd);
151bfe1d560SDave Jiang 	return 0;
152bfe1d560SDave Jiang 
1535fc8e85fSDave Jiang  err_wq_irqs:
1545fc8e85fSDave Jiang 	while (--i >= 0) {
1555fc8e85fSDave Jiang 		irq_entry = &idxd->irq_entries[i];
1565fc8e85fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
157eb15e715SDave Jiang 		if (i != 0)
158eb15e715SDave Jiang 			idxd_device_release_int_handle(idxd,
159eb15e715SDave Jiang 						       idxd->int_handles[i], IDXD_IRQ_MSIX);
1605fc8e85fSDave Jiang 	}
1615fc8e85fSDave Jiang  err_misc_irq:
162bfe1d560SDave Jiang 	/* Disable error interrupt generation */
163bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
1645fc8e85fSDave Jiang  err_irq_entries:
1655fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
166bfe1d560SDave Jiang 	dev_err(dev, "No usable interrupts\n");
167bfe1d560SDave Jiang 	return rc;
168bfe1d560SDave Jiang }
169bfe1d560SDave Jiang 
1707c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd)
1717c5dd23eSDave Jiang {
1727c5dd23eSDave Jiang 	struct device *dev = &idxd->pdev->dev;
1737c5dd23eSDave Jiang 	struct idxd_wq *wq;
1747c5dd23eSDave Jiang 	int i, rc;
1757c5dd23eSDave Jiang 
1767c5dd23eSDave Jiang 	idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
1777c5dd23eSDave Jiang 				 GFP_KERNEL, dev_to_node(dev));
1787c5dd23eSDave Jiang 	if (!idxd->wqs)
1797c5dd23eSDave Jiang 		return -ENOMEM;
1807c5dd23eSDave Jiang 
1817c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
1827c5dd23eSDave Jiang 		wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
1837c5dd23eSDave Jiang 		if (!wq) {
1847c5dd23eSDave Jiang 			rc = -ENOMEM;
1857c5dd23eSDave Jiang 			goto err;
1867c5dd23eSDave Jiang 		}
1877c5dd23eSDave Jiang 
1887c5dd23eSDave Jiang 		wq->id = i;
1897c5dd23eSDave Jiang 		wq->idxd = idxd;
1907c5dd23eSDave Jiang 		device_initialize(&wq->conf_dev);
1917c5dd23eSDave Jiang 		wq->conf_dev.parent = &idxd->conf_dev;
1924b73e4ebSDave Jiang 		wq->conf_dev.bus = &dsa_bus_type;
1937c5dd23eSDave Jiang 		wq->conf_dev.type = &idxd_wq_device_type;
1947c5dd23eSDave Jiang 		rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id);
1957c5dd23eSDave Jiang 		if (rc < 0) {
1967c5dd23eSDave Jiang 			put_device(&wq->conf_dev);
1977c5dd23eSDave Jiang 			goto err;
1987c5dd23eSDave Jiang 		}
1997c5dd23eSDave Jiang 
2007c5dd23eSDave Jiang 		mutex_init(&wq->wq_lock);
20104922b74SDave Jiang 		init_waitqueue_head(&wq->err_queue);
20293a40a6dSDave Jiang 		init_completion(&wq->wq_dead);
2037c5dd23eSDave Jiang 		wq->max_xfer_bytes = idxd->max_xfer_bytes;
2047c5dd23eSDave Jiang 		wq->max_batch_size = idxd->max_batch_size;
2057c5dd23eSDave Jiang 		wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
2067c5dd23eSDave Jiang 		if (!wq->wqcfg) {
2077c5dd23eSDave Jiang 			put_device(&wq->conf_dev);
2087c5dd23eSDave Jiang 			rc = -ENOMEM;
2097c5dd23eSDave Jiang 			goto err;
2107c5dd23eSDave Jiang 		}
2117c5dd23eSDave Jiang 		idxd->wqs[i] = wq;
2127c5dd23eSDave Jiang 	}
2137c5dd23eSDave Jiang 
2147c5dd23eSDave Jiang 	return 0;
2157c5dd23eSDave Jiang 
2167c5dd23eSDave Jiang  err:
2177c5dd23eSDave Jiang 	while (--i >= 0)
2187c5dd23eSDave Jiang 		put_device(&idxd->wqs[i]->conf_dev);
2197c5dd23eSDave Jiang 	return rc;
2207c5dd23eSDave Jiang }
2217c5dd23eSDave Jiang 
22275b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd)
22375b91130SDave Jiang {
22475b91130SDave Jiang 	struct idxd_engine *engine;
22575b91130SDave Jiang 	struct device *dev = &idxd->pdev->dev;
22675b91130SDave Jiang 	int i, rc;
22775b91130SDave Jiang 
22875b91130SDave Jiang 	idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
22975b91130SDave Jiang 				     GFP_KERNEL, dev_to_node(dev));
23075b91130SDave Jiang 	if (!idxd->engines)
23175b91130SDave Jiang 		return -ENOMEM;
23275b91130SDave Jiang 
23375b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++) {
23475b91130SDave Jiang 		engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
23575b91130SDave Jiang 		if (!engine) {
23675b91130SDave Jiang 			rc = -ENOMEM;
23775b91130SDave Jiang 			goto err;
23875b91130SDave Jiang 		}
23975b91130SDave Jiang 
24075b91130SDave Jiang 		engine->id = i;
24175b91130SDave Jiang 		engine->idxd = idxd;
24275b91130SDave Jiang 		device_initialize(&engine->conf_dev);
24375b91130SDave Jiang 		engine->conf_dev.parent = &idxd->conf_dev;
24475b91130SDave Jiang 		engine->conf_dev.type = &idxd_engine_device_type;
24575b91130SDave Jiang 		rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id);
24675b91130SDave Jiang 		if (rc < 0) {
24775b91130SDave Jiang 			put_device(&engine->conf_dev);
24875b91130SDave Jiang 			goto err;
24975b91130SDave Jiang 		}
25075b91130SDave Jiang 
25175b91130SDave Jiang 		idxd->engines[i] = engine;
25275b91130SDave Jiang 	}
25375b91130SDave Jiang 
25475b91130SDave Jiang 	return 0;
25575b91130SDave Jiang 
25675b91130SDave Jiang  err:
25775b91130SDave Jiang 	while (--i >= 0)
25875b91130SDave Jiang 		put_device(&idxd->engines[i]->conf_dev);
25975b91130SDave Jiang 	return rc;
26075b91130SDave Jiang }
26175b91130SDave Jiang 
262defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd)
263defe49f9SDave Jiang {
264defe49f9SDave Jiang 	struct device *dev = &idxd->pdev->dev;
265defe49f9SDave Jiang 	struct idxd_group *group;
266defe49f9SDave Jiang 	int i, rc;
267defe49f9SDave Jiang 
268defe49f9SDave Jiang 	idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
269defe49f9SDave Jiang 				    GFP_KERNEL, dev_to_node(dev));
270defe49f9SDave Jiang 	if (!idxd->groups)
271defe49f9SDave Jiang 		return -ENOMEM;
272defe49f9SDave Jiang 
273defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++) {
274defe49f9SDave Jiang 		group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
275defe49f9SDave Jiang 		if (!group) {
276defe49f9SDave Jiang 			rc = -ENOMEM;
277defe49f9SDave Jiang 			goto err;
278defe49f9SDave Jiang 		}
279defe49f9SDave Jiang 
280defe49f9SDave Jiang 		group->id = i;
281defe49f9SDave Jiang 		group->idxd = idxd;
282defe49f9SDave Jiang 		device_initialize(&group->conf_dev);
283defe49f9SDave Jiang 		group->conf_dev.parent = &idxd->conf_dev;
2844b73e4ebSDave Jiang 		group->conf_dev.bus = &dsa_bus_type;
285defe49f9SDave Jiang 		group->conf_dev.type = &idxd_group_device_type;
286defe49f9SDave Jiang 		rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id);
287defe49f9SDave Jiang 		if (rc < 0) {
288defe49f9SDave Jiang 			put_device(&group->conf_dev);
289defe49f9SDave Jiang 			goto err;
290defe49f9SDave Jiang 		}
291defe49f9SDave Jiang 
292defe49f9SDave Jiang 		idxd->groups[i] = group;
293defe49f9SDave Jiang 		group->tc_a = -1;
294defe49f9SDave Jiang 		group->tc_b = -1;
295defe49f9SDave Jiang 	}
296defe49f9SDave Jiang 
297defe49f9SDave Jiang 	return 0;
298defe49f9SDave Jiang 
299defe49f9SDave Jiang  err:
300defe49f9SDave Jiang 	while (--i >= 0)
301defe49f9SDave Jiang 		put_device(&idxd->groups[i]->conf_dev);
302defe49f9SDave Jiang 	return rc;
303defe49f9SDave Jiang }
304defe49f9SDave Jiang 
305bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd)
306bfe1d560SDave Jiang {
307bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
308defe49f9SDave Jiang 	int rc, i;
309bfe1d560SDave Jiang 
3100d5c10b4SDave Jiang 	init_waitqueue_head(&idxd->cmd_waitq);
3117c5dd23eSDave Jiang 
312eb15e715SDave Jiang 	if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
313eb15e715SDave Jiang 		idxd->int_handles = devm_kcalloc(dev, idxd->max_wqs, sizeof(int), GFP_KERNEL);
314eb15e715SDave Jiang 		if (!idxd->int_handles)
315eb15e715SDave Jiang 			return -ENOMEM;
316eb15e715SDave Jiang 	}
317eb15e715SDave Jiang 
3187c5dd23eSDave Jiang 	rc = idxd_setup_wqs(idxd);
3197c5dd23eSDave Jiang 	if (rc < 0)
320eb15e715SDave Jiang 		goto err_wqs;
3217c5dd23eSDave Jiang 
32275b91130SDave Jiang 	rc = idxd_setup_engines(idxd);
32375b91130SDave Jiang 	if (rc < 0)
32475b91130SDave Jiang 		goto err_engine;
32575b91130SDave Jiang 
326defe49f9SDave Jiang 	rc = idxd_setup_groups(idxd);
327defe49f9SDave Jiang 	if (rc < 0)
328defe49f9SDave Jiang 		goto err_group;
329bfe1d560SDave Jiang 
3300d5c10b4SDave Jiang 	idxd->wq = create_workqueue(dev_name(dev));
3317c5dd23eSDave Jiang 	if (!idxd->wq) {
3327c5dd23eSDave Jiang 		rc = -ENOMEM;
333defe49f9SDave Jiang 		goto err_wkq_create;
3347c5dd23eSDave Jiang 	}
3350d5c10b4SDave Jiang 
336bfe1d560SDave Jiang 	return 0;
3377c5dd23eSDave Jiang 
338defe49f9SDave Jiang  err_wkq_create:
339defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
340defe49f9SDave Jiang 		put_device(&idxd->groups[i]->conf_dev);
341defe49f9SDave Jiang  err_group:
34275b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
34375b91130SDave Jiang 		put_device(&idxd->engines[i]->conf_dev);
34475b91130SDave Jiang  err_engine:
3457c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
3467c5dd23eSDave Jiang 		put_device(&idxd->wqs[i]->conf_dev);
347eb15e715SDave Jiang  err_wqs:
348eb15e715SDave Jiang 	kfree(idxd->int_handles);
3497c5dd23eSDave Jiang 	return rc;
350bfe1d560SDave Jiang }
351bfe1d560SDave Jiang 
352bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd)
353bfe1d560SDave Jiang {
354bfe1d560SDave Jiang 	union offsets_reg offsets;
355bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
356bfe1d560SDave Jiang 
357bfe1d560SDave Jiang 	offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
3582f8417a9SDave Jiang 	offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
3592f8417a9SDave Jiang 	idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
360bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
3612f8417a9SDave Jiang 	idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
3622f8417a9SDave Jiang 	dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
3632f8417a9SDave Jiang 	idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
3642f8417a9SDave Jiang 	dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
3652f8417a9SDave Jiang 	idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
366bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
367bfe1d560SDave Jiang }
368bfe1d560SDave Jiang 
369bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd)
370bfe1d560SDave Jiang {
371bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
372bfe1d560SDave Jiang 	int i;
373bfe1d560SDave Jiang 
374bfe1d560SDave Jiang 	/* reading generic capabilities */
375bfe1d560SDave Jiang 	idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
376bfe1d560SDave Jiang 	dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
377eb15e715SDave Jiang 
378eb15e715SDave Jiang 	if (idxd->hw.gen_cap.cmd_cap) {
379eb15e715SDave Jiang 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
380eb15e715SDave Jiang 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
381eb15e715SDave Jiang 	}
382eb15e715SDave Jiang 
383bfe1d560SDave Jiang 	idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
384bfe1d560SDave Jiang 	dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
385bfe1d560SDave Jiang 	idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
386bfe1d560SDave Jiang 	dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
387bfe1d560SDave Jiang 	if (idxd->hw.gen_cap.config_en)
388bfe1d560SDave Jiang 		set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
389bfe1d560SDave Jiang 
390bfe1d560SDave Jiang 	/* reading group capabilities */
391bfe1d560SDave Jiang 	idxd->hw.group_cap.bits =
392bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
393bfe1d560SDave Jiang 	dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
394bfe1d560SDave Jiang 	idxd->max_groups = idxd->hw.group_cap.num_groups;
395bfe1d560SDave Jiang 	dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
396bfe1d560SDave Jiang 	idxd->max_tokens = idxd->hw.group_cap.total_tokens;
397bfe1d560SDave Jiang 	dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
398c52ca478SDave Jiang 	idxd->nr_tokens = idxd->max_tokens;
399bfe1d560SDave Jiang 
400bfe1d560SDave Jiang 	/* read engine capabilities */
401bfe1d560SDave Jiang 	idxd->hw.engine_cap.bits =
402bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
403bfe1d560SDave Jiang 	dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
404bfe1d560SDave Jiang 	idxd->max_engines = idxd->hw.engine_cap.num_engines;
405bfe1d560SDave Jiang 	dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
406bfe1d560SDave Jiang 
407bfe1d560SDave Jiang 	/* read workqueue capabilities */
408bfe1d560SDave Jiang 	idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
409bfe1d560SDave Jiang 	dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
410bfe1d560SDave Jiang 	idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
411bfe1d560SDave Jiang 	dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
412bfe1d560SDave Jiang 	idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
413bfe1d560SDave Jiang 	dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
414d98793b5SDave Jiang 	idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
415d98793b5SDave Jiang 	dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
416bfe1d560SDave Jiang 
417bfe1d560SDave Jiang 	/* reading operation capabilities */
418bfe1d560SDave Jiang 	for (i = 0; i < 4; i++) {
419bfe1d560SDave Jiang 		idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
420bfe1d560SDave Jiang 				IDXD_OPCAP_OFFSET + i * sizeof(u64));
421bfe1d560SDave Jiang 		dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
422bfe1d560SDave Jiang 	}
423bfe1d560SDave Jiang }
424bfe1d560SDave Jiang 
425435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
426bfe1d560SDave Jiang {
427bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
428bfe1d560SDave Jiang 	struct idxd_device *idxd;
42947c16ac2SDave Jiang 	int rc;
430bfe1d560SDave Jiang 
43147c16ac2SDave Jiang 	idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
432bfe1d560SDave Jiang 	if (!idxd)
433bfe1d560SDave Jiang 		return NULL;
434bfe1d560SDave Jiang 
435bfe1d560SDave Jiang 	idxd->pdev = pdev;
436435b512dSDave Jiang 	idxd->data = data;
4374b73e4ebSDave Jiang 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
43847c16ac2SDave Jiang 	if (idxd->id < 0)
43947c16ac2SDave Jiang 		return NULL;
44047c16ac2SDave Jiang 
44147c16ac2SDave Jiang 	device_initialize(&idxd->conf_dev);
44247c16ac2SDave Jiang 	idxd->conf_dev.parent = dev;
4434b73e4ebSDave Jiang 	idxd->conf_dev.bus = &dsa_bus_type;
444435b512dSDave Jiang 	idxd->conf_dev.type = idxd->data->dev_type;
445435b512dSDave Jiang 	rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
44647c16ac2SDave Jiang 	if (rc < 0) {
44747c16ac2SDave Jiang 		put_device(&idxd->conf_dev);
44847c16ac2SDave Jiang 		return NULL;
44947c16ac2SDave Jiang 	}
45047c16ac2SDave Jiang 
451bfe1d560SDave Jiang 	spin_lock_init(&idxd->dev_lock);
452bfe1d560SDave Jiang 
453bfe1d560SDave Jiang 	return idxd;
454bfe1d560SDave Jiang }
455bfe1d560SDave Jiang 
4568e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd)
4578e50d392SDave Jiang {
4588e50d392SDave Jiang 	int flags;
4598e50d392SDave Jiang 	unsigned int pasid;
4608e50d392SDave Jiang 	struct iommu_sva *sva;
4618e50d392SDave Jiang 
4628e50d392SDave Jiang 	flags = SVM_FLAG_SUPERVISOR_MODE;
4638e50d392SDave Jiang 
4648e50d392SDave Jiang 	sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
4658e50d392SDave Jiang 	if (IS_ERR(sva)) {
4668e50d392SDave Jiang 		dev_warn(&idxd->pdev->dev,
4678e50d392SDave Jiang 			 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
4688e50d392SDave Jiang 		return PTR_ERR(sva);
4698e50d392SDave Jiang 	}
4708e50d392SDave Jiang 
4718e50d392SDave Jiang 	pasid = iommu_sva_get_pasid(sva);
4728e50d392SDave Jiang 	if (pasid == IOMMU_PASID_INVALID) {
4738e50d392SDave Jiang 		iommu_sva_unbind_device(sva);
4748e50d392SDave Jiang 		return -ENODEV;
4758e50d392SDave Jiang 	}
4768e50d392SDave Jiang 
4778e50d392SDave Jiang 	idxd->sva = sva;
4788e50d392SDave Jiang 	idxd->pasid = pasid;
4798e50d392SDave Jiang 	dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
4808e50d392SDave Jiang 	return 0;
4818e50d392SDave Jiang }
4828e50d392SDave Jiang 
4838e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd)
4848e50d392SDave Jiang {
4858e50d392SDave Jiang 
4868e50d392SDave Jiang 	iommu_sva_unbind_device(idxd->sva);
4878e50d392SDave Jiang 	idxd->sva = NULL;
4888e50d392SDave Jiang }
4898e50d392SDave Jiang 
490bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd)
491bfe1d560SDave Jiang {
492bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
493bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
494bfe1d560SDave Jiang 	int rc;
495bfe1d560SDave Jiang 
496bfe1d560SDave Jiang 	dev_dbg(dev, "%s entered and resetting device\n", __func__);
49789e3becdSDave Jiang 	rc = idxd_device_init_reset(idxd);
49889e3becdSDave Jiang 	if (rc < 0)
49989e3becdSDave Jiang 		return rc;
50089e3becdSDave Jiang 
501bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD reset complete\n");
502bfe1d560SDave Jiang 
50303d939c7SDave Jiang 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
504cf5f86a7SDave Jiang 		rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA);
505cf5f86a7SDave Jiang 		if (rc == 0) {
5068e50d392SDave Jiang 			rc = idxd_enable_system_pasid(idxd);
507cf5f86a7SDave Jiang 			if (rc < 0) {
508cf5f86a7SDave Jiang 				iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
5098e50d392SDave Jiang 				dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
510cf5f86a7SDave Jiang 			} else {
5118e50d392SDave Jiang 				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
512cf5f86a7SDave Jiang 			}
513cf5f86a7SDave Jiang 		} else {
514cf5f86a7SDave Jiang 			dev_warn(dev, "Unable to turn on SVA feature.\n");
515cf5f86a7SDave Jiang 		}
51603d939c7SDave Jiang 	} else if (!sva) {
51703d939c7SDave Jiang 		dev_warn(dev, "User forced SVA off via module param.\n");
5188e50d392SDave Jiang 	}
5198e50d392SDave Jiang 
520bfe1d560SDave Jiang 	idxd_read_caps(idxd);
521bfe1d560SDave Jiang 	idxd_read_table_offsets(idxd);
522bfe1d560SDave Jiang 
523bfe1d560SDave Jiang 	rc = idxd_setup_internals(idxd);
524bfe1d560SDave Jiang 	if (rc)
5257c5dd23eSDave Jiang 		goto err;
526bfe1d560SDave Jiang 
5278c66bbdcSDave Jiang 	/* If the configs are readonly, then load them from device */
5288c66bbdcSDave Jiang 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
5298c66bbdcSDave Jiang 		dev_dbg(dev, "Loading RO device config\n");
5308c66bbdcSDave Jiang 		rc = idxd_device_load_config(idxd);
5318c66bbdcSDave Jiang 		if (rc < 0)
5328c66bbdcSDave Jiang 			goto err;
5338c66bbdcSDave Jiang 	}
5348c66bbdcSDave Jiang 
535bfe1d560SDave Jiang 	rc = idxd_setup_interrupts(idxd);
536bfe1d560SDave Jiang 	if (rc)
5377c5dd23eSDave Jiang 		goto err;
538bfe1d560SDave Jiang 
539bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD interrupt setup complete.\n");
540bfe1d560SDave Jiang 
54142d279f9SDave Jiang 	idxd->major = idxd_cdev_get_major(idxd);
54242d279f9SDave Jiang 
543bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
544bfe1d560SDave Jiang 	return 0;
545bfe1d560SDave Jiang 
5467c5dd23eSDave Jiang  err:
5478e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
5488e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
549cf5f86a7SDave Jiang 	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
550bfe1d560SDave Jiang 	return rc;
551bfe1d560SDave Jiang }
552bfe1d560SDave Jiang 
553bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
554bfe1d560SDave Jiang {
555bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
556bfe1d560SDave Jiang 	struct idxd_device *idxd;
557435b512dSDave Jiang 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
558bfe1d560SDave Jiang 	int rc;
559bfe1d560SDave Jiang 
560a39c7cd0SDave Jiang 	rc = pci_enable_device(pdev);
561bfe1d560SDave Jiang 	if (rc)
562bfe1d560SDave Jiang 		return rc;
563bfe1d560SDave Jiang 
5648e50d392SDave Jiang 	dev_dbg(dev, "Alloc IDXD context\n");
565435b512dSDave Jiang 	idxd = idxd_alloc(pdev, data);
566a39c7cd0SDave Jiang 	if (!idxd) {
567a39c7cd0SDave Jiang 		rc = -ENOMEM;
568a39c7cd0SDave Jiang 		goto err_idxd_alloc;
569a39c7cd0SDave Jiang 	}
570bfe1d560SDave Jiang 
5718e50d392SDave Jiang 	dev_dbg(dev, "Mapping BARs\n");
572a39c7cd0SDave Jiang 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
573a39c7cd0SDave Jiang 	if (!idxd->reg_base) {
574a39c7cd0SDave Jiang 		rc = -ENOMEM;
575a39c7cd0SDave Jiang 		goto err_iomap;
576a39c7cd0SDave Jiang 	}
577bfe1d560SDave Jiang 
578bfe1d560SDave Jiang 	dev_dbg(dev, "Set DMA masks\n");
579bfe1d560SDave Jiang 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
580bfe1d560SDave Jiang 	if (rc)
581bfe1d560SDave Jiang 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
582bfe1d560SDave Jiang 	if (rc)
583a39c7cd0SDave Jiang 		goto err;
584bfe1d560SDave Jiang 
585bfe1d560SDave Jiang 	rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
586bfe1d560SDave Jiang 	if (rc)
587bfe1d560SDave Jiang 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
588bfe1d560SDave Jiang 	if (rc)
589a39c7cd0SDave Jiang 		goto err;
590bfe1d560SDave Jiang 
591bfe1d560SDave Jiang 	dev_dbg(dev, "Set PCI master\n");
592bfe1d560SDave Jiang 	pci_set_master(pdev);
593bfe1d560SDave Jiang 	pci_set_drvdata(pdev, idxd);
594bfe1d560SDave Jiang 
595bfe1d560SDave Jiang 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
596bfe1d560SDave Jiang 	rc = idxd_probe(idxd);
597bfe1d560SDave Jiang 	if (rc) {
598bfe1d560SDave Jiang 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
599a39c7cd0SDave Jiang 		goto err;
600bfe1d560SDave Jiang 	}
601bfe1d560SDave Jiang 
60247c16ac2SDave Jiang 	rc = idxd_register_devices(idxd);
603c52ca478SDave Jiang 	if (rc) {
604c52ca478SDave Jiang 		dev_err(dev, "IDXD sysfs setup failed\n");
605a39c7cd0SDave Jiang 		goto err;
606c52ca478SDave Jiang 	}
607c52ca478SDave Jiang 
608c52ca478SDave Jiang 	idxd->state = IDXD_DEV_CONF_READY;
609c52ca478SDave Jiang 
610bfe1d560SDave Jiang 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
611bfe1d560SDave Jiang 		 idxd->hw.version);
612bfe1d560SDave Jiang 
613bfe1d560SDave Jiang 	return 0;
614a39c7cd0SDave Jiang 
615a39c7cd0SDave Jiang  err:
616a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
617a39c7cd0SDave Jiang  err_iomap:
61847c16ac2SDave Jiang 	put_device(&idxd->conf_dev);
619a39c7cd0SDave Jiang  err_idxd_alloc:
620a39c7cd0SDave Jiang 	pci_disable_device(pdev);
621a39c7cd0SDave Jiang 	return rc;
622bfe1d560SDave Jiang }
623bfe1d560SDave Jiang 
6248f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
6258f47d1a5SDave Jiang {
6268f47d1a5SDave Jiang 	struct idxd_desc *desc, *itr;
6278f47d1a5SDave Jiang 	struct llist_node *head;
6288f47d1a5SDave Jiang 
6298f47d1a5SDave Jiang 	head = llist_del_all(&ie->pending_llist);
6308f47d1a5SDave Jiang 	if (!head)
6318f47d1a5SDave Jiang 		return;
6328f47d1a5SDave Jiang 
6338f47d1a5SDave Jiang 	llist_for_each_entry_safe(desc, itr, head, llnode) {
6348f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
6358f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
6368f47d1a5SDave Jiang 	}
6378f47d1a5SDave Jiang }
6388f47d1a5SDave Jiang 
6398f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie)
6408f47d1a5SDave Jiang {
6418f47d1a5SDave Jiang 	struct idxd_desc *desc, *iter;
6428f47d1a5SDave Jiang 
6438f47d1a5SDave Jiang 	list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
6448f47d1a5SDave Jiang 		list_del(&desc->list);
6458f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
6468f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
6478f47d1a5SDave Jiang 	}
6488f47d1a5SDave Jiang }
6498f47d1a5SDave Jiang 
650*5b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd)
651*5b0c68c4SDave Jiang {
652*5b0c68c4SDave Jiang 	struct idxd_wq *wq;
653*5b0c68c4SDave Jiang 	int i;
654*5b0c68c4SDave Jiang 
655*5b0c68c4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
656*5b0c68c4SDave Jiang 		wq = idxd->wqs[i];
657*5b0c68c4SDave Jiang 		if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
658*5b0c68c4SDave Jiang 			idxd_wq_quiesce(wq);
659*5b0c68c4SDave Jiang 	}
660*5b0c68c4SDave Jiang }
661*5b0c68c4SDave Jiang 
662eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd)
663eb15e715SDave Jiang {
664eb15e715SDave Jiang 	struct device *dev = &idxd->pdev->dev;
665eb15e715SDave Jiang 	int i, rc;
666eb15e715SDave Jiang 
667eb15e715SDave Jiang 	for (i = 0; i < idxd->num_wq_irqs; i++) {
668eb15e715SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) {
669eb15e715SDave Jiang 			rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i],
670eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
671eb15e715SDave Jiang 			if (rc < 0)
672eb15e715SDave Jiang 				dev_warn(dev, "irq handle %d release failed\n",
673eb15e715SDave Jiang 					 idxd->int_handles[i]);
674eb15e715SDave Jiang 			else
675eb15e715SDave Jiang 				dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]);
676eb15e715SDave Jiang 		}
677eb15e715SDave Jiang 	}
678eb15e715SDave Jiang }
679eb15e715SDave Jiang 
680bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
681bfe1d560SDave Jiang {
682bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
683bfe1d560SDave Jiang 	int rc, i;
684bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
685bfe1d560SDave Jiang 	int msixcnt = pci_msix_vec_count(pdev);
686bfe1d560SDave Jiang 
687bfe1d560SDave Jiang 	rc = idxd_device_disable(idxd);
688bfe1d560SDave Jiang 	if (rc)
689bfe1d560SDave Jiang 		dev_err(&pdev->dev, "Disabling device failed\n");
690bfe1d560SDave Jiang 
691bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
692bfe1d560SDave Jiang 	idxd_mask_msix_vectors(idxd);
693bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
694bfe1d560SDave Jiang 
695bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
696bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
6975fc8e85fSDave Jiang 		synchronize_irq(irq_entry->vector);
6985fc8e85fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
699bfe1d560SDave Jiang 		if (i == 0)
700bfe1d560SDave Jiang 			continue;
7018f47d1a5SDave Jiang 		idxd_flush_pending_llist(irq_entry);
7028f47d1a5SDave Jiang 		idxd_flush_work_list(irq_entry);
703bfe1d560SDave Jiang 	}
7040d5c10b4SDave Jiang 
7056df0e6c5SDave Jiang 	idxd_msix_perm_clear(idxd);
706eb15e715SDave Jiang 	idxd_release_int_handles(idxd);
7075fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
708a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
709a39c7cd0SDave Jiang 	pci_disable_device(pdev);
7100d5c10b4SDave Jiang 	destroy_workqueue(idxd->wq);
711bfe1d560SDave Jiang }
712bfe1d560SDave Jiang 
713bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
714bfe1d560SDave Jiang {
715bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
716bfe1d560SDave Jiang 
717bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
718bfe1d560SDave Jiang 	idxd_shutdown(pdev);
7198e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
7208e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
72147c16ac2SDave Jiang 	idxd_unregister_devices(idxd);
722cf5f86a7SDave Jiang 	iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
723bfe1d560SDave Jiang }
724bfe1d560SDave Jiang 
725bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
726bfe1d560SDave Jiang 	.name		= DRV_NAME,
727bfe1d560SDave Jiang 	.id_table	= idxd_pci_tbl,
728bfe1d560SDave Jiang 	.probe		= idxd_pci_probe,
729bfe1d560SDave Jiang 	.remove		= idxd_remove,
730bfe1d560SDave Jiang 	.shutdown	= idxd_shutdown,
731bfe1d560SDave Jiang };
732bfe1d560SDave Jiang 
733bfe1d560SDave Jiang static int __init idxd_init_module(void)
734bfe1d560SDave Jiang {
7354b73e4ebSDave Jiang 	int err;
736bfe1d560SDave Jiang 
737bfe1d560SDave Jiang 	/*
7388e50d392SDave Jiang 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
739bfe1d560SDave Jiang 	 * enumerating the device. We can not utilize it.
740bfe1d560SDave Jiang 	 */
741bfe1d560SDave Jiang 	if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) {
742bfe1d560SDave Jiang 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
743bfe1d560SDave Jiang 		return -ENODEV;
744bfe1d560SDave Jiang 	}
745bfe1d560SDave Jiang 
7468e50d392SDave Jiang 	if (!boot_cpu_has(X86_FEATURE_ENQCMD))
7478e50d392SDave Jiang 		pr_warn("Platform does not have ENQCMD(S) support.\n");
7488e50d392SDave Jiang 	else
7498e50d392SDave Jiang 		support_enqcmd = true;
750bfe1d560SDave Jiang 
751c52ca478SDave Jiang 	err = idxd_register_bus_type();
752c52ca478SDave Jiang 	if (err < 0)
753bfe1d560SDave Jiang 		return err;
754bfe1d560SDave Jiang 
755c52ca478SDave Jiang 	err = idxd_register_driver();
756c52ca478SDave Jiang 	if (err < 0)
757c52ca478SDave Jiang 		goto err_idxd_driver_register;
758c52ca478SDave Jiang 
75942d279f9SDave Jiang 	err = idxd_cdev_register();
76042d279f9SDave Jiang 	if (err)
76142d279f9SDave Jiang 		goto err_cdev_register;
76242d279f9SDave Jiang 
763c52ca478SDave Jiang 	err = pci_register_driver(&idxd_pci_driver);
764c52ca478SDave Jiang 	if (err)
765c52ca478SDave Jiang 		goto err_pci_register;
766c52ca478SDave Jiang 
767bfe1d560SDave Jiang 	return 0;
768c52ca478SDave Jiang 
769c52ca478SDave Jiang err_pci_register:
77042d279f9SDave Jiang 	idxd_cdev_remove();
77142d279f9SDave Jiang err_cdev_register:
772c52ca478SDave Jiang 	idxd_unregister_driver();
773c52ca478SDave Jiang err_idxd_driver_register:
774c52ca478SDave Jiang 	idxd_unregister_bus_type();
775c52ca478SDave Jiang 	return err;
776bfe1d560SDave Jiang }
777bfe1d560SDave Jiang module_init(idxd_init_module);
778bfe1d560SDave Jiang 
779bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
780bfe1d560SDave Jiang {
781bfe1d560SDave Jiang 	pci_unregister_driver(&idxd_pci_driver);
78242d279f9SDave Jiang 	idxd_cdev_remove();
783c52ca478SDave Jiang 	idxd_unregister_bus_type();
784bfe1d560SDave Jiang }
785bfe1d560SDave Jiang module_exit(idxd_exit_module);
786