xref: /linux/drivers/dma/idxd/init.c (revision 56fc39f5a36794c4f27f5fee047b641eac3f5b89)
1bfe1d560SDave Jiang // SPDX-License-Identifier: GPL-2.0
2bfe1d560SDave Jiang /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
3bfe1d560SDave Jiang #include <linux/init.h>
4bfe1d560SDave Jiang #include <linux/kernel.h>
5bfe1d560SDave Jiang #include <linux/module.h>
6bfe1d560SDave Jiang #include <linux/slab.h>
7bfe1d560SDave Jiang #include <linux/pci.h>
8bfe1d560SDave Jiang #include <linux/interrupt.h>
9bfe1d560SDave Jiang #include <linux/delay.h>
10bfe1d560SDave Jiang #include <linux/dma-mapping.h>
11bfe1d560SDave Jiang #include <linux/workqueue.h>
12bfe1d560SDave Jiang #include <linux/aer.h>
13bfe1d560SDave Jiang #include <linux/fs.h>
14bfe1d560SDave Jiang #include <linux/io-64-nonatomic-lo-hi.h>
15bfe1d560SDave Jiang #include <linux/device.h>
16bfe1d560SDave Jiang #include <linux/idr.h>
178e50d392SDave Jiang #include <linux/intel-svm.h>
188e50d392SDave Jiang #include <linux/iommu.h>
19bfe1d560SDave Jiang #include <uapi/linux/idxd.h>
208f47d1a5SDave Jiang #include <linux/dmaengine.h>
218f47d1a5SDave Jiang #include "../dmaengine.h"
22bfe1d560SDave Jiang #include "registers.h"
23bfe1d560SDave Jiang #include "idxd.h"
240bde4444STom Zanussi #include "perfmon.h"
25bfe1d560SDave Jiang 
26bfe1d560SDave Jiang MODULE_VERSION(IDXD_DRIVER_VERSION);
27bfe1d560SDave Jiang MODULE_LICENSE("GPL v2");
28bfe1d560SDave Jiang MODULE_AUTHOR("Intel Corporation");
29d9e5481fSDave Jiang MODULE_IMPORT_NS(IDXD);
30bfe1d560SDave Jiang 
3103d939c7SDave Jiang static bool sva = true;
3203d939c7SDave Jiang module_param(sva, bool, 0644);
3303d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
3403d939c7SDave Jiang 
35ade8a86bSDave Jiang bool tc_override;
36ade8a86bSDave Jiang module_param(tc_override, bool, 0644);
37ade8a86bSDave Jiang MODULE_PARM_DESC(tc_override, "Override traffic class defaults");
38ade8a86bSDave Jiang 
39bfe1d560SDave Jiang #define DRV_NAME "idxd"
40bfe1d560SDave Jiang 
418e50d392SDave Jiang bool support_enqcmd;
424b73e4ebSDave Jiang DEFINE_IDA(idxd_ida);
43bfe1d560SDave Jiang 
44435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = {
45435b512dSDave Jiang 	[IDXD_TYPE_DSA] = {
46435b512dSDave Jiang 		.name_prefix = "dsa",
47435b512dSDave Jiang 		.type = IDXD_TYPE_DSA,
48435b512dSDave Jiang 		.compl_size = sizeof(struct dsa_completion_record),
49435b512dSDave Jiang 		.align = 32,
50435b512dSDave Jiang 		.dev_type = &dsa_device_type,
51435b512dSDave Jiang 	},
52435b512dSDave Jiang 	[IDXD_TYPE_IAX] = {
53435b512dSDave Jiang 		.name_prefix = "iax",
54435b512dSDave Jiang 		.type = IDXD_TYPE_IAX,
55435b512dSDave Jiang 		.compl_size = sizeof(struct iax_completion_record),
56435b512dSDave Jiang 		.align = 64,
57435b512dSDave Jiang 		.dev_type = &iax_device_type,
58435b512dSDave Jiang 	},
59435b512dSDave Jiang };
60435b512dSDave Jiang 
61bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = {
62bfe1d560SDave Jiang 	/* DSA ver 1.0 platforms */
63435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
64f25b4638SDave Jiang 
65f25b4638SDave Jiang 	/* IAX ver 1.0 platforms */
66435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
67bfe1d560SDave Jiang 	{ 0, }
68bfe1d560SDave Jiang };
69bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
70bfe1d560SDave Jiang 
71bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd)
72bfe1d560SDave Jiang {
73bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
74bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
75bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
76bfe1d560SDave Jiang 	int i, msixcnt;
77bfe1d560SDave Jiang 	int rc = 0;
78bfe1d560SDave Jiang 
79bfe1d560SDave Jiang 	msixcnt = pci_msix_vec_count(pdev);
80bfe1d560SDave Jiang 	if (msixcnt < 0) {
81bfe1d560SDave Jiang 		dev_err(dev, "Not MSI-X interrupt capable.\n");
825fc8e85fSDave Jiang 		return -ENOSPC;
83bfe1d560SDave Jiang 	}
848b67426eSDave Jiang 	idxd->irq_cnt = msixcnt;
85bfe1d560SDave Jiang 
865fc8e85fSDave Jiang 	rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
875fc8e85fSDave Jiang 	if (rc != msixcnt) {
885fc8e85fSDave Jiang 		dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
895fc8e85fSDave Jiang 		return -ENOSPC;
90bfe1d560SDave Jiang 	}
91bfe1d560SDave Jiang 	dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
92bfe1d560SDave Jiang 
93bfe1d560SDave Jiang 	/*
94bfe1d560SDave Jiang 	 * We implement 1 completion list per MSI-X entry except for
95bfe1d560SDave Jiang 	 * entry 0, which is for errors and others.
96bfe1d560SDave Jiang 	 */
9747c16ac2SDave Jiang 	idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
9847c16ac2SDave Jiang 					 GFP_KERNEL, dev_to_node(dev));
99bfe1d560SDave Jiang 	if (!idxd->irq_entries) {
100bfe1d560SDave Jiang 		rc = -ENOMEM;
1015fc8e85fSDave Jiang 		goto err_irq_entries;
102bfe1d560SDave Jiang 	}
103bfe1d560SDave Jiang 
104bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
105bfe1d560SDave Jiang 		idxd->irq_entries[i].id = i;
106bfe1d560SDave Jiang 		idxd->irq_entries[i].idxd = idxd;
1078b67426eSDave Jiang 		/*
1088b67426eSDave Jiang 		 * Association of WQ should be assigned starting with irq_entry 1.
1098b67426eSDave Jiang 		 * irq_entry 0 is for misc interrupts and has no wq association
1108b67426eSDave Jiang 		 */
1118b67426eSDave Jiang 		if (i > 0)
1128b67426eSDave Jiang 			idxd->irq_entries[i].wq = idxd->wqs[i - 1];
1135fc8e85fSDave Jiang 		idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
1148b67426eSDave Jiang 		idxd->irq_entries[i].int_handle = INVALID_INT_HANDLE;
1158b67426eSDave Jiang 		if (device_pasid_enabled(idxd) && i > 0)
1168b67426eSDave Jiang 			idxd->irq_entries[i].pasid = idxd->pasid;
1178b67426eSDave Jiang 		else
1188b67426eSDave Jiang 			idxd->irq_entries[i].pasid = INVALID_IOASID;
119e4f4d8cdSDave Jiang 		spin_lock_init(&idxd->irq_entries[i].list_lock);
120bfe1d560SDave Jiang 	}
121bfe1d560SDave Jiang 
122d5c10e0fSDave Jiang 	idxd_msix_perm_setup(idxd);
123d5c10e0fSDave Jiang 
124bfe1d560SDave Jiang 	irq_entry = &idxd->irq_entries[0];
125a1610461SDave Jiang 	rc = request_threaded_irq(irq_entry->vector, NULL, idxd_misc_thread,
1265fc8e85fSDave Jiang 				  0, "idxd-misc", irq_entry);
127bfe1d560SDave Jiang 	if (rc < 0) {
128bfe1d560SDave Jiang 		dev_err(dev, "Failed to allocate misc interrupt.\n");
1295fc8e85fSDave Jiang 		goto err_misc_irq;
130bfe1d560SDave Jiang 	}
131bfe1d560SDave Jiang 
1325fc8e85fSDave Jiang 	dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
133bfe1d560SDave Jiang 
134bfe1d560SDave Jiang 	/* first MSI-X entry is not for wq interrupts */
135bfe1d560SDave Jiang 	idxd->num_wq_irqs = msixcnt - 1;
136bfe1d560SDave Jiang 
137bfe1d560SDave Jiang 	for (i = 1; i < msixcnt; i++) {
138bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
139bfe1d560SDave Jiang 
140bfe1d560SDave Jiang 		init_llist_head(&idxd->irq_entries[i].pending_llist);
141bfe1d560SDave Jiang 		INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
142a1610461SDave Jiang 		rc = request_threaded_irq(irq_entry->vector, NULL,
1435fc8e85fSDave Jiang 					  idxd_wq_thread, 0, "idxd-portal", irq_entry);
144bfe1d560SDave Jiang 		if (rc < 0) {
1455fc8e85fSDave Jiang 			dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
1465fc8e85fSDave Jiang 			goto err_wq_irqs;
147bfe1d560SDave Jiang 		}
148eb15e715SDave Jiang 
1495fc8e85fSDave Jiang 		dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
1508b67426eSDave Jiang 		if (idxd->request_int_handles) {
1518b67426eSDave Jiang 			rc = idxd_device_request_int_handle(idxd, i, &irq_entry->int_handle,
152eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
153eb15e715SDave Jiang 			if (rc < 0) {
154eb15e715SDave Jiang 				free_irq(irq_entry->vector, irq_entry);
155eb15e715SDave Jiang 				goto err_wq_irqs;
156eb15e715SDave Jiang 			}
1578b67426eSDave Jiang 			dev_dbg(dev, "int handle requested: %u\n", irq_entry->int_handle);
158eb15e715SDave Jiang 		}
159bfe1d560SDave Jiang 	}
160bfe1d560SDave Jiang 
161bfe1d560SDave Jiang 	idxd_unmask_error_interrupts(idxd);
162bfe1d560SDave Jiang 	return 0;
163bfe1d560SDave Jiang 
1645fc8e85fSDave Jiang  err_wq_irqs:
1655fc8e85fSDave Jiang 	while (--i >= 0) {
1665fc8e85fSDave Jiang 		irq_entry = &idxd->irq_entries[i];
1675fc8e85fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
1688b67426eSDave Jiang 		if (irq_entry->int_handle != INVALID_INT_HANDLE) {
1698b67426eSDave Jiang 			idxd_device_release_int_handle(idxd, irq_entry->int_handle,
1708b67426eSDave Jiang 						       IDXD_IRQ_MSIX);
1718b67426eSDave Jiang 			irq_entry->int_handle = INVALID_INT_HANDLE;
1728b67426eSDave Jiang 			irq_entry->pasid = INVALID_IOASID;
1738b67426eSDave Jiang 		}
1748b67426eSDave Jiang 		irq_entry->vector = -1;
1758b67426eSDave Jiang 		irq_entry->wq = NULL;
1768b67426eSDave Jiang 		irq_entry->idxd = NULL;
1775fc8e85fSDave Jiang 	}
1785fc8e85fSDave Jiang  err_misc_irq:
179bfe1d560SDave Jiang 	/* Disable error interrupt generation */
180bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
181d5c10e0fSDave Jiang 	idxd_msix_perm_clear(idxd);
1825fc8e85fSDave Jiang  err_irq_entries:
1835fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
184bfe1d560SDave Jiang 	dev_err(dev, "No usable interrupts\n");
185bfe1d560SDave Jiang 	return rc;
186bfe1d560SDave Jiang }
187bfe1d560SDave Jiang 
188ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd)
189ddf742d4SDave Jiang {
190ddf742d4SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
191ddf742d4SDave Jiang 	struct idxd_irq_entry *irq_entry;
1928b67426eSDave Jiang 	int i;
193ddf742d4SDave Jiang 
1948b67426eSDave Jiang 	for (i = 0; i < idxd->irq_cnt; i++) {
195ddf742d4SDave Jiang 		irq_entry = &idxd->irq_entries[i];
1968b67426eSDave Jiang 		if (irq_entry->int_handle != INVALID_INT_HANDLE) {
1978b67426eSDave Jiang 			idxd_device_release_int_handle(idxd, irq_entry->int_handle,
198ddf742d4SDave Jiang 						       IDXD_IRQ_MSIX);
1998b67426eSDave Jiang 			irq_entry->int_handle = INVALID_INT_HANDLE;
2008b67426eSDave Jiang 			irq_entry->pasid = INVALID_IOASID;
2018b67426eSDave Jiang 		}
2028b67426eSDave Jiang 		irq_entry->vector = -1;
2038b67426eSDave Jiang 		irq_entry->wq = NULL;
2048b67426eSDave Jiang 		irq_entry->idxd = NULL;
205ddf742d4SDave Jiang 		free_irq(irq_entry->vector, irq_entry);
206ddf742d4SDave Jiang 	}
207ddf742d4SDave Jiang 
208ddf742d4SDave Jiang 	idxd_mask_error_interrupts(idxd);
209ddf742d4SDave Jiang 	pci_free_irq_vectors(pdev);
210ddf742d4SDave Jiang }
211ddf742d4SDave Jiang 
2127c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd)
2137c5dd23eSDave Jiang {
2147c5dd23eSDave Jiang 	struct device *dev = &idxd->pdev->dev;
2157c5dd23eSDave Jiang 	struct idxd_wq *wq;
216700af3a0SDave Jiang 	struct device *conf_dev;
2177c5dd23eSDave Jiang 	int i, rc;
2187c5dd23eSDave Jiang 
2197c5dd23eSDave Jiang 	idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
2207c5dd23eSDave Jiang 				 GFP_KERNEL, dev_to_node(dev));
2217c5dd23eSDave Jiang 	if (!idxd->wqs)
2227c5dd23eSDave Jiang 		return -ENOMEM;
2237c5dd23eSDave Jiang 
2247c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
2257c5dd23eSDave Jiang 		wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
2267c5dd23eSDave Jiang 		if (!wq) {
2277c5dd23eSDave Jiang 			rc = -ENOMEM;
2287c5dd23eSDave Jiang 			goto err;
2297c5dd23eSDave Jiang 		}
2307c5dd23eSDave Jiang 
231700af3a0SDave Jiang 		idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
232700af3a0SDave Jiang 		conf_dev = wq_confdev(wq);
2337c5dd23eSDave Jiang 		wq->id = i;
2347c5dd23eSDave Jiang 		wq->idxd = idxd;
235700af3a0SDave Jiang 		device_initialize(wq_confdev(wq));
236700af3a0SDave Jiang 		conf_dev->parent = idxd_confdev(idxd);
237700af3a0SDave Jiang 		conf_dev->bus = &dsa_bus_type;
238700af3a0SDave Jiang 		conf_dev->type = &idxd_wq_device_type;
239700af3a0SDave Jiang 		rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
2407c5dd23eSDave Jiang 		if (rc < 0) {
241700af3a0SDave Jiang 			put_device(conf_dev);
2427c5dd23eSDave Jiang 			goto err;
2437c5dd23eSDave Jiang 		}
2447c5dd23eSDave Jiang 
2457c5dd23eSDave Jiang 		mutex_init(&wq->wq_lock);
24604922b74SDave Jiang 		init_waitqueue_head(&wq->err_queue);
24793a40a6dSDave Jiang 		init_completion(&wq->wq_dead);
248*56fc39f5SDave Jiang 		init_completion(&wq->wq_resurrect);
2497c5dd23eSDave Jiang 		wq->max_xfer_bytes = idxd->max_xfer_bytes;
2507c5dd23eSDave Jiang 		wq->max_batch_size = idxd->max_batch_size;
2517c5dd23eSDave Jiang 		wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
2527c5dd23eSDave Jiang 		if (!wq->wqcfg) {
253700af3a0SDave Jiang 			put_device(conf_dev);
2547c5dd23eSDave Jiang 			rc = -ENOMEM;
2557c5dd23eSDave Jiang 			goto err;
2567c5dd23eSDave Jiang 		}
2577c5dd23eSDave Jiang 		idxd->wqs[i] = wq;
2587c5dd23eSDave Jiang 	}
2597c5dd23eSDave Jiang 
2607c5dd23eSDave Jiang 	return 0;
2617c5dd23eSDave Jiang 
2627c5dd23eSDave Jiang  err:
263700af3a0SDave Jiang 	while (--i >= 0) {
264700af3a0SDave Jiang 		wq = idxd->wqs[i];
265700af3a0SDave Jiang 		conf_dev = wq_confdev(wq);
266700af3a0SDave Jiang 		put_device(conf_dev);
267700af3a0SDave Jiang 	}
2687c5dd23eSDave Jiang 	return rc;
2697c5dd23eSDave Jiang }
2707c5dd23eSDave Jiang 
27175b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd)
27275b91130SDave Jiang {
27375b91130SDave Jiang 	struct idxd_engine *engine;
27475b91130SDave Jiang 	struct device *dev = &idxd->pdev->dev;
275700af3a0SDave Jiang 	struct device *conf_dev;
27675b91130SDave Jiang 	int i, rc;
27775b91130SDave Jiang 
27875b91130SDave Jiang 	idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
27975b91130SDave Jiang 				     GFP_KERNEL, dev_to_node(dev));
28075b91130SDave Jiang 	if (!idxd->engines)
28175b91130SDave Jiang 		return -ENOMEM;
28275b91130SDave Jiang 
28375b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++) {
28475b91130SDave Jiang 		engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
28575b91130SDave Jiang 		if (!engine) {
28675b91130SDave Jiang 			rc = -ENOMEM;
28775b91130SDave Jiang 			goto err;
28875b91130SDave Jiang 		}
28975b91130SDave Jiang 
290700af3a0SDave Jiang 		idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE);
291700af3a0SDave Jiang 		conf_dev = engine_confdev(engine);
29275b91130SDave Jiang 		engine->id = i;
29375b91130SDave Jiang 		engine->idxd = idxd;
294700af3a0SDave Jiang 		device_initialize(conf_dev);
295700af3a0SDave Jiang 		conf_dev->parent = idxd_confdev(idxd);
296700af3a0SDave Jiang 		conf_dev->bus = &dsa_bus_type;
297700af3a0SDave Jiang 		conf_dev->type = &idxd_engine_device_type;
298700af3a0SDave Jiang 		rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
29975b91130SDave Jiang 		if (rc < 0) {
300700af3a0SDave Jiang 			put_device(conf_dev);
30175b91130SDave Jiang 			goto err;
30275b91130SDave Jiang 		}
30375b91130SDave Jiang 
30475b91130SDave Jiang 		idxd->engines[i] = engine;
30575b91130SDave Jiang 	}
30675b91130SDave Jiang 
30775b91130SDave Jiang 	return 0;
30875b91130SDave Jiang 
30975b91130SDave Jiang  err:
310700af3a0SDave Jiang 	while (--i >= 0) {
311700af3a0SDave Jiang 		engine = idxd->engines[i];
312700af3a0SDave Jiang 		conf_dev = engine_confdev(engine);
313700af3a0SDave Jiang 		put_device(conf_dev);
314700af3a0SDave Jiang 	}
31575b91130SDave Jiang 	return rc;
31675b91130SDave Jiang }
31775b91130SDave Jiang 
318defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd)
319defe49f9SDave Jiang {
320defe49f9SDave Jiang 	struct device *dev = &idxd->pdev->dev;
321700af3a0SDave Jiang 	struct device *conf_dev;
322defe49f9SDave Jiang 	struct idxd_group *group;
323defe49f9SDave Jiang 	int i, rc;
324defe49f9SDave Jiang 
325defe49f9SDave Jiang 	idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
326defe49f9SDave Jiang 				    GFP_KERNEL, dev_to_node(dev));
327defe49f9SDave Jiang 	if (!idxd->groups)
328defe49f9SDave Jiang 		return -ENOMEM;
329defe49f9SDave Jiang 
330defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++) {
331defe49f9SDave Jiang 		group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
332defe49f9SDave Jiang 		if (!group) {
333defe49f9SDave Jiang 			rc = -ENOMEM;
334defe49f9SDave Jiang 			goto err;
335defe49f9SDave Jiang 		}
336defe49f9SDave Jiang 
337700af3a0SDave Jiang 		idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP);
338700af3a0SDave Jiang 		conf_dev = group_confdev(group);
339defe49f9SDave Jiang 		group->id = i;
340defe49f9SDave Jiang 		group->idxd = idxd;
341700af3a0SDave Jiang 		device_initialize(conf_dev);
342700af3a0SDave Jiang 		conf_dev->parent = idxd_confdev(idxd);
343700af3a0SDave Jiang 		conf_dev->bus = &dsa_bus_type;
344700af3a0SDave Jiang 		conf_dev->type = &idxd_group_device_type;
345700af3a0SDave Jiang 		rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
346defe49f9SDave Jiang 		if (rc < 0) {
347700af3a0SDave Jiang 			put_device(conf_dev);
348defe49f9SDave Jiang 			goto err;
349defe49f9SDave Jiang 		}
350defe49f9SDave Jiang 
351defe49f9SDave Jiang 		idxd->groups[i] = group;
352ade8a86bSDave Jiang 		if (idxd->hw.version < DEVICE_VERSION_2 && !tc_override) {
353ade8a86bSDave Jiang 			group->tc_a = 1;
354ade8a86bSDave Jiang 			group->tc_b = 1;
355ade8a86bSDave Jiang 		} else {
356defe49f9SDave Jiang 			group->tc_a = -1;
357defe49f9SDave Jiang 			group->tc_b = -1;
358defe49f9SDave Jiang 		}
359ade8a86bSDave Jiang 	}
360defe49f9SDave Jiang 
361defe49f9SDave Jiang 	return 0;
362defe49f9SDave Jiang 
363defe49f9SDave Jiang  err:
364700af3a0SDave Jiang 	while (--i >= 0) {
365700af3a0SDave Jiang 		group = idxd->groups[i];
366700af3a0SDave Jiang 		put_device(group_confdev(group));
367700af3a0SDave Jiang 	}
368defe49f9SDave Jiang 	return rc;
369defe49f9SDave Jiang }
370defe49f9SDave Jiang 
371ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd)
372ddf742d4SDave Jiang {
373ddf742d4SDave Jiang 	int i;
374ddf742d4SDave Jiang 
375ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
376700af3a0SDave Jiang 		put_device(group_confdev(idxd->groups[i]));
377ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
378700af3a0SDave Jiang 		put_device(engine_confdev(idxd->engines[i]));
379ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
380700af3a0SDave Jiang 		put_device(wq_confdev(idxd->wqs[i]));
381ddf742d4SDave Jiang 	destroy_workqueue(idxd->wq);
382ddf742d4SDave Jiang }
383ddf742d4SDave Jiang 
384bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd)
385bfe1d560SDave Jiang {
386bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
387defe49f9SDave Jiang 	int rc, i;
388bfe1d560SDave Jiang 
3890d5c10b4SDave Jiang 	init_waitqueue_head(&idxd->cmd_waitq);
3907c5dd23eSDave Jiang 
3917c5dd23eSDave Jiang 	rc = idxd_setup_wqs(idxd);
3927c5dd23eSDave Jiang 	if (rc < 0)
393eb15e715SDave Jiang 		goto err_wqs;
3947c5dd23eSDave Jiang 
39575b91130SDave Jiang 	rc = idxd_setup_engines(idxd);
39675b91130SDave Jiang 	if (rc < 0)
39775b91130SDave Jiang 		goto err_engine;
39875b91130SDave Jiang 
399defe49f9SDave Jiang 	rc = idxd_setup_groups(idxd);
400defe49f9SDave Jiang 	if (rc < 0)
401defe49f9SDave Jiang 		goto err_group;
402bfe1d560SDave Jiang 
4030d5c10b4SDave Jiang 	idxd->wq = create_workqueue(dev_name(dev));
4047c5dd23eSDave Jiang 	if (!idxd->wq) {
4057c5dd23eSDave Jiang 		rc = -ENOMEM;
406defe49f9SDave Jiang 		goto err_wkq_create;
4077c5dd23eSDave Jiang 	}
4080d5c10b4SDave Jiang 
409bfe1d560SDave Jiang 	return 0;
4107c5dd23eSDave Jiang 
411defe49f9SDave Jiang  err_wkq_create:
412defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
413700af3a0SDave Jiang 		put_device(group_confdev(idxd->groups[i]));
414defe49f9SDave Jiang  err_group:
41575b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
416700af3a0SDave Jiang 		put_device(engine_confdev(idxd->engines[i]));
41775b91130SDave Jiang  err_engine:
4187c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
419700af3a0SDave Jiang 		put_device(wq_confdev(idxd->wqs[i]));
420eb15e715SDave Jiang  err_wqs:
4217c5dd23eSDave Jiang 	return rc;
422bfe1d560SDave Jiang }
423bfe1d560SDave Jiang 
424bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd)
425bfe1d560SDave Jiang {
426bfe1d560SDave Jiang 	union offsets_reg offsets;
427bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
428bfe1d560SDave Jiang 
429bfe1d560SDave Jiang 	offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
4302f8417a9SDave Jiang 	offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
4312f8417a9SDave Jiang 	idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
432bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
4332f8417a9SDave Jiang 	idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
4342f8417a9SDave Jiang 	dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
4352f8417a9SDave Jiang 	idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
4362f8417a9SDave Jiang 	dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
4372f8417a9SDave Jiang 	idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
438bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
439bfe1d560SDave Jiang }
440bfe1d560SDave Jiang 
441bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd)
442bfe1d560SDave Jiang {
443bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
444bfe1d560SDave Jiang 	int i;
445bfe1d560SDave Jiang 
446bfe1d560SDave Jiang 	/* reading generic capabilities */
447bfe1d560SDave Jiang 	idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
448bfe1d560SDave Jiang 	dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
449eb15e715SDave Jiang 
450eb15e715SDave Jiang 	if (idxd->hw.gen_cap.cmd_cap) {
451eb15e715SDave Jiang 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
452eb15e715SDave Jiang 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
453eb15e715SDave Jiang 	}
454eb15e715SDave Jiang 
4558b67426eSDave Jiang 	/* reading command capabilities */
4568b67426eSDave Jiang 	if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE))
4578b67426eSDave Jiang 		idxd->request_int_handles = true;
4588b67426eSDave Jiang 
459bfe1d560SDave Jiang 	idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
460bfe1d560SDave Jiang 	dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
461bfe1d560SDave Jiang 	idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
462bfe1d560SDave Jiang 	dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
463bfe1d560SDave Jiang 	if (idxd->hw.gen_cap.config_en)
464bfe1d560SDave Jiang 		set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
465bfe1d560SDave Jiang 
466bfe1d560SDave Jiang 	/* reading group capabilities */
467bfe1d560SDave Jiang 	idxd->hw.group_cap.bits =
468bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
469bfe1d560SDave Jiang 	dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
470bfe1d560SDave Jiang 	idxd->max_groups = idxd->hw.group_cap.num_groups;
471bfe1d560SDave Jiang 	dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
472bfe1d560SDave Jiang 	idxd->max_tokens = idxd->hw.group_cap.total_tokens;
473bfe1d560SDave Jiang 	dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
474c52ca478SDave Jiang 	idxd->nr_tokens = idxd->max_tokens;
475bfe1d560SDave Jiang 
476bfe1d560SDave Jiang 	/* read engine capabilities */
477bfe1d560SDave Jiang 	idxd->hw.engine_cap.bits =
478bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
479bfe1d560SDave Jiang 	dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
480bfe1d560SDave Jiang 	idxd->max_engines = idxd->hw.engine_cap.num_engines;
481bfe1d560SDave Jiang 	dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
482bfe1d560SDave Jiang 
483bfe1d560SDave Jiang 	/* read workqueue capabilities */
484bfe1d560SDave Jiang 	idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
485bfe1d560SDave Jiang 	dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
486bfe1d560SDave Jiang 	idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
487bfe1d560SDave Jiang 	dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
488bfe1d560SDave Jiang 	idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
489bfe1d560SDave Jiang 	dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
490d98793b5SDave Jiang 	idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
491d98793b5SDave Jiang 	dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
492bfe1d560SDave Jiang 
493bfe1d560SDave Jiang 	/* reading operation capabilities */
494bfe1d560SDave Jiang 	for (i = 0; i < 4; i++) {
495bfe1d560SDave Jiang 		idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
496bfe1d560SDave Jiang 				IDXD_OPCAP_OFFSET + i * sizeof(u64));
497bfe1d560SDave Jiang 		dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
498bfe1d560SDave Jiang 	}
499bfe1d560SDave Jiang }
500bfe1d560SDave Jiang 
501435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
502bfe1d560SDave Jiang {
503bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
504700af3a0SDave Jiang 	struct device *conf_dev;
505bfe1d560SDave Jiang 	struct idxd_device *idxd;
50647c16ac2SDave Jiang 	int rc;
507bfe1d560SDave Jiang 
50847c16ac2SDave Jiang 	idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
509bfe1d560SDave Jiang 	if (!idxd)
510bfe1d560SDave Jiang 		return NULL;
511bfe1d560SDave Jiang 
512700af3a0SDave Jiang 	conf_dev = idxd_confdev(idxd);
513bfe1d560SDave Jiang 	idxd->pdev = pdev;
514435b512dSDave Jiang 	idxd->data = data;
515700af3a0SDave Jiang 	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
5164b73e4ebSDave Jiang 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
51747c16ac2SDave Jiang 	if (idxd->id < 0)
51847c16ac2SDave Jiang 		return NULL;
51947c16ac2SDave Jiang 
520700af3a0SDave Jiang 	device_initialize(conf_dev);
521700af3a0SDave Jiang 	conf_dev->parent = dev;
522700af3a0SDave Jiang 	conf_dev->bus = &dsa_bus_type;
523700af3a0SDave Jiang 	conf_dev->type = idxd->data->dev_type;
524700af3a0SDave Jiang 	rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
52547c16ac2SDave Jiang 	if (rc < 0) {
526700af3a0SDave Jiang 		put_device(conf_dev);
52747c16ac2SDave Jiang 		return NULL;
52847c16ac2SDave Jiang 	}
52947c16ac2SDave Jiang 
530bfe1d560SDave Jiang 	spin_lock_init(&idxd->dev_lock);
53153b2ee7fSDave Jiang 	spin_lock_init(&idxd->cmd_lock);
532bfe1d560SDave Jiang 
533bfe1d560SDave Jiang 	return idxd;
534bfe1d560SDave Jiang }
535bfe1d560SDave Jiang 
5368e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd)
5378e50d392SDave Jiang {
5388e50d392SDave Jiang 	int flags;
5398e50d392SDave Jiang 	unsigned int pasid;
5408e50d392SDave Jiang 	struct iommu_sva *sva;
5418e50d392SDave Jiang 
5428e50d392SDave Jiang 	flags = SVM_FLAG_SUPERVISOR_MODE;
5438e50d392SDave Jiang 
5448e50d392SDave Jiang 	sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
5458e50d392SDave Jiang 	if (IS_ERR(sva)) {
5468e50d392SDave Jiang 		dev_warn(&idxd->pdev->dev,
5478e50d392SDave Jiang 			 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
5488e50d392SDave Jiang 		return PTR_ERR(sva);
5498e50d392SDave Jiang 	}
5508e50d392SDave Jiang 
5518e50d392SDave Jiang 	pasid = iommu_sva_get_pasid(sva);
5528e50d392SDave Jiang 	if (pasid == IOMMU_PASID_INVALID) {
5538e50d392SDave Jiang 		iommu_sva_unbind_device(sva);
5548e50d392SDave Jiang 		return -ENODEV;
5558e50d392SDave Jiang 	}
5568e50d392SDave Jiang 
5578e50d392SDave Jiang 	idxd->sva = sva;
5588e50d392SDave Jiang 	idxd->pasid = pasid;
5598e50d392SDave Jiang 	dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
5608e50d392SDave Jiang 	return 0;
5618e50d392SDave Jiang }
5628e50d392SDave Jiang 
5638e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd)
5648e50d392SDave Jiang {
5658e50d392SDave Jiang 
5668e50d392SDave Jiang 	iommu_sva_unbind_device(idxd->sva);
5678e50d392SDave Jiang 	idxd->sva = NULL;
5688e50d392SDave Jiang }
5698e50d392SDave Jiang 
570bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd)
571bfe1d560SDave Jiang {
572bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
573bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
574bfe1d560SDave Jiang 	int rc;
575bfe1d560SDave Jiang 
576bfe1d560SDave Jiang 	dev_dbg(dev, "%s entered and resetting device\n", __func__);
57789e3becdSDave Jiang 	rc = idxd_device_init_reset(idxd);
57889e3becdSDave Jiang 	if (rc < 0)
57989e3becdSDave Jiang 		return rc;
58089e3becdSDave Jiang 
581bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD reset complete\n");
582bfe1d560SDave Jiang 
58303d939c7SDave Jiang 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
584cf5f86a7SDave Jiang 		rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA);
585cf5f86a7SDave Jiang 		if (rc == 0) {
5868e50d392SDave Jiang 			rc = idxd_enable_system_pasid(idxd);
587cf5f86a7SDave Jiang 			if (rc < 0) {
588cf5f86a7SDave Jiang 				iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
5898e50d392SDave Jiang 				dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
590cf5f86a7SDave Jiang 			} else {
5918e50d392SDave Jiang 				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
592cf5f86a7SDave Jiang 			}
593cf5f86a7SDave Jiang 		} else {
594cf5f86a7SDave Jiang 			dev_warn(dev, "Unable to turn on SVA feature.\n");
595cf5f86a7SDave Jiang 		}
59603d939c7SDave Jiang 	} else if (!sva) {
59703d939c7SDave Jiang 		dev_warn(dev, "User forced SVA off via module param.\n");
5988e50d392SDave Jiang 	}
5998e50d392SDave Jiang 
600bfe1d560SDave Jiang 	idxd_read_caps(idxd);
601bfe1d560SDave Jiang 	idxd_read_table_offsets(idxd);
602bfe1d560SDave Jiang 
603bfe1d560SDave Jiang 	rc = idxd_setup_internals(idxd);
604bfe1d560SDave Jiang 	if (rc)
6057c5dd23eSDave Jiang 		goto err;
606bfe1d560SDave Jiang 
6078c66bbdcSDave Jiang 	/* If the configs are readonly, then load them from device */
6088c66bbdcSDave Jiang 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
6098c66bbdcSDave Jiang 		dev_dbg(dev, "Loading RO device config\n");
6108c66bbdcSDave Jiang 		rc = idxd_device_load_config(idxd);
6118c66bbdcSDave Jiang 		if (rc < 0)
612ddf742d4SDave Jiang 			goto err_config;
6138c66bbdcSDave Jiang 	}
6148c66bbdcSDave Jiang 
615bfe1d560SDave Jiang 	rc = idxd_setup_interrupts(idxd);
616bfe1d560SDave Jiang 	if (rc)
617ddf742d4SDave Jiang 		goto err_config;
618bfe1d560SDave Jiang 
619bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD interrupt setup complete.\n");
620bfe1d560SDave Jiang 
62142d279f9SDave Jiang 	idxd->major = idxd_cdev_get_major(idxd);
62242d279f9SDave Jiang 
6230bde4444STom Zanussi 	rc = perfmon_pmu_init(idxd);
6240bde4444STom Zanussi 	if (rc < 0)
6250bde4444STom Zanussi 		dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
6260bde4444STom Zanussi 
627bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
628bfe1d560SDave Jiang 	return 0;
629bfe1d560SDave Jiang 
630ddf742d4SDave Jiang  err_config:
631ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
6327c5dd23eSDave Jiang  err:
6338e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
6348e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
635cf5f86a7SDave Jiang 	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
636bfe1d560SDave Jiang 	return rc;
637bfe1d560SDave Jiang }
638bfe1d560SDave Jiang 
639ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd)
640ddf742d4SDave Jiang {
641ddf742d4SDave Jiang 	struct device *dev = &idxd->pdev->dev;
642ddf742d4SDave Jiang 
643ddf742d4SDave Jiang 	perfmon_pmu_remove(idxd);
644ddf742d4SDave Jiang 	idxd_cleanup_interrupts(idxd);
645ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
646ddf742d4SDave Jiang 	if (device_pasid_enabled(idxd))
647ddf742d4SDave Jiang 		idxd_disable_system_pasid(idxd);
648ddf742d4SDave Jiang 	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
649ddf742d4SDave Jiang }
650ddf742d4SDave Jiang 
651bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
652bfe1d560SDave Jiang {
653bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
654bfe1d560SDave Jiang 	struct idxd_device *idxd;
655435b512dSDave Jiang 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
656bfe1d560SDave Jiang 	int rc;
657bfe1d560SDave Jiang 
658a39c7cd0SDave Jiang 	rc = pci_enable_device(pdev);
659bfe1d560SDave Jiang 	if (rc)
660bfe1d560SDave Jiang 		return rc;
661bfe1d560SDave Jiang 
6628e50d392SDave Jiang 	dev_dbg(dev, "Alloc IDXD context\n");
663435b512dSDave Jiang 	idxd = idxd_alloc(pdev, data);
664a39c7cd0SDave Jiang 	if (!idxd) {
665a39c7cd0SDave Jiang 		rc = -ENOMEM;
666a39c7cd0SDave Jiang 		goto err_idxd_alloc;
667a39c7cd0SDave Jiang 	}
668bfe1d560SDave Jiang 
6698e50d392SDave Jiang 	dev_dbg(dev, "Mapping BARs\n");
670a39c7cd0SDave Jiang 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
671a39c7cd0SDave Jiang 	if (!idxd->reg_base) {
672a39c7cd0SDave Jiang 		rc = -ENOMEM;
673a39c7cd0SDave Jiang 		goto err_iomap;
674a39c7cd0SDave Jiang 	}
675bfe1d560SDave Jiang 
676bfe1d560SDave Jiang 	dev_dbg(dev, "Set DMA masks\n");
67753b50458SChristophe JAILLET 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
678bfe1d560SDave Jiang 	if (rc)
67953b50458SChristophe JAILLET 		rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
680bfe1d560SDave Jiang 	if (rc)
681a39c7cd0SDave Jiang 		goto err;
682bfe1d560SDave Jiang 
683bfe1d560SDave Jiang 	dev_dbg(dev, "Set PCI master\n");
684bfe1d560SDave Jiang 	pci_set_master(pdev);
685bfe1d560SDave Jiang 	pci_set_drvdata(pdev, idxd);
686bfe1d560SDave Jiang 
687bfe1d560SDave Jiang 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
688bfe1d560SDave Jiang 	rc = idxd_probe(idxd);
689bfe1d560SDave Jiang 	if (rc) {
690bfe1d560SDave Jiang 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
691a39c7cd0SDave Jiang 		goto err;
692bfe1d560SDave Jiang 	}
693bfe1d560SDave Jiang 
69447c16ac2SDave Jiang 	rc = idxd_register_devices(idxd);
695c52ca478SDave Jiang 	if (rc) {
696c52ca478SDave Jiang 		dev_err(dev, "IDXD sysfs setup failed\n");
697ddf742d4SDave Jiang 		goto err_dev_register;
698c52ca478SDave Jiang 	}
699c52ca478SDave Jiang 
700bfe1d560SDave Jiang 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
701bfe1d560SDave Jiang 		 idxd->hw.version);
702bfe1d560SDave Jiang 
703bfe1d560SDave Jiang 	return 0;
704a39c7cd0SDave Jiang 
705ddf742d4SDave Jiang  err_dev_register:
706ddf742d4SDave Jiang 	idxd_cleanup(idxd);
707a39c7cd0SDave Jiang  err:
708a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
709a39c7cd0SDave Jiang  err_iomap:
710700af3a0SDave Jiang 	put_device(idxd_confdev(idxd));
711a39c7cd0SDave Jiang  err_idxd_alloc:
712a39c7cd0SDave Jiang 	pci_disable_device(pdev);
713a39c7cd0SDave Jiang 	return rc;
714bfe1d560SDave Jiang }
715bfe1d560SDave Jiang 
7168f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
7178f47d1a5SDave Jiang {
7188f47d1a5SDave Jiang 	struct idxd_desc *desc, *itr;
7198f47d1a5SDave Jiang 	struct llist_node *head;
7208f47d1a5SDave Jiang 
7218f47d1a5SDave Jiang 	head = llist_del_all(&ie->pending_llist);
7228f47d1a5SDave Jiang 	if (!head)
7238f47d1a5SDave Jiang 		return;
7248f47d1a5SDave Jiang 
7255d78abb6SDave Jiang 	llist_for_each_entry_safe(desc, itr, head, llnode)
7265d78abb6SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true);
7278f47d1a5SDave Jiang }
7288f47d1a5SDave Jiang 
7298f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie)
7308f47d1a5SDave Jiang {
7318f47d1a5SDave Jiang 	struct idxd_desc *desc, *iter;
7328f47d1a5SDave Jiang 
7338f47d1a5SDave Jiang 	list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
7348f47d1a5SDave Jiang 		list_del(&desc->list);
7355d78abb6SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true);
7368f47d1a5SDave Jiang 	}
7378f47d1a5SDave Jiang }
7388f47d1a5SDave Jiang 
7395b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd)
7405b0c68c4SDave Jiang {
7415b0c68c4SDave Jiang 	struct idxd_wq *wq;
7425b0c68c4SDave Jiang 	int i;
7435b0c68c4SDave Jiang 
7445b0c68c4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
7455b0c68c4SDave Jiang 		wq = idxd->wqs[i];
7465b0c68c4SDave Jiang 		if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
7475b0c68c4SDave Jiang 			idxd_wq_quiesce(wq);
7485b0c68c4SDave Jiang 	}
7495b0c68c4SDave Jiang }
7505b0c68c4SDave Jiang 
751eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd)
752eb15e715SDave Jiang {
753eb15e715SDave Jiang 	struct device *dev = &idxd->pdev->dev;
754eb15e715SDave Jiang 	int i, rc;
755eb15e715SDave Jiang 
7568b67426eSDave Jiang 	for (i = 1; i < idxd->irq_cnt; i++) {
7578b67426eSDave Jiang 		struct idxd_irq_entry *ie = &idxd->irq_entries[i];
7588b67426eSDave Jiang 
7598b67426eSDave Jiang 		if (ie->int_handle != INVALID_INT_HANDLE) {
7608b67426eSDave Jiang 			rc = idxd_device_release_int_handle(idxd, ie->int_handle, IDXD_IRQ_MSIX);
761eb15e715SDave Jiang 			if (rc < 0)
7628b67426eSDave Jiang 				dev_warn(dev, "irq handle %d release failed\n", ie->int_handle);
763eb15e715SDave Jiang 			else
7648b67426eSDave Jiang 				dev_dbg(dev, "int handle released: %u\n", ie->int_handle);
765eb15e715SDave Jiang 		}
766eb15e715SDave Jiang 	}
767eb15e715SDave Jiang }
768eb15e715SDave Jiang 
769bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
770bfe1d560SDave Jiang {
771bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
772bfe1d560SDave Jiang 	int rc, i;
773bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
774bfe1d560SDave Jiang 	int msixcnt = pci_msix_vec_count(pdev);
775bfe1d560SDave Jiang 
776bfe1d560SDave Jiang 	rc = idxd_device_disable(idxd);
777bfe1d560SDave Jiang 	if (rc)
778bfe1d560SDave Jiang 		dev_err(&pdev->dev, "Disabling device failed\n");
779bfe1d560SDave Jiang 
780bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
781bfe1d560SDave Jiang 	idxd_mask_msix_vectors(idxd);
782bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
783bfe1d560SDave Jiang 
784bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
785bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
7865fc8e85fSDave Jiang 		synchronize_irq(irq_entry->vector);
787bfe1d560SDave Jiang 		if (i == 0)
788bfe1d560SDave Jiang 			continue;
7898f47d1a5SDave Jiang 		idxd_flush_pending_llist(irq_entry);
7908f47d1a5SDave Jiang 		idxd_flush_work_list(irq_entry);
791bfe1d560SDave Jiang 	}
79249c4959fSDave Jiang 	flush_workqueue(idxd->wq);
793bfe1d560SDave Jiang }
794bfe1d560SDave Jiang 
795bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
796bfe1d560SDave Jiang {
797bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
79849c4959fSDave Jiang 	struct idxd_irq_entry *irq_entry;
79949c4959fSDave Jiang 	int msixcnt = pci_msix_vec_count(pdev);
80049c4959fSDave Jiang 	int i;
801bfe1d560SDave Jiang 
80298da0106SDave Jiang 	idxd_unregister_devices(idxd);
80398da0106SDave Jiang 	/*
80498da0106SDave Jiang 	 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
80598da0106SDave Jiang 	 * to the idxd context. The driver still needs those bits in order to do the rest of
80698da0106SDave Jiang 	 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
80798da0106SDave Jiang 	 * on the device here to hold off the freeing while allowing the idxd sub-driver
80898da0106SDave Jiang 	 * to unbind.
80998da0106SDave Jiang 	 */
81098da0106SDave Jiang 	get_device(idxd_confdev(idxd));
81198da0106SDave Jiang 	device_unregister(idxd_confdev(idxd));
812bfe1d560SDave Jiang 	idxd_shutdown(pdev);
8138e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
8148e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
81549c4959fSDave Jiang 
81649c4959fSDave Jiang 	for (i = 0; i < msixcnt; i++) {
81749c4959fSDave Jiang 		irq_entry = &idxd->irq_entries[i];
81849c4959fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
81949c4959fSDave Jiang 	}
82049c4959fSDave Jiang 	idxd_msix_perm_clear(idxd);
82149c4959fSDave Jiang 	idxd_release_int_handles(idxd);
82249c4959fSDave Jiang 	pci_free_irq_vectors(pdev);
82349c4959fSDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
824cf5f86a7SDave Jiang 	iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
82549c4959fSDave Jiang 	pci_disable_device(pdev);
82649c4959fSDave Jiang 	destroy_workqueue(idxd->wq);
82749c4959fSDave Jiang 	perfmon_pmu_remove(idxd);
82898da0106SDave Jiang 	put_device(idxd_confdev(idxd));
829bfe1d560SDave Jiang }
830bfe1d560SDave Jiang 
831bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
832bfe1d560SDave Jiang 	.name		= DRV_NAME,
833bfe1d560SDave Jiang 	.id_table	= idxd_pci_tbl,
834bfe1d560SDave Jiang 	.probe		= idxd_pci_probe,
835bfe1d560SDave Jiang 	.remove		= idxd_remove,
836bfe1d560SDave Jiang 	.shutdown	= idxd_shutdown,
837bfe1d560SDave Jiang };
838bfe1d560SDave Jiang 
839bfe1d560SDave Jiang static int __init idxd_init_module(void)
840bfe1d560SDave Jiang {
8414b73e4ebSDave Jiang 	int err;
842bfe1d560SDave Jiang 
843bfe1d560SDave Jiang 	/*
8448e50d392SDave Jiang 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
845bfe1d560SDave Jiang 	 * enumerating the device. We can not utilize it.
846bfe1d560SDave Jiang 	 */
84774b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
848bfe1d560SDave Jiang 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
849bfe1d560SDave Jiang 		return -ENODEV;
850bfe1d560SDave Jiang 	}
851bfe1d560SDave Jiang 
85274b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
8538e50d392SDave Jiang 		pr_warn("Platform does not have ENQCMD(S) support.\n");
8548e50d392SDave Jiang 	else
8558e50d392SDave Jiang 		support_enqcmd = true;
856bfe1d560SDave Jiang 
8570bde4444STom Zanussi 	perfmon_init();
8580bde4444STom Zanussi 
859034b3290SDave Jiang 	err = idxd_driver_register(&idxd_drv);
860034b3290SDave Jiang 	if (err < 0)
861034b3290SDave Jiang 		goto err_idxd_driver_register;
862034b3290SDave Jiang 
8630cda4f69SDave Jiang 	err = idxd_driver_register(&idxd_dmaengine_drv);
8640cda4f69SDave Jiang 	if (err < 0)
8650cda4f69SDave Jiang 		goto err_idxd_dmaengine_driver_register;
8660cda4f69SDave Jiang 
867448c3de8SDave Jiang 	err = idxd_driver_register(&idxd_user_drv);
868448c3de8SDave Jiang 	if (err < 0)
869448c3de8SDave Jiang 		goto err_idxd_user_driver_register;
870448c3de8SDave Jiang 
87142d279f9SDave Jiang 	err = idxd_cdev_register();
87242d279f9SDave Jiang 	if (err)
87342d279f9SDave Jiang 		goto err_cdev_register;
87442d279f9SDave Jiang 
875c52ca478SDave Jiang 	err = pci_register_driver(&idxd_pci_driver);
876c52ca478SDave Jiang 	if (err)
877c52ca478SDave Jiang 		goto err_pci_register;
878c52ca478SDave Jiang 
879bfe1d560SDave Jiang 	return 0;
880c52ca478SDave Jiang 
881c52ca478SDave Jiang err_pci_register:
88242d279f9SDave Jiang 	idxd_cdev_remove();
88342d279f9SDave Jiang err_cdev_register:
884448c3de8SDave Jiang 	idxd_driver_unregister(&idxd_user_drv);
885448c3de8SDave Jiang err_idxd_user_driver_register:
8860cda4f69SDave Jiang 	idxd_driver_unregister(&idxd_dmaengine_drv);
8870cda4f69SDave Jiang err_idxd_dmaengine_driver_register:
888034b3290SDave Jiang 	idxd_driver_unregister(&idxd_drv);
889034b3290SDave Jiang err_idxd_driver_register:
890c52ca478SDave Jiang 	return err;
891bfe1d560SDave Jiang }
892bfe1d560SDave Jiang module_init(idxd_init_module);
893bfe1d560SDave Jiang 
894bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
895bfe1d560SDave Jiang {
896448c3de8SDave Jiang 	idxd_driver_unregister(&idxd_user_drv);
8970cda4f69SDave Jiang 	idxd_driver_unregister(&idxd_dmaengine_drv);
898034b3290SDave Jiang 	idxd_driver_unregister(&idxd_drv);
899bfe1d560SDave Jiang 	pci_unregister_driver(&idxd_pci_driver);
90042d279f9SDave Jiang 	idxd_cdev_remove();
9010bde4444STom Zanussi 	perfmon_exit();
902bfe1d560SDave Jiang }
903bfe1d560SDave Jiang module_exit(idxd_exit_module);
904