xref: /linux/drivers/dma/idxd/init.c (revision 244da66cda359227d80ccb41dbcb99da40eae186)
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/fs.h>
13bfe1d560SDave Jiang #include <linux/io-64-nonatomic-lo-hi.h>
14bfe1d560SDave Jiang #include <linux/device.h>
15bfe1d560SDave Jiang #include <linux/idr.h>
168e50d392SDave Jiang #include <linux/iommu.h>
17bfe1d560SDave Jiang #include <uapi/linux/idxd.h>
188f47d1a5SDave Jiang #include <linux/dmaengine.h>
198f47d1a5SDave Jiang #include "../dmaengine.h"
20bfe1d560SDave Jiang #include "registers.h"
21bfe1d560SDave Jiang #include "idxd.h"
220bde4444STom Zanussi #include "perfmon.h"
23bfe1d560SDave Jiang 
24bfe1d560SDave Jiang MODULE_VERSION(IDXD_DRIVER_VERSION);
25bfe1d560SDave Jiang MODULE_LICENSE("GPL v2");
26bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation");
27d9e5481fSDave Jiang MODULE_IMPORT_NS(IDXD);
28bfe1d560SDave Jiang 
2903d939c7SDave Jiang static bool sva = true;
3003d939c7SDave Jiang module_param(sva, bool, 0644);
3103d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
3203d939c7SDave Jiang 
33ade8a86bSDave Jiang bool tc_override;
34ade8a86bSDave Jiang module_param(tc_override, bool, 0644);
35ade8a86bSDave Jiang MODULE_PARM_DESC(tc_override, "Override traffic class defaults");
36ade8a86bSDave Jiang 
37bfe1d560SDave Jiang #define DRV_NAME "idxd"
38bfe1d560SDave Jiang 
398e50d392SDave Jiang bool support_enqcmd;
404b73e4ebSDave Jiang DEFINE_IDA(idxd_ida);
41bfe1d560SDave Jiang 
42435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = {
43435b512dSDave Jiang 	[IDXD_TYPE_DSA] = {
44435b512dSDave Jiang 		.name_prefix = "dsa",
45435b512dSDave Jiang 		.type = IDXD_TYPE_DSA,
46435b512dSDave Jiang 		.compl_size = sizeof(struct dsa_completion_record),
47435b512dSDave Jiang 		.align = 32,
48435b512dSDave Jiang 		.dev_type = &dsa_device_type,
49435b512dSDave Jiang 	},
50435b512dSDave Jiang 	[IDXD_TYPE_IAX] = {
51435b512dSDave Jiang 		.name_prefix = "iax",
52435b512dSDave Jiang 		.type = IDXD_TYPE_IAX,
53435b512dSDave Jiang 		.compl_size = sizeof(struct iax_completion_record),
54435b512dSDave Jiang 		.align = 64,
55435b512dSDave Jiang 		.dev_type = &iax_device_type,
56435b512dSDave Jiang 	},
57435b512dSDave Jiang };
58435b512dSDave Jiang 
59bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = {
60bfe1d560SDave Jiang 	/* DSA ver 1.0 platforms */
61435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
62f25b4638SDave Jiang 
63f25b4638SDave Jiang 	/* IAX ver 1.0 platforms */
64435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
65bfe1d560SDave Jiang 	{ 0, }
66bfe1d560SDave Jiang };
67bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
68bfe1d560SDave Jiang 
69bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd)
70bfe1d560SDave Jiang {
71bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
72bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
73ec0d6423SDave Jiang 	struct idxd_irq_entry *ie;
74bfe1d560SDave Jiang 	int i, msixcnt;
75bfe1d560SDave Jiang 	int rc = 0;
76bfe1d560SDave Jiang 
77bfe1d560SDave Jiang 	msixcnt = pci_msix_vec_count(pdev);
78bfe1d560SDave Jiang 	if (msixcnt < 0) {
79bfe1d560SDave Jiang 		dev_err(dev, "Not MSI-X interrupt capable.\n");
805fc8e85fSDave Jiang 		return -ENOSPC;
81bfe1d560SDave Jiang 	}
828b67426eSDave Jiang 	idxd->irq_cnt = msixcnt;
83bfe1d560SDave Jiang 
845fc8e85fSDave Jiang 	rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
855fc8e85fSDave Jiang 	if (rc != msixcnt) {
865fc8e85fSDave Jiang 		dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
875fc8e85fSDave Jiang 		return -ENOSPC;
88bfe1d560SDave Jiang 	}
89bfe1d560SDave Jiang 	dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
90bfe1d560SDave Jiang 
91d5c10e0fSDave Jiang 
92ec0d6423SDave Jiang 	ie = idxd_get_ie(idxd, 0);
93ec0d6423SDave Jiang 	ie->vector = pci_irq_vector(pdev, 0);
94ec0d6423SDave Jiang 	rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie);
95bfe1d560SDave Jiang 	if (rc < 0) {
96bfe1d560SDave Jiang 		dev_err(dev, "Failed to allocate misc interrupt.\n");
975fc8e85fSDave Jiang 		goto err_misc_irq;
98bfe1d560SDave Jiang 	}
99403a2e23SDave Jiang 	dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector);
100bfe1d560SDave Jiang 
101ec0d6423SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
102ec0d6423SDave Jiang 		int msix_idx = i + 1;
103bfe1d560SDave Jiang 
104ec0d6423SDave Jiang 		ie = idxd_get_ie(idxd, msix_idx);
105ec0d6423SDave Jiang 		ie->id = msix_idx;
106ec0d6423SDave Jiang 		ie->int_handle = INVALID_INT_HANDLE;
107ec0d6423SDave Jiang 		ie->pasid = INVALID_IOASID;
108403a2e23SDave Jiang 
109ec0d6423SDave Jiang 		spin_lock_init(&ie->list_lock);
110ec0d6423SDave Jiang 		init_llist_head(&ie->pending_llist);
111ec0d6423SDave Jiang 		INIT_LIST_HEAD(&ie->work_list);
112bfe1d560SDave Jiang 	}
113bfe1d560SDave Jiang 
114bfe1d560SDave Jiang 	idxd_unmask_error_interrupts(idxd);
115bfe1d560SDave Jiang 	return 0;
116bfe1d560SDave Jiang 
1175fc8e85fSDave Jiang  err_misc_irq:
118bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
1195fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
120bfe1d560SDave Jiang 	dev_err(dev, "No usable interrupts\n");
121bfe1d560SDave Jiang 	return rc;
122bfe1d560SDave Jiang }
123bfe1d560SDave Jiang 
124ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd)
125ddf742d4SDave Jiang {
126ddf742d4SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
127ec0d6423SDave Jiang 	struct idxd_irq_entry *ie;
128403a2e23SDave Jiang 	int msixcnt;
129ddf742d4SDave Jiang 
130403a2e23SDave Jiang 	msixcnt = pci_msix_vec_count(pdev);
131403a2e23SDave Jiang 	if (msixcnt <= 0)
132403a2e23SDave Jiang 		return;
133ddf742d4SDave Jiang 
134403a2e23SDave Jiang 	ie = idxd_get_ie(idxd, 0);
135ddf742d4SDave Jiang 	idxd_mask_error_interrupts(idxd);
136403a2e23SDave Jiang 	free_irq(ie->vector, ie);
137ddf742d4SDave Jiang 	pci_free_irq_vectors(pdev);
138ddf742d4SDave Jiang }
139ddf742d4SDave Jiang 
1407c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd)
1417c5dd23eSDave Jiang {
1427c5dd23eSDave Jiang 	struct device *dev = &idxd->pdev->dev;
1437c5dd23eSDave Jiang 	struct idxd_wq *wq;
144700af3a0SDave Jiang 	struct device *conf_dev;
1457c5dd23eSDave Jiang 	int i, rc;
1467c5dd23eSDave Jiang 
1477c5dd23eSDave Jiang 	idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
1487c5dd23eSDave Jiang 				 GFP_KERNEL, dev_to_node(dev));
1497c5dd23eSDave Jiang 	if (!idxd->wqs)
1507c5dd23eSDave Jiang 		return -ENOMEM;
1517c5dd23eSDave Jiang 
152de5819b9SJerry Snitselaar 	idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
153de5819b9SJerry Snitselaar 	if (!idxd->wq_enable_map) {
154de5819b9SJerry Snitselaar 		kfree(idxd->wqs);
155de5819b9SJerry Snitselaar 		return -ENOMEM;
156de5819b9SJerry Snitselaar 	}
157de5819b9SJerry Snitselaar 
1587c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
1597c5dd23eSDave Jiang 		wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
1607c5dd23eSDave Jiang 		if (!wq) {
1617c5dd23eSDave Jiang 			rc = -ENOMEM;
1627c5dd23eSDave Jiang 			goto err;
1637c5dd23eSDave Jiang 		}
1647c5dd23eSDave Jiang 
165700af3a0SDave Jiang 		idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
166700af3a0SDave Jiang 		conf_dev = wq_confdev(wq);
1677c5dd23eSDave Jiang 		wq->id = i;
1687c5dd23eSDave Jiang 		wq->idxd = idxd;
169700af3a0SDave Jiang 		device_initialize(wq_confdev(wq));
170700af3a0SDave Jiang 		conf_dev->parent = idxd_confdev(idxd);
171700af3a0SDave Jiang 		conf_dev->bus = &dsa_bus_type;
172700af3a0SDave Jiang 		conf_dev->type = &idxd_wq_device_type;
173700af3a0SDave Jiang 		rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
1747c5dd23eSDave Jiang 		if (rc < 0) {
175700af3a0SDave Jiang 			put_device(conf_dev);
1767c5dd23eSDave Jiang 			goto err;
1777c5dd23eSDave Jiang 		}
1787c5dd23eSDave Jiang 
1797c5dd23eSDave Jiang 		mutex_init(&wq->wq_lock);
18004922b74SDave Jiang 		init_waitqueue_head(&wq->err_queue);
18193a40a6dSDave Jiang 		init_completion(&wq->wq_dead);
18256fc39f5SDave Jiang 		init_completion(&wq->wq_resurrect);
18392452a72SDave Jiang 		wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
184e8dbd644SXiaochen Shen 		idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH);
1857930d855SDave Jiang 		wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
1867c5dd23eSDave Jiang 		wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
1877c5dd23eSDave Jiang 		if (!wq->wqcfg) {
188700af3a0SDave Jiang 			put_device(conf_dev);
1897c5dd23eSDave Jiang 			rc = -ENOMEM;
1907c5dd23eSDave Jiang 			goto err;
1917c5dd23eSDave Jiang 		}
192b0325aefSDave Jiang 
193b0325aefSDave Jiang 		if (idxd->hw.wq_cap.op_config) {
194b0325aefSDave Jiang 			wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
195b0325aefSDave Jiang 			if (!wq->opcap_bmap) {
196b0325aefSDave Jiang 				put_device(conf_dev);
197b0325aefSDave Jiang 				rc = -ENOMEM;
198b0325aefSDave Jiang 				goto err;
199b0325aefSDave Jiang 			}
200b0325aefSDave Jiang 			bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
201b0325aefSDave Jiang 		}
2027c5dd23eSDave Jiang 		idxd->wqs[i] = wq;
2037c5dd23eSDave Jiang 	}
2047c5dd23eSDave Jiang 
2057c5dd23eSDave Jiang 	return 0;
2067c5dd23eSDave Jiang 
2077c5dd23eSDave Jiang  err:
208700af3a0SDave Jiang 	while (--i >= 0) {
209700af3a0SDave Jiang 		wq = idxd->wqs[i];
210700af3a0SDave Jiang 		conf_dev = wq_confdev(wq);
211700af3a0SDave Jiang 		put_device(conf_dev);
212700af3a0SDave Jiang 	}
2137c5dd23eSDave Jiang 	return rc;
2147c5dd23eSDave Jiang }
2157c5dd23eSDave Jiang 
21675b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd)
21775b91130SDave Jiang {
21875b91130SDave Jiang 	struct idxd_engine *engine;
21975b91130SDave Jiang 	struct device *dev = &idxd->pdev->dev;
220700af3a0SDave Jiang 	struct device *conf_dev;
22175b91130SDave Jiang 	int i, rc;
22275b91130SDave Jiang 
22375b91130SDave Jiang 	idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
22475b91130SDave Jiang 				     GFP_KERNEL, dev_to_node(dev));
22575b91130SDave Jiang 	if (!idxd->engines)
22675b91130SDave Jiang 		return -ENOMEM;
22775b91130SDave Jiang 
22875b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++) {
22975b91130SDave Jiang 		engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
23075b91130SDave Jiang 		if (!engine) {
23175b91130SDave Jiang 			rc = -ENOMEM;
23275b91130SDave Jiang 			goto err;
23375b91130SDave Jiang 		}
23475b91130SDave Jiang 
235700af3a0SDave Jiang 		idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE);
236700af3a0SDave Jiang 		conf_dev = engine_confdev(engine);
23775b91130SDave Jiang 		engine->id = i;
23875b91130SDave Jiang 		engine->idxd = idxd;
239700af3a0SDave Jiang 		device_initialize(conf_dev);
240700af3a0SDave Jiang 		conf_dev->parent = idxd_confdev(idxd);
241700af3a0SDave Jiang 		conf_dev->bus = &dsa_bus_type;
242700af3a0SDave Jiang 		conf_dev->type = &idxd_engine_device_type;
243700af3a0SDave Jiang 		rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
24475b91130SDave Jiang 		if (rc < 0) {
245700af3a0SDave Jiang 			put_device(conf_dev);
24675b91130SDave Jiang 			goto err;
24775b91130SDave Jiang 		}
24875b91130SDave Jiang 
24975b91130SDave Jiang 		idxd->engines[i] = engine;
25075b91130SDave Jiang 	}
25175b91130SDave Jiang 
25275b91130SDave Jiang 	return 0;
25375b91130SDave Jiang 
25475b91130SDave Jiang  err:
255700af3a0SDave Jiang 	while (--i >= 0) {
256700af3a0SDave Jiang 		engine = idxd->engines[i];
257700af3a0SDave Jiang 		conf_dev = engine_confdev(engine);
258700af3a0SDave Jiang 		put_device(conf_dev);
259700af3a0SDave Jiang 	}
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;
266700af3a0SDave Jiang 	struct device *conf_dev;
267defe49f9SDave Jiang 	struct idxd_group *group;
268defe49f9SDave Jiang 	int i, rc;
269defe49f9SDave Jiang 
270defe49f9SDave Jiang 	idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
271defe49f9SDave Jiang 				    GFP_KERNEL, dev_to_node(dev));
272defe49f9SDave Jiang 	if (!idxd->groups)
273defe49f9SDave Jiang 		return -ENOMEM;
274defe49f9SDave Jiang 
275defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++) {
276defe49f9SDave Jiang 		group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
277defe49f9SDave Jiang 		if (!group) {
278defe49f9SDave Jiang 			rc = -ENOMEM;
279defe49f9SDave Jiang 			goto err;
280defe49f9SDave Jiang 		}
281defe49f9SDave Jiang 
282700af3a0SDave Jiang 		idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP);
283700af3a0SDave Jiang 		conf_dev = group_confdev(group);
284defe49f9SDave Jiang 		group->id = i;
285defe49f9SDave Jiang 		group->idxd = idxd;
286700af3a0SDave Jiang 		device_initialize(conf_dev);
287700af3a0SDave Jiang 		conf_dev->parent = idxd_confdev(idxd);
288700af3a0SDave Jiang 		conf_dev->bus = &dsa_bus_type;
289700af3a0SDave Jiang 		conf_dev->type = &idxd_group_device_type;
290700af3a0SDave Jiang 		rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
291defe49f9SDave Jiang 		if (rc < 0) {
292700af3a0SDave Jiang 			put_device(conf_dev);
293defe49f9SDave Jiang 			goto err;
294defe49f9SDave Jiang 		}
295defe49f9SDave Jiang 
296defe49f9SDave Jiang 		idxd->groups[i] = group;
2979735bde3SFenghua Yu 		if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) {
298ade8a86bSDave Jiang 			group->tc_a = 1;
299ade8a86bSDave Jiang 			group->tc_b = 1;
300ade8a86bSDave Jiang 		} else {
301defe49f9SDave Jiang 			group->tc_a = -1;
302defe49f9SDave Jiang 			group->tc_b = -1;
303defe49f9SDave Jiang 		}
304601bdadaSFenghua Yu 		/*
305601bdadaSFenghua Yu 		 * The default value is the same as the value of
306601bdadaSFenghua Yu 		 * total read buffers in GRPCAP.
307601bdadaSFenghua Yu 		 */
308601bdadaSFenghua Yu 		group->rdbufs_allowed = idxd->max_rdbufs;
309ade8a86bSDave Jiang 	}
310defe49f9SDave Jiang 
311defe49f9SDave Jiang 	return 0;
312defe49f9SDave Jiang 
313defe49f9SDave Jiang  err:
314700af3a0SDave Jiang 	while (--i >= 0) {
315700af3a0SDave Jiang 		group = idxd->groups[i];
316700af3a0SDave Jiang 		put_device(group_confdev(group));
317700af3a0SDave Jiang 	}
318defe49f9SDave Jiang 	return rc;
319defe49f9SDave Jiang }
320defe49f9SDave Jiang 
321ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd)
322ddf742d4SDave Jiang {
323ddf742d4SDave Jiang 	int i;
324ddf742d4SDave Jiang 
325ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
326700af3a0SDave Jiang 		put_device(group_confdev(idxd->groups[i]));
327ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
328700af3a0SDave Jiang 		put_device(engine_confdev(idxd->engines[i]));
329ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
330700af3a0SDave Jiang 		put_device(wq_confdev(idxd->wqs[i]));
331ddf742d4SDave Jiang 	destroy_workqueue(idxd->wq);
332ddf742d4SDave Jiang }
333ddf742d4SDave Jiang 
3341649091fSDave Jiang static int idxd_init_evl(struct idxd_device *idxd)
3351649091fSDave Jiang {
3361649091fSDave Jiang 	struct device *dev = &idxd->pdev->dev;
3371649091fSDave Jiang 	struct idxd_evl *evl;
3381649091fSDave Jiang 
3391649091fSDave Jiang 	if (idxd->hw.gen_cap.evl_support == 0)
3401649091fSDave Jiang 		return 0;
3411649091fSDave Jiang 
3421649091fSDave Jiang 	evl = kzalloc_node(sizeof(*evl), GFP_KERNEL, dev_to_node(dev));
3431649091fSDave Jiang 	if (!evl)
3441649091fSDave Jiang 		return -ENOMEM;
3451649091fSDave Jiang 
346*244da66cSDave Jiang 	spin_lock_init(&evl->lock);
3471649091fSDave Jiang 	evl->size = IDXD_EVL_SIZE_MIN;
3481649091fSDave Jiang 	idxd->evl = evl;
3491649091fSDave Jiang 	return 0;
3501649091fSDave Jiang }
3511649091fSDave Jiang 
352bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd)
353bfe1d560SDave Jiang {
354bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
355defe49f9SDave Jiang 	int rc, i;
356bfe1d560SDave Jiang 
3570d5c10b4SDave Jiang 	init_waitqueue_head(&idxd->cmd_waitq);
3587c5dd23eSDave Jiang 
3597c5dd23eSDave Jiang 	rc = idxd_setup_wqs(idxd);
3607c5dd23eSDave Jiang 	if (rc < 0)
361eb15e715SDave Jiang 		goto err_wqs;
3627c5dd23eSDave Jiang 
36375b91130SDave Jiang 	rc = idxd_setup_engines(idxd);
36475b91130SDave Jiang 	if (rc < 0)
36575b91130SDave Jiang 		goto err_engine;
36675b91130SDave Jiang 
367defe49f9SDave Jiang 	rc = idxd_setup_groups(idxd);
368defe49f9SDave Jiang 	if (rc < 0)
369defe49f9SDave Jiang 		goto err_group;
370bfe1d560SDave Jiang 
3710d5c10b4SDave Jiang 	idxd->wq = create_workqueue(dev_name(dev));
3727c5dd23eSDave Jiang 	if (!idxd->wq) {
3737c5dd23eSDave Jiang 		rc = -ENOMEM;
374defe49f9SDave Jiang 		goto err_wkq_create;
3757c5dd23eSDave Jiang 	}
3760d5c10b4SDave Jiang 
3771649091fSDave Jiang 	rc = idxd_init_evl(idxd);
3781649091fSDave Jiang 	if (rc < 0)
3791649091fSDave Jiang 		goto err_evl;
3801649091fSDave Jiang 
381bfe1d560SDave Jiang 	return 0;
3827c5dd23eSDave Jiang 
3831649091fSDave Jiang  err_evl:
3841649091fSDave Jiang 	destroy_workqueue(idxd->wq);
385defe49f9SDave Jiang  err_wkq_create:
386defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
387700af3a0SDave Jiang 		put_device(group_confdev(idxd->groups[i]));
388defe49f9SDave Jiang  err_group:
38975b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
390700af3a0SDave Jiang 		put_device(engine_confdev(idxd->engines[i]));
39175b91130SDave Jiang  err_engine:
3927c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
393700af3a0SDave Jiang 		put_device(wq_confdev(idxd->wqs[i]));
394eb15e715SDave Jiang  err_wqs:
3957c5dd23eSDave Jiang 	return rc;
396bfe1d560SDave Jiang }
397bfe1d560SDave Jiang 
398bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd)
399bfe1d560SDave Jiang {
400bfe1d560SDave Jiang 	union offsets_reg offsets;
401bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
402bfe1d560SDave Jiang 
403bfe1d560SDave Jiang 	offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
4042f8417a9SDave Jiang 	offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
4052f8417a9SDave Jiang 	idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
406bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
4072f8417a9SDave Jiang 	idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
4082f8417a9SDave Jiang 	dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
4092f8417a9SDave Jiang 	idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
4102f8417a9SDave Jiang 	dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
4112f8417a9SDave Jiang 	idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
412bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
413bfe1d560SDave Jiang }
414bfe1d560SDave Jiang 
41534ca0066SDave Jiang void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count)
416a8563a33SDave Jiang {
417a8563a33SDave Jiang 	int i, j, nr;
418a8563a33SDave Jiang 
419a8563a33SDave Jiang 	for (i = 0, nr = 0; i < count; i++) {
420a8563a33SDave Jiang 		for (j = 0; j < BITS_PER_LONG_LONG; j++) {
421a8563a33SDave Jiang 			if (val[i] & BIT(j))
422a8563a33SDave Jiang 				set_bit(nr, bmap);
423a8563a33SDave Jiang 			nr++;
424a8563a33SDave Jiang 		}
425a8563a33SDave Jiang 	}
426a8563a33SDave Jiang }
427a8563a33SDave Jiang 
428bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd)
429bfe1d560SDave Jiang {
430bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
431bfe1d560SDave Jiang 	int i;
432bfe1d560SDave Jiang 
433bfe1d560SDave Jiang 	/* reading generic capabilities */
434bfe1d560SDave Jiang 	idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
435bfe1d560SDave Jiang 	dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
436eb15e715SDave Jiang 
437eb15e715SDave Jiang 	if (idxd->hw.gen_cap.cmd_cap) {
438eb15e715SDave Jiang 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
439eb15e715SDave Jiang 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
440eb15e715SDave Jiang 	}
441eb15e715SDave Jiang 
4428b67426eSDave Jiang 	/* reading command capabilities */
4438b67426eSDave Jiang 	if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE))
4448b67426eSDave Jiang 		idxd->request_int_handles = true;
4458b67426eSDave Jiang 
446bfe1d560SDave Jiang 	idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
447bfe1d560SDave Jiang 	dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
448e8dbd644SXiaochen Shen 	idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift);
449bfe1d560SDave Jiang 	dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
450bfe1d560SDave Jiang 	if (idxd->hw.gen_cap.config_en)
451bfe1d560SDave Jiang 		set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
452bfe1d560SDave Jiang 
453bfe1d560SDave Jiang 	/* reading group capabilities */
454bfe1d560SDave Jiang 	idxd->hw.group_cap.bits =
455bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
456bfe1d560SDave Jiang 	dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
457bfe1d560SDave Jiang 	idxd->max_groups = idxd->hw.group_cap.num_groups;
458bfe1d560SDave Jiang 	dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
4597ed6f1b8SDave Jiang 	idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs;
4607ed6f1b8SDave Jiang 	dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs);
4617ed6f1b8SDave Jiang 	idxd->nr_rdbufs = idxd->max_rdbufs;
462bfe1d560SDave Jiang 
463bfe1d560SDave Jiang 	/* read engine capabilities */
464bfe1d560SDave Jiang 	idxd->hw.engine_cap.bits =
465bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
466bfe1d560SDave Jiang 	dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
467bfe1d560SDave Jiang 	idxd->max_engines = idxd->hw.engine_cap.num_engines;
468bfe1d560SDave Jiang 	dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
469bfe1d560SDave Jiang 
470bfe1d560SDave Jiang 	/* read workqueue capabilities */
471bfe1d560SDave Jiang 	idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
472bfe1d560SDave Jiang 	dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
473bfe1d560SDave Jiang 	idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
474bfe1d560SDave Jiang 	dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
475bfe1d560SDave Jiang 	idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
476bfe1d560SDave Jiang 	dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
477d98793b5SDave Jiang 	idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
478d98793b5SDave Jiang 	dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
479bfe1d560SDave Jiang 
480bfe1d560SDave Jiang 	/* reading operation capabilities */
481bfe1d560SDave Jiang 	for (i = 0; i < 4; i++) {
482bfe1d560SDave Jiang 		idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
483bfe1d560SDave Jiang 				IDXD_OPCAP_OFFSET + i * sizeof(u64));
484bfe1d560SDave Jiang 		dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
485bfe1d560SDave Jiang 	}
486a8563a33SDave Jiang 	multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4);
4879f0d99b3SDave Jiang 
4889f0d99b3SDave Jiang 	/* read iaa cap */
4899f0d99b3SDave Jiang 	if (idxd->data->type == IDXD_TYPE_IAX && idxd->hw.version >= DEVICE_VERSION_2)
4909f0d99b3SDave Jiang 		idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET);
491bfe1d560SDave Jiang }
492bfe1d560SDave Jiang 
493435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
494bfe1d560SDave Jiang {
495bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
496700af3a0SDave Jiang 	struct device *conf_dev;
497bfe1d560SDave Jiang 	struct idxd_device *idxd;
49847c16ac2SDave Jiang 	int rc;
499bfe1d560SDave Jiang 
50047c16ac2SDave Jiang 	idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
501bfe1d560SDave Jiang 	if (!idxd)
502bfe1d560SDave Jiang 		return NULL;
503bfe1d560SDave Jiang 
504700af3a0SDave Jiang 	conf_dev = idxd_confdev(idxd);
505bfe1d560SDave Jiang 	idxd->pdev = pdev;
506435b512dSDave Jiang 	idxd->data = data;
507700af3a0SDave Jiang 	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
5084b73e4ebSDave Jiang 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
50947c16ac2SDave Jiang 	if (idxd->id < 0)
51047c16ac2SDave Jiang 		return NULL;
51147c16ac2SDave Jiang 
512a8563a33SDave Jiang 	idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev));
513a8563a33SDave Jiang 	if (!idxd->opcap_bmap) {
514a8563a33SDave Jiang 		ida_free(&idxd_ida, idxd->id);
515a8563a33SDave Jiang 		return NULL;
516a8563a33SDave Jiang 	}
517a8563a33SDave Jiang 
518700af3a0SDave Jiang 	device_initialize(conf_dev);
519700af3a0SDave Jiang 	conf_dev->parent = dev;
520700af3a0SDave Jiang 	conf_dev->bus = &dsa_bus_type;
521700af3a0SDave Jiang 	conf_dev->type = idxd->data->dev_type;
522700af3a0SDave Jiang 	rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
52347c16ac2SDave Jiang 	if (rc < 0) {
524700af3a0SDave Jiang 		put_device(conf_dev);
52547c16ac2SDave Jiang 		return NULL;
52647c16ac2SDave Jiang 	}
52747c16ac2SDave Jiang 
528bfe1d560SDave Jiang 	spin_lock_init(&idxd->dev_lock);
52953b2ee7fSDave Jiang 	spin_lock_init(&idxd->cmd_lock);
530bfe1d560SDave Jiang 
531bfe1d560SDave Jiang 	return idxd;
532bfe1d560SDave Jiang }
533bfe1d560SDave Jiang 
5348e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd)
5358e50d392SDave Jiang {
536942fd543SLu Baolu 	return -EOPNOTSUPP;
5378e50d392SDave Jiang }
5388e50d392SDave Jiang 
5398e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd)
5408e50d392SDave Jiang {
5418e50d392SDave Jiang 
5428e50d392SDave Jiang 	iommu_sva_unbind_device(idxd->sva);
5438e50d392SDave Jiang 	idxd->sva = NULL;
5448e50d392SDave Jiang }
5458e50d392SDave Jiang 
546bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd)
547bfe1d560SDave Jiang {
548bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
549bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
550bfe1d560SDave Jiang 	int rc;
551bfe1d560SDave Jiang 
552bfe1d560SDave Jiang 	dev_dbg(dev, "%s entered and resetting device\n", __func__);
55389e3becdSDave Jiang 	rc = idxd_device_init_reset(idxd);
55489e3becdSDave Jiang 	if (rc < 0)
55589e3becdSDave Jiang 		return rc;
55689e3becdSDave Jiang 
557bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD reset complete\n");
558bfe1d560SDave Jiang 
55903d939c7SDave Jiang 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
5608ffccd11SJerry Snitselaar 		if (iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA)) {
56142a1b738SDave Jiang 			dev_warn(dev, "Unable to turn on user SVA feature.\n");
5628ffccd11SJerry Snitselaar 		} else {
56342a1b738SDave Jiang 			set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
56442a1b738SDave Jiang 
56542a1b738SDave Jiang 			if (idxd_enable_system_pasid(idxd))
56642a1b738SDave Jiang 				dev_warn(dev, "No in-kernel DMA with PASID.\n");
56742a1b738SDave Jiang 			else
5688e50d392SDave Jiang 				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
5698ffccd11SJerry Snitselaar 		}
57003d939c7SDave Jiang 	} else if (!sva) {
57103d939c7SDave Jiang 		dev_warn(dev, "User forced SVA off via module param.\n");
5728e50d392SDave Jiang 	}
5738e50d392SDave Jiang 
574bfe1d560SDave Jiang 	idxd_read_caps(idxd);
575bfe1d560SDave Jiang 	idxd_read_table_offsets(idxd);
576bfe1d560SDave Jiang 
577bfe1d560SDave Jiang 	rc = idxd_setup_internals(idxd);
578bfe1d560SDave Jiang 	if (rc)
5797c5dd23eSDave Jiang 		goto err;
580bfe1d560SDave Jiang 
5818c66bbdcSDave Jiang 	/* If the configs are readonly, then load them from device */
5828c66bbdcSDave Jiang 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
5838c66bbdcSDave Jiang 		dev_dbg(dev, "Loading RO device config\n");
5848c66bbdcSDave Jiang 		rc = idxd_device_load_config(idxd);
5858c66bbdcSDave Jiang 		if (rc < 0)
586ddf742d4SDave Jiang 			goto err_config;
5878c66bbdcSDave Jiang 	}
5888c66bbdcSDave Jiang 
589bfe1d560SDave Jiang 	rc = idxd_setup_interrupts(idxd);
590bfe1d560SDave Jiang 	if (rc)
591ddf742d4SDave Jiang 		goto err_config;
592bfe1d560SDave Jiang 
59342d279f9SDave Jiang 	idxd->major = idxd_cdev_get_major(idxd);
59442d279f9SDave Jiang 
5950bde4444STom Zanussi 	rc = perfmon_pmu_init(idxd);
5960bde4444STom Zanussi 	if (rc < 0)
5970bde4444STom Zanussi 		dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
5980bde4444STom Zanussi 
599bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
600bfe1d560SDave Jiang 	return 0;
601bfe1d560SDave Jiang 
602ddf742d4SDave Jiang  err_config:
603ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
6047c5dd23eSDave Jiang  err:
6058e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
6068e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
60742a1b738SDave Jiang 	if (device_user_pasid_enabled(idxd))
608cf5f86a7SDave Jiang 		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
609bfe1d560SDave Jiang 	return rc;
610bfe1d560SDave Jiang }
611bfe1d560SDave Jiang 
612ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd)
613ddf742d4SDave Jiang {
614ddf742d4SDave Jiang 	struct device *dev = &idxd->pdev->dev;
615ddf742d4SDave Jiang 
616ddf742d4SDave Jiang 	perfmon_pmu_remove(idxd);
617ddf742d4SDave Jiang 	idxd_cleanup_interrupts(idxd);
618ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
619ddf742d4SDave Jiang 	if (device_pasid_enabled(idxd))
620ddf742d4SDave Jiang 		idxd_disable_system_pasid(idxd);
62142a1b738SDave Jiang 	if (device_user_pasid_enabled(idxd))
622ddf742d4SDave Jiang 		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
623ddf742d4SDave Jiang }
624ddf742d4SDave Jiang 
625bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
626bfe1d560SDave Jiang {
627bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
628bfe1d560SDave Jiang 	struct idxd_device *idxd;
629435b512dSDave Jiang 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
630bfe1d560SDave Jiang 	int rc;
631bfe1d560SDave Jiang 
632a39c7cd0SDave Jiang 	rc = pci_enable_device(pdev);
633bfe1d560SDave Jiang 	if (rc)
634bfe1d560SDave Jiang 		return rc;
635bfe1d560SDave Jiang 
6368e50d392SDave Jiang 	dev_dbg(dev, "Alloc IDXD context\n");
637435b512dSDave Jiang 	idxd = idxd_alloc(pdev, data);
638a39c7cd0SDave Jiang 	if (!idxd) {
639a39c7cd0SDave Jiang 		rc = -ENOMEM;
640a39c7cd0SDave Jiang 		goto err_idxd_alloc;
641a39c7cd0SDave Jiang 	}
642bfe1d560SDave Jiang 
6438e50d392SDave Jiang 	dev_dbg(dev, "Mapping BARs\n");
644a39c7cd0SDave Jiang 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
645a39c7cd0SDave Jiang 	if (!idxd->reg_base) {
646a39c7cd0SDave Jiang 		rc = -ENOMEM;
647a39c7cd0SDave Jiang 		goto err_iomap;
648a39c7cd0SDave Jiang 	}
649bfe1d560SDave Jiang 
650bfe1d560SDave Jiang 	dev_dbg(dev, "Set DMA masks\n");
65153b50458SChristophe JAILLET 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
652bfe1d560SDave Jiang 	if (rc)
653a39c7cd0SDave Jiang 		goto err;
654bfe1d560SDave Jiang 
655bfe1d560SDave Jiang 	dev_dbg(dev, "Set PCI master\n");
656bfe1d560SDave Jiang 	pci_set_master(pdev);
657bfe1d560SDave Jiang 	pci_set_drvdata(pdev, idxd);
658bfe1d560SDave Jiang 
659bfe1d560SDave Jiang 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
660bfe1d560SDave Jiang 	rc = idxd_probe(idxd);
661bfe1d560SDave Jiang 	if (rc) {
662bfe1d560SDave Jiang 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
663a39c7cd0SDave Jiang 		goto err;
664bfe1d560SDave Jiang 	}
665bfe1d560SDave Jiang 
66647c16ac2SDave Jiang 	rc = idxd_register_devices(idxd);
667c52ca478SDave Jiang 	if (rc) {
668c52ca478SDave Jiang 		dev_err(dev, "IDXD sysfs setup failed\n");
669ddf742d4SDave Jiang 		goto err_dev_register;
670c52ca478SDave Jiang 	}
671c52ca478SDave Jiang 
672bfe1d560SDave Jiang 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
673bfe1d560SDave Jiang 		 idxd->hw.version);
674bfe1d560SDave Jiang 
675bfe1d560SDave Jiang 	return 0;
676a39c7cd0SDave Jiang 
677ddf742d4SDave Jiang  err_dev_register:
678ddf742d4SDave Jiang 	idxd_cleanup(idxd);
679a39c7cd0SDave Jiang  err:
680a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
681a39c7cd0SDave Jiang  err_iomap:
682700af3a0SDave Jiang 	put_device(idxd_confdev(idxd));
683a39c7cd0SDave Jiang  err_idxd_alloc:
684a39c7cd0SDave Jiang 	pci_disable_device(pdev);
685a39c7cd0SDave Jiang 	return rc;
686bfe1d560SDave Jiang }
687bfe1d560SDave Jiang 
6885b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd)
6895b0c68c4SDave Jiang {
6905b0c68c4SDave Jiang 	struct idxd_wq *wq;
6915b0c68c4SDave Jiang 	int i;
6925b0c68c4SDave Jiang 
6935b0c68c4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
6945b0c68c4SDave Jiang 		wq = idxd->wqs[i];
6955b0c68c4SDave Jiang 		if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
6965b0c68c4SDave Jiang 			idxd_wq_quiesce(wq);
6975b0c68c4SDave Jiang 	}
6985b0c68c4SDave Jiang }
6995b0c68c4SDave Jiang 
700bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
701bfe1d560SDave Jiang {
702bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
703bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
704403a2e23SDave Jiang 	int rc;
705bfe1d560SDave Jiang 
706bfe1d560SDave Jiang 	rc = idxd_device_disable(idxd);
707bfe1d560SDave Jiang 	if (rc)
708bfe1d560SDave Jiang 		dev_err(&pdev->dev, "Disabling device failed\n");
709bfe1d560SDave Jiang 
710403a2e23SDave Jiang 	irq_entry = &idxd->ie;
7115fc8e85fSDave Jiang 	synchronize_irq(irq_entry->vector);
712403a2e23SDave Jiang 	idxd_mask_error_interrupts(idxd);
71349c4959fSDave Jiang 	flush_workqueue(idxd->wq);
714bfe1d560SDave Jiang }
715bfe1d560SDave Jiang 
716bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
717bfe1d560SDave Jiang {
718bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
71949c4959fSDave Jiang 	struct idxd_irq_entry *irq_entry;
720bfe1d560SDave Jiang 
72198da0106SDave Jiang 	idxd_unregister_devices(idxd);
72298da0106SDave Jiang 	/*
72398da0106SDave Jiang 	 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
72498da0106SDave Jiang 	 * to the idxd context. The driver still needs those bits in order to do the rest of
72598da0106SDave Jiang 	 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
72698da0106SDave Jiang 	 * on the device here to hold off the freeing while allowing the idxd sub-driver
72798da0106SDave Jiang 	 * to unbind.
72898da0106SDave Jiang 	 */
72998da0106SDave Jiang 	get_device(idxd_confdev(idxd));
73098da0106SDave Jiang 	device_unregister(idxd_confdev(idxd));
731bfe1d560SDave Jiang 	idxd_shutdown(pdev);
7328e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
7338e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
73449c4959fSDave Jiang 
735403a2e23SDave Jiang 	irq_entry = idxd_get_ie(idxd, 0);
73649c4959fSDave Jiang 	free_irq(irq_entry->vector, irq_entry);
73749c4959fSDave Jiang 	pci_free_irq_vectors(pdev);
73849c4959fSDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
73942a1b738SDave Jiang 	if (device_user_pasid_enabled(idxd))
740cf5f86a7SDave Jiang 		iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
74149c4959fSDave Jiang 	pci_disable_device(pdev);
74249c4959fSDave Jiang 	destroy_workqueue(idxd->wq);
74349c4959fSDave Jiang 	perfmon_pmu_remove(idxd);
74498da0106SDave Jiang 	put_device(idxd_confdev(idxd));
745bfe1d560SDave Jiang }
746bfe1d560SDave Jiang 
747bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
748bfe1d560SDave Jiang 	.name		= DRV_NAME,
749bfe1d560SDave Jiang 	.id_table	= idxd_pci_tbl,
750bfe1d560SDave Jiang 	.probe		= idxd_pci_probe,
751bfe1d560SDave Jiang 	.remove		= idxd_remove,
752bfe1d560SDave Jiang 	.shutdown	= idxd_shutdown,
753bfe1d560SDave Jiang };
754bfe1d560SDave Jiang 
755bfe1d560SDave Jiang static int __init idxd_init_module(void)
756bfe1d560SDave Jiang {
7574b73e4ebSDave Jiang 	int err;
758bfe1d560SDave Jiang 
759bfe1d560SDave Jiang 	/*
7608e50d392SDave Jiang 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
761bfe1d560SDave Jiang 	 * enumerating the device. We can not utilize it.
762bfe1d560SDave Jiang 	 */
76374b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
764bfe1d560SDave Jiang 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
765bfe1d560SDave Jiang 		return -ENODEV;
766bfe1d560SDave Jiang 	}
767bfe1d560SDave Jiang 
76874b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
7698e50d392SDave Jiang 		pr_warn("Platform does not have ENQCMD(S) support.\n");
7708e50d392SDave Jiang 	else
7718e50d392SDave Jiang 		support_enqcmd = true;
772bfe1d560SDave Jiang 
7730bde4444STom Zanussi 	perfmon_init();
7740bde4444STom Zanussi 
775034b3290SDave Jiang 	err = idxd_driver_register(&idxd_drv);
776034b3290SDave Jiang 	if (err < 0)
777034b3290SDave Jiang 		goto err_idxd_driver_register;
778034b3290SDave Jiang 
7790cda4f69SDave Jiang 	err = idxd_driver_register(&idxd_dmaengine_drv);
7800cda4f69SDave Jiang 	if (err < 0)
7810cda4f69SDave Jiang 		goto err_idxd_dmaengine_driver_register;
7820cda4f69SDave Jiang 
783448c3de8SDave Jiang 	err = idxd_driver_register(&idxd_user_drv);
784448c3de8SDave Jiang 	if (err < 0)
785448c3de8SDave Jiang 		goto err_idxd_user_driver_register;
786448c3de8SDave Jiang 
78742d279f9SDave Jiang 	err = idxd_cdev_register();
78842d279f9SDave Jiang 	if (err)
78942d279f9SDave Jiang 		goto err_cdev_register;
79042d279f9SDave Jiang 
791c52ca478SDave Jiang 	err = pci_register_driver(&idxd_pci_driver);
792c52ca478SDave Jiang 	if (err)
793c52ca478SDave Jiang 		goto err_pci_register;
794c52ca478SDave Jiang 
795bfe1d560SDave Jiang 	return 0;
796c52ca478SDave Jiang 
797c52ca478SDave Jiang err_pci_register:
79842d279f9SDave Jiang 	idxd_cdev_remove();
79942d279f9SDave Jiang err_cdev_register:
800448c3de8SDave Jiang 	idxd_driver_unregister(&idxd_user_drv);
801448c3de8SDave Jiang err_idxd_user_driver_register:
8020cda4f69SDave Jiang 	idxd_driver_unregister(&idxd_dmaengine_drv);
8030cda4f69SDave Jiang err_idxd_dmaengine_driver_register:
804034b3290SDave Jiang 	idxd_driver_unregister(&idxd_drv);
805034b3290SDave Jiang err_idxd_driver_register:
806c52ca478SDave Jiang 	return err;
807bfe1d560SDave Jiang }
808bfe1d560SDave Jiang module_init(idxd_init_module);
809bfe1d560SDave Jiang 
810bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
811bfe1d560SDave Jiang {
812448c3de8SDave Jiang 	idxd_driver_unregister(&idxd_user_drv);
8130cda4f69SDave Jiang 	idxd_driver_unregister(&idxd_dmaengine_drv);
814034b3290SDave Jiang 	idxd_driver_unregister(&idxd_drv);
815bfe1d560SDave Jiang 	pci_unregister_driver(&idxd_pci_driver);
81642d279f9SDave Jiang 	idxd_cdev_remove();
8170bde4444STom Zanussi 	perfmon_exit();
818bfe1d560SDave Jiang }
819bfe1d560SDave Jiang module_exit(idxd_exit_module);
820