xref: /linux/drivers/dma/idxd/init.c (revision 49c4959f04b587c8909b33adca4066995c768d60)
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");
29bfe1d560SDave Jiang 
3003d939c7SDave Jiang static bool sva = true;
3103d939c7SDave Jiang module_param(sva, bool, 0644);
3203d939c7SDave Jiang MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
3303d939c7SDave Jiang 
34bfe1d560SDave Jiang #define DRV_NAME "idxd"
35bfe1d560SDave Jiang 
368e50d392SDave Jiang bool support_enqcmd;
374b73e4ebSDave Jiang DEFINE_IDA(idxd_ida);
38bfe1d560SDave Jiang 
39435b512dSDave Jiang static struct idxd_driver_data idxd_driver_data[] = {
40435b512dSDave Jiang 	[IDXD_TYPE_DSA] = {
41435b512dSDave Jiang 		.name_prefix = "dsa",
42435b512dSDave Jiang 		.type = IDXD_TYPE_DSA,
43435b512dSDave Jiang 		.compl_size = sizeof(struct dsa_completion_record),
44435b512dSDave Jiang 		.align = 32,
45435b512dSDave Jiang 		.dev_type = &dsa_device_type,
46435b512dSDave Jiang 	},
47435b512dSDave Jiang 	[IDXD_TYPE_IAX] = {
48435b512dSDave Jiang 		.name_prefix = "iax",
49435b512dSDave Jiang 		.type = IDXD_TYPE_IAX,
50435b512dSDave Jiang 		.compl_size = sizeof(struct iax_completion_record),
51435b512dSDave Jiang 		.align = 64,
52435b512dSDave Jiang 		.dev_type = &iax_device_type,
53435b512dSDave Jiang 	},
54435b512dSDave Jiang };
55435b512dSDave Jiang 
56bfe1d560SDave Jiang static struct pci_device_id idxd_pci_tbl[] = {
57bfe1d560SDave Jiang 	/* DSA ver 1.0 platforms */
58435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
59f25b4638SDave Jiang 
60f25b4638SDave Jiang 	/* IAX ver 1.0 platforms */
61435b512dSDave Jiang 	{ PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
62bfe1d560SDave Jiang 	{ 0, }
63bfe1d560SDave Jiang };
64bfe1d560SDave Jiang MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
65bfe1d560SDave Jiang 
66bfe1d560SDave Jiang static int idxd_setup_interrupts(struct idxd_device *idxd)
67bfe1d560SDave Jiang {
68bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
69bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
70bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
71bfe1d560SDave Jiang 	int i, msixcnt;
72bfe1d560SDave Jiang 	int rc = 0;
73bfe1d560SDave Jiang 
74bfe1d560SDave Jiang 	msixcnt = pci_msix_vec_count(pdev);
75bfe1d560SDave Jiang 	if (msixcnt < 0) {
76bfe1d560SDave Jiang 		dev_err(dev, "Not MSI-X interrupt capable.\n");
775fc8e85fSDave Jiang 		return -ENOSPC;
78bfe1d560SDave Jiang 	}
79bfe1d560SDave Jiang 
805fc8e85fSDave Jiang 	rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
815fc8e85fSDave Jiang 	if (rc != msixcnt) {
825fc8e85fSDave Jiang 		dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
835fc8e85fSDave Jiang 		return -ENOSPC;
84bfe1d560SDave Jiang 	}
85bfe1d560SDave Jiang 	dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
86bfe1d560SDave Jiang 
87bfe1d560SDave Jiang 	/*
88bfe1d560SDave Jiang 	 * We implement 1 completion list per MSI-X entry except for
89bfe1d560SDave Jiang 	 * entry 0, which is for errors and others.
90bfe1d560SDave Jiang 	 */
9147c16ac2SDave Jiang 	idxd->irq_entries = kcalloc_node(msixcnt, sizeof(struct idxd_irq_entry),
9247c16ac2SDave Jiang 					 GFP_KERNEL, dev_to_node(dev));
93bfe1d560SDave Jiang 	if (!idxd->irq_entries) {
94bfe1d560SDave Jiang 		rc = -ENOMEM;
955fc8e85fSDave Jiang 		goto err_irq_entries;
96bfe1d560SDave Jiang 	}
97bfe1d560SDave Jiang 
98bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
99bfe1d560SDave Jiang 		idxd->irq_entries[i].id = i;
100bfe1d560SDave Jiang 		idxd->irq_entries[i].idxd = idxd;
1015fc8e85fSDave Jiang 		idxd->irq_entries[i].vector = pci_irq_vector(pdev, i);
102e4f4d8cdSDave Jiang 		spin_lock_init(&idxd->irq_entries[i].list_lock);
103bfe1d560SDave Jiang 	}
104bfe1d560SDave Jiang 
105d5c10e0fSDave Jiang 	idxd_msix_perm_setup(idxd);
106d5c10e0fSDave Jiang 
107bfe1d560SDave Jiang 	irq_entry = &idxd->irq_entries[0];
108a1610461SDave Jiang 	rc = request_threaded_irq(irq_entry->vector, NULL, idxd_misc_thread,
1095fc8e85fSDave Jiang 				  0, "idxd-misc", irq_entry);
110bfe1d560SDave Jiang 	if (rc < 0) {
111bfe1d560SDave Jiang 		dev_err(dev, "Failed to allocate misc interrupt.\n");
1125fc8e85fSDave Jiang 		goto err_misc_irq;
113bfe1d560SDave Jiang 	}
114bfe1d560SDave Jiang 
1155fc8e85fSDave Jiang 	dev_dbg(dev, "Allocated idxd-misc handler on msix vector %d\n", irq_entry->vector);
116bfe1d560SDave Jiang 
117bfe1d560SDave Jiang 	/* first MSI-X entry is not for wq interrupts */
118bfe1d560SDave Jiang 	idxd->num_wq_irqs = msixcnt - 1;
119bfe1d560SDave Jiang 
120bfe1d560SDave Jiang 	for (i = 1; i < msixcnt; i++) {
121bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
122bfe1d560SDave Jiang 
123bfe1d560SDave Jiang 		init_llist_head(&idxd->irq_entries[i].pending_llist);
124bfe1d560SDave Jiang 		INIT_LIST_HEAD(&idxd->irq_entries[i].work_list);
125a1610461SDave Jiang 		rc = request_threaded_irq(irq_entry->vector, NULL,
1265fc8e85fSDave Jiang 					  idxd_wq_thread, 0, "idxd-portal", irq_entry);
127bfe1d560SDave Jiang 		if (rc < 0) {
1285fc8e85fSDave Jiang 			dev_err(dev, "Failed to allocate irq %d.\n", irq_entry->vector);
1295fc8e85fSDave Jiang 			goto err_wq_irqs;
130bfe1d560SDave Jiang 		}
131eb15e715SDave Jiang 
1325fc8e85fSDave Jiang 		dev_dbg(dev, "Allocated idxd-msix %d for vector %d\n", i, irq_entry->vector);
133eb15e715SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
134eb15e715SDave Jiang 			/*
135eb15e715SDave Jiang 			 * The MSIX vector enumeration starts at 1 with vector 0 being the
136eb15e715SDave Jiang 			 * misc interrupt that handles non I/O completion events. The
137eb15e715SDave Jiang 			 * interrupt handles are for IMS enumeration on guest. The misc
138eb15e715SDave Jiang 			 * interrupt vector does not require a handle and therefore we start
139eb15e715SDave Jiang 			 * the int_handles at index 0. Since 'i' starts at 1, the first
140eb15e715SDave Jiang 			 * int_handles index will be 0.
141eb15e715SDave Jiang 			 */
142eb15e715SDave Jiang 			rc = idxd_device_request_int_handle(idxd, i, &idxd->int_handles[i - 1],
143eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
144eb15e715SDave Jiang 			if (rc < 0) {
145eb15e715SDave Jiang 				free_irq(irq_entry->vector, irq_entry);
146eb15e715SDave Jiang 				goto err_wq_irqs;
147eb15e715SDave Jiang 			}
148eb15e715SDave Jiang 			dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i - 1]);
149eb15e715SDave Jiang 		}
150bfe1d560SDave Jiang 	}
151bfe1d560SDave Jiang 
152bfe1d560SDave Jiang 	idxd_unmask_error_interrupts(idxd);
153bfe1d560SDave Jiang 	return 0;
154bfe1d560SDave Jiang 
1555fc8e85fSDave Jiang  err_wq_irqs:
1565fc8e85fSDave Jiang 	while (--i >= 0) {
1575fc8e85fSDave Jiang 		irq_entry = &idxd->irq_entries[i];
1585fc8e85fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
159eb15e715SDave Jiang 		if (i != 0)
160eb15e715SDave Jiang 			idxd_device_release_int_handle(idxd,
161eb15e715SDave Jiang 						       idxd->int_handles[i], IDXD_IRQ_MSIX);
1625fc8e85fSDave Jiang 	}
1635fc8e85fSDave Jiang  err_misc_irq:
164bfe1d560SDave Jiang 	/* Disable error interrupt generation */
165bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
166d5c10e0fSDave Jiang 	idxd_msix_perm_clear(idxd);
1675fc8e85fSDave Jiang  err_irq_entries:
1685fc8e85fSDave Jiang 	pci_free_irq_vectors(pdev);
169bfe1d560SDave Jiang 	dev_err(dev, "No usable interrupts\n");
170bfe1d560SDave Jiang 	return rc;
171bfe1d560SDave Jiang }
172bfe1d560SDave Jiang 
173ddf742d4SDave Jiang static void idxd_cleanup_interrupts(struct idxd_device *idxd)
174ddf742d4SDave Jiang {
175ddf742d4SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
176ddf742d4SDave Jiang 	struct idxd_irq_entry *irq_entry;
177ddf742d4SDave Jiang 	int i, msixcnt;
178ddf742d4SDave Jiang 
179ddf742d4SDave Jiang 	msixcnt = pci_msix_vec_count(pdev);
180ddf742d4SDave Jiang 	if (msixcnt <= 0)
181ddf742d4SDave Jiang 		return;
182ddf742d4SDave Jiang 
183ddf742d4SDave Jiang 	irq_entry = &idxd->irq_entries[0];
184ddf742d4SDave Jiang 	free_irq(irq_entry->vector, irq_entry);
185ddf742d4SDave Jiang 
186ddf742d4SDave Jiang 	for (i = 1; i < msixcnt; i++) {
187ddf742d4SDave Jiang 
188ddf742d4SDave Jiang 		irq_entry = &idxd->irq_entries[i];
189ddf742d4SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE))
190ddf742d4SDave Jiang 			idxd_device_release_int_handle(idxd, idxd->int_handles[i],
191ddf742d4SDave Jiang 						       IDXD_IRQ_MSIX);
192ddf742d4SDave Jiang 		free_irq(irq_entry->vector, irq_entry);
193ddf742d4SDave Jiang 	}
194ddf742d4SDave Jiang 
195ddf742d4SDave Jiang 	idxd_mask_error_interrupts(idxd);
196ddf742d4SDave Jiang 	pci_free_irq_vectors(pdev);
197ddf742d4SDave Jiang }
198ddf742d4SDave Jiang 
1997c5dd23eSDave Jiang static int idxd_setup_wqs(struct idxd_device *idxd)
2007c5dd23eSDave Jiang {
2017c5dd23eSDave Jiang 	struct device *dev = &idxd->pdev->dev;
2027c5dd23eSDave Jiang 	struct idxd_wq *wq;
2037c5dd23eSDave Jiang 	int i, rc;
2047c5dd23eSDave Jiang 
2057c5dd23eSDave Jiang 	idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
2067c5dd23eSDave Jiang 				 GFP_KERNEL, dev_to_node(dev));
2077c5dd23eSDave Jiang 	if (!idxd->wqs)
2087c5dd23eSDave Jiang 		return -ENOMEM;
2097c5dd23eSDave Jiang 
2107c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
2117c5dd23eSDave Jiang 		wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
2127c5dd23eSDave Jiang 		if (!wq) {
2137c5dd23eSDave Jiang 			rc = -ENOMEM;
2147c5dd23eSDave Jiang 			goto err;
2157c5dd23eSDave Jiang 		}
2167c5dd23eSDave Jiang 
2177c5dd23eSDave Jiang 		wq->id = i;
2187c5dd23eSDave Jiang 		wq->idxd = idxd;
2197c5dd23eSDave Jiang 		device_initialize(&wq->conf_dev);
2207c5dd23eSDave Jiang 		wq->conf_dev.parent = &idxd->conf_dev;
2214b73e4ebSDave Jiang 		wq->conf_dev.bus = &dsa_bus_type;
2227c5dd23eSDave Jiang 		wq->conf_dev.type = &idxd_wq_device_type;
2237c5dd23eSDave Jiang 		rc = dev_set_name(&wq->conf_dev, "wq%d.%d", idxd->id, wq->id);
2247c5dd23eSDave Jiang 		if (rc < 0) {
2257c5dd23eSDave Jiang 			put_device(&wq->conf_dev);
2267c5dd23eSDave Jiang 			goto err;
2277c5dd23eSDave Jiang 		}
2287c5dd23eSDave Jiang 
2297c5dd23eSDave Jiang 		mutex_init(&wq->wq_lock);
23004922b74SDave Jiang 		init_waitqueue_head(&wq->err_queue);
23193a40a6dSDave Jiang 		init_completion(&wq->wq_dead);
2327c5dd23eSDave Jiang 		wq->max_xfer_bytes = idxd->max_xfer_bytes;
2337c5dd23eSDave Jiang 		wq->max_batch_size = idxd->max_batch_size;
2347c5dd23eSDave Jiang 		wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
2357c5dd23eSDave Jiang 		if (!wq->wqcfg) {
2367c5dd23eSDave Jiang 			put_device(&wq->conf_dev);
2377c5dd23eSDave Jiang 			rc = -ENOMEM;
2387c5dd23eSDave Jiang 			goto err;
2397c5dd23eSDave Jiang 		}
2407c5dd23eSDave Jiang 		idxd->wqs[i] = wq;
2417c5dd23eSDave Jiang 	}
2427c5dd23eSDave Jiang 
2437c5dd23eSDave Jiang 	return 0;
2447c5dd23eSDave Jiang 
2457c5dd23eSDave Jiang  err:
2467c5dd23eSDave Jiang 	while (--i >= 0)
2477c5dd23eSDave Jiang 		put_device(&idxd->wqs[i]->conf_dev);
2487c5dd23eSDave Jiang 	return rc;
2497c5dd23eSDave Jiang }
2507c5dd23eSDave Jiang 
25175b91130SDave Jiang static int idxd_setup_engines(struct idxd_device *idxd)
25275b91130SDave Jiang {
25375b91130SDave Jiang 	struct idxd_engine *engine;
25475b91130SDave Jiang 	struct device *dev = &idxd->pdev->dev;
25575b91130SDave Jiang 	int i, rc;
25675b91130SDave Jiang 
25775b91130SDave Jiang 	idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
25875b91130SDave Jiang 				     GFP_KERNEL, dev_to_node(dev));
25975b91130SDave Jiang 	if (!idxd->engines)
26075b91130SDave Jiang 		return -ENOMEM;
26175b91130SDave Jiang 
26275b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++) {
26375b91130SDave Jiang 		engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
26475b91130SDave Jiang 		if (!engine) {
26575b91130SDave Jiang 			rc = -ENOMEM;
26675b91130SDave Jiang 			goto err;
26775b91130SDave Jiang 		}
26875b91130SDave Jiang 
26975b91130SDave Jiang 		engine->id = i;
27075b91130SDave Jiang 		engine->idxd = idxd;
27175b91130SDave Jiang 		device_initialize(&engine->conf_dev);
27275b91130SDave Jiang 		engine->conf_dev.parent = &idxd->conf_dev;
2731c4841ccSDave Jiang 		engine->conf_dev.bus = &dsa_bus_type;
27475b91130SDave Jiang 		engine->conf_dev.type = &idxd_engine_device_type;
27575b91130SDave Jiang 		rc = dev_set_name(&engine->conf_dev, "engine%d.%d", idxd->id, engine->id);
27675b91130SDave Jiang 		if (rc < 0) {
27775b91130SDave Jiang 			put_device(&engine->conf_dev);
27875b91130SDave Jiang 			goto err;
27975b91130SDave Jiang 		}
28075b91130SDave Jiang 
28175b91130SDave Jiang 		idxd->engines[i] = engine;
28275b91130SDave Jiang 	}
28375b91130SDave Jiang 
28475b91130SDave Jiang 	return 0;
28575b91130SDave Jiang 
28675b91130SDave Jiang  err:
28775b91130SDave Jiang 	while (--i >= 0)
28875b91130SDave Jiang 		put_device(&idxd->engines[i]->conf_dev);
28975b91130SDave Jiang 	return rc;
29075b91130SDave Jiang }
29175b91130SDave Jiang 
292defe49f9SDave Jiang static int idxd_setup_groups(struct idxd_device *idxd)
293defe49f9SDave Jiang {
294defe49f9SDave Jiang 	struct device *dev = &idxd->pdev->dev;
295defe49f9SDave Jiang 	struct idxd_group *group;
296defe49f9SDave Jiang 	int i, rc;
297defe49f9SDave Jiang 
298defe49f9SDave Jiang 	idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
299defe49f9SDave Jiang 				    GFP_KERNEL, dev_to_node(dev));
300defe49f9SDave Jiang 	if (!idxd->groups)
301defe49f9SDave Jiang 		return -ENOMEM;
302defe49f9SDave Jiang 
303defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++) {
304defe49f9SDave Jiang 		group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
305defe49f9SDave Jiang 		if (!group) {
306defe49f9SDave Jiang 			rc = -ENOMEM;
307defe49f9SDave Jiang 			goto err;
308defe49f9SDave Jiang 		}
309defe49f9SDave Jiang 
310defe49f9SDave Jiang 		group->id = i;
311defe49f9SDave Jiang 		group->idxd = idxd;
312defe49f9SDave Jiang 		device_initialize(&group->conf_dev);
313defe49f9SDave Jiang 		group->conf_dev.parent = &idxd->conf_dev;
3144b73e4ebSDave Jiang 		group->conf_dev.bus = &dsa_bus_type;
315defe49f9SDave Jiang 		group->conf_dev.type = &idxd_group_device_type;
316defe49f9SDave Jiang 		rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id);
317defe49f9SDave Jiang 		if (rc < 0) {
318defe49f9SDave Jiang 			put_device(&group->conf_dev);
319defe49f9SDave Jiang 			goto err;
320defe49f9SDave Jiang 		}
321defe49f9SDave Jiang 
322defe49f9SDave Jiang 		idxd->groups[i] = group;
323defe49f9SDave Jiang 		group->tc_a = -1;
324defe49f9SDave Jiang 		group->tc_b = -1;
325defe49f9SDave Jiang 	}
326defe49f9SDave Jiang 
327defe49f9SDave Jiang 	return 0;
328defe49f9SDave Jiang 
329defe49f9SDave Jiang  err:
330defe49f9SDave Jiang 	while (--i >= 0)
331defe49f9SDave Jiang 		put_device(&idxd->groups[i]->conf_dev);
332defe49f9SDave Jiang 	return rc;
333defe49f9SDave Jiang }
334defe49f9SDave Jiang 
335ddf742d4SDave Jiang static void idxd_cleanup_internals(struct idxd_device *idxd)
336ddf742d4SDave Jiang {
337ddf742d4SDave Jiang 	int i;
338ddf742d4SDave Jiang 
339ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
340ddf742d4SDave Jiang 		put_device(&idxd->groups[i]->conf_dev);
341ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
342ddf742d4SDave Jiang 		put_device(&idxd->engines[i]->conf_dev);
343ddf742d4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
344ddf742d4SDave Jiang 		put_device(&idxd->wqs[i]->conf_dev);
345ddf742d4SDave Jiang 	destroy_workqueue(idxd->wq);
346ddf742d4SDave Jiang }
347ddf742d4SDave Jiang 
348bfe1d560SDave Jiang static int idxd_setup_internals(struct idxd_device *idxd)
349bfe1d560SDave Jiang {
350bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
351defe49f9SDave Jiang 	int rc, i;
352bfe1d560SDave Jiang 
3530d5c10b4SDave Jiang 	init_waitqueue_head(&idxd->cmd_waitq);
3547c5dd23eSDave Jiang 
355eb15e715SDave Jiang 	if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)) {
35633f9f3c3SDave Jiang 		idxd->int_handles = kcalloc_node(idxd->max_wqs, sizeof(int), GFP_KERNEL,
35733f9f3c3SDave Jiang 						 dev_to_node(dev));
358eb15e715SDave Jiang 		if (!idxd->int_handles)
359eb15e715SDave Jiang 			return -ENOMEM;
360eb15e715SDave Jiang 	}
361eb15e715SDave Jiang 
3627c5dd23eSDave Jiang 	rc = idxd_setup_wqs(idxd);
3637c5dd23eSDave Jiang 	if (rc < 0)
364eb15e715SDave Jiang 		goto err_wqs;
3657c5dd23eSDave Jiang 
36675b91130SDave Jiang 	rc = idxd_setup_engines(idxd);
36775b91130SDave Jiang 	if (rc < 0)
36875b91130SDave Jiang 		goto err_engine;
36975b91130SDave Jiang 
370defe49f9SDave Jiang 	rc = idxd_setup_groups(idxd);
371defe49f9SDave Jiang 	if (rc < 0)
372defe49f9SDave Jiang 		goto err_group;
373bfe1d560SDave Jiang 
3740d5c10b4SDave Jiang 	idxd->wq = create_workqueue(dev_name(dev));
3757c5dd23eSDave Jiang 	if (!idxd->wq) {
3767c5dd23eSDave Jiang 		rc = -ENOMEM;
377defe49f9SDave Jiang 		goto err_wkq_create;
3787c5dd23eSDave Jiang 	}
3790d5c10b4SDave Jiang 
380bfe1d560SDave Jiang 	return 0;
3817c5dd23eSDave Jiang 
382defe49f9SDave Jiang  err_wkq_create:
383defe49f9SDave Jiang 	for (i = 0; i < idxd->max_groups; i++)
384defe49f9SDave Jiang 		put_device(&idxd->groups[i]->conf_dev);
385defe49f9SDave Jiang  err_group:
38675b91130SDave Jiang 	for (i = 0; i < idxd->max_engines; i++)
38775b91130SDave Jiang 		put_device(&idxd->engines[i]->conf_dev);
38875b91130SDave Jiang  err_engine:
3897c5dd23eSDave Jiang 	for (i = 0; i < idxd->max_wqs; i++)
3907c5dd23eSDave Jiang 		put_device(&idxd->wqs[i]->conf_dev);
391eb15e715SDave Jiang  err_wqs:
392eb15e715SDave Jiang 	kfree(idxd->int_handles);
3937c5dd23eSDave Jiang 	return rc;
394bfe1d560SDave Jiang }
395bfe1d560SDave Jiang 
396bfe1d560SDave Jiang static void idxd_read_table_offsets(struct idxd_device *idxd)
397bfe1d560SDave Jiang {
398bfe1d560SDave Jiang 	union offsets_reg offsets;
399bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
400bfe1d560SDave Jiang 
401bfe1d560SDave Jiang 	offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
4022f8417a9SDave Jiang 	offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
4032f8417a9SDave Jiang 	idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
404bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
4052f8417a9SDave Jiang 	idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
4062f8417a9SDave Jiang 	dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
4072f8417a9SDave Jiang 	idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
4082f8417a9SDave Jiang 	dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
4092f8417a9SDave Jiang 	idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
410bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
411bfe1d560SDave Jiang }
412bfe1d560SDave Jiang 
413bfe1d560SDave Jiang static void idxd_read_caps(struct idxd_device *idxd)
414bfe1d560SDave Jiang {
415bfe1d560SDave Jiang 	struct device *dev = &idxd->pdev->dev;
416bfe1d560SDave Jiang 	int i;
417bfe1d560SDave Jiang 
418bfe1d560SDave Jiang 	/* reading generic capabilities */
419bfe1d560SDave Jiang 	idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
420bfe1d560SDave Jiang 	dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
421eb15e715SDave Jiang 
422eb15e715SDave Jiang 	if (idxd->hw.gen_cap.cmd_cap) {
423eb15e715SDave Jiang 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
424eb15e715SDave Jiang 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
425eb15e715SDave Jiang 	}
426eb15e715SDave Jiang 
427bfe1d560SDave Jiang 	idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
428bfe1d560SDave Jiang 	dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
429bfe1d560SDave Jiang 	idxd->max_batch_size = 1U << idxd->hw.gen_cap.max_batch_shift;
430bfe1d560SDave Jiang 	dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
431bfe1d560SDave Jiang 	if (idxd->hw.gen_cap.config_en)
432bfe1d560SDave Jiang 		set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
433bfe1d560SDave Jiang 
434bfe1d560SDave Jiang 	/* reading group capabilities */
435bfe1d560SDave Jiang 	idxd->hw.group_cap.bits =
436bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
437bfe1d560SDave Jiang 	dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
438bfe1d560SDave Jiang 	idxd->max_groups = idxd->hw.group_cap.num_groups;
439bfe1d560SDave Jiang 	dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
440bfe1d560SDave Jiang 	idxd->max_tokens = idxd->hw.group_cap.total_tokens;
441bfe1d560SDave Jiang 	dev_dbg(dev, "max tokens: %u\n", idxd->max_tokens);
442c52ca478SDave Jiang 	idxd->nr_tokens = idxd->max_tokens;
443bfe1d560SDave Jiang 
444bfe1d560SDave Jiang 	/* read engine capabilities */
445bfe1d560SDave Jiang 	idxd->hw.engine_cap.bits =
446bfe1d560SDave Jiang 		ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
447bfe1d560SDave Jiang 	dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
448bfe1d560SDave Jiang 	idxd->max_engines = idxd->hw.engine_cap.num_engines;
449bfe1d560SDave Jiang 	dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
450bfe1d560SDave Jiang 
451bfe1d560SDave Jiang 	/* read workqueue capabilities */
452bfe1d560SDave Jiang 	idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
453bfe1d560SDave Jiang 	dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
454bfe1d560SDave Jiang 	idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
455bfe1d560SDave Jiang 	dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
456bfe1d560SDave Jiang 	idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
457bfe1d560SDave Jiang 	dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
458d98793b5SDave Jiang 	idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
459d98793b5SDave Jiang 	dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
460bfe1d560SDave Jiang 
461bfe1d560SDave Jiang 	/* reading operation capabilities */
462bfe1d560SDave Jiang 	for (i = 0; i < 4; i++) {
463bfe1d560SDave Jiang 		idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
464bfe1d560SDave Jiang 				IDXD_OPCAP_OFFSET + i * sizeof(u64));
465bfe1d560SDave Jiang 		dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
466bfe1d560SDave Jiang 	}
467bfe1d560SDave Jiang }
468bfe1d560SDave Jiang 
469435b512dSDave Jiang static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
470bfe1d560SDave Jiang {
471bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
472bfe1d560SDave Jiang 	struct idxd_device *idxd;
47347c16ac2SDave Jiang 	int rc;
474bfe1d560SDave Jiang 
47547c16ac2SDave Jiang 	idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
476bfe1d560SDave Jiang 	if (!idxd)
477bfe1d560SDave Jiang 		return NULL;
478bfe1d560SDave Jiang 
479bfe1d560SDave Jiang 	idxd->pdev = pdev;
480435b512dSDave Jiang 	idxd->data = data;
4814b73e4ebSDave Jiang 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
48247c16ac2SDave Jiang 	if (idxd->id < 0)
48347c16ac2SDave Jiang 		return NULL;
48447c16ac2SDave Jiang 
48547c16ac2SDave Jiang 	device_initialize(&idxd->conf_dev);
48647c16ac2SDave Jiang 	idxd->conf_dev.parent = dev;
4874b73e4ebSDave Jiang 	idxd->conf_dev.bus = &dsa_bus_type;
488435b512dSDave Jiang 	idxd->conf_dev.type = idxd->data->dev_type;
489435b512dSDave Jiang 	rc = dev_set_name(&idxd->conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
49047c16ac2SDave Jiang 	if (rc < 0) {
49147c16ac2SDave Jiang 		put_device(&idxd->conf_dev);
49247c16ac2SDave Jiang 		return NULL;
49347c16ac2SDave Jiang 	}
49447c16ac2SDave Jiang 
495bfe1d560SDave Jiang 	spin_lock_init(&idxd->dev_lock);
49653b2ee7fSDave Jiang 	spin_lock_init(&idxd->cmd_lock);
497bfe1d560SDave Jiang 
498bfe1d560SDave Jiang 	return idxd;
499bfe1d560SDave Jiang }
500bfe1d560SDave Jiang 
5018e50d392SDave Jiang static int idxd_enable_system_pasid(struct idxd_device *idxd)
5028e50d392SDave Jiang {
5038e50d392SDave Jiang 	int flags;
5048e50d392SDave Jiang 	unsigned int pasid;
5058e50d392SDave Jiang 	struct iommu_sva *sva;
5068e50d392SDave Jiang 
5078e50d392SDave Jiang 	flags = SVM_FLAG_SUPERVISOR_MODE;
5088e50d392SDave Jiang 
5098e50d392SDave Jiang 	sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
5108e50d392SDave Jiang 	if (IS_ERR(sva)) {
5118e50d392SDave Jiang 		dev_warn(&idxd->pdev->dev,
5128e50d392SDave Jiang 			 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
5138e50d392SDave Jiang 		return PTR_ERR(sva);
5148e50d392SDave Jiang 	}
5158e50d392SDave Jiang 
5168e50d392SDave Jiang 	pasid = iommu_sva_get_pasid(sva);
5178e50d392SDave Jiang 	if (pasid == IOMMU_PASID_INVALID) {
5188e50d392SDave Jiang 		iommu_sva_unbind_device(sva);
5198e50d392SDave Jiang 		return -ENODEV;
5208e50d392SDave Jiang 	}
5218e50d392SDave Jiang 
5228e50d392SDave Jiang 	idxd->sva = sva;
5238e50d392SDave Jiang 	idxd->pasid = pasid;
5248e50d392SDave Jiang 	dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
5258e50d392SDave Jiang 	return 0;
5268e50d392SDave Jiang }
5278e50d392SDave Jiang 
5288e50d392SDave Jiang static void idxd_disable_system_pasid(struct idxd_device *idxd)
5298e50d392SDave Jiang {
5308e50d392SDave Jiang 
5318e50d392SDave Jiang 	iommu_sva_unbind_device(idxd->sva);
5328e50d392SDave Jiang 	idxd->sva = NULL;
5338e50d392SDave Jiang }
5348e50d392SDave Jiang 
535bfe1d560SDave Jiang static int idxd_probe(struct idxd_device *idxd)
536bfe1d560SDave Jiang {
537bfe1d560SDave Jiang 	struct pci_dev *pdev = idxd->pdev;
538bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
539bfe1d560SDave Jiang 	int rc;
540bfe1d560SDave Jiang 
541bfe1d560SDave Jiang 	dev_dbg(dev, "%s entered and resetting device\n", __func__);
54289e3becdSDave Jiang 	rc = idxd_device_init_reset(idxd);
54389e3becdSDave Jiang 	if (rc < 0)
54489e3becdSDave Jiang 		return rc;
54589e3becdSDave Jiang 
546bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD reset complete\n");
547bfe1d560SDave Jiang 
54803d939c7SDave Jiang 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
549cf5f86a7SDave Jiang 		rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA);
550cf5f86a7SDave Jiang 		if (rc == 0) {
5518e50d392SDave Jiang 			rc = idxd_enable_system_pasid(idxd);
552cf5f86a7SDave Jiang 			if (rc < 0) {
553cf5f86a7SDave Jiang 				iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
5548e50d392SDave Jiang 				dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
555cf5f86a7SDave Jiang 			} else {
5568e50d392SDave Jiang 				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
557cf5f86a7SDave Jiang 			}
558cf5f86a7SDave Jiang 		} else {
559cf5f86a7SDave Jiang 			dev_warn(dev, "Unable to turn on SVA feature.\n");
560cf5f86a7SDave Jiang 		}
56103d939c7SDave Jiang 	} else if (!sva) {
56203d939c7SDave Jiang 		dev_warn(dev, "User forced SVA off via module param.\n");
5638e50d392SDave Jiang 	}
5648e50d392SDave Jiang 
565bfe1d560SDave Jiang 	idxd_read_caps(idxd);
566bfe1d560SDave Jiang 	idxd_read_table_offsets(idxd);
567bfe1d560SDave Jiang 
568bfe1d560SDave Jiang 	rc = idxd_setup_internals(idxd);
569bfe1d560SDave Jiang 	if (rc)
5707c5dd23eSDave Jiang 		goto err;
571bfe1d560SDave Jiang 
5728c66bbdcSDave Jiang 	/* If the configs are readonly, then load them from device */
5738c66bbdcSDave Jiang 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
5748c66bbdcSDave Jiang 		dev_dbg(dev, "Loading RO device config\n");
5758c66bbdcSDave Jiang 		rc = idxd_device_load_config(idxd);
5768c66bbdcSDave Jiang 		if (rc < 0)
577ddf742d4SDave Jiang 			goto err_config;
5788c66bbdcSDave Jiang 	}
5798c66bbdcSDave Jiang 
580bfe1d560SDave Jiang 	rc = idxd_setup_interrupts(idxd);
581bfe1d560SDave Jiang 	if (rc)
582ddf742d4SDave Jiang 		goto err_config;
583bfe1d560SDave Jiang 
584bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD interrupt setup complete.\n");
585bfe1d560SDave Jiang 
58642d279f9SDave Jiang 	idxd->major = idxd_cdev_get_major(idxd);
58742d279f9SDave Jiang 
5880bde4444STom Zanussi 	rc = perfmon_pmu_init(idxd);
5890bde4444STom Zanussi 	if (rc < 0)
5900bde4444STom Zanussi 		dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
5910bde4444STom Zanussi 
592bfe1d560SDave Jiang 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
593bfe1d560SDave Jiang 	return 0;
594bfe1d560SDave Jiang 
595ddf742d4SDave Jiang  err_config:
596ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
5977c5dd23eSDave Jiang  err:
5988e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
5998e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
600cf5f86a7SDave Jiang 	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
601bfe1d560SDave Jiang 	return rc;
602bfe1d560SDave Jiang }
603bfe1d560SDave Jiang 
604ddf742d4SDave Jiang static void idxd_cleanup(struct idxd_device *idxd)
605ddf742d4SDave Jiang {
606ddf742d4SDave Jiang 	struct device *dev = &idxd->pdev->dev;
607ddf742d4SDave Jiang 
608ddf742d4SDave Jiang 	perfmon_pmu_remove(idxd);
609ddf742d4SDave Jiang 	idxd_cleanup_interrupts(idxd);
610ddf742d4SDave Jiang 	idxd_cleanup_internals(idxd);
611ddf742d4SDave Jiang 	if (device_pasid_enabled(idxd))
612ddf742d4SDave Jiang 		idxd_disable_system_pasid(idxd);
613ddf742d4SDave Jiang 	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
614ddf742d4SDave Jiang }
615ddf742d4SDave Jiang 
616bfe1d560SDave Jiang static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
617bfe1d560SDave Jiang {
618bfe1d560SDave Jiang 	struct device *dev = &pdev->dev;
619bfe1d560SDave Jiang 	struct idxd_device *idxd;
620435b512dSDave Jiang 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
621bfe1d560SDave Jiang 	int rc;
622bfe1d560SDave Jiang 
623a39c7cd0SDave Jiang 	rc = pci_enable_device(pdev);
624bfe1d560SDave Jiang 	if (rc)
625bfe1d560SDave Jiang 		return rc;
626bfe1d560SDave Jiang 
6278e50d392SDave Jiang 	dev_dbg(dev, "Alloc IDXD context\n");
628435b512dSDave Jiang 	idxd = idxd_alloc(pdev, data);
629a39c7cd0SDave Jiang 	if (!idxd) {
630a39c7cd0SDave Jiang 		rc = -ENOMEM;
631a39c7cd0SDave Jiang 		goto err_idxd_alloc;
632a39c7cd0SDave Jiang 	}
633bfe1d560SDave Jiang 
6348e50d392SDave Jiang 	dev_dbg(dev, "Mapping BARs\n");
635a39c7cd0SDave Jiang 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
636a39c7cd0SDave Jiang 	if (!idxd->reg_base) {
637a39c7cd0SDave Jiang 		rc = -ENOMEM;
638a39c7cd0SDave Jiang 		goto err_iomap;
639a39c7cd0SDave Jiang 	}
640bfe1d560SDave Jiang 
641bfe1d560SDave Jiang 	dev_dbg(dev, "Set DMA masks\n");
64253b50458SChristophe JAILLET 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
643bfe1d560SDave Jiang 	if (rc)
64453b50458SChristophe JAILLET 		rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
645bfe1d560SDave Jiang 	if (rc)
646a39c7cd0SDave Jiang 		goto err;
647bfe1d560SDave Jiang 
648bfe1d560SDave Jiang 	dev_dbg(dev, "Set PCI master\n");
649bfe1d560SDave Jiang 	pci_set_master(pdev);
650bfe1d560SDave Jiang 	pci_set_drvdata(pdev, idxd);
651bfe1d560SDave Jiang 
652bfe1d560SDave Jiang 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
653bfe1d560SDave Jiang 	rc = idxd_probe(idxd);
654bfe1d560SDave Jiang 	if (rc) {
655bfe1d560SDave Jiang 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
656a39c7cd0SDave Jiang 		goto err;
657bfe1d560SDave Jiang 	}
658bfe1d560SDave Jiang 
65947c16ac2SDave Jiang 	rc = idxd_register_devices(idxd);
660c52ca478SDave Jiang 	if (rc) {
661c52ca478SDave Jiang 		dev_err(dev, "IDXD sysfs setup failed\n");
662ddf742d4SDave Jiang 		goto err_dev_register;
663c52ca478SDave Jiang 	}
664c52ca478SDave Jiang 
665c52ca478SDave Jiang 	idxd->state = IDXD_DEV_CONF_READY;
666c52ca478SDave Jiang 
667bfe1d560SDave Jiang 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
668bfe1d560SDave Jiang 		 idxd->hw.version);
669bfe1d560SDave Jiang 
670bfe1d560SDave Jiang 	return 0;
671a39c7cd0SDave Jiang 
672ddf742d4SDave Jiang  err_dev_register:
673ddf742d4SDave Jiang 	idxd_cleanup(idxd);
674a39c7cd0SDave Jiang  err:
675a39c7cd0SDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
676a39c7cd0SDave Jiang  err_iomap:
67747c16ac2SDave Jiang 	put_device(&idxd->conf_dev);
678a39c7cd0SDave Jiang  err_idxd_alloc:
679a39c7cd0SDave Jiang 	pci_disable_device(pdev);
680a39c7cd0SDave Jiang 	return rc;
681bfe1d560SDave Jiang }
682bfe1d560SDave Jiang 
6838f47d1a5SDave Jiang static void idxd_flush_pending_llist(struct idxd_irq_entry *ie)
6848f47d1a5SDave Jiang {
6858f47d1a5SDave Jiang 	struct idxd_desc *desc, *itr;
6868f47d1a5SDave Jiang 	struct llist_node *head;
6878f47d1a5SDave Jiang 
6888f47d1a5SDave Jiang 	head = llist_del_all(&ie->pending_llist);
6898f47d1a5SDave Jiang 	if (!head)
6908f47d1a5SDave Jiang 		return;
6918f47d1a5SDave Jiang 
6928f47d1a5SDave Jiang 	llist_for_each_entry_safe(desc, itr, head, llnode) {
6938f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
6948f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
6958f47d1a5SDave Jiang 	}
6968f47d1a5SDave Jiang }
6978f47d1a5SDave Jiang 
6988f47d1a5SDave Jiang static void idxd_flush_work_list(struct idxd_irq_entry *ie)
6998f47d1a5SDave Jiang {
7008f47d1a5SDave Jiang 	struct idxd_desc *desc, *iter;
7018f47d1a5SDave Jiang 
7028f47d1a5SDave Jiang 	list_for_each_entry_safe(desc, iter, &ie->work_list, list) {
7038f47d1a5SDave Jiang 		list_del(&desc->list);
7048f47d1a5SDave Jiang 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT);
7058f47d1a5SDave Jiang 		idxd_free_desc(desc->wq, desc);
7068f47d1a5SDave Jiang 	}
7078f47d1a5SDave Jiang }
7088f47d1a5SDave Jiang 
7095b0c68c4SDave Jiang void idxd_wqs_quiesce(struct idxd_device *idxd)
7105b0c68c4SDave Jiang {
7115b0c68c4SDave Jiang 	struct idxd_wq *wq;
7125b0c68c4SDave Jiang 	int i;
7135b0c68c4SDave Jiang 
7145b0c68c4SDave Jiang 	for (i = 0; i < idxd->max_wqs; i++) {
7155b0c68c4SDave Jiang 		wq = idxd->wqs[i];
7165b0c68c4SDave Jiang 		if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
7175b0c68c4SDave Jiang 			idxd_wq_quiesce(wq);
7185b0c68c4SDave Jiang 	}
7195b0c68c4SDave Jiang }
7205b0c68c4SDave Jiang 
721eb15e715SDave Jiang static void idxd_release_int_handles(struct idxd_device *idxd)
722eb15e715SDave Jiang {
723eb15e715SDave Jiang 	struct device *dev = &idxd->pdev->dev;
724eb15e715SDave Jiang 	int i, rc;
725eb15e715SDave Jiang 
726eb15e715SDave Jiang 	for (i = 0; i < idxd->num_wq_irqs; i++) {
727eb15e715SDave Jiang 		if (idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)) {
728eb15e715SDave Jiang 			rc = idxd_device_release_int_handle(idxd, idxd->int_handles[i],
729eb15e715SDave Jiang 							    IDXD_IRQ_MSIX);
730eb15e715SDave Jiang 			if (rc < 0)
731eb15e715SDave Jiang 				dev_warn(dev, "irq handle %d release failed\n",
732eb15e715SDave Jiang 					 idxd->int_handles[i]);
733eb15e715SDave Jiang 			else
734eb15e715SDave Jiang 				dev_dbg(dev, "int handle requested: %u\n", idxd->int_handles[i]);
735eb15e715SDave Jiang 		}
736eb15e715SDave Jiang 	}
737eb15e715SDave Jiang }
738eb15e715SDave Jiang 
739bfe1d560SDave Jiang static void idxd_shutdown(struct pci_dev *pdev)
740bfe1d560SDave Jiang {
741bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
742bfe1d560SDave Jiang 	int rc, i;
743bfe1d560SDave Jiang 	struct idxd_irq_entry *irq_entry;
744bfe1d560SDave Jiang 	int msixcnt = pci_msix_vec_count(pdev);
745bfe1d560SDave Jiang 
746bfe1d560SDave Jiang 	rc = idxd_device_disable(idxd);
747bfe1d560SDave Jiang 	if (rc)
748bfe1d560SDave Jiang 		dev_err(&pdev->dev, "Disabling device failed\n");
749bfe1d560SDave Jiang 
750bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
751bfe1d560SDave Jiang 	idxd_mask_msix_vectors(idxd);
752bfe1d560SDave Jiang 	idxd_mask_error_interrupts(idxd);
753bfe1d560SDave Jiang 
754bfe1d560SDave Jiang 	for (i = 0; i < msixcnt; i++) {
755bfe1d560SDave Jiang 		irq_entry = &idxd->irq_entries[i];
7565fc8e85fSDave Jiang 		synchronize_irq(irq_entry->vector);
757bfe1d560SDave Jiang 		if (i == 0)
758bfe1d560SDave Jiang 			continue;
7598f47d1a5SDave Jiang 		idxd_flush_pending_llist(irq_entry);
7608f47d1a5SDave Jiang 		idxd_flush_work_list(irq_entry);
761bfe1d560SDave Jiang 	}
762*49c4959fSDave Jiang 	flush_workqueue(idxd->wq);
763bfe1d560SDave Jiang }
764bfe1d560SDave Jiang 
765bfe1d560SDave Jiang static void idxd_remove(struct pci_dev *pdev)
766bfe1d560SDave Jiang {
767bfe1d560SDave Jiang 	struct idxd_device *idxd = pci_get_drvdata(pdev);
768*49c4959fSDave Jiang 	struct idxd_irq_entry *irq_entry;
769*49c4959fSDave Jiang 	int msixcnt = pci_msix_vec_count(pdev);
770*49c4959fSDave Jiang 	int i;
771bfe1d560SDave Jiang 
772bfe1d560SDave Jiang 	dev_dbg(&pdev->dev, "%s called\n", __func__);
773bfe1d560SDave Jiang 	idxd_shutdown(pdev);
7748e50d392SDave Jiang 	if (device_pasid_enabled(idxd))
7758e50d392SDave Jiang 		idxd_disable_system_pasid(idxd);
77647c16ac2SDave Jiang 	idxd_unregister_devices(idxd);
777*49c4959fSDave Jiang 
778*49c4959fSDave Jiang 	for (i = 0; i < msixcnt; i++) {
779*49c4959fSDave Jiang 		irq_entry = &idxd->irq_entries[i];
780*49c4959fSDave Jiang 		free_irq(irq_entry->vector, irq_entry);
781*49c4959fSDave Jiang 	}
782*49c4959fSDave Jiang 	idxd_msix_perm_clear(idxd);
783*49c4959fSDave Jiang 	idxd_release_int_handles(idxd);
784*49c4959fSDave Jiang 	pci_free_irq_vectors(pdev);
785*49c4959fSDave Jiang 	pci_iounmap(pdev, idxd->reg_base);
786cf5f86a7SDave Jiang 	iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
787*49c4959fSDave Jiang 	pci_disable_device(pdev);
788*49c4959fSDave Jiang 	destroy_workqueue(idxd->wq);
789*49c4959fSDave Jiang 	perfmon_pmu_remove(idxd);
790*49c4959fSDave Jiang 	device_unregister(&idxd->conf_dev);
791bfe1d560SDave Jiang }
792bfe1d560SDave Jiang 
793bfe1d560SDave Jiang static struct pci_driver idxd_pci_driver = {
794bfe1d560SDave Jiang 	.name		= DRV_NAME,
795bfe1d560SDave Jiang 	.id_table	= idxd_pci_tbl,
796bfe1d560SDave Jiang 	.probe		= idxd_pci_probe,
797bfe1d560SDave Jiang 	.remove		= idxd_remove,
798bfe1d560SDave Jiang 	.shutdown	= idxd_shutdown,
799bfe1d560SDave Jiang };
800bfe1d560SDave Jiang 
801bfe1d560SDave Jiang static int __init idxd_init_module(void)
802bfe1d560SDave Jiang {
8034b73e4ebSDave Jiang 	int err;
804bfe1d560SDave Jiang 
805bfe1d560SDave Jiang 	/*
8068e50d392SDave Jiang 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
807bfe1d560SDave Jiang 	 * enumerating the device. We can not utilize it.
808bfe1d560SDave Jiang 	 */
80974b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
810bfe1d560SDave Jiang 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
811bfe1d560SDave Jiang 		return -ENODEV;
812bfe1d560SDave Jiang 	}
813bfe1d560SDave Jiang 
81474b2fc88SBorislav Petkov 	if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
8158e50d392SDave Jiang 		pr_warn("Platform does not have ENQCMD(S) support.\n");
8168e50d392SDave Jiang 	else
8178e50d392SDave Jiang 		support_enqcmd = true;
818bfe1d560SDave Jiang 
8190bde4444STom Zanussi 	perfmon_init();
8200bde4444STom Zanussi 
821c52ca478SDave Jiang 	err = idxd_register_bus_type();
822c52ca478SDave Jiang 	if (err < 0)
823bfe1d560SDave Jiang 		return err;
824bfe1d560SDave Jiang 
825c52ca478SDave Jiang 	err = idxd_register_driver();
826c52ca478SDave Jiang 	if (err < 0)
827c52ca478SDave Jiang 		goto err_idxd_driver_register;
828c52ca478SDave Jiang 
82942d279f9SDave Jiang 	err = idxd_cdev_register();
83042d279f9SDave Jiang 	if (err)
83142d279f9SDave Jiang 		goto err_cdev_register;
83242d279f9SDave Jiang 
833c52ca478SDave Jiang 	err = pci_register_driver(&idxd_pci_driver);
834c52ca478SDave Jiang 	if (err)
835c52ca478SDave Jiang 		goto err_pci_register;
836c52ca478SDave Jiang 
837bfe1d560SDave Jiang 	return 0;
838c52ca478SDave Jiang 
839c52ca478SDave Jiang err_pci_register:
84042d279f9SDave Jiang 	idxd_cdev_remove();
84142d279f9SDave Jiang err_cdev_register:
842c52ca478SDave Jiang 	idxd_unregister_driver();
843c52ca478SDave Jiang err_idxd_driver_register:
844c52ca478SDave Jiang 	idxd_unregister_bus_type();
845c52ca478SDave Jiang 	return err;
846bfe1d560SDave Jiang }
847bfe1d560SDave Jiang module_init(idxd_init_module);
848bfe1d560SDave Jiang 
849bfe1d560SDave Jiang static void __exit idxd_exit_module(void)
850bfe1d560SDave Jiang {
851077cdb35SDave Jiang 	idxd_unregister_driver();
852bfe1d560SDave Jiang 	pci_unregister_driver(&idxd_pci_driver);
85342d279f9SDave Jiang 	idxd_cdev_remove();
854c52ca478SDave Jiang 	idxd_unregister_bus_type();
8550bde4444STom Zanussi 	perfmon_exit();
856bfe1d560SDave Jiang }
857bfe1d560SDave Jiang module_exit(idxd_exit_module);
858