xref: /linux/drivers/dma/idxd/init.c (revision fffaed1e24b8d114e958d180cb4a8aed3febbb5a)
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/iommu.h>
18bfe1d560SDave Jiang #include <uapi/linux/idxd.h>
198f47d1a5SDave Jiang #include <linux/dmaengine.h>
208f47d1a5SDave Jiang #include "../dmaengine.h"
21bfe1d560SDave Jiang #include "registers.h"
22bfe1d560SDave Jiang #include "idxd.h"
230bde4444STom Zanussi #include "perfmon.h"
24bfe1d560SDave Jiang 
25bfe1d560SDave Jiang MODULE_VERSION(IDXD_DRIVER_VERSION);
26bfe1d560SDave Jiang MODULE_LICENSE("GPL v2");
27bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation");
28d9e5481fSDave Jiang MODULE_IMPORT_NS(IDXD);
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 
34ade8a86bSDave Jiang bool tc_override;
35ade8a86bSDave Jiang module_param(tc_override, bool, 0644);
36ade8a86bSDave Jiang MODULE_PARM_DESC(tc_override, "Override traffic class defaults");
37ade8a86bSDave Jiang 
38bfe1d560SDave Jiang #define DRV_NAME "idxd"
39bfe1d560SDave Jiang 
408e50d392SDave Jiang bool support_enqcmd;
414b73e4ebSDave Jiang DEFINE_IDA(idxd_ida);
42bfe1d560SDave Jiang 
43435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = {
44435b512dSDave Jiang 	[IDXD_TYPE_DSA] = {
45435b512dSDave Jiang 		.name_prefix = "dsa",
46435b512dSDave Jiang 		.type = IDXD_TYPE_DSA,
47435b512dSDave Jiang 		.compl_size = sizeof(struct dsa_completion_record),
48435b512dSDave Jiang 		.align = 32,
49435b512dSDave Jiang 		.dev_type = &dsa_device_type,
50435b512dSDave Jiang 	},
51435b512dSDave Jiang 	[IDXD_TYPE_IAX] = {
52435b512dSDave Jiang 		.name_prefix = "iax",
53435b512dSDave Jiang 		.type = IDXD_TYPE_IAX,
54435b512dSDave Jiang 		.compl_size = sizeof(struct iax_completion_record),
55435b512dSDave Jiang 		.align = 64,
56435b512dSDave Jiang 		.dev_type = &iax_device_type,
57435b512dSDave Jiang 	},
58435b512dSDave Jiang };
59435b512dSDave Jiang 
60bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = {
61bfe1d560SDave Jiang 	/* DSA ver 1.0 platforms */
62435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
63f25b4638SDave Jiang 
64f25b4638SDave Jiang 	/* IAX ver 1.0 platforms */
65435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
66bfe1d560SDave Jiang 	{ 0, }
67bfe1d560SDave Jiang };
68bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
69bfe1d560SDave Jiang 
70bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd)
71bfe1d560SDave Jiang {
72bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
73bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
74ec0d6423SDave Jiang 	struct idxd_irq_entry *ie;
75bfe1d560SDave Jiang 	int i, msixcnt;
76bfe1d560SDave Jiang 	int rc = 0;
77bfe1d560SDave Jiang 
78bfe1d560SDave Jiang 	msixcnt = pci_msix_vec_count(pdev);
79bfe1d560SDave Jiang 	if (msixcnt < 0) {
80bfe1d560SDave Jiang 		dev_err(dev, "Not MSI-X interrupt capable.\n");
815fc8e85fSDave Jiang 		return -ENOSPC;
82bfe1d560SDave Jiang 	}
838b67426eSDave Jiang 	idxd->irq_cnt = msixcnt;
84bfe1d560SDave Jiang 
855fc8e85fSDave Jiang 	rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
865fc8e85fSDave Jiang 	if (rc != msixcnt) {
875fc8e85fSDave Jiang 		dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
885fc8e85fSDave Jiang 		return -ENOSPC;
89bfe1d560SDave Jiang 	}
90bfe1d560SDave Jiang 	dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
91bfe1d560SDave Jiang 
92d5c10e0fSDave Jiang 
93ec0d6423SDave Jiang 	ie = idxd_get_ie(idxd, 0);
94ec0d6423SDave Jiang 	ie->vector = pci_irq_vector(pdev, 0);
95ec0d6423SDave Jiang 	rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie);
96bfe1d560SDave Jiang 	if (rc < 0) {
97bfe1d560SDave Jiang 		dev_err(dev, "Failed to allocate misc interrupt.\n");
985fc8e85fSDave Jiang 		goto err_misc_irq;
99bfe1d560SDave Jiang 	}
100403a2e23SDave Jiang 	dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector);
101bfe1d560SDave Jiang 
102ec0d6423SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
103ec0d6423SDave Jiang 		int msix_idx = i + 1;
104bfe1d560SDave Jiang 
105ec0d6423SDave Jiang 		ie = idxd_get_ie(idxd, msix_idx);
106ec0d6423SDave Jiang 		ie->id = msix_idx;
107ec0d6423SDave Jiang 		ie->int_handle = INVALID_INT_HANDLE;
108*fffaed1eSJacob Pan 		ie->pasid = IOMMU_PASID_INVALID;
109403a2e23SDave Jiang 
110ec0d6423SDave Jiang 		spin_lock_init(&ie->list_lock);
111ec0d6423SDave Jiang 		init_llist_head(&ie->pending_llist);
112ec0d6423SDave Jiang 		INIT_LIST_HEAD(&ie->work_list);
113bfe1d560SDave Jiang 	}
114bfe1d560SDave Jiang 
115bfe1d560SDave Jiang 	idxd_unmask_error_interrupts(idxd);
116bfe1d560SDave Jiang 	return 0;
117bfe1d560SDave Jiang 
1185fc8e85fSDave Jiang  err_misc_irq:
119bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
1205fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
121bfe1d560SDave Jiang 	dev_err(dev, "No usable interrupts\n");
122bfe1d560SDave Jiang 	return rc;
123bfe1d560SDave Jiang }
124bfe1d560SDave Jiang 
125ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd)
126ddf742d4SDave Jiang {
127ddf742d4SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
128ec0d6423SDave Jiang 	struct idxd_irq_entry *ie;
129403a2e23SDave Jiang 	int msixcnt;
130ddf742d4SDave Jiang 
131403a2e23SDave Jiang 	msixcnt = pci_msix_vec_count(pdev);
132403a2e23SDave Jiang 	if (msixcnt <= 0)
133403a2e23SDave Jiang 		return;
134ddf742d4SDave Jiang 
135403a2e23SDave Jiang 	ie = idxd_get_ie(idxd, 0);
136ddf742d4SDave Jiang 	idxd_mask_error_interrupts(idxd);
137403a2e23SDave Jiang 	free_irq(ie->vector, ie);
138ddf742d4SDave Jiang 	pci_free_irq_vectors(pdev);
139ddf742d4SDave Jiang }
140ddf742d4SDave Jiang 
1417c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd)
1427c5dd23eSDave Jiang {
1437c5dd23eSDave Jiang 	struct device *dev = &idxd->pdev->dev;
1447c5dd23eSDave Jiang 	struct idxd_wq *wq;
145700af3a0SDave Jiang 	struct device *conf_dev;
1467c5dd23eSDave Jiang 	int i, rc;
1477c5dd23eSDave Jiang 
1487c5dd23eSDave Jiang 	idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
1497c5dd23eSDave Jiang 				 GFP_KERNEL, dev_to_node(dev));
1507c5dd23eSDave Jiang 	if (!idxd->wqs)
1517c5dd23eSDave Jiang 		return -ENOMEM;
1527c5dd23eSDave Jiang 
153de5819b9SJerry Snitselaar 	idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
154de5819b9SJerry Snitselaar 	if (!idxd->wq_enable_map) {
155de5819b9SJerry Snitselaar 		kfree(idxd->wqs);
156de5819b9SJerry Snitselaar 		return -ENOMEM;
157de5819b9SJerry Snitselaar 	}
158de5819b9SJerry Snitselaar 
1597c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
1607c5dd23eSDave Jiang 		wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
1617c5dd23eSDave Jiang 		if (!wq) {
1627c5dd23eSDave Jiang 			rc = -ENOMEM;
1637c5dd23eSDave Jiang 			goto err;
1647c5dd23eSDave Jiang 		}
1657c5dd23eSDave Jiang 
166700af3a0SDave Jiang 		idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
167700af3a0SDave Jiang 		conf_dev = wq_confdev(wq);
1687c5dd23eSDave Jiang 		wq->id = i;
1697c5dd23eSDave Jiang 		wq->idxd = idxd;
170700af3a0SDave Jiang 		device_initialize(wq_confdev(wq));
171700af3a0SDave Jiang 		conf_dev->parent = idxd_confdev(idxd);
172700af3a0SDave Jiang 		conf_dev->bus = &dsa_bus_type;
173700af3a0SDave Jiang 		conf_dev->type = &idxd_wq_device_type;
174700af3a0SDave Jiang 		rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
1757c5dd23eSDave Jiang 		if (rc < 0) {
176700af3a0SDave Jiang 			put_device(conf_dev);
1777c5dd23eSDave Jiang 			goto err;
1787c5dd23eSDave Jiang 		}
1797c5dd23eSDave Jiang 
1807c5dd23eSDave Jiang 		mutex_init(&wq->wq_lock);
18104922b74SDave Jiang 		init_waitqueue_head(&wq->err_queue);
18293a40a6dSDave Jiang 		init_completion(&wq->wq_dead);
18356fc39f5SDave Jiang 		init_completion(&wq->wq_resurrect);
18492452a72SDave Jiang 		wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
185e8dbd644SXiaochen Shen 		idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH);
1867930d855SDave Jiang 		wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
1877c5dd23eSDave Jiang 		wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
1887c5dd23eSDave Jiang 		if (!wq->wqcfg) {
189700af3a0SDave Jiang 			put_device(conf_dev);
1907c5dd23eSDave Jiang 			rc = -ENOMEM;
1917c5dd23eSDave Jiang 			goto err;
1927c5dd23eSDave Jiang 		}
193b0325aefSDave Jiang 
194b0325aefSDave Jiang 		if (idxd->hw.wq_cap.op_config) {
195b0325aefSDave Jiang 			wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
196b0325aefSDave Jiang 			if (!wq->opcap_bmap) {
197b0325aefSDave Jiang 				put_device(conf_dev);
198b0325aefSDave Jiang 				rc = -ENOMEM;
199b0325aefSDave Jiang 				goto err;
200b0325aefSDave Jiang 			}
201b0325aefSDave Jiang 			bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
202b0325aefSDave Jiang 		}
2037c5dd23eSDave Jiang 		idxd->wqs[i] = wq;
2047c5dd23eSDave Jiang 	}
2057c5dd23eSDave Jiang 
2067c5dd23eSDave Jiang 	return 0;
2077c5dd23eSDave Jiang 
2087c5dd23eSDave Jiang  err:
209700af3a0SDave Jiang 	while (--i >= 0) {
210700af3a0SDave Jiang 		wq = idxd->wqs[i];
211700af3a0SDave Jiang 		conf_dev = wq_confdev(wq);
212700af3a0SDave Jiang 		put_device(conf_dev);
213700af3a0SDave Jiang 	}
2147c5dd23eSDave Jiang 	return rc;
2157c5dd23eSDave Jiang }
2167c5dd23eSDave Jiang 
21775b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd)
21875b91130SDave Jiang {
21975b91130SDave Jiang 	struct idxd_engine *engine;
22075b91130SDave Jiang 	struct device *dev = &idxd->pdev->dev;
221700af3a0SDave Jiang 	struct device *conf_dev;
22275b91130SDave Jiang 	int i, rc;
22375b91130SDave Jiang 
22475b91130SDave Jiang 	idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
22575b91130SDave Jiang 				     GFP_KERNEL, dev_to_node(dev));
22675b91130SDave Jiang 	if (!idxd->engines)
22775b91130SDave Jiang 		return -ENOMEM;
22875b91130SDave Jiang 
22975b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++) {
23075b91130SDave Jiang 		engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
23175b91130SDave Jiang 		if (!engine) {
23275b91130SDave Jiang 			rc = -ENOMEM;
23375b91130SDave Jiang 			goto err;
23475b91130SDave Jiang 		}
23575b91130SDave Jiang 
236700af3a0SDave Jiang 		idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE);
237700af3a0SDave Jiang 		conf_dev = engine_confdev(engine);
23875b91130SDave Jiang 		engine->id = i;
23975b91130SDave Jiang 		engine->idxd = idxd;
240700af3a0SDave Jiang 		device_initialize(conf_dev);
241700af3a0SDave Jiang 		conf_dev->parent = idxd_confdev(idxd);
242700af3a0SDave Jiang 		conf_dev->bus = &dsa_bus_type;
243700af3a0SDave Jiang 		conf_dev->type = &idxd_engine_device_type;
244700af3a0SDave Jiang 		rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
24575b91130SDave Jiang 		if (rc < 0) {
246700af3a0SDave Jiang 			put_device(conf_dev);
24775b91130SDave Jiang 			goto err;
24875b91130SDave Jiang 		}
24975b91130SDave Jiang 
25075b91130SDave Jiang 		idxd->engines[i] = engine;
25175b91130SDave Jiang 	}
25275b91130SDave Jiang 
25375b91130SDave Jiang 	return 0;
25475b91130SDave Jiang 
25575b91130SDave Jiang  err:
256700af3a0SDave Jiang 	while (--i >= 0) {
257700af3a0SDave Jiang 		engine = idxd->engines[i];
258700af3a0SDave Jiang 		conf_dev = engine_confdev(engine);
259700af3a0SDave Jiang 		put_device(conf_dev);
260700af3a0SDave Jiang 	}
26175b91130SDave Jiang 	return rc;
26275b91130SDave Jiang }
26375b91130SDave Jiang 
264defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd)
265defe49f9SDave Jiang {
266defe49f9SDave Jiang 	struct device *dev = &idxd->pdev->dev;
267700af3a0SDave Jiang 	struct device *conf_dev;
268defe49f9SDave Jiang 	struct idxd_group *group;
269defe49f9SDave Jiang 	int i, rc;
270defe49f9SDave Jiang 
271defe49f9SDave Jiang 	idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
272defe49f9SDave Jiang 				    GFP_KERNEL, dev_to_node(dev));
273defe49f9SDave Jiang 	if (!idxd->groups)
274defe49f9SDave Jiang 		return -ENOMEM;
275defe49f9SDave Jiang 
276defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++) {
277defe49f9SDave Jiang 		group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
278defe49f9SDave Jiang 		if (!group) {
279defe49f9SDave Jiang 			rc = -ENOMEM;
280defe49f9SDave Jiang 			goto err;
281defe49f9SDave Jiang 		}
282defe49f9SDave Jiang 
283700af3a0SDave Jiang 		idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP);
284700af3a0SDave Jiang 		conf_dev = group_confdev(group);
285defe49f9SDave Jiang 		group->id = i;
286defe49f9SDave Jiang 		group->idxd = idxd;
287700af3a0SDave Jiang 		device_initialize(conf_dev);
288700af3a0SDave Jiang 		conf_dev->parent = idxd_confdev(idxd);
289700af3a0SDave Jiang 		conf_dev->bus = &dsa_bus_type;
290700af3a0SDave Jiang 		conf_dev->type = &idxd_group_device_type;
291700af3a0SDave Jiang 		rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
292defe49f9SDave Jiang 		if (rc < 0) {
293700af3a0SDave Jiang 			put_device(conf_dev);
294defe49f9SDave Jiang 			goto err;
295defe49f9SDave Jiang 		}
296defe49f9SDave Jiang 
297defe49f9SDave Jiang 		idxd->groups[i] = group;
2989735bde3SFenghua Yu 		if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) {
299ade8a86bSDave Jiang 			group->tc_a = 1;
300ade8a86bSDave Jiang 			group->tc_b = 1;
301ade8a86bSDave Jiang 		} else {
302defe49f9SDave Jiang 			group->tc_a = -1;
303defe49f9SDave Jiang 			group->tc_b = -1;
304defe49f9SDave Jiang 		}
305601bdadaSFenghua Yu 		/*
306601bdadaSFenghua Yu 		 * The default value is the same as the value of
307601bdadaSFenghua Yu 		 * total read buffers in GRPCAP.
308601bdadaSFenghua Yu 		 */
309601bdadaSFenghua Yu 		group->rdbufs_allowed = idxd->max_rdbufs;
310ade8a86bSDave Jiang 	}
311defe49f9SDave Jiang 
312defe49f9SDave Jiang 	return 0;
313defe49f9SDave Jiang 
314defe49f9SDave Jiang  err:
315700af3a0SDave Jiang 	while (--i >= 0) {
316700af3a0SDave Jiang 		group = idxd->groups[i];
317700af3a0SDave Jiang 		put_device(group_confdev(group));
318700af3a0SDave Jiang 	}
319defe49f9SDave Jiang 	return rc;
320defe49f9SDave Jiang }
321defe49f9SDave Jiang 
322ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd)
323ddf742d4SDave Jiang {
324ddf742d4SDave Jiang 	int i;
325ddf742d4SDave Jiang 
326ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
327700af3a0SDave Jiang 		put_device(group_confdev(idxd->groups[i]));
328ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
329700af3a0SDave Jiang 		put_device(engine_confdev(idxd->engines[i]));
330ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
331700af3a0SDave Jiang 		put_device(wq_confdev(idxd->wqs[i]));
332ddf742d4SDave Jiang 	destroy_workqueue(idxd->wq);
333ddf742d4SDave Jiang }
334ddf742d4SDave Jiang 
335bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd)
336bfe1d560SDave Jiang {
337bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
338defe49f9SDave Jiang 	int rc, i;
339bfe1d560SDave Jiang 
3400d5c10b4SDave Jiang 	init_waitqueue_head(&idxd->cmd_waitq);
3417c5dd23eSDave Jiang 
3427c5dd23eSDave Jiang 	rc = idxd_setup_wqs(idxd);
3437c5dd23eSDave Jiang 	if (rc < 0)
344eb15e715SDave Jiang 		goto err_wqs;
3457c5dd23eSDave Jiang 
34675b91130SDave Jiang 	rc = idxd_setup_engines(idxd);
34775b91130SDave Jiang 	if (rc < 0)
34875b91130SDave Jiang 		goto err_engine;
34975b91130SDave Jiang 
350defe49f9SDave Jiang 	rc = idxd_setup_groups(idxd);
351defe49f9SDave Jiang 	if (rc < 0)
352defe49f9SDave Jiang 		goto err_group;
353bfe1d560SDave Jiang 
3540d5c10b4SDave Jiang 	idxd->wq = create_workqueue(dev_name(dev));
3557c5dd23eSDave Jiang 	if (!idxd->wq) {
3567c5dd23eSDave Jiang 		rc = -ENOMEM;
357defe49f9SDave Jiang 		goto err_wkq_create;
3587c5dd23eSDave Jiang 	}
3590d5c10b4SDave Jiang 
360bfe1d560SDave Jiang 	return 0;
3617c5dd23eSDave Jiang 
362defe49f9SDave Jiang  err_wkq_create:
363defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
364700af3a0SDave Jiang 		put_device(group_confdev(idxd->groups[i]));
365defe49f9SDave Jiang  err_group:
36675b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
367700af3a0SDave Jiang 		put_device(engine_confdev(idxd->engines[i]));
36875b91130SDave Jiang  err_engine:
3697c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
370700af3a0SDave Jiang 		put_device(wq_confdev(idxd->wqs[i]));
371eb15e715SDave Jiang  err_wqs:
3727c5dd23eSDave Jiang 	return rc;
373bfe1d560SDave Jiang }
374bfe1d560SDave Jiang 
375bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd)
376bfe1d560SDave Jiang {
377bfe1d560SDave Jiang 	union offsets_reg offsets;
378bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
379bfe1d560SDave Jiang 
380bfe1d560SDave Jiang 	offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
3812f8417a9SDave Jiang 	offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
3822f8417a9SDave Jiang 	idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
383bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
3842f8417a9SDave Jiang 	idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
3852f8417a9SDave Jiang 	dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
3862f8417a9SDave Jiang 	idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
3872f8417a9SDave Jiang 	dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
3882f8417a9SDave Jiang 	idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
389bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
390bfe1d560SDave Jiang }
391bfe1d560SDave Jiang 
392a8563a33SDave Jiang static void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count)
393a8563a33SDave Jiang {
394a8563a33SDave Jiang 	int i, j, nr;
395a8563a33SDave Jiang 
396a8563a33SDave Jiang 	for (i = 0, nr = 0; i < count; i++) {
397a8563a33SDave Jiang 		for (j = 0; j < BITS_PER_LONG_LONG; j++) {
398a8563a33SDave Jiang 			if (val[i] & BIT(j))
399a8563a33SDave Jiang 				set_bit(nr, bmap);
400a8563a33SDave Jiang 			nr++;
401a8563a33SDave Jiang 		}
402a8563a33SDave Jiang 	}
403a8563a33SDave Jiang }
404a8563a33SDave Jiang 
405bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd)
406bfe1d560SDave Jiang {
407bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
408bfe1d560SDave Jiang 	int i;
409bfe1d560SDave Jiang 
410bfe1d560SDave Jiang 	/* reading generic capabilities */
411bfe1d560SDave Jiang 	idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
412bfe1d560SDave Jiang 	dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
413eb15e715SDave Jiang 
414eb15e715SDave Jiang 	if (idxd->hw.gen_cap.cmd_cap) {
415eb15e715SDave Jiang 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
416eb15e715SDave Jiang 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
417eb15e715SDave Jiang 	}
418eb15e715SDave Jiang 
4198b67426eSDave Jiang 	/* reading command capabilities */
4208b67426eSDave Jiang 	if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE))
4218b67426eSDave Jiang 		idxd->request_int_handles = true;
4228b67426eSDave Jiang 
423bfe1d560SDave Jiang 	idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
424bfe1d560SDave Jiang 	dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
425e8dbd644SXiaochen Shen 	idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift);
426bfe1d560SDave Jiang 	dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
427bfe1d560SDave Jiang 	if (idxd->hw.gen_cap.config_en)
428bfe1d560SDave Jiang 		set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
429bfe1d560SDave Jiang 
430bfe1d560SDave Jiang 	/* reading group capabilities */
431bfe1d560SDave Jiang 	idxd->hw.group_cap.bits =
432bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
433bfe1d560SDave Jiang 	dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
434bfe1d560SDave Jiang 	idxd->max_groups = idxd->hw.group_cap.num_groups;
435bfe1d560SDave Jiang 	dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
4367ed6f1b8SDave Jiang 	idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs;
4377ed6f1b8SDave Jiang 	dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs);
4387ed6f1b8SDave Jiang 	idxd->nr_rdbufs = idxd->max_rdbufs;
439bfe1d560SDave Jiang 
440bfe1d560SDave Jiang 	/* read engine capabilities */
441bfe1d560SDave Jiang 	idxd->hw.engine_cap.bits =
442bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
443bfe1d560SDave Jiang 	dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
444bfe1d560SDave Jiang 	idxd->max_engines = idxd->hw.engine_cap.num_engines;
445bfe1d560SDave Jiang 	dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
446bfe1d560SDave Jiang 
447bfe1d560SDave Jiang 	/* read workqueue capabilities */
448bfe1d560SDave Jiang 	idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
449bfe1d560SDave Jiang 	dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
450bfe1d560SDave Jiang 	idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
451bfe1d560SDave Jiang 	dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
452bfe1d560SDave Jiang 	idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
453bfe1d560SDave Jiang 	dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
454d98793b5SDave Jiang 	idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
455d98793b5SDave Jiang 	dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
456bfe1d560SDave Jiang 
457bfe1d560SDave Jiang 	/* reading operation capabilities */
458bfe1d560SDave Jiang 	for (i = 0; i < 4; i++) {
459bfe1d560SDave Jiang 		idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
460bfe1d560SDave Jiang 				IDXD_OPCAP_OFFSET + i * sizeof(u64));
461bfe1d560SDave Jiang 		dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
462bfe1d560SDave Jiang 	}
463a8563a33SDave Jiang 	multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4);
464bfe1d560SDave Jiang }
465bfe1d560SDave Jiang 
466435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
467bfe1d560SDave Jiang {
468bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
469700af3a0SDave Jiang 	struct device *conf_dev;
470bfe1d560SDave Jiang 	struct idxd_device *idxd;
47147c16ac2SDave Jiang 	int rc;
472bfe1d560SDave Jiang 
47347c16ac2SDave Jiang 	idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
474bfe1d560SDave Jiang 	if (!idxd)
475bfe1d560SDave Jiang 		return NULL;
476bfe1d560SDave Jiang 
477700af3a0SDave Jiang 	conf_dev = idxd_confdev(idxd);
478bfe1d560SDave Jiang 	idxd->pdev = pdev;
479435b512dSDave Jiang 	idxd->data = data;
480700af3a0SDave Jiang 	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
4814b73e4ebSDave Jiang 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
48247c16ac2SDave Jiang 	if (idxd->id < 0)
48347c16ac2SDave Jiang 		return NULL;
48447c16ac2SDave Jiang 
485a8563a33SDave Jiang 	idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev));
486a8563a33SDave Jiang 	if (!idxd->opcap_bmap) {
487a8563a33SDave Jiang 		ida_free(&idxd_ida, idxd->id);
488a8563a33SDave Jiang 		return NULL;
489a8563a33SDave Jiang 	}
490a8563a33SDave Jiang 
491700af3a0SDave Jiang 	device_initialize(conf_dev);
492700af3a0SDave Jiang 	conf_dev->parent = dev;
493700af3a0SDave Jiang 	conf_dev->bus = &dsa_bus_type;
494700af3a0SDave Jiang 	conf_dev->type = idxd->data->dev_type;
495700af3a0SDave Jiang 	rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
49647c16ac2SDave Jiang 	if (rc < 0) {
497700af3a0SDave Jiang 		put_device(conf_dev);
49847c16ac2SDave Jiang 		return NULL;
49947c16ac2SDave Jiang 	}
50047c16ac2SDave Jiang 
501bfe1d560SDave Jiang 	spin_lock_init(&idxd->dev_lock);
50253b2ee7fSDave Jiang 	spin_lock_init(&idxd->cmd_lock);
503bfe1d560SDave Jiang 
504bfe1d560SDave Jiang 	return idxd;
505bfe1d560SDave Jiang }
506bfe1d560SDave Jiang 
5078e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd)
5088e50d392SDave Jiang {
509942fd543SLu Baolu 	return -EOPNOTSUPP;
5108e50d392SDave Jiang }
5118e50d392SDave Jiang 
5128e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd)
5138e50d392SDave Jiang {
5148e50d392SDave Jiang 
5158e50d392SDave Jiang 	iommu_sva_unbind_device(idxd->sva);
5168e50d392SDave Jiang 	idxd->sva = NULL;
5178e50d392SDave Jiang }
5188e50d392SDave Jiang 
519bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd)
520bfe1d560SDave Jiang {
521bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
522bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
523bfe1d560SDave Jiang 	int rc;
524bfe1d560SDave Jiang 
525bfe1d560SDave Jiang 	dev_dbg(dev, "%s entered and resetting device\n", __func__);
52689e3becdSDave Jiang 	rc = idxd_device_init_reset(idxd);
52789e3becdSDave Jiang 	if (rc < 0)
52889e3becdSDave Jiang 		return rc;
52989e3becdSDave Jiang 
530bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD reset complete\n");
531bfe1d560SDave Jiang 
53203d939c7SDave Jiang 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
5338ffccd11SJerry Snitselaar 		if (iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA)) {
53442a1b738SDave Jiang 			dev_warn(dev, "Unable to turn on user SVA feature.\n");
5358ffccd11SJerry Snitselaar 		} else {
53642a1b738SDave Jiang 			set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
53742a1b738SDave Jiang 
53842a1b738SDave Jiang 			if (idxd_enable_system_pasid(idxd))
53942a1b738SDave Jiang 				dev_warn(dev, "No in-kernel DMA with PASID.\n");
54042a1b738SDave Jiang 			else
5418e50d392SDave Jiang 				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
5428ffccd11SJerry Snitselaar 		}
54303d939c7SDave Jiang 	} else if (!sva) {
54403d939c7SDave Jiang 		dev_warn(dev, "User forced SVA off via module param.\n");
5458e50d392SDave Jiang 	}
5468e50d392SDave Jiang 
547bfe1d560SDave Jiang 	idxd_read_caps(idxd);
548bfe1d560SDave Jiang 	idxd_read_table_offsets(idxd);
549bfe1d560SDave Jiang 
550bfe1d560SDave Jiang 	rc = idxd_setup_internals(idxd);
551bfe1d560SDave Jiang 	if (rc)
5527c5dd23eSDave Jiang 		goto err;
553bfe1d560SDave Jiang 
5548c66bbdcSDave Jiang 	/* If the configs are readonly, then load them from device */
5558c66bbdcSDave Jiang 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
5568c66bbdcSDave Jiang 		dev_dbg(dev, "Loading RO device config\n");
5578c66bbdcSDave Jiang 		rc = idxd_device_load_config(idxd);
5588c66bbdcSDave Jiang 		if (rc < 0)
559ddf742d4SDave Jiang 			goto err_config;
5608c66bbdcSDave Jiang 	}
5618c66bbdcSDave Jiang 
562bfe1d560SDave Jiang 	rc = idxd_setup_interrupts(idxd);
563bfe1d560SDave Jiang 	if (rc)
564ddf742d4SDave Jiang 		goto err_config;
565bfe1d560SDave Jiang 
56642d279f9SDave Jiang 	idxd->major = idxd_cdev_get_major(idxd);
56742d279f9SDave Jiang 
5680bde4444STom Zanussi 	rc = perfmon_pmu_init(idxd);
5690bde4444STom Zanussi 	if (rc < 0)
5700bde4444STom Zanussi 		dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
5710bde4444STom Zanussi 
572bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
573bfe1d560SDave Jiang 	return 0;
574bfe1d560SDave Jiang 
575ddf742d4SDave Jiang  err_config:
576ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
5777c5dd23eSDave Jiang  err:
5788e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
5798e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
58042a1b738SDave Jiang 	if (device_user_pasid_enabled(idxd))
581cf5f86a7SDave Jiang 		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
582bfe1d560SDave Jiang 	return rc;
583bfe1d560SDave Jiang }
584bfe1d560SDave Jiang 
585ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd)
586ddf742d4SDave Jiang {
587ddf742d4SDave Jiang 	struct device *dev = &idxd->pdev->dev;
588ddf742d4SDave Jiang 
589ddf742d4SDave Jiang 	perfmon_pmu_remove(idxd);
590ddf742d4SDave Jiang 	idxd_cleanup_interrupts(idxd);
591ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
592ddf742d4SDave Jiang 	if (device_pasid_enabled(idxd))
593ddf742d4SDave Jiang 		idxd_disable_system_pasid(idxd);
59442a1b738SDave Jiang 	if (device_user_pasid_enabled(idxd))
595ddf742d4SDave Jiang 		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
596ddf742d4SDave Jiang }
597ddf742d4SDave Jiang 
598bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
599bfe1d560SDave Jiang {
600bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
601bfe1d560SDave Jiang 	struct idxd_device *idxd;
602435b512dSDave Jiang 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
603bfe1d560SDave Jiang 	int rc;
604bfe1d560SDave Jiang 
605a39c7cd0SDave Jiang 	rc = pci_enable_device(pdev);
606bfe1d560SDave Jiang 	if (rc)
607bfe1d560SDave Jiang 		return rc;
608bfe1d560SDave Jiang 
6098e50d392SDave Jiang 	dev_dbg(dev, "Alloc IDXD context\n");
610435b512dSDave Jiang 	idxd = idxd_alloc(pdev, data);
611a39c7cd0SDave Jiang 	if (!idxd) {
612a39c7cd0SDave Jiang 		rc = -ENOMEM;
613a39c7cd0SDave Jiang 		goto err_idxd_alloc;
614a39c7cd0SDave Jiang 	}
615bfe1d560SDave Jiang 
6168e50d392SDave Jiang 	dev_dbg(dev, "Mapping BARs\n");
617a39c7cd0SDave Jiang 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
618a39c7cd0SDave Jiang 	if (!idxd->reg_base) {
619a39c7cd0SDave Jiang 		rc = -ENOMEM;
620a39c7cd0SDave Jiang 		goto err_iomap;
621a39c7cd0SDave Jiang 	}
622bfe1d560SDave Jiang 
623bfe1d560SDave Jiang 	dev_dbg(dev, "Set DMA masks\n");
62453b50458SChristophe JAILLET 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
625bfe1d560SDave Jiang 	if (rc)
626a39c7cd0SDave Jiang 		goto err;
627bfe1d560SDave Jiang 
628bfe1d560SDave Jiang 	dev_dbg(dev, "Set PCI master\n");
629bfe1d560SDave Jiang 	pci_set_master(pdev);
630bfe1d560SDave Jiang 	pci_set_drvdata(pdev, idxd);
631bfe1d560SDave Jiang 
632bfe1d560SDave Jiang 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
633bfe1d560SDave Jiang 	rc = idxd_probe(idxd);
634bfe1d560SDave Jiang 	if (rc) {
635bfe1d560SDave Jiang 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
636a39c7cd0SDave Jiang 		goto err;
637bfe1d560SDave Jiang 	}
638bfe1d560SDave Jiang 
63947c16ac2SDave Jiang 	rc = idxd_register_devices(idxd);
640c52ca478SDave Jiang 	if (rc) {
641c52ca478SDave Jiang 		dev_err(dev, "IDXD sysfs setup failed\n");
642ddf742d4SDave Jiang 		goto err_dev_register;
643c52ca478SDave Jiang 	}
644c52ca478SDave Jiang 
645bfe1d560SDave Jiang 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
646bfe1d560SDave Jiang 		 idxd->hw.version);
647bfe1d560SDave Jiang 
648bfe1d560SDave Jiang 	return 0;
649a39c7cd0SDave Jiang 
650ddf742d4SDave Jiang  err_dev_register:
651ddf742d4SDave Jiang 	idxd_cleanup(idxd);
652a39c7cd0SDave Jiang  err:
653a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
654a39c7cd0SDave Jiang  err_iomap:
655700af3a0SDave Jiang 	put_device(idxd_confdev(idxd));
656a39c7cd0SDave Jiang  err_idxd_alloc:
657a39c7cd0SDave Jiang 	pci_disable_device(pdev);
658a39c7cd0SDave Jiang 	return rc;
659bfe1d560SDave Jiang }
660bfe1d560SDave Jiang 
6615b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd)
6625b0c68c4SDave Jiang {
6635b0c68c4SDave Jiang 	struct idxd_wq *wq;
6645b0c68c4SDave Jiang 	int i;
6655b0c68c4SDave Jiang 
6665b0c68c4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
6675b0c68c4SDave Jiang 		wq = idxd->wqs[i];
6685b0c68c4SDave Jiang 		if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
6695b0c68c4SDave Jiang 			idxd_wq_quiesce(wq);
6705b0c68c4SDave Jiang 	}
6715b0c68c4SDave Jiang }
6725b0c68c4SDave Jiang 
673bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
674bfe1d560SDave Jiang {
675bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
676bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
677403a2e23SDave Jiang 	int rc;
678bfe1d560SDave Jiang 
679bfe1d560SDave Jiang 	rc = idxd_device_disable(idxd);
680bfe1d560SDave Jiang 	if (rc)
681bfe1d560SDave Jiang 		dev_err(&pdev->dev, "Disabling device failed\n");
682bfe1d560SDave Jiang 
683403a2e23SDave Jiang 	irq_entry = &idxd->ie;
6845fc8e85fSDave Jiang 	synchronize_irq(irq_entry->vector);
685403a2e23SDave Jiang 	idxd_mask_error_interrupts(idxd);
68649c4959fSDave Jiang 	flush_workqueue(idxd->wq);
687bfe1d560SDave Jiang }
688bfe1d560SDave Jiang 
689bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
690bfe1d560SDave Jiang {
691bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
69249c4959fSDave Jiang 	struct idxd_irq_entry *irq_entry;
693bfe1d560SDave Jiang 
69498da0106SDave Jiang 	idxd_unregister_devices(idxd);
69598da0106SDave Jiang 	/*
69698da0106SDave Jiang 	 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
69798da0106SDave Jiang 	 * to the idxd context. The driver still needs those bits in order to do the rest of
69898da0106SDave Jiang 	 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
69998da0106SDave Jiang 	 * on the device here to hold off the freeing while allowing the idxd sub-driver
70098da0106SDave Jiang 	 * to unbind.
70198da0106SDave Jiang 	 */
70298da0106SDave Jiang 	get_device(idxd_confdev(idxd));
70398da0106SDave Jiang 	device_unregister(idxd_confdev(idxd));
704bfe1d560SDave Jiang 	idxd_shutdown(pdev);
7058e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
7068e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
70749c4959fSDave Jiang 
708403a2e23SDave Jiang 	irq_entry = idxd_get_ie(idxd, 0);
70949c4959fSDave Jiang 	free_irq(irq_entry->vector, irq_entry);
71049c4959fSDave Jiang 	pci_free_irq_vectors(pdev);
71149c4959fSDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
71242a1b738SDave Jiang 	if (device_user_pasid_enabled(idxd))
713cf5f86a7SDave Jiang 		iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
71449c4959fSDave Jiang 	pci_disable_device(pdev);
71549c4959fSDave Jiang 	destroy_workqueue(idxd->wq);
71649c4959fSDave Jiang 	perfmon_pmu_remove(idxd);
71798da0106SDave Jiang 	put_device(idxd_confdev(idxd));
718bfe1d560SDave Jiang }
719bfe1d560SDave Jiang 
720bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
721bfe1d560SDave Jiang 	.name		= DRV_NAME,
722bfe1d560SDave Jiang 	.id_table	= idxd_pci_tbl,
723bfe1d560SDave Jiang 	.probe		= idxd_pci_probe,
724bfe1d560SDave Jiang 	.remove		= idxd_remove,
725bfe1d560SDave Jiang 	.shutdown	= idxd_shutdown,
726bfe1d560SDave Jiang };
727bfe1d560SDave Jiang 
728bfe1d560SDave Jiang static int __init idxd_init_module(void)
729bfe1d560SDave Jiang {
7304b73e4ebSDave Jiang 	int err;
731bfe1d560SDave Jiang 
732bfe1d560SDave Jiang 	/*
7338e50d392SDave Jiang 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
734bfe1d560SDave Jiang 	 * enumerating the device. We can not utilize it.
735bfe1d560SDave Jiang 	 */
73674b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
737bfe1d560SDave Jiang 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
738bfe1d560SDave Jiang 		return -ENODEV;
739bfe1d560SDave Jiang 	}
740bfe1d560SDave Jiang 
74174b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
7428e50d392SDave Jiang 		pr_warn("Platform does not have ENQCMD(S) support.\n");
7438e50d392SDave Jiang 	else
7448e50d392SDave Jiang 		support_enqcmd = true;
745bfe1d560SDave Jiang 
7460bde4444STom Zanussi 	perfmon_init();
7470bde4444STom Zanussi 
748034b3290SDave Jiang 	err = idxd_driver_register(&idxd_drv);
749034b3290SDave Jiang 	if (err < 0)
750034b3290SDave Jiang 		goto err_idxd_driver_register;
751034b3290SDave Jiang 
7520cda4f69SDave Jiang 	err = idxd_driver_register(&idxd_dmaengine_drv);
7530cda4f69SDave Jiang 	if (err < 0)
7540cda4f69SDave Jiang 		goto err_idxd_dmaengine_driver_register;
7550cda4f69SDave Jiang 
756448c3de8SDave Jiang 	err = idxd_driver_register(&idxd_user_drv);
757448c3de8SDave Jiang 	if (err < 0)
758448c3de8SDave Jiang 		goto err_idxd_user_driver_register;
759448c3de8SDave Jiang 
76042d279f9SDave Jiang 	err = idxd_cdev_register();
76142d279f9SDave Jiang 	if (err)
76242d279f9SDave Jiang 		goto err_cdev_register;
76342d279f9SDave Jiang 
764c52ca478SDave Jiang 	err = pci_register_driver(&idxd_pci_driver);
765c52ca478SDave Jiang 	if (err)
766c52ca478SDave Jiang 		goto err_pci_register;
767c52ca478SDave Jiang 
768bfe1d560SDave Jiang 	return 0;
769c52ca478SDave Jiang 
770c52ca478SDave Jiang err_pci_register:
77142d279f9SDave Jiang 	idxd_cdev_remove();
77242d279f9SDave Jiang err_cdev_register:
773448c3de8SDave Jiang 	idxd_driver_unregister(&idxd_user_drv);
774448c3de8SDave Jiang err_idxd_user_driver_register:
7750cda4f69SDave Jiang 	idxd_driver_unregister(&idxd_dmaengine_drv);
7760cda4f69SDave Jiang err_idxd_dmaengine_driver_register:
777034b3290SDave Jiang 	idxd_driver_unregister(&idxd_drv);
778034b3290SDave Jiang err_idxd_driver_register:
779c52ca478SDave Jiang 	return err;
780bfe1d560SDave Jiang }
781bfe1d560SDave Jiang module_init(idxd_init_module);
782bfe1d560SDave Jiang 
783bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
784bfe1d560SDave Jiang {
785448c3de8SDave Jiang 	idxd_driver_unregister(&idxd_user_drv);
7860cda4f69SDave Jiang 	idxd_driver_unregister(&idxd_dmaengine_drv);
787034b3290SDave Jiang 	idxd_driver_unregister(&idxd_drv);
788bfe1d560SDave Jiang 	pci_unregister_driver(&idxd_pci_driver);
78942d279f9SDave Jiang 	idxd_cdev_remove();
7900bde4444STom Zanussi 	perfmon_exit();
791bfe1d560SDave Jiang }
792bfe1d560SDave Jiang module_exit(idxd_exit_module);
793