xref: /linux/drivers/dma/idxd/init.c (revision 9735bde36487da43d3c3fc910df49639f72decbf)
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;
108ec0d6423SDave Jiang 		ie->pasid = INVALID_IOASID;
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;
298*9735bde3SFenghua 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 		}
305ade8a86bSDave Jiang 	}
306defe49f9SDave Jiang 
307defe49f9SDave Jiang 	return 0;
308defe49f9SDave Jiang 
309defe49f9SDave Jiang  err:
310700af3a0SDave Jiang 	while (--i >= 0) {
311700af3a0SDave Jiang 		group = idxd->groups[i];
312700af3a0SDave Jiang 		put_device(group_confdev(group));
313700af3a0SDave Jiang 	}
314defe49f9SDave Jiang 	return rc;
315defe49f9SDave Jiang }
316defe49f9SDave Jiang 
317ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd)
318ddf742d4SDave Jiang {
319ddf742d4SDave Jiang 	int i;
320ddf742d4SDave Jiang 
321ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
322700af3a0SDave Jiang 		put_device(group_confdev(idxd->groups[i]));
323ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
324700af3a0SDave Jiang 		put_device(engine_confdev(idxd->engines[i]));
325ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
326700af3a0SDave Jiang 		put_device(wq_confdev(idxd->wqs[i]));
327ddf742d4SDave Jiang 	destroy_workqueue(idxd->wq);
328ddf742d4SDave Jiang }
329ddf742d4SDave Jiang 
330bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd)
331bfe1d560SDave Jiang {
332bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
333defe49f9SDave Jiang 	int rc, i;
334bfe1d560SDave Jiang 
3350d5c10b4SDave Jiang 	init_waitqueue_head(&idxd->cmd_waitq);
3367c5dd23eSDave Jiang 
3377c5dd23eSDave Jiang 	rc = idxd_setup_wqs(idxd);
3387c5dd23eSDave Jiang 	if (rc < 0)
339eb15e715SDave Jiang 		goto err_wqs;
3407c5dd23eSDave Jiang 
34175b91130SDave Jiang 	rc = idxd_setup_engines(idxd);
34275b91130SDave Jiang 	if (rc < 0)
34375b91130SDave Jiang 		goto err_engine;
34475b91130SDave Jiang 
345defe49f9SDave Jiang 	rc = idxd_setup_groups(idxd);
346defe49f9SDave Jiang 	if (rc < 0)
347defe49f9SDave Jiang 		goto err_group;
348bfe1d560SDave Jiang 
3490d5c10b4SDave Jiang 	idxd->wq = create_workqueue(dev_name(dev));
3507c5dd23eSDave Jiang 	if (!idxd->wq) {
3517c5dd23eSDave Jiang 		rc = -ENOMEM;
352defe49f9SDave Jiang 		goto err_wkq_create;
3537c5dd23eSDave Jiang 	}
3540d5c10b4SDave Jiang 
355bfe1d560SDave Jiang 	return 0;
3567c5dd23eSDave Jiang 
357defe49f9SDave Jiang  err_wkq_create:
358defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
359700af3a0SDave Jiang 		put_device(group_confdev(idxd->groups[i]));
360defe49f9SDave Jiang  err_group:
36175b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
362700af3a0SDave Jiang 		put_device(engine_confdev(idxd->engines[i]));
36375b91130SDave Jiang  err_engine:
3647c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
365700af3a0SDave Jiang 		put_device(wq_confdev(idxd->wqs[i]));
366eb15e715SDave Jiang  err_wqs:
3677c5dd23eSDave Jiang 	return rc;
368bfe1d560SDave Jiang }
369bfe1d560SDave Jiang 
370bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd)
371bfe1d560SDave Jiang {
372bfe1d560SDave Jiang 	union offsets_reg offsets;
373bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
374bfe1d560SDave Jiang 
375bfe1d560SDave Jiang 	offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
3762f8417a9SDave Jiang 	offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
3772f8417a9SDave Jiang 	idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
378bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
3792f8417a9SDave Jiang 	idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
3802f8417a9SDave Jiang 	dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
3812f8417a9SDave Jiang 	idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
3822f8417a9SDave Jiang 	dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
3832f8417a9SDave Jiang 	idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
384bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
385bfe1d560SDave Jiang }
386bfe1d560SDave Jiang 
387a8563a33SDave Jiang static void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count)
388a8563a33SDave Jiang {
389a8563a33SDave Jiang 	int i, j, nr;
390a8563a33SDave Jiang 
391a8563a33SDave Jiang 	for (i = 0, nr = 0; i < count; i++) {
392a8563a33SDave Jiang 		for (j = 0; j < BITS_PER_LONG_LONG; j++) {
393a8563a33SDave Jiang 			if (val[i] & BIT(j))
394a8563a33SDave Jiang 				set_bit(nr, bmap);
395a8563a33SDave Jiang 			nr++;
396a8563a33SDave Jiang 		}
397a8563a33SDave Jiang 	}
398a8563a33SDave Jiang }
399a8563a33SDave Jiang 
400bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd)
401bfe1d560SDave Jiang {
402bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
403bfe1d560SDave Jiang 	int i;
404bfe1d560SDave Jiang 
405bfe1d560SDave Jiang 	/* reading generic capabilities */
406bfe1d560SDave Jiang 	idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
407bfe1d560SDave Jiang 	dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
408eb15e715SDave Jiang 
409eb15e715SDave Jiang 	if (idxd->hw.gen_cap.cmd_cap) {
410eb15e715SDave Jiang 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
411eb15e715SDave Jiang 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
412eb15e715SDave Jiang 	}
413eb15e715SDave Jiang 
4148b67426eSDave Jiang 	/* reading command capabilities */
4158b67426eSDave Jiang 	if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE))
4168b67426eSDave Jiang 		idxd->request_int_handles = true;
4178b67426eSDave Jiang 
418bfe1d560SDave Jiang 	idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
419bfe1d560SDave Jiang 	dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
420e8dbd644SXiaochen Shen 	idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift);
421bfe1d560SDave Jiang 	dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
422bfe1d560SDave Jiang 	if (idxd->hw.gen_cap.config_en)
423bfe1d560SDave Jiang 		set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
424bfe1d560SDave Jiang 
425bfe1d560SDave Jiang 	/* reading group capabilities */
426bfe1d560SDave Jiang 	idxd->hw.group_cap.bits =
427bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
428bfe1d560SDave Jiang 	dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
429bfe1d560SDave Jiang 	idxd->max_groups = idxd->hw.group_cap.num_groups;
430bfe1d560SDave Jiang 	dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
4317ed6f1b8SDave Jiang 	idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs;
4327ed6f1b8SDave Jiang 	dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs);
4337ed6f1b8SDave Jiang 	idxd->nr_rdbufs = idxd->max_rdbufs;
434bfe1d560SDave Jiang 
435bfe1d560SDave Jiang 	/* read engine capabilities */
436bfe1d560SDave Jiang 	idxd->hw.engine_cap.bits =
437bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
438bfe1d560SDave Jiang 	dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
439bfe1d560SDave Jiang 	idxd->max_engines = idxd->hw.engine_cap.num_engines;
440bfe1d560SDave Jiang 	dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
441bfe1d560SDave Jiang 
442bfe1d560SDave Jiang 	/* read workqueue capabilities */
443bfe1d560SDave Jiang 	idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
444bfe1d560SDave Jiang 	dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
445bfe1d560SDave Jiang 	idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
446bfe1d560SDave Jiang 	dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
447bfe1d560SDave Jiang 	idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
448bfe1d560SDave Jiang 	dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
449d98793b5SDave Jiang 	idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
450d98793b5SDave Jiang 	dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
451bfe1d560SDave Jiang 
452bfe1d560SDave Jiang 	/* reading operation capabilities */
453bfe1d560SDave Jiang 	for (i = 0; i < 4; i++) {
454bfe1d560SDave Jiang 		idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
455bfe1d560SDave Jiang 				IDXD_OPCAP_OFFSET + i * sizeof(u64));
456bfe1d560SDave Jiang 		dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
457bfe1d560SDave Jiang 	}
458a8563a33SDave Jiang 	multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4);
459bfe1d560SDave Jiang }
460bfe1d560SDave Jiang 
461435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
462bfe1d560SDave Jiang {
463bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
464700af3a0SDave Jiang 	struct device *conf_dev;
465bfe1d560SDave Jiang 	struct idxd_device *idxd;
46647c16ac2SDave Jiang 	int rc;
467bfe1d560SDave Jiang 
46847c16ac2SDave Jiang 	idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
469bfe1d560SDave Jiang 	if (!idxd)
470bfe1d560SDave Jiang 		return NULL;
471bfe1d560SDave Jiang 
472700af3a0SDave Jiang 	conf_dev = idxd_confdev(idxd);
473bfe1d560SDave Jiang 	idxd->pdev = pdev;
474435b512dSDave Jiang 	idxd->data = data;
475700af3a0SDave Jiang 	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
4764b73e4ebSDave Jiang 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
47747c16ac2SDave Jiang 	if (idxd->id < 0)
47847c16ac2SDave Jiang 		return NULL;
47947c16ac2SDave Jiang 
480a8563a33SDave Jiang 	idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev));
481a8563a33SDave Jiang 	if (!idxd->opcap_bmap) {
482a8563a33SDave Jiang 		ida_free(&idxd_ida, idxd->id);
483a8563a33SDave Jiang 		return NULL;
484a8563a33SDave Jiang 	}
485a8563a33SDave Jiang 
486700af3a0SDave Jiang 	device_initialize(conf_dev);
487700af3a0SDave Jiang 	conf_dev->parent = dev;
488700af3a0SDave Jiang 	conf_dev->bus = &dsa_bus_type;
489700af3a0SDave Jiang 	conf_dev->type = idxd->data->dev_type;
490700af3a0SDave Jiang 	rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
49147c16ac2SDave Jiang 	if (rc < 0) {
492700af3a0SDave Jiang 		put_device(conf_dev);
49347c16ac2SDave Jiang 		return NULL;
49447c16ac2SDave Jiang 	}
49547c16ac2SDave Jiang 
496bfe1d560SDave Jiang 	spin_lock_init(&idxd->dev_lock);
49753b2ee7fSDave Jiang 	spin_lock_init(&idxd->cmd_lock);
498bfe1d560SDave Jiang 
499bfe1d560SDave Jiang 	return idxd;
500bfe1d560SDave Jiang }
501bfe1d560SDave Jiang 
5028e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd)
5038e50d392SDave Jiang {
504942fd543SLu Baolu 	return -EOPNOTSUPP;
5058e50d392SDave Jiang }
5068e50d392SDave Jiang 
5078e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd)
5088e50d392SDave Jiang {
5098e50d392SDave Jiang 
5108e50d392SDave Jiang 	iommu_sva_unbind_device(idxd->sva);
5118e50d392SDave Jiang 	idxd->sva = NULL;
5128e50d392SDave Jiang }
5138e50d392SDave Jiang 
514bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd)
515bfe1d560SDave Jiang {
516bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
517bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
518bfe1d560SDave Jiang 	int rc;
519bfe1d560SDave Jiang 
520bfe1d560SDave Jiang 	dev_dbg(dev, "%s entered and resetting device\n", __func__);
52189e3becdSDave Jiang 	rc = idxd_device_init_reset(idxd);
52289e3becdSDave Jiang 	if (rc < 0)
52389e3becdSDave Jiang 		return rc;
52489e3becdSDave Jiang 
525bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD reset complete\n");
526bfe1d560SDave Jiang 
52703d939c7SDave Jiang 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
5288ffccd11SJerry Snitselaar 		if (iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA)) {
52942a1b738SDave Jiang 			dev_warn(dev, "Unable to turn on user SVA feature.\n");
5308ffccd11SJerry Snitselaar 		} else {
53142a1b738SDave Jiang 			set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
53242a1b738SDave Jiang 
53342a1b738SDave Jiang 			if (idxd_enable_system_pasid(idxd))
53442a1b738SDave Jiang 				dev_warn(dev, "No in-kernel DMA with PASID.\n");
53542a1b738SDave Jiang 			else
5368e50d392SDave Jiang 				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
5378ffccd11SJerry Snitselaar 		}
53803d939c7SDave Jiang 	} else if (!sva) {
53903d939c7SDave Jiang 		dev_warn(dev, "User forced SVA off via module param.\n");
5408e50d392SDave Jiang 	}
5418e50d392SDave Jiang 
542bfe1d560SDave Jiang 	idxd_read_caps(idxd);
543bfe1d560SDave Jiang 	idxd_read_table_offsets(idxd);
544bfe1d560SDave Jiang 
545bfe1d560SDave Jiang 	rc = idxd_setup_internals(idxd);
546bfe1d560SDave Jiang 	if (rc)
5477c5dd23eSDave Jiang 		goto err;
548bfe1d560SDave Jiang 
5498c66bbdcSDave Jiang 	/* If the configs are readonly, then load them from device */
5508c66bbdcSDave Jiang 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
5518c66bbdcSDave Jiang 		dev_dbg(dev, "Loading RO device config\n");
5528c66bbdcSDave Jiang 		rc = idxd_device_load_config(idxd);
5538c66bbdcSDave Jiang 		if (rc < 0)
554ddf742d4SDave Jiang 			goto err_config;
5558c66bbdcSDave Jiang 	}
5568c66bbdcSDave Jiang 
557bfe1d560SDave Jiang 	rc = idxd_setup_interrupts(idxd);
558bfe1d560SDave Jiang 	if (rc)
559ddf742d4SDave Jiang 		goto err_config;
560bfe1d560SDave Jiang 
56142d279f9SDave Jiang 	idxd->major = idxd_cdev_get_major(idxd);
56242d279f9SDave Jiang 
5630bde4444STom Zanussi 	rc = perfmon_pmu_init(idxd);
5640bde4444STom Zanussi 	if (rc < 0)
5650bde4444STom Zanussi 		dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
5660bde4444STom Zanussi 
567bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
568bfe1d560SDave Jiang 	return 0;
569bfe1d560SDave Jiang 
570ddf742d4SDave Jiang  err_config:
571ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
5727c5dd23eSDave Jiang  err:
5738e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
5748e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
57542a1b738SDave Jiang 	if (device_user_pasid_enabled(idxd))
576cf5f86a7SDave Jiang 		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
577bfe1d560SDave Jiang 	return rc;
578bfe1d560SDave Jiang }
579bfe1d560SDave Jiang 
580ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd)
581ddf742d4SDave Jiang {
582ddf742d4SDave Jiang 	struct device *dev = &idxd->pdev->dev;
583ddf742d4SDave Jiang 
584ddf742d4SDave Jiang 	perfmon_pmu_remove(idxd);
585ddf742d4SDave Jiang 	idxd_cleanup_interrupts(idxd);
586ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
587ddf742d4SDave Jiang 	if (device_pasid_enabled(idxd))
588ddf742d4SDave Jiang 		idxd_disable_system_pasid(idxd);
58942a1b738SDave Jiang 	if (device_user_pasid_enabled(idxd))
590ddf742d4SDave Jiang 		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
591ddf742d4SDave Jiang }
592ddf742d4SDave Jiang 
593bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
594bfe1d560SDave Jiang {
595bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
596bfe1d560SDave Jiang 	struct idxd_device *idxd;
597435b512dSDave Jiang 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
598bfe1d560SDave Jiang 	int rc;
599bfe1d560SDave Jiang 
600a39c7cd0SDave Jiang 	rc = pci_enable_device(pdev);
601bfe1d560SDave Jiang 	if (rc)
602bfe1d560SDave Jiang 		return rc;
603bfe1d560SDave Jiang 
6048e50d392SDave Jiang 	dev_dbg(dev, "Alloc IDXD context\n");
605435b512dSDave Jiang 	idxd = idxd_alloc(pdev, data);
606a39c7cd0SDave Jiang 	if (!idxd) {
607a39c7cd0SDave Jiang 		rc = -ENOMEM;
608a39c7cd0SDave Jiang 		goto err_idxd_alloc;
609a39c7cd0SDave Jiang 	}
610bfe1d560SDave Jiang 
6118e50d392SDave Jiang 	dev_dbg(dev, "Mapping BARs\n");
612a39c7cd0SDave Jiang 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
613a39c7cd0SDave Jiang 	if (!idxd->reg_base) {
614a39c7cd0SDave Jiang 		rc = -ENOMEM;
615a39c7cd0SDave Jiang 		goto err_iomap;
616a39c7cd0SDave Jiang 	}
617bfe1d560SDave Jiang 
618bfe1d560SDave Jiang 	dev_dbg(dev, "Set DMA masks\n");
61953b50458SChristophe JAILLET 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
620bfe1d560SDave Jiang 	if (rc)
621a39c7cd0SDave Jiang 		goto err;
622bfe1d560SDave Jiang 
623bfe1d560SDave Jiang 	dev_dbg(dev, "Set PCI master\n");
624bfe1d560SDave Jiang 	pci_set_master(pdev);
625bfe1d560SDave Jiang 	pci_set_drvdata(pdev, idxd);
626bfe1d560SDave Jiang 
627bfe1d560SDave Jiang 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
628bfe1d560SDave Jiang 	rc = idxd_probe(idxd);
629bfe1d560SDave Jiang 	if (rc) {
630bfe1d560SDave Jiang 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
631a39c7cd0SDave Jiang 		goto err;
632bfe1d560SDave Jiang 	}
633bfe1d560SDave Jiang 
63447c16ac2SDave Jiang 	rc = idxd_register_devices(idxd);
635c52ca478SDave Jiang 	if (rc) {
636c52ca478SDave Jiang 		dev_err(dev, "IDXD sysfs setup failed\n");
637ddf742d4SDave Jiang 		goto err_dev_register;
638c52ca478SDave Jiang 	}
639c52ca478SDave Jiang 
640bfe1d560SDave Jiang 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
641bfe1d560SDave Jiang 		 idxd->hw.version);
642bfe1d560SDave Jiang 
643bfe1d560SDave Jiang 	return 0;
644a39c7cd0SDave Jiang 
645ddf742d4SDave Jiang  err_dev_register:
646ddf742d4SDave Jiang 	idxd_cleanup(idxd);
647a39c7cd0SDave Jiang  err:
648a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
649a39c7cd0SDave Jiang  err_iomap:
650700af3a0SDave Jiang 	put_device(idxd_confdev(idxd));
651a39c7cd0SDave Jiang  err_idxd_alloc:
652a39c7cd0SDave Jiang 	pci_disable_device(pdev);
653a39c7cd0SDave Jiang 	return rc;
654bfe1d560SDave Jiang }
655bfe1d560SDave 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 
668bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
669bfe1d560SDave Jiang {
670bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
671bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
672403a2e23SDave Jiang 	int rc;
673bfe1d560SDave Jiang 
674bfe1d560SDave Jiang 	rc = idxd_device_disable(idxd);
675bfe1d560SDave Jiang 	if (rc)
676bfe1d560SDave Jiang 		dev_err(&pdev->dev, "Disabling device failed\n");
677bfe1d560SDave Jiang 
678403a2e23SDave Jiang 	irq_entry = &idxd->ie;
6795fc8e85fSDave Jiang 	synchronize_irq(irq_entry->vector);
680403a2e23SDave Jiang 	idxd_mask_error_interrupts(idxd);
68149c4959fSDave Jiang 	flush_workqueue(idxd->wq);
682bfe1d560SDave Jiang }
683bfe1d560SDave Jiang 
684bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
685bfe1d560SDave Jiang {
686bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
68749c4959fSDave Jiang 	struct idxd_irq_entry *irq_entry;
688bfe1d560SDave Jiang 
68998da0106SDave Jiang 	idxd_unregister_devices(idxd);
69098da0106SDave Jiang 	/*
69198da0106SDave Jiang 	 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
69298da0106SDave Jiang 	 * to the idxd context. The driver still needs those bits in order to do the rest of
69398da0106SDave Jiang 	 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
69498da0106SDave Jiang 	 * on the device here to hold off the freeing while allowing the idxd sub-driver
69598da0106SDave Jiang 	 * to unbind.
69698da0106SDave Jiang 	 */
69798da0106SDave Jiang 	get_device(idxd_confdev(idxd));
69898da0106SDave Jiang 	device_unregister(idxd_confdev(idxd));
699bfe1d560SDave Jiang 	idxd_shutdown(pdev);
7008e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
7018e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
70249c4959fSDave Jiang 
703403a2e23SDave Jiang 	irq_entry = idxd_get_ie(idxd, 0);
70449c4959fSDave Jiang 	free_irq(irq_entry->vector, irq_entry);
70549c4959fSDave Jiang 	pci_free_irq_vectors(pdev);
70649c4959fSDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
70742a1b738SDave Jiang 	if (device_user_pasid_enabled(idxd))
708cf5f86a7SDave Jiang 		iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
70949c4959fSDave Jiang 	pci_disable_device(pdev);
71049c4959fSDave Jiang 	destroy_workqueue(idxd->wq);
71149c4959fSDave Jiang 	perfmon_pmu_remove(idxd);
71298da0106SDave Jiang 	put_device(idxd_confdev(idxd));
713bfe1d560SDave Jiang }
714bfe1d560SDave Jiang 
715bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
716bfe1d560SDave Jiang 	.name		= DRV_NAME,
717bfe1d560SDave Jiang 	.id_table	= idxd_pci_tbl,
718bfe1d560SDave Jiang 	.probe		= idxd_pci_probe,
719bfe1d560SDave Jiang 	.remove		= idxd_remove,
720bfe1d560SDave Jiang 	.shutdown	= idxd_shutdown,
721bfe1d560SDave Jiang };
722bfe1d560SDave Jiang 
723bfe1d560SDave Jiang static int __init idxd_init_module(void)
724bfe1d560SDave Jiang {
7254b73e4ebSDave Jiang 	int err;
726bfe1d560SDave Jiang 
727bfe1d560SDave Jiang 	/*
7288e50d392SDave Jiang 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
729bfe1d560SDave Jiang 	 * enumerating the device. We can not utilize it.
730bfe1d560SDave Jiang 	 */
73174b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
732bfe1d560SDave Jiang 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
733bfe1d560SDave Jiang 		return -ENODEV;
734bfe1d560SDave Jiang 	}
735bfe1d560SDave Jiang 
73674b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
7378e50d392SDave Jiang 		pr_warn("Platform does not have ENQCMD(S) support.\n");
7388e50d392SDave Jiang 	else
7398e50d392SDave Jiang 		support_enqcmd = true;
740bfe1d560SDave Jiang 
7410bde4444STom Zanussi 	perfmon_init();
7420bde4444STom Zanussi 
743034b3290SDave Jiang 	err = idxd_driver_register(&idxd_drv);
744034b3290SDave Jiang 	if (err < 0)
745034b3290SDave Jiang 		goto err_idxd_driver_register;
746034b3290SDave Jiang 
7470cda4f69SDave Jiang 	err = idxd_driver_register(&idxd_dmaengine_drv);
7480cda4f69SDave Jiang 	if (err < 0)
7490cda4f69SDave Jiang 		goto err_idxd_dmaengine_driver_register;
7500cda4f69SDave Jiang 
751448c3de8SDave Jiang 	err = idxd_driver_register(&idxd_user_drv);
752448c3de8SDave Jiang 	if (err < 0)
753448c3de8SDave Jiang 		goto err_idxd_user_driver_register;
754448c3de8SDave Jiang 
75542d279f9SDave Jiang 	err = idxd_cdev_register();
75642d279f9SDave Jiang 	if (err)
75742d279f9SDave Jiang 		goto err_cdev_register;
75842d279f9SDave Jiang 
759c52ca478SDave Jiang 	err = pci_register_driver(&idxd_pci_driver);
760c52ca478SDave Jiang 	if (err)
761c52ca478SDave Jiang 		goto err_pci_register;
762c52ca478SDave Jiang 
763bfe1d560SDave Jiang 	return 0;
764c52ca478SDave Jiang 
765c52ca478SDave Jiang err_pci_register:
76642d279f9SDave Jiang 	idxd_cdev_remove();
76742d279f9SDave Jiang err_cdev_register:
768448c3de8SDave Jiang 	idxd_driver_unregister(&idxd_user_drv);
769448c3de8SDave Jiang err_idxd_user_driver_register:
7700cda4f69SDave Jiang 	idxd_driver_unregister(&idxd_dmaengine_drv);
7710cda4f69SDave Jiang err_idxd_dmaengine_driver_register:
772034b3290SDave Jiang 	idxd_driver_unregister(&idxd_drv);
773034b3290SDave Jiang err_idxd_driver_register:
774c52ca478SDave Jiang 	return err;
775bfe1d560SDave Jiang }
776bfe1d560SDave Jiang module_init(idxd_init_module);
777bfe1d560SDave Jiang 
778bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
779bfe1d560SDave Jiang {
780448c3de8SDave Jiang 	idxd_driver_unregister(&idxd_user_drv);
7810cda4f69SDave Jiang 	idxd_driver_unregister(&idxd_dmaengine_drv);
782034b3290SDave Jiang 	idxd_driver_unregister(&idxd_drv);
783bfe1d560SDave Jiang 	pci_unregister_driver(&idxd_pci_driver);
78442d279f9SDave Jiang 	idxd_cdev_remove();
7850bde4444STom Zanussi 	perfmon_exit();
786bfe1d560SDave Jiang }
787bfe1d560SDave Jiang module_exit(idxd_exit_module);
788