xref: /linux/drivers/dma/idxd/init.c (revision eb15e7154fbfa3e61c777704b2ff28eb3a0d4796)
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 		}
128*eb15e715SDave Jiang 
1295fc8e85fSDave Jiang 		dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
130*eb15e715SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
131*eb15e715SDave Jiang 			/*
132*eb15e715SDave Jiang 			 * The MSIX vector enumeration starts at 1 with vector 0 being the
133*eb15e715SDave Jiang 			 * misc interrupt that handles non I/O completion events. The
134*eb15e715SDave Jiang 			 * interrupt handles are for IMS enumeration on guest. The misc
135*eb15e715SDave Jiang 			 * interrupt vector does not require a handle and therefore we start
136*eb15e715SDave Jiang 			 * the int_handles at index 0. Since 'i' starts at 1, the first
137*eb15e715SDave Jiang 			 * int_handles index will be 0.
138*eb15e715SDave Jiang 			 */
139*eb15e715SDave Jiang 			rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1],
140*eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
141*eb15e715SDave Jiang 			if (rc < 0) {
142*eb15e715SDave Jiang 				free_irq(irq_entry->vector, irq_entry);
143*eb15e715SDave Jiang 				goto err_wq_irqs;
144*eb15e715SDave Jiang 			}
145*eb15e715SDave Jiang 			dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]);
146*eb15e715SDave 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);
157*eb15e715SDave Jiang 		if (i != 0)
158*eb15e715SDave Jiang 			idxd_device_release_int_handle(idxd,
159*eb15e715SDave 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 
312*eb15e715SDave Jiang 	if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
313*eb15e715SDave Jiang 		idxd->int_handles = devm_kcalloc(dev, idxd->max_wqs, sizeof(int), GFP_KERNEL);
314*eb15e715SDave Jiang 		if (!idxd->int_handles)
315*eb15e715SDave Jiang 			return -ENOMEM;
316*eb15e715SDave Jiang 	}
317*eb15e715SDave Jiang 
3187c5dd23eSDave Jiang 	rc = idxd_setup_wqs(idxd);
3197c5dd23eSDave Jiang 	if (rc < 0)
320*eb15e715SDave 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);
347*eb15e715SDave Jiang  err_wqs:
348*eb15e715SDave 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);
377*eb15e715SDave Jiang 
378*eb15e715SDave Jiang 	if (idxd->hw.gen_cap.cmd_cap) {
379*eb15e715SDave Jiang 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
380*eb15e715SDave Jiang 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
381*eb15e715SDave Jiang 	}
382*eb15e715SDave 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) {
5048e50d392SDave Jiang 		rc = idxd_enable_system_pasid(idxd);
5058e50d392SDave Jiang 		if (rc < 0)
5068e50d392SDave Jiang 			dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
5078e50d392SDave Jiang 		else
5088e50d392SDave Jiang 			set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
50903d939c7SDave Jiang 	} else if (!sva) {
51003d939c7SDave Jiang 		dev_warn(dev, "User forced SVA off via module param.\n");
5118e50d392SDave Jiang 	}
5128e50d392SDave Jiang 
513bfe1d560SDave Jiang 	idxd_read_caps(idxd);
514bfe1d560SDave Jiang 	idxd_read_table_offsets(idxd);
515bfe1d560SDave Jiang 
516bfe1d560SDave Jiang 	rc = idxd_setup_internals(idxd);
517bfe1d560SDave Jiang 	if (rc)
5187c5dd23eSDave Jiang 		goto err;
519bfe1d560SDave Jiang 
5208c66bbdcSDave Jiang 	/* If the configs are readonly, then load them from device */
5218c66bbdcSDave Jiang 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
5228c66bbdcSDave Jiang 		dev_dbg(dev, "Loading RO device config\n");
5238c66bbdcSDave Jiang 		rc = idxd_device_load_config(idxd);
5248c66bbdcSDave Jiang 		if (rc < 0)
5258c66bbdcSDave Jiang 			goto err;
5268c66bbdcSDave Jiang 	}
5278c66bbdcSDave Jiang 
528bfe1d560SDave Jiang 	rc = idxd_setup_interrupts(idxd);
529bfe1d560SDave Jiang 	if (rc)
5307c5dd23eSDave Jiang 		goto err;
531bfe1d560SDave Jiang 
532bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD interrupt setup complete.\n");
533bfe1d560SDave Jiang 
53442d279f9SDave Jiang 	idxd->major = idxd_cdev_get_major(idxd);
53542d279f9SDave Jiang 
536bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
537bfe1d560SDave Jiang 	return 0;
538bfe1d560SDave Jiang 
5397c5dd23eSDave Jiang  err:
5408e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
5418e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
542bfe1d560SDave Jiang 	return rc;
543bfe1d560SDave Jiang }
544bfe1d560SDave Jiang 
545bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
546bfe1d560SDave Jiang {
547bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
548bfe1d560SDave Jiang 	struct idxd_device *idxd;
549435b512dSDave Jiang 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
550bfe1d560SDave Jiang 	int rc;
551bfe1d560SDave Jiang 
552a39c7cd0SDave Jiang 	rc = pci_enable_device(pdev);
553bfe1d560SDave Jiang 	if (rc)
554bfe1d560SDave Jiang 		return rc;
555bfe1d560SDave Jiang 
5568e50d392SDave Jiang 	dev_dbg(dev, "Alloc IDXD context\n");
557435b512dSDave Jiang 	idxd = idxd_alloc(pdev, data);
558a39c7cd0SDave Jiang 	if (!idxd) {
559a39c7cd0SDave Jiang 		rc = -ENOMEM;
560a39c7cd0SDave Jiang 		goto err_idxd_alloc;
561a39c7cd0SDave Jiang 	}
562bfe1d560SDave Jiang 
5638e50d392SDave Jiang 	dev_dbg(dev, "Mapping BARs\n");
564a39c7cd0SDave Jiang 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
565a39c7cd0SDave Jiang 	if (!idxd->reg_base) {
566a39c7cd0SDave Jiang 		rc = -ENOMEM;
567a39c7cd0SDave Jiang 		goto err_iomap;
568a39c7cd0SDave Jiang 	}
569bfe1d560SDave Jiang 
570bfe1d560SDave Jiang 	dev_dbg(dev, "Set DMA masks\n");
571bfe1d560SDave Jiang 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
572bfe1d560SDave Jiang 	if (rc)
573bfe1d560SDave Jiang 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
574bfe1d560SDave Jiang 	if (rc)
575a39c7cd0SDave Jiang 		goto err;
576bfe1d560SDave Jiang 
577bfe1d560SDave Jiang 	rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
578bfe1d560SDave Jiang 	if (rc)
579bfe1d560SDave Jiang 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
580bfe1d560SDave Jiang 	if (rc)
581a39c7cd0SDave Jiang 		goto err;
582bfe1d560SDave Jiang 
583bfe1d560SDave Jiang 	dev_dbg(dev, "Set PCI master\n");
584bfe1d560SDave Jiang 	pci_set_master(pdev);
585bfe1d560SDave Jiang 	pci_set_drvdata(pdev, idxd);
586bfe1d560SDave Jiang 
587bfe1d560SDave Jiang 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
588bfe1d560SDave Jiang 	rc = idxd_probe(idxd);
589bfe1d560SDave Jiang 	if (rc) {
590bfe1d560SDave Jiang 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
591a39c7cd0SDave Jiang 		goto err;
592bfe1d560SDave Jiang 	}
593bfe1d560SDave Jiang 
59447c16ac2SDave Jiang 	rc = idxd_register_devices(idxd);
595c52ca478SDave Jiang 	if (rc) {
596c52ca478SDave Jiang 		dev_err(dev, "IDXD sysfs setup failed\n");
597a39c7cd0SDave Jiang 		goto err;
598c52ca478SDave Jiang 	}
599c52ca478SDave Jiang 
600c52ca478SDave Jiang 	idxd->state = IDXD_DEV_CONF_READY;
601c52ca478SDave Jiang 
602bfe1d560SDave Jiang 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
603bfe1d560SDave Jiang 		 idxd->hw.version);
604bfe1d560SDave Jiang 
605bfe1d560SDave Jiang 	return 0;
606a39c7cd0SDave Jiang 
607a39c7cd0SDave Jiang  err:
608a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
609a39c7cd0SDave Jiang  err_iomap:
61047c16ac2SDave Jiang 	put_device(&idxd->conf_dev);
611a39c7cd0SDave Jiang  err_idxd_alloc:
612a39c7cd0SDave Jiang 	pci_disable_device(pdev);
613a39c7cd0SDave Jiang 	return rc;
614bfe1d560SDave Jiang }
615bfe1d560SDave Jiang 
6168f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
6178f47d1a5SDave Jiang {
6188f47d1a5SDave Jiang 	struct idxd_desc *desc, *itr;
6198f47d1a5SDave Jiang 	struct llist_node *head;
6208f47d1a5SDave Jiang 
6218f47d1a5SDave Jiang 	head = llist_del_all(&ie->pending_llist);
6228f47d1a5SDave Jiang 	if (!head)
6238f47d1a5SDave Jiang 		return;
6248f47d1a5SDave Jiang 
6258f47d1a5SDave Jiang 	llist_for_each_entry_safe(desc, itr, head, llnode) {
6268f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
6278f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
6288f47d1a5SDave Jiang 	}
6298f47d1a5SDave Jiang }
6308f47d1a5SDave Jiang 
6318f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie)
6328f47d1a5SDave Jiang {
6338f47d1a5SDave Jiang 	struct idxd_desc *desc, *iter;
6348f47d1a5SDave Jiang 
6358f47d1a5SDave Jiang 	list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
6368f47d1a5SDave Jiang 		list_del(&desc->list);
6378f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
6388f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
6398f47d1a5SDave Jiang 	}
6408f47d1a5SDave Jiang }
6418f47d1a5SDave Jiang 
642*eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd)
643*eb15e715SDave Jiang {
644*eb15e715SDave Jiang 	struct device *dev = &idxd->pdev->dev;
645*eb15e715SDave Jiang 	int i, rc;
646*eb15e715SDave Jiang 
647*eb15e715SDave Jiang 	for (i = 0; i < idxd->num_wq_irqs; i++) {
648*eb15e715SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) {
649*eb15e715SDave Jiang 			rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i],
650*eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
651*eb15e715SDave Jiang 			if (rc < 0)
652*eb15e715SDave Jiang 				dev_warn(dev, "irq handle %d release failed\n",
653*eb15e715SDave Jiang 					 idxd->int_handles[i]);
654*eb15e715SDave Jiang 			else
655*eb15e715SDave Jiang 				dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]);
656*eb15e715SDave Jiang 		}
657*eb15e715SDave Jiang 	}
658*eb15e715SDave Jiang }
659*eb15e715SDave Jiang 
660bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
661bfe1d560SDave Jiang {
662bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
663bfe1d560SDave Jiang 	int rc, i;
664bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
665bfe1d560SDave Jiang 	int msixcnt = pci_msix_vec_count(pdev);
666bfe1d560SDave Jiang 
667bfe1d560SDave Jiang 	rc = idxd_device_disable(idxd);
668bfe1d560SDave Jiang 	if (rc)
669bfe1d560SDave Jiang 		dev_err(&pdev->dev, "Disabling device failed\n");
670bfe1d560SDave Jiang 
671bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
672bfe1d560SDave Jiang 	idxd_mask_msix_vectors(idxd);
673bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
674bfe1d560SDave Jiang 
675bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
676bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
6775fc8e85fSDave Jiang 		synchronize_irq(irq_entry->vector);
6785fc8e85fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
679bfe1d560SDave Jiang 		if (i == 0)
680bfe1d560SDave Jiang 			continue;
6818f47d1a5SDave Jiang 		idxd_flush_pending_llist(irq_entry);
6828f47d1a5SDave Jiang 		idxd_flush_work_list(irq_entry);
683bfe1d560SDave Jiang 	}
6840d5c10b4SDave Jiang 
6856df0e6c5SDave Jiang 	idxd_msix_perm_clear(idxd);
686*eb15e715SDave Jiang 	idxd_release_int_handles(idxd);
6875fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
688a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
689a39c7cd0SDave Jiang 	pci_disable_device(pdev);
6900d5c10b4SDave Jiang 	destroy_workqueue(idxd->wq);
691bfe1d560SDave Jiang }
692bfe1d560SDave Jiang 
693bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
694bfe1d560SDave Jiang {
695bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
696bfe1d560SDave Jiang 
697bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
698bfe1d560SDave Jiang 	idxd_shutdown(pdev);
6998e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
7008e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
70147c16ac2SDave Jiang 	idxd_unregister_devices(idxd);
702bfe1d560SDave Jiang }
703bfe1d560SDave Jiang 
704bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
705bfe1d560SDave Jiang 	.name		= DRV_NAME,
706bfe1d560SDave Jiang 	.id_table	= idxd_pci_tbl,
707bfe1d560SDave Jiang 	.probe		= idxd_pci_probe,
708bfe1d560SDave Jiang 	.remove		= idxd_remove,
709bfe1d560SDave Jiang 	.shutdown	= idxd_shutdown,
710bfe1d560SDave Jiang };
711bfe1d560SDave Jiang 
712bfe1d560SDave Jiang static int __init idxd_init_module(void)
713bfe1d560SDave Jiang {
7144b73e4ebSDave Jiang 	int err;
715bfe1d560SDave Jiang 
716bfe1d560SDave Jiang 	/*
7178e50d392SDave Jiang 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
718bfe1d560SDave Jiang 	 * enumerating the device. We can not utilize it.
719bfe1d560SDave Jiang 	 */
720bfe1d560SDave Jiang 	if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) {
721bfe1d560SDave Jiang 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
722bfe1d560SDave Jiang 		return -ENODEV;
723bfe1d560SDave Jiang 	}
724bfe1d560SDave Jiang 
7258e50d392SDave Jiang 	if (!boot_cpu_has(X86_FEATURE_ENQCMD))
7268e50d392SDave Jiang 		pr_warn("Platform does not have ENQCMD(S) support.\n");
7278e50d392SDave Jiang 	else
7288e50d392SDave Jiang 		support_enqcmd = true;
729bfe1d560SDave Jiang 
730c52ca478SDave Jiang 	err = idxd_register_bus_type();
731c52ca478SDave Jiang 	if (err < 0)
732bfe1d560SDave Jiang 		return err;
733bfe1d560SDave Jiang 
734c52ca478SDave Jiang 	err = idxd_register_driver();
735c52ca478SDave Jiang 	if (err < 0)
736c52ca478SDave Jiang 		goto err_idxd_driver_register;
737c52ca478SDave Jiang 
73842d279f9SDave Jiang 	err = idxd_cdev_register();
73942d279f9SDave Jiang 	if (err)
74042d279f9SDave Jiang 		goto err_cdev_register;
74142d279f9SDave Jiang 
742c52ca478SDave Jiang 	err = pci_register_driver(&idxd_pci_driver);
743c52ca478SDave Jiang 	if (err)
744c52ca478SDave Jiang 		goto err_pci_register;
745c52ca478SDave Jiang 
746bfe1d560SDave Jiang 	return 0;
747c52ca478SDave Jiang 
748c52ca478SDave Jiang err_pci_register:
74942d279f9SDave Jiang 	idxd_cdev_remove();
75042d279f9SDave Jiang err_cdev_register:
751c52ca478SDave Jiang 	idxd_unregister_driver();
752c52ca478SDave Jiang err_idxd_driver_register:
753c52ca478SDave Jiang 	idxd_unregister_bus_type();
754c52ca478SDave Jiang 	return err;
755bfe1d560SDave Jiang }
756bfe1d560SDave Jiang module_init(idxd_init_module);
757bfe1d560SDave Jiang 
758bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
759bfe1d560SDave Jiang {
760bfe1d560SDave Jiang 	pci_unregister_driver(&idxd_pci_driver);
76142d279f9SDave Jiang 	idxd_cdev_remove();
762c52ca478SDave Jiang 	idxd_unregister_bus_type();
763bfe1d560SDave Jiang }
764bfe1d560SDave Jiang module_exit(idxd_exit_module);
765