xref: /linux/drivers/dma/idxd/init.c (revision 0bde4444ec44b8e64bbd4af72fcaef58bcdbd4ce)
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"
24*0bde4444STom 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)) {
314eb15e715SDave Jiang 		idxd->int_handles = devm_kcalloc(dev, idxd->max_wqs, sizeof(int), GFP_KERNEL);
315eb15e715SDave Jiang 		if (!idxd->int_handles)
316eb15e715SDave Jiang 			return -ENOMEM;
317eb15e715SDave Jiang 	}
318eb15e715SDave Jiang 
3197c5dd23eSDave Jiang 	rc = idxd_setup_wqs(idxd);
3207c5dd23eSDave Jiang 	if (rc < 0)
321eb15e715SDave Jiang 		goto err_wqs;
3227c5dd23eSDave Jiang 
32375b91130SDave Jiang 	rc = idxd_setup_engines(idxd);
32475b91130SDave Jiang 	if (rc < 0)
32575b91130SDave Jiang 		goto err_engine;
32675b91130SDave Jiang 
327defe49f9SDave Jiang 	rc = idxd_setup_groups(idxd);
328defe49f9SDave Jiang 	if (rc < 0)
329defe49f9SDave Jiang 		goto err_group;
330bfe1d560SDave Jiang 
3310d5c10b4SDave Jiang 	idxd->wq = create_workqueue(dev_name(dev));
3327c5dd23eSDave Jiang 	if (!idxd->wq) {
3337c5dd23eSDave Jiang 		rc = -ENOMEM;
334defe49f9SDave Jiang 		goto err_wkq_create;
3357c5dd23eSDave Jiang 	}
3360d5c10b4SDave Jiang 
337bfe1d560SDave Jiang 	return 0;
3387c5dd23eSDave Jiang 
339defe49f9SDave Jiang  err_wkq_create:
340defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
341defe49f9SDave Jiang 		put_device(&idxd->groups[i]->conf_dev);
342defe49f9SDave Jiang  err_group:
34375b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
34475b91130SDave Jiang 		put_device(&idxd->engines[i]->conf_dev);
34575b91130SDave Jiang  err_engine:
3467c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
3477c5dd23eSDave Jiang 		put_device(&idxd->wqs[i]->conf_dev);
348eb15e715SDave Jiang  err_wqs:
349eb15e715SDave Jiang 	kfree(idxd->int_handles);
3507c5dd23eSDave Jiang 	return rc;
351bfe1d560SDave Jiang }
352bfe1d560SDave Jiang 
353bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd)
354bfe1d560SDave Jiang {
355bfe1d560SDave Jiang 	union offsets_reg offsets;
356bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
357bfe1d560SDave Jiang 
358bfe1d560SDave Jiang 	offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
3592f8417a9SDave Jiang 	offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
3602f8417a9SDave Jiang 	idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
361bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
3622f8417a9SDave Jiang 	idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
3632f8417a9SDave Jiang 	dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
3642f8417a9SDave Jiang 	idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
3652f8417a9SDave Jiang 	dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
3662f8417a9SDave Jiang 	idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
367bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
368bfe1d560SDave Jiang }
369bfe1d560SDave Jiang 
370bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd)
371bfe1d560SDave Jiang {
372bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
373bfe1d560SDave Jiang 	int i;
374bfe1d560SDave Jiang 
375bfe1d560SDave Jiang 	/* reading generic capabilities */
376bfe1d560SDave Jiang 	idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
377bfe1d560SDave Jiang 	dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
378eb15e715SDave Jiang 
379eb15e715SDave Jiang 	if (idxd->hw.gen_cap.cmd_cap) {
380eb15e715SDave Jiang 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
381eb15e715SDave Jiang 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
382eb15e715SDave Jiang 	}
383eb15e715SDave Jiang 
384bfe1d560SDave Jiang 	idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
385bfe1d560SDave Jiang 	dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
386bfe1d560SDave Jiang 	idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
387bfe1d560SDave Jiang 	dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
388bfe1d560SDave Jiang 	if (idxd->hw.gen_cap.config_en)
389bfe1d560SDave Jiang 		set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
390bfe1d560SDave Jiang 
391bfe1d560SDave Jiang 	/* reading group capabilities */
392bfe1d560SDave Jiang 	idxd->hw.group_cap.bits =
393bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
394bfe1d560SDave Jiang 	dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
395bfe1d560SDave Jiang 	idxd->max_groups = idxd->hw.group_cap.num_groups;
396bfe1d560SDave Jiang 	dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
397bfe1d560SDave Jiang 	idxd->max_tokens = idxd->hw.group_cap.total_tokens;
398bfe1d560SDave Jiang 	dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
399c52ca478SDave Jiang 	idxd->nr_tokens = idxd->max_tokens;
400bfe1d560SDave Jiang 
401bfe1d560SDave Jiang 	/* read engine capabilities */
402bfe1d560SDave Jiang 	idxd->hw.engine_cap.bits =
403bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
404bfe1d560SDave Jiang 	dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
405bfe1d560SDave Jiang 	idxd->max_engines = idxd->hw.engine_cap.num_engines;
406bfe1d560SDave Jiang 	dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
407bfe1d560SDave Jiang 
408bfe1d560SDave Jiang 	/* read workqueue capabilities */
409bfe1d560SDave Jiang 	idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
410bfe1d560SDave Jiang 	dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
411bfe1d560SDave Jiang 	idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
412bfe1d560SDave Jiang 	dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
413bfe1d560SDave Jiang 	idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
414bfe1d560SDave Jiang 	dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
415d98793b5SDave Jiang 	idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
416d98793b5SDave Jiang 	dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
417bfe1d560SDave Jiang 
418bfe1d560SDave Jiang 	/* reading operation capabilities */
419bfe1d560SDave Jiang 	for (i = 0; i < 4; i++) {
420bfe1d560SDave Jiang 		idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
421bfe1d560SDave Jiang 				IDXD_OPCAP_OFFSET + i * sizeof(u64));
422bfe1d560SDave Jiang 		dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
423bfe1d560SDave Jiang 	}
424bfe1d560SDave Jiang }
425bfe1d560SDave Jiang 
426435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
427bfe1d560SDave Jiang {
428bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
429bfe1d560SDave Jiang 	struct idxd_device *idxd;
43047c16ac2SDave Jiang 	int rc;
431bfe1d560SDave Jiang 
43247c16ac2SDave Jiang 	idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
433bfe1d560SDave Jiang 	if (!idxd)
434bfe1d560SDave Jiang 		return NULL;
435bfe1d560SDave Jiang 
436bfe1d560SDave Jiang 	idxd->pdev = pdev;
437435b512dSDave Jiang 	idxd->data = data;
4384b73e4ebSDave Jiang 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
43947c16ac2SDave Jiang 	if (idxd->id < 0)
44047c16ac2SDave Jiang 		return NULL;
44147c16ac2SDave Jiang 
44247c16ac2SDave Jiang 	device_initialize(&idxd->conf_dev);
44347c16ac2SDave Jiang 	idxd->conf_dev.parent = dev;
4444b73e4ebSDave Jiang 	idxd->conf_dev.bus = &dsa_bus_type;
445435b512dSDave Jiang 	idxd->conf_dev.type = idxd->data->dev_type;
446435b512dSDave Jiang 	rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
44747c16ac2SDave Jiang 	if (rc < 0) {
44847c16ac2SDave Jiang 		put_device(&idxd->conf_dev);
44947c16ac2SDave Jiang 		return NULL;
45047c16ac2SDave Jiang 	}
45147c16ac2SDave Jiang 
452bfe1d560SDave Jiang 	spin_lock_init(&idxd->dev_lock);
45353b2ee7fSDave Jiang 	spin_lock_init(&idxd->cmd_lock);
454bfe1d560SDave Jiang 
455bfe1d560SDave Jiang 	return idxd;
456bfe1d560SDave Jiang }
457bfe1d560SDave Jiang 
4588e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd)
4598e50d392SDave Jiang {
4608e50d392SDave Jiang 	int flags;
4618e50d392SDave Jiang 	unsigned int pasid;
4628e50d392SDave Jiang 	struct iommu_sva *sva;
4638e50d392SDave Jiang 
4648e50d392SDave Jiang 	flags = SVM_FLAG_SUPERVISOR_MODE;
4658e50d392SDave Jiang 
4668e50d392SDave Jiang 	sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
4678e50d392SDave Jiang 	if (IS_ERR(sva)) {
4688e50d392SDave Jiang 		dev_warn(&idxd->pdev->dev,
4698e50d392SDave Jiang 			 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
4708e50d392SDave Jiang 		return PTR_ERR(sva);
4718e50d392SDave Jiang 	}
4728e50d392SDave Jiang 
4738e50d392SDave Jiang 	pasid = iommu_sva_get_pasid(sva);
4748e50d392SDave Jiang 	if (pasid == IOMMU_PASID_INVALID) {
4758e50d392SDave Jiang 		iommu_sva_unbind_device(sva);
4768e50d392SDave Jiang 		return -ENODEV;
4778e50d392SDave Jiang 	}
4788e50d392SDave Jiang 
4798e50d392SDave Jiang 	idxd->sva = sva;
4808e50d392SDave Jiang 	idxd->pasid = pasid;
4818e50d392SDave Jiang 	dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
4828e50d392SDave Jiang 	return 0;
4838e50d392SDave Jiang }
4848e50d392SDave Jiang 
4858e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd)
4868e50d392SDave Jiang {
4878e50d392SDave Jiang 
4888e50d392SDave Jiang 	iommu_sva_unbind_device(idxd->sva);
4898e50d392SDave Jiang 	idxd->sva = NULL;
4908e50d392SDave Jiang }
4918e50d392SDave Jiang 
492bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd)
493bfe1d560SDave Jiang {
494bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
495bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
496bfe1d560SDave Jiang 	int rc;
497bfe1d560SDave Jiang 
498bfe1d560SDave Jiang 	dev_dbg(dev, "%s entered and resetting device\n", __func__);
49989e3becdSDave Jiang 	rc = idxd_device_init_reset(idxd);
50089e3becdSDave Jiang 	if (rc < 0)
50189e3becdSDave Jiang 		return rc;
50289e3becdSDave Jiang 
503bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD reset complete\n");
504bfe1d560SDave Jiang 
50503d939c7SDave Jiang 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
506cf5f86a7SDave Jiang 		rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA);
507cf5f86a7SDave Jiang 		if (rc == 0) {
5088e50d392SDave Jiang 			rc = idxd_enable_system_pasid(idxd);
509cf5f86a7SDave Jiang 			if (rc < 0) {
510cf5f86a7SDave Jiang 				iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
5118e50d392SDave Jiang 				dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
512cf5f86a7SDave Jiang 			} else {
5138e50d392SDave Jiang 				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
514cf5f86a7SDave Jiang 			}
515cf5f86a7SDave Jiang 		} else {
516cf5f86a7SDave Jiang 			dev_warn(dev, "Unable to turn on SVA feature.\n");
517cf5f86a7SDave Jiang 		}
51803d939c7SDave Jiang 	} else if (!sva) {
51903d939c7SDave Jiang 		dev_warn(dev, "User forced SVA off via module param.\n");
5208e50d392SDave Jiang 	}
5218e50d392SDave Jiang 
522bfe1d560SDave Jiang 	idxd_read_caps(idxd);
523bfe1d560SDave Jiang 	idxd_read_table_offsets(idxd);
524bfe1d560SDave Jiang 
525bfe1d560SDave Jiang 	rc = idxd_setup_internals(idxd);
526bfe1d560SDave Jiang 	if (rc)
5277c5dd23eSDave Jiang 		goto err;
528bfe1d560SDave Jiang 
5298c66bbdcSDave Jiang 	/* If the configs are readonly, then load them from device */
5308c66bbdcSDave Jiang 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
5318c66bbdcSDave Jiang 		dev_dbg(dev, "Loading RO device config\n");
5328c66bbdcSDave Jiang 		rc = idxd_device_load_config(idxd);
5338c66bbdcSDave Jiang 		if (rc < 0)
5348c66bbdcSDave Jiang 			goto err;
5358c66bbdcSDave Jiang 	}
5368c66bbdcSDave Jiang 
537bfe1d560SDave Jiang 	rc = idxd_setup_interrupts(idxd);
538bfe1d560SDave Jiang 	if (rc)
5397c5dd23eSDave Jiang 		goto err;
540bfe1d560SDave Jiang 
541bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD interrupt setup complete.\n");
542bfe1d560SDave Jiang 
54342d279f9SDave Jiang 	idxd->major = idxd_cdev_get_major(idxd);
54442d279f9SDave Jiang 
545*0bde4444STom Zanussi 	rc = perfmon_pmu_init(idxd);
546*0bde4444STom Zanussi 	if (rc < 0)
547*0bde4444STom Zanussi 		dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
548*0bde4444STom Zanussi 
549bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
550bfe1d560SDave Jiang 	return 0;
551bfe1d560SDave Jiang 
5527c5dd23eSDave Jiang  err:
5538e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
5548e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
555cf5f86a7SDave Jiang 	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
556bfe1d560SDave Jiang 	return rc;
557bfe1d560SDave Jiang }
558bfe1d560SDave Jiang 
559bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
560bfe1d560SDave Jiang {
561bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
562bfe1d560SDave Jiang 	struct idxd_device *idxd;
563435b512dSDave Jiang 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
564bfe1d560SDave Jiang 	int rc;
565bfe1d560SDave Jiang 
566a39c7cd0SDave Jiang 	rc = pci_enable_device(pdev);
567bfe1d560SDave Jiang 	if (rc)
568bfe1d560SDave Jiang 		return rc;
569bfe1d560SDave Jiang 
5708e50d392SDave Jiang 	dev_dbg(dev, "Alloc IDXD context\n");
571435b512dSDave Jiang 	idxd = idxd_alloc(pdev, data);
572a39c7cd0SDave Jiang 	if (!idxd) {
573a39c7cd0SDave Jiang 		rc = -ENOMEM;
574a39c7cd0SDave Jiang 		goto err_idxd_alloc;
575a39c7cd0SDave Jiang 	}
576bfe1d560SDave Jiang 
5778e50d392SDave Jiang 	dev_dbg(dev, "Mapping BARs\n");
578a39c7cd0SDave Jiang 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
579a39c7cd0SDave Jiang 	if (!idxd->reg_base) {
580a39c7cd0SDave Jiang 		rc = -ENOMEM;
581a39c7cd0SDave Jiang 		goto err_iomap;
582a39c7cd0SDave Jiang 	}
583bfe1d560SDave Jiang 
584bfe1d560SDave Jiang 	dev_dbg(dev, "Set DMA masks\n");
585bfe1d560SDave Jiang 	rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
586bfe1d560SDave Jiang 	if (rc)
587bfe1d560SDave Jiang 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
588bfe1d560SDave Jiang 	if (rc)
589a39c7cd0SDave Jiang 		goto err;
590bfe1d560SDave Jiang 
591bfe1d560SDave Jiang 	rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
592bfe1d560SDave Jiang 	if (rc)
593bfe1d560SDave Jiang 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
594bfe1d560SDave Jiang 	if (rc)
595a39c7cd0SDave Jiang 		goto err;
596bfe1d560SDave Jiang 
597bfe1d560SDave Jiang 	dev_dbg(dev, "Set PCI master\n");
598bfe1d560SDave Jiang 	pci_set_master(pdev);
599bfe1d560SDave Jiang 	pci_set_drvdata(pdev, idxd);
600bfe1d560SDave Jiang 
601bfe1d560SDave Jiang 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
602bfe1d560SDave Jiang 	rc = idxd_probe(idxd);
603bfe1d560SDave Jiang 	if (rc) {
604bfe1d560SDave Jiang 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
605a39c7cd0SDave Jiang 		goto err;
606bfe1d560SDave Jiang 	}
607bfe1d560SDave Jiang 
60847c16ac2SDave Jiang 	rc = idxd_register_devices(idxd);
609c52ca478SDave Jiang 	if (rc) {
610c52ca478SDave Jiang 		dev_err(dev, "IDXD sysfs setup failed\n");
611a39c7cd0SDave Jiang 		goto err;
612c52ca478SDave Jiang 	}
613c52ca478SDave Jiang 
614c52ca478SDave Jiang 	idxd->state = IDXD_DEV_CONF_READY;
615c52ca478SDave Jiang 
616bfe1d560SDave Jiang 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
617bfe1d560SDave Jiang 		 idxd->hw.version);
618bfe1d560SDave Jiang 
619bfe1d560SDave Jiang 	return 0;
620a39c7cd0SDave Jiang 
621a39c7cd0SDave Jiang  err:
622a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
623a39c7cd0SDave Jiang  err_iomap:
62447c16ac2SDave Jiang 	put_device(&idxd->conf_dev);
625a39c7cd0SDave Jiang  err_idxd_alloc:
626a39c7cd0SDave Jiang 	pci_disable_device(pdev);
627a39c7cd0SDave Jiang 	return rc;
628bfe1d560SDave Jiang }
629bfe1d560SDave Jiang 
6308f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
6318f47d1a5SDave Jiang {
6328f47d1a5SDave Jiang 	struct idxd_desc *desc, *itr;
6338f47d1a5SDave Jiang 	struct llist_node *head;
6348f47d1a5SDave Jiang 
6358f47d1a5SDave Jiang 	head = llist_del_all(&ie->pending_llist);
6368f47d1a5SDave Jiang 	if (!head)
6378f47d1a5SDave Jiang 		return;
6388f47d1a5SDave Jiang 
6398f47d1a5SDave Jiang 	llist_for_each_entry_safe(desc, itr, head, llnode) {
6408f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
6418f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
6428f47d1a5SDave Jiang 	}
6438f47d1a5SDave Jiang }
6448f47d1a5SDave Jiang 
6458f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie)
6468f47d1a5SDave Jiang {
6478f47d1a5SDave Jiang 	struct idxd_desc *desc, *iter;
6488f47d1a5SDave Jiang 
6498f47d1a5SDave Jiang 	list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
6508f47d1a5SDave Jiang 		list_del(&desc->list);
6518f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
6528f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
6538f47d1a5SDave Jiang 	}
6548f47d1a5SDave Jiang }
6558f47d1a5SDave Jiang 
6565b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd)
6575b0c68c4SDave Jiang {
6585b0c68c4SDave Jiang 	struct idxd_wq *wq;
6595b0c68c4SDave Jiang 	int i;
6605b0c68c4SDave Jiang 
6615b0c68c4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
6625b0c68c4SDave Jiang 		wq = idxd->wqs[i];
6635b0c68c4SDave Jiang 		if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
6645b0c68c4SDave Jiang 			idxd_wq_quiesce(wq);
6655b0c68c4SDave Jiang 	}
6665b0c68c4SDave Jiang }
6675b0c68c4SDave Jiang 
668eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd)
669eb15e715SDave Jiang {
670eb15e715SDave Jiang 	struct device *dev = &idxd->pdev->dev;
671eb15e715SDave Jiang 	int i, rc;
672eb15e715SDave Jiang 
673eb15e715SDave Jiang 	for (i = 0; i < idxd->num_wq_irqs; i++) {
674eb15e715SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) {
675eb15e715SDave Jiang 			rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i],
676eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
677eb15e715SDave Jiang 			if (rc < 0)
678eb15e715SDave Jiang 				dev_warn(dev, "irq handle %d release failed\n",
679eb15e715SDave Jiang 					 idxd->int_handles[i]);
680eb15e715SDave Jiang 			else
681eb15e715SDave Jiang 				dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]);
682eb15e715SDave Jiang 		}
683eb15e715SDave Jiang 	}
684eb15e715SDave Jiang }
685eb15e715SDave Jiang 
686bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
687bfe1d560SDave Jiang {
688bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
689bfe1d560SDave Jiang 	int rc, i;
690bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
691bfe1d560SDave Jiang 	int msixcnt = pci_msix_vec_count(pdev);
692bfe1d560SDave Jiang 
693bfe1d560SDave Jiang 	rc = idxd_device_disable(idxd);
694bfe1d560SDave Jiang 	if (rc)
695bfe1d560SDave Jiang 		dev_err(&pdev->dev, "Disabling device failed\n");
696bfe1d560SDave Jiang 
697bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
698bfe1d560SDave Jiang 	idxd_mask_msix_vectors(idxd);
699bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
700bfe1d560SDave Jiang 
701bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
702bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
7035fc8e85fSDave Jiang 		synchronize_irq(irq_entry->vector);
7045fc8e85fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
705bfe1d560SDave Jiang 		if (i == 0)
706bfe1d560SDave Jiang 			continue;
7078f47d1a5SDave Jiang 		idxd_flush_pending_llist(irq_entry);
7088f47d1a5SDave Jiang 		idxd_flush_work_list(irq_entry);
709bfe1d560SDave Jiang 	}
7100d5c10b4SDave Jiang 
7116df0e6c5SDave Jiang 	idxd_msix_perm_clear(idxd);
712eb15e715SDave Jiang 	idxd_release_int_handles(idxd);
7135fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
714a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
715a39c7cd0SDave Jiang 	pci_disable_device(pdev);
7160d5c10b4SDave Jiang 	destroy_workqueue(idxd->wq);
717bfe1d560SDave Jiang }
718bfe1d560SDave Jiang 
719bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
720bfe1d560SDave Jiang {
721bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
722bfe1d560SDave Jiang 
723bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
724bfe1d560SDave Jiang 	idxd_shutdown(pdev);
7258e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
7268e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
72747c16ac2SDave Jiang 	idxd_unregister_devices(idxd);
728*0bde4444STom Zanussi 	perfmon_pmu_remove(idxd);
729cf5f86a7SDave Jiang 	iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
730bfe1d560SDave Jiang }
731bfe1d560SDave Jiang 
732bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
733bfe1d560SDave Jiang 	.name		= DRV_NAME,
734bfe1d560SDave Jiang 	.id_table	= idxd_pci_tbl,
735bfe1d560SDave Jiang 	.probe		= idxd_pci_probe,
736bfe1d560SDave Jiang 	.remove		= idxd_remove,
737bfe1d560SDave Jiang 	.shutdown	= idxd_shutdown,
738bfe1d560SDave Jiang };
739bfe1d560SDave Jiang 
740bfe1d560SDave Jiang static int __init idxd_init_module(void)
741bfe1d560SDave Jiang {
7424b73e4ebSDave Jiang 	int err;
743bfe1d560SDave Jiang 
744bfe1d560SDave Jiang 	/*
7458e50d392SDave Jiang 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
746bfe1d560SDave Jiang 	 * enumerating the device. We can not utilize it.
747bfe1d560SDave Jiang 	 */
748bfe1d560SDave Jiang 	if (!boot_cpu_has(X86_FEATURE_MOVDIR64B)) {
749bfe1d560SDave Jiang 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
750bfe1d560SDave Jiang 		return -ENODEV;
751bfe1d560SDave Jiang 	}
752bfe1d560SDave Jiang 
7538e50d392SDave Jiang 	if (!boot_cpu_has(X86_FEATURE_ENQCMD))
7548e50d392SDave Jiang 		pr_warn("Platform does not have ENQCMD(S) support.\n");
7558e50d392SDave Jiang 	else
7568e50d392SDave Jiang 		support_enqcmd = true;
757bfe1d560SDave Jiang 
758*0bde4444STom Zanussi 	perfmon_init();
759*0bde4444STom Zanussi 
760c52ca478SDave Jiang 	err = idxd_register_bus_type();
761c52ca478SDave Jiang 	if (err < 0)
762bfe1d560SDave Jiang 		return err;
763bfe1d560SDave Jiang 
764c52ca478SDave Jiang 	err = idxd_register_driver();
765c52ca478SDave Jiang 	if (err < 0)
766c52ca478SDave Jiang 		goto err_idxd_driver_register;
767c52ca478SDave Jiang 
76842d279f9SDave Jiang 	err = idxd_cdev_register();
76942d279f9SDave Jiang 	if (err)
77042d279f9SDave Jiang 		goto err_cdev_register;
77142d279f9SDave Jiang 
772c52ca478SDave Jiang 	err = pci_register_driver(&idxd_pci_driver);
773c52ca478SDave Jiang 	if (err)
774c52ca478SDave Jiang 		goto err_pci_register;
775c52ca478SDave Jiang 
776bfe1d560SDave Jiang 	return 0;
777c52ca478SDave Jiang 
778c52ca478SDave Jiang err_pci_register:
77942d279f9SDave Jiang 	idxd_cdev_remove();
78042d279f9SDave Jiang err_cdev_register:
781c52ca478SDave Jiang 	idxd_unregister_driver();
782c52ca478SDave Jiang err_idxd_driver_register:
783c52ca478SDave Jiang 	idxd_unregister_bus_type();
784c52ca478SDave Jiang 	return err;
785bfe1d560SDave Jiang }
786bfe1d560SDave Jiang module_init(idxd_init_module);
787bfe1d560SDave Jiang 
788bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
789bfe1d560SDave Jiang {
790bfe1d560SDave Jiang 	pci_unregister_driver(&idxd_pci_driver);
79142d279f9SDave Jiang 	idxd_cdev_remove();
792c52ca478SDave Jiang 	idxd_unregister_bus_type();
793*0bde4444STom Zanussi 	perfmon_exit();
794bfe1d560SDave Jiang }
795bfe1d560SDave Jiang module_exit(idxd_exit_module);
796