xref: /openbmc/linux/drivers/dma/idxd/init.c (revision 244da66c)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
3 #include <linux/init.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/slab.h>
7 #include <linux/pci.h>
8 #include <linux/interrupt.h>
9 #include <linux/delay.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/workqueue.h>
12 #include <linux/fs.h>
13 #include <linux/io-64-nonatomic-lo-hi.h>
14 #include <linux/device.h>
15 #include <linux/idr.h>
16 #include <linux/iommu.h>
17 #include <uapi/linux/idxd.h>
18 #include <linux/dmaengine.h>
19 #include "../dmaengine.h"
20 #include "registers.h"
21 #include "idxd.h"
22 #include "perfmon.h"
23 
24 MODULE_VERSION(IDXD_DRIVER_VERSION);
25 MODULE_LICENSE("GPL v2");
26 MODULE_AUTHOR("Intel Corporation");
27 MODULE_IMPORT_NS(IDXD);
28 
29 static bool sva = true;
30 module_param(sva, bool, 0644);
31 MODULE_PARM_DESC(sva, "Toggle SVA support on/off");
32 
33 bool tc_override;
34 module_param(tc_override, bool, 0644);
35 MODULE_PARM_DESC(tc_override, "Override traffic class defaults");
36 
37 #define DRV_NAME "idxd"
38 
39 bool support_enqcmd;
40 DEFINE_IDA(idxd_ida);
41 
42 static struct idxd_driver_data idxd_driver_data[] = {
43 	[IDXD_TYPE_DSA] = {
44 		.name_prefix = "dsa",
45 		.type = IDXD_TYPE_DSA,
46 		.compl_size = sizeof(struct dsa_completion_record),
47 		.align = 32,
48 		.dev_type = &dsa_device_type,
49 	},
50 	[IDXD_TYPE_IAX] = {
51 		.name_prefix = "iax",
52 		.type = IDXD_TYPE_IAX,
53 		.compl_size = sizeof(struct iax_completion_record),
54 		.align = 64,
55 		.dev_type = &iax_device_type,
56 	},
57 };
58 
59 static struct pci_device_id idxd_pci_tbl[] = {
60 	/* DSA ver 1.0 platforms */
61 	{ PCI_DEVICE_DATA(INTEL, DSA_SPR0, &idxd_driver_data[IDXD_TYPE_DSA]) },
62 
63 	/* IAX ver 1.0 platforms */
64 	{ PCI_DEVICE_DATA(INTEL, IAX_SPR0, &idxd_driver_data[IDXD_TYPE_IAX]) },
65 	{ 0, }
66 };
67 MODULE_DEVICE_TABLE(pci, idxd_pci_tbl);
68 
69 static int idxd_setup_interrupts(struct idxd_device *idxd)
70 {
71 	struct pci_dev *pdev = idxd->pdev;
72 	struct device *dev = &pdev->dev;
73 	struct idxd_irq_entry *ie;
74 	int i, msixcnt;
75 	int rc = 0;
76 
77 	msixcnt = pci_msix_vec_count(pdev);
78 	if (msixcnt < 0) {
79 		dev_err(dev, "Not MSI-X interrupt capable.\n");
80 		return -ENOSPC;
81 	}
82 	idxd->irq_cnt = msixcnt;
83 
84 	rc = pci_alloc_irq_vectors(pdev, msixcnt, msixcnt, PCI_IRQ_MSIX);
85 	if (rc != msixcnt) {
86 		dev_err(dev, "Failed enabling %d MSIX entries: %d\n", msixcnt, rc);
87 		return -ENOSPC;
88 	}
89 	dev_dbg(dev, "Enabled %d msix vectors\n", msixcnt);
90 
91 
92 	ie = idxd_get_ie(idxd, 0);
93 	ie->vector = pci_irq_vector(pdev, 0);
94 	rc = request_threaded_irq(ie->vector, NULL, idxd_misc_thread, 0, "idxd-misc", ie);
95 	if (rc < 0) {
96 		dev_err(dev, "Failed to allocate misc interrupt.\n");
97 		goto err_misc_irq;
98 	}
99 	dev_dbg(dev, "Requested idxd-misc handler on msix vector %d\n", ie->vector);
100 
101 	for (i = 0; i < idxd->max_wqs; i++) {
102 		int msix_idx = i + 1;
103 
104 		ie = idxd_get_ie(idxd, msix_idx);
105 		ie->id = msix_idx;
106 		ie->int_handle = INVALID_INT_HANDLE;
107 		ie->pasid = INVALID_IOASID;
108 
109 		spin_lock_init(&ie->list_lock);
110 		init_llist_head(&ie->pending_llist);
111 		INIT_LIST_HEAD(&ie->work_list);
112 	}
113 
114 	idxd_unmask_error_interrupts(idxd);
115 	return 0;
116 
117  err_misc_irq:
118 	idxd_mask_error_interrupts(idxd);
119 	pci_free_irq_vectors(pdev);
120 	dev_err(dev, "No usable interrupts\n");
121 	return rc;
122 }
123 
124 static void idxd_cleanup_interrupts(struct idxd_device *idxd)
125 {
126 	struct pci_dev *pdev = idxd->pdev;
127 	struct idxd_irq_entry *ie;
128 	int msixcnt;
129 
130 	msixcnt = pci_msix_vec_count(pdev);
131 	if (msixcnt <= 0)
132 		return;
133 
134 	ie = idxd_get_ie(idxd, 0);
135 	idxd_mask_error_interrupts(idxd);
136 	free_irq(ie->vector, ie);
137 	pci_free_irq_vectors(pdev);
138 }
139 
140 static int idxd_setup_wqs(struct idxd_device *idxd)
141 {
142 	struct device *dev = &idxd->pdev->dev;
143 	struct idxd_wq *wq;
144 	struct device *conf_dev;
145 	int i, rc;
146 
147 	idxd->wqs = kcalloc_node(idxd->max_wqs, sizeof(struct idxd_wq *),
148 				 GFP_KERNEL, dev_to_node(dev));
149 	if (!idxd->wqs)
150 		return -ENOMEM;
151 
152 	idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev));
153 	if (!idxd->wq_enable_map) {
154 		kfree(idxd->wqs);
155 		return -ENOMEM;
156 	}
157 
158 	for (i = 0; i < idxd->max_wqs; i++) {
159 		wq = kzalloc_node(sizeof(*wq), GFP_KERNEL, dev_to_node(dev));
160 		if (!wq) {
161 			rc = -ENOMEM;
162 			goto err;
163 		}
164 
165 		idxd_dev_set_type(&wq->idxd_dev, IDXD_DEV_WQ);
166 		conf_dev = wq_confdev(wq);
167 		wq->id = i;
168 		wq->idxd = idxd;
169 		device_initialize(wq_confdev(wq));
170 		conf_dev->parent = idxd_confdev(idxd);
171 		conf_dev->bus = &dsa_bus_type;
172 		conf_dev->type = &idxd_wq_device_type;
173 		rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id);
174 		if (rc < 0) {
175 			put_device(conf_dev);
176 			goto err;
177 		}
178 
179 		mutex_init(&wq->wq_lock);
180 		init_waitqueue_head(&wq->err_queue);
181 		init_completion(&wq->wq_dead);
182 		init_completion(&wq->wq_resurrect);
183 		wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
184 		idxd_wq_set_max_batch_size(idxd->data->type, wq, WQ_DEFAULT_MAX_BATCH);
185 		wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
186 		wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev));
187 		if (!wq->wqcfg) {
188 			put_device(conf_dev);
189 			rc = -ENOMEM;
190 			goto err;
191 		}
192 
193 		if (idxd->hw.wq_cap.op_config) {
194 			wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL);
195 			if (!wq->opcap_bmap) {
196 				put_device(conf_dev);
197 				rc = -ENOMEM;
198 				goto err;
199 			}
200 			bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
201 		}
202 		idxd->wqs[i] = wq;
203 	}
204 
205 	return 0;
206 
207  err:
208 	while (--i >= 0) {
209 		wq = idxd->wqs[i];
210 		conf_dev = wq_confdev(wq);
211 		put_device(conf_dev);
212 	}
213 	return rc;
214 }
215 
216 static int idxd_setup_engines(struct idxd_device *idxd)
217 {
218 	struct idxd_engine *engine;
219 	struct device *dev = &idxd->pdev->dev;
220 	struct device *conf_dev;
221 	int i, rc;
222 
223 	idxd->engines = kcalloc_node(idxd->max_engines, sizeof(struct idxd_engine *),
224 				     GFP_KERNEL, dev_to_node(dev));
225 	if (!idxd->engines)
226 		return -ENOMEM;
227 
228 	for (i = 0; i < idxd->max_engines; i++) {
229 		engine = kzalloc_node(sizeof(*engine), GFP_KERNEL, dev_to_node(dev));
230 		if (!engine) {
231 			rc = -ENOMEM;
232 			goto err;
233 		}
234 
235 		idxd_dev_set_type(&engine->idxd_dev, IDXD_DEV_ENGINE);
236 		conf_dev = engine_confdev(engine);
237 		engine->id = i;
238 		engine->idxd = idxd;
239 		device_initialize(conf_dev);
240 		conf_dev->parent = idxd_confdev(idxd);
241 		conf_dev->bus = &dsa_bus_type;
242 		conf_dev->type = &idxd_engine_device_type;
243 		rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id);
244 		if (rc < 0) {
245 			put_device(conf_dev);
246 			goto err;
247 		}
248 
249 		idxd->engines[i] = engine;
250 	}
251 
252 	return 0;
253 
254  err:
255 	while (--i >= 0) {
256 		engine = idxd->engines[i];
257 		conf_dev = engine_confdev(engine);
258 		put_device(conf_dev);
259 	}
260 	return rc;
261 }
262 
263 static int idxd_setup_groups(struct idxd_device *idxd)
264 {
265 	struct device *dev = &idxd->pdev->dev;
266 	struct device *conf_dev;
267 	struct idxd_group *group;
268 	int i, rc;
269 
270 	idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
271 				    GFP_KERNEL, dev_to_node(dev));
272 	if (!idxd->groups)
273 		return -ENOMEM;
274 
275 	for (i = 0; i < idxd->max_groups; i++) {
276 		group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
277 		if (!group) {
278 			rc = -ENOMEM;
279 			goto err;
280 		}
281 
282 		idxd_dev_set_type(&group->idxd_dev, IDXD_DEV_GROUP);
283 		conf_dev = group_confdev(group);
284 		group->id = i;
285 		group->idxd = idxd;
286 		device_initialize(conf_dev);
287 		conf_dev->parent = idxd_confdev(idxd);
288 		conf_dev->bus = &dsa_bus_type;
289 		conf_dev->type = &idxd_group_device_type;
290 		rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id);
291 		if (rc < 0) {
292 			put_device(conf_dev);
293 			goto err;
294 		}
295 
296 		idxd->groups[i] = group;
297 		if (idxd->hw.version <= DEVICE_VERSION_2 && !tc_override) {
298 			group->tc_a = 1;
299 			group->tc_b = 1;
300 		} else {
301 			group->tc_a = -1;
302 			group->tc_b = -1;
303 		}
304 		/*
305 		 * The default value is the same as the value of
306 		 * total read buffers in GRPCAP.
307 		 */
308 		group->rdbufs_allowed = idxd->max_rdbufs;
309 	}
310 
311 	return 0;
312 
313  err:
314 	while (--i >= 0) {
315 		group = idxd->groups[i];
316 		put_device(group_confdev(group));
317 	}
318 	return rc;
319 }
320 
321 static void idxd_cleanup_internals(struct idxd_device *idxd)
322 {
323 	int i;
324 
325 	for (i = 0; i < idxd->max_groups; i++)
326 		put_device(group_confdev(idxd->groups[i]));
327 	for (i = 0; i < idxd->max_engines; i++)
328 		put_device(engine_confdev(idxd->engines[i]));
329 	for (i = 0; i < idxd->max_wqs; i++)
330 		put_device(wq_confdev(idxd->wqs[i]));
331 	destroy_workqueue(idxd->wq);
332 }
333 
334 static int idxd_init_evl(struct idxd_device *idxd)
335 {
336 	struct device *dev = &idxd->pdev->dev;
337 	struct idxd_evl *evl;
338 
339 	if (idxd->hw.gen_cap.evl_support == 0)
340 		return 0;
341 
342 	evl = kzalloc_node(sizeof(*evl), GFP_KERNEL, dev_to_node(dev));
343 	if (!evl)
344 		return -ENOMEM;
345 
346 	spin_lock_init(&evl->lock);
347 	evl->size = IDXD_EVL_SIZE_MIN;
348 	idxd->evl = evl;
349 	return 0;
350 }
351 
352 static int idxd_setup_internals(struct idxd_device *idxd)
353 {
354 	struct device *dev = &idxd->pdev->dev;
355 	int rc, i;
356 
357 	init_waitqueue_head(&idxd->cmd_waitq);
358 
359 	rc = idxd_setup_wqs(idxd);
360 	if (rc < 0)
361 		goto err_wqs;
362 
363 	rc = idxd_setup_engines(idxd);
364 	if (rc < 0)
365 		goto err_engine;
366 
367 	rc = idxd_setup_groups(idxd);
368 	if (rc < 0)
369 		goto err_group;
370 
371 	idxd->wq = create_workqueue(dev_name(dev));
372 	if (!idxd->wq) {
373 		rc = -ENOMEM;
374 		goto err_wkq_create;
375 	}
376 
377 	rc = idxd_init_evl(idxd);
378 	if (rc < 0)
379 		goto err_evl;
380 
381 	return 0;
382 
383  err_evl:
384 	destroy_workqueue(idxd->wq);
385  err_wkq_create:
386 	for (i = 0; i < idxd->max_groups; i++)
387 		put_device(group_confdev(idxd->groups[i]));
388  err_group:
389 	for (i = 0; i < idxd->max_engines; i++)
390 		put_device(engine_confdev(idxd->engines[i]));
391  err_engine:
392 	for (i = 0; i < idxd->max_wqs; i++)
393 		put_device(wq_confdev(idxd->wqs[i]));
394  err_wqs:
395 	return rc;
396 }
397 
398 static void idxd_read_table_offsets(struct idxd_device *idxd)
399 {
400 	union offsets_reg offsets;
401 	struct device *dev = &idxd->pdev->dev;
402 
403 	offsets.bits[0] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET);
404 	offsets.bits[1] = ioread64(idxd->reg_base + IDXD_TABLE_OFFSET + sizeof(u64));
405 	idxd->grpcfg_offset = offsets.grpcfg * IDXD_TABLE_MULT;
406 	dev_dbg(dev, "IDXD Group Config Offset: %#x\n", idxd->grpcfg_offset);
407 	idxd->wqcfg_offset = offsets.wqcfg * IDXD_TABLE_MULT;
408 	dev_dbg(dev, "IDXD Work Queue Config Offset: %#x\n", idxd->wqcfg_offset);
409 	idxd->msix_perm_offset = offsets.msix_perm * IDXD_TABLE_MULT;
410 	dev_dbg(dev, "IDXD MSIX Permission Offset: %#x\n", idxd->msix_perm_offset);
411 	idxd->perfmon_offset = offsets.perfmon * IDXD_TABLE_MULT;
412 	dev_dbg(dev, "IDXD Perfmon Offset: %#x\n", idxd->perfmon_offset);
413 }
414 
415 void multi_u64_to_bmap(unsigned long *bmap, u64 *val, int count)
416 {
417 	int i, j, nr;
418 
419 	for (i = 0, nr = 0; i < count; i++) {
420 		for (j = 0; j < BITS_PER_LONG_LONG; j++) {
421 			if (val[i] & BIT(j))
422 				set_bit(nr, bmap);
423 			nr++;
424 		}
425 	}
426 }
427 
428 static void idxd_read_caps(struct idxd_device *idxd)
429 {
430 	struct device *dev = &idxd->pdev->dev;
431 	int i;
432 
433 	/* reading generic capabilities */
434 	idxd->hw.gen_cap.bits = ioread64(idxd->reg_base + IDXD_GENCAP_OFFSET);
435 	dev_dbg(dev, "gen_cap: %#llx\n", idxd->hw.gen_cap.bits);
436 
437 	if (idxd->hw.gen_cap.cmd_cap) {
438 		idxd->hw.cmd_cap = ioread32(idxd->reg_base + IDXD_CMDCAP_OFFSET);
439 		dev_dbg(dev, "cmd_cap: %#x\n", idxd->hw.cmd_cap);
440 	}
441 
442 	/* reading command capabilities */
443 	if (idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE))
444 		idxd->request_int_handles = true;
445 
446 	idxd->max_xfer_bytes = 1ULL << idxd->hw.gen_cap.max_xfer_shift;
447 	dev_dbg(dev, "max xfer size: %llu bytes\n", idxd->max_xfer_bytes);
448 	idxd_set_max_batch_size(idxd->data->type, idxd, 1U << idxd->hw.gen_cap.max_batch_shift);
449 	dev_dbg(dev, "max batch size: %u\n", idxd->max_batch_size);
450 	if (idxd->hw.gen_cap.config_en)
451 		set_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags);
452 
453 	/* reading group capabilities */
454 	idxd->hw.group_cap.bits =
455 		ioread64(idxd->reg_base + IDXD_GRPCAP_OFFSET);
456 	dev_dbg(dev, "group_cap: %#llx\n", idxd->hw.group_cap.bits);
457 	idxd->max_groups = idxd->hw.group_cap.num_groups;
458 	dev_dbg(dev, "max groups: %u\n", idxd->max_groups);
459 	idxd->max_rdbufs = idxd->hw.group_cap.total_rdbufs;
460 	dev_dbg(dev, "max read buffers: %u\n", idxd->max_rdbufs);
461 	idxd->nr_rdbufs = idxd->max_rdbufs;
462 
463 	/* read engine capabilities */
464 	idxd->hw.engine_cap.bits =
465 		ioread64(idxd->reg_base + IDXD_ENGCAP_OFFSET);
466 	dev_dbg(dev, "engine_cap: %#llx\n", idxd->hw.engine_cap.bits);
467 	idxd->max_engines = idxd->hw.engine_cap.num_engines;
468 	dev_dbg(dev, "max engines: %u\n", idxd->max_engines);
469 
470 	/* read workqueue capabilities */
471 	idxd->hw.wq_cap.bits = ioread64(idxd->reg_base + IDXD_WQCAP_OFFSET);
472 	dev_dbg(dev, "wq_cap: %#llx\n", idxd->hw.wq_cap.bits);
473 	idxd->max_wq_size = idxd->hw.wq_cap.total_wq_size;
474 	dev_dbg(dev, "total workqueue size: %u\n", idxd->max_wq_size);
475 	idxd->max_wqs = idxd->hw.wq_cap.num_wqs;
476 	dev_dbg(dev, "max workqueues: %u\n", idxd->max_wqs);
477 	idxd->wqcfg_size = 1 << (idxd->hw.wq_cap.wqcfg_size + IDXD_WQCFG_MIN);
478 	dev_dbg(dev, "wqcfg size: %u\n", idxd->wqcfg_size);
479 
480 	/* reading operation capabilities */
481 	for (i = 0; i < 4; i++) {
482 		idxd->hw.opcap.bits[i] = ioread64(idxd->reg_base +
483 				IDXD_OPCAP_OFFSET + i * sizeof(u64));
484 		dev_dbg(dev, "opcap[%d]: %#llx\n", i, idxd->hw.opcap.bits[i]);
485 	}
486 	multi_u64_to_bmap(idxd->opcap_bmap, &idxd->hw.opcap.bits[0], 4);
487 
488 	/* read iaa cap */
489 	if (idxd->data->type == IDXD_TYPE_IAX && idxd->hw.version >= DEVICE_VERSION_2)
490 		idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET);
491 }
492 
493 static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data)
494 {
495 	struct device *dev = &pdev->dev;
496 	struct device *conf_dev;
497 	struct idxd_device *idxd;
498 	int rc;
499 
500 	idxd = kzalloc_node(sizeof(*idxd), GFP_KERNEL, dev_to_node(dev));
501 	if (!idxd)
502 		return NULL;
503 
504 	conf_dev = idxd_confdev(idxd);
505 	idxd->pdev = pdev;
506 	idxd->data = data;
507 	idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type);
508 	idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL);
509 	if (idxd->id < 0)
510 		return NULL;
511 
512 	idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev));
513 	if (!idxd->opcap_bmap) {
514 		ida_free(&idxd_ida, idxd->id);
515 		return NULL;
516 	}
517 
518 	device_initialize(conf_dev);
519 	conf_dev->parent = dev;
520 	conf_dev->bus = &dsa_bus_type;
521 	conf_dev->type = idxd->data->dev_type;
522 	rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id);
523 	if (rc < 0) {
524 		put_device(conf_dev);
525 		return NULL;
526 	}
527 
528 	spin_lock_init(&idxd->dev_lock);
529 	spin_lock_init(&idxd->cmd_lock);
530 
531 	return idxd;
532 }
533 
534 static int idxd_enable_system_pasid(struct idxd_device *idxd)
535 {
536 	return -EOPNOTSUPP;
537 }
538 
539 static void idxd_disable_system_pasid(struct idxd_device *idxd)
540 {
541 
542 	iommu_sva_unbind_device(idxd->sva);
543 	idxd->sva = NULL;
544 }
545 
546 static int idxd_probe(struct idxd_device *idxd)
547 {
548 	struct pci_dev *pdev = idxd->pdev;
549 	struct device *dev = &pdev->dev;
550 	int rc;
551 
552 	dev_dbg(dev, "%s entered and resetting device\n", __func__);
553 	rc = idxd_device_init_reset(idxd);
554 	if (rc < 0)
555 		return rc;
556 
557 	dev_dbg(dev, "IDXD reset complete\n");
558 
559 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
560 		if (iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA)) {
561 			dev_warn(dev, "Unable to turn on user SVA feature.\n");
562 		} else {
563 			set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
564 
565 			if (idxd_enable_system_pasid(idxd))
566 				dev_warn(dev, "No in-kernel DMA with PASID.\n");
567 			else
568 				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
569 		}
570 	} else if (!sva) {
571 		dev_warn(dev, "User forced SVA off via module param.\n");
572 	}
573 
574 	idxd_read_caps(idxd);
575 	idxd_read_table_offsets(idxd);
576 
577 	rc = idxd_setup_internals(idxd);
578 	if (rc)
579 		goto err;
580 
581 	/* If the configs are readonly, then load them from device */
582 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
583 		dev_dbg(dev, "Loading RO device config\n");
584 		rc = idxd_device_load_config(idxd);
585 		if (rc < 0)
586 			goto err_config;
587 	}
588 
589 	rc = idxd_setup_interrupts(idxd);
590 	if (rc)
591 		goto err_config;
592 
593 	idxd->major = idxd_cdev_get_major(idxd);
594 
595 	rc = perfmon_pmu_init(idxd);
596 	if (rc < 0)
597 		dev_warn(dev, "Failed to initialize perfmon. No PMU support: %d\n", rc);
598 
599 	dev_dbg(dev, "IDXD device %d probed successfully\n", idxd->id);
600 	return 0;
601 
602  err_config:
603 	idxd_cleanup_internals(idxd);
604  err:
605 	if (device_pasid_enabled(idxd))
606 		idxd_disable_system_pasid(idxd);
607 	if (device_user_pasid_enabled(idxd))
608 		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
609 	return rc;
610 }
611 
612 static void idxd_cleanup(struct idxd_device *idxd)
613 {
614 	struct device *dev = &idxd->pdev->dev;
615 
616 	perfmon_pmu_remove(idxd);
617 	idxd_cleanup_interrupts(idxd);
618 	idxd_cleanup_internals(idxd);
619 	if (device_pasid_enabled(idxd))
620 		idxd_disable_system_pasid(idxd);
621 	if (device_user_pasid_enabled(idxd))
622 		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
623 }
624 
625 static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
626 {
627 	struct device *dev = &pdev->dev;
628 	struct idxd_device *idxd;
629 	struct idxd_driver_data *data = (struct idxd_driver_data *)id->driver_data;
630 	int rc;
631 
632 	rc = pci_enable_device(pdev);
633 	if (rc)
634 		return rc;
635 
636 	dev_dbg(dev, "Alloc IDXD context\n");
637 	idxd = idxd_alloc(pdev, data);
638 	if (!idxd) {
639 		rc = -ENOMEM;
640 		goto err_idxd_alloc;
641 	}
642 
643 	dev_dbg(dev, "Mapping BARs\n");
644 	idxd->reg_base = pci_iomap(pdev, IDXD_MMIO_BAR, 0);
645 	if (!idxd->reg_base) {
646 		rc = -ENOMEM;
647 		goto err_iomap;
648 	}
649 
650 	dev_dbg(dev, "Set DMA masks\n");
651 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
652 	if (rc)
653 		goto err;
654 
655 	dev_dbg(dev, "Set PCI master\n");
656 	pci_set_master(pdev);
657 	pci_set_drvdata(pdev, idxd);
658 
659 	idxd->hw.version = ioread32(idxd->reg_base + IDXD_VER_OFFSET);
660 	rc = idxd_probe(idxd);
661 	if (rc) {
662 		dev_err(dev, "Intel(R) IDXD DMA Engine init failed\n");
663 		goto err;
664 	}
665 
666 	rc = idxd_register_devices(idxd);
667 	if (rc) {
668 		dev_err(dev, "IDXD sysfs setup failed\n");
669 		goto err_dev_register;
670 	}
671 
672 	dev_info(&pdev->dev, "Intel(R) Accelerator Device (v%x)\n",
673 		 idxd->hw.version);
674 
675 	return 0;
676 
677  err_dev_register:
678 	idxd_cleanup(idxd);
679  err:
680 	pci_iounmap(pdev, idxd->reg_base);
681  err_iomap:
682 	put_device(idxd_confdev(idxd));
683  err_idxd_alloc:
684 	pci_disable_device(pdev);
685 	return rc;
686 }
687 
688 void idxd_wqs_quiesce(struct idxd_device *idxd)
689 {
690 	struct idxd_wq *wq;
691 	int i;
692 
693 	for (i = 0; i < idxd->max_wqs; i++) {
694 		wq = idxd->wqs[i];
695 		if (wq->state == IDXD_WQ_ENABLED && wq->type == IDXD_WQT_KERNEL)
696 			idxd_wq_quiesce(wq);
697 	}
698 }
699 
700 static void idxd_shutdown(struct pci_dev *pdev)
701 {
702 	struct idxd_device *idxd = pci_get_drvdata(pdev);
703 	struct idxd_irq_entry *irq_entry;
704 	int rc;
705 
706 	rc = idxd_device_disable(idxd);
707 	if (rc)
708 		dev_err(&pdev->dev, "Disabling device failed\n");
709 
710 	irq_entry = &idxd->ie;
711 	synchronize_irq(irq_entry->vector);
712 	idxd_mask_error_interrupts(idxd);
713 	flush_workqueue(idxd->wq);
714 }
715 
716 static void idxd_remove(struct pci_dev *pdev)
717 {
718 	struct idxd_device *idxd = pci_get_drvdata(pdev);
719 	struct idxd_irq_entry *irq_entry;
720 
721 	idxd_unregister_devices(idxd);
722 	/*
723 	 * When ->release() is called for the idxd->conf_dev, it frees all the memory related
724 	 * to the idxd context. The driver still needs those bits in order to do the rest of
725 	 * the cleanup. However, we do need to unbound the idxd sub-driver. So take a ref
726 	 * on the device here to hold off the freeing while allowing the idxd sub-driver
727 	 * to unbind.
728 	 */
729 	get_device(idxd_confdev(idxd));
730 	device_unregister(idxd_confdev(idxd));
731 	idxd_shutdown(pdev);
732 	if (device_pasid_enabled(idxd))
733 		idxd_disable_system_pasid(idxd);
734 
735 	irq_entry = idxd_get_ie(idxd, 0);
736 	free_irq(irq_entry->vector, irq_entry);
737 	pci_free_irq_vectors(pdev);
738 	pci_iounmap(pdev, idxd->reg_base);
739 	if (device_user_pasid_enabled(idxd))
740 		iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
741 	pci_disable_device(pdev);
742 	destroy_workqueue(idxd->wq);
743 	perfmon_pmu_remove(idxd);
744 	put_device(idxd_confdev(idxd));
745 }
746 
747 static struct pci_driver idxd_pci_driver = {
748 	.name		= DRV_NAME,
749 	.id_table	= idxd_pci_tbl,
750 	.probe		= idxd_pci_probe,
751 	.remove		= idxd_remove,
752 	.shutdown	= idxd_shutdown,
753 };
754 
755 static int __init idxd_init_module(void)
756 {
757 	int err;
758 
759 	/*
760 	 * If the CPU does not support MOVDIR64B or ENQCMDS, there's no point in
761 	 * enumerating the device. We can not utilize it.
762 	 */
763 	if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
764 		pr_warn("idxd driver failed to load without MOVDIR64B.\n");
765 		return -ENODEV;
766 	}
767 
768 	if (!cpu_feature_enabled(X86_FEATURE_ENQCMD))
769 		pr_warn("Platform does not have ENQCMD(S) support.\n");
770 	else
771 		support_enqcmd = true;
772 
773 	perfmon_init();
774 
775 	err = idxd_driver_register(&idxd_drv);
776 	if (err < 0)
777 		goto err_idxd_driver_register;
778 
779 	err = idxd_driver_register(&idxd_dmaengine_drv);
780 	if (err < 0)
781 		goto err_idxd_dmaengine_driver_register;
782 
783 	err = idxd_driver_register(&idxd_user_drv);
784 	if (err < 0)
785 		goto err_idxd_user_driver_register;
786 
787 	err = idxd_cdev_register();
788 	if (err)
789 		goto err_cdev_register;
790 
791 	err = pci_register_driver(&idxd_pci_driver);
792 	if (err)
793 		goto err_pci_register;
794 
795 	return 0;
796 
797 err_pci_register:
798 	idxd_cdev_remove();
799 err_cdev_register:
800 	idxd_driver_unregister(&idxd_user_drv);
801 err_idxd_user_driver_register:
802 	idxd_driver_unregister(&idxd_dmaengine_drv);
803 err_idxd_dmaengine_driver_register:
804 	idxd_driver_unregister(&idxd_drv);
805 err_idxd_driver_register:
806 	return err;
807 }
808 module_init(idxd_init_module);
809 
810 static void __exit idxd_exit_module(void)
811 {
812 	idxd_driver_unregister(&idxd_user_drv);
813 	idxd_driver_unregister(&idxd_dmaengine_drv);
814 	idxd_driver_unregister(&idxd_drv);
815 	pci_unregister_driver(&idxd_pci_driver);
816 	idxd_cdev_remove();
817 	perfmon_exit();
818 }
819 module_exit(idxd_exit_module);
820