xref: /openbmc/linux/drivers/dma/idxd/device.c (revision 5921eb36)
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/pci.h>
7 #include <linux/io-64-nonatomic-lo-hi.h>
8 #include <linux/dmaengine.h>
9 #include <linux/irq.h>
10 #include <linux/msi.h>
11 #include <uapi/linux/idxd.h>
12 #include "../dmaengine.h"
13 #include "idxd.h"
14 #include "registers.h"
15 
16 static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
17 			  u32 *status);
18 static void idxd_device_wqs_clear_state(struct idxd_device *idxd);
19 static void idxd_wq_disable_cleanup(struct idxd_wq *wq);
20 
21 /* Interrupt control bits */
22 void idxd_unmask_error_interrupts(struct idxd_device *idxd)
23 {
24 	union genctrl_reg genctrl;
25 
26 	genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
27 	genctrl.softerr_int_en = 1;
28 	genctrl.halt_int_en = 1;
29 	iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
30 }
31 
32 void idxd_mask_error_interrupts(struct idxd_device *idxd)
33 {
34 	union genctrl_reg genctrl;
35 
36 	genctrl.bits = ioread32(idxd->reg_base + IDXD_GENCTRL_OFFSET);
37 	genctrl.softerr_int_en = 0;
38 	genctrl.halt_int_en = 0;
39 	iowrite32(genctrl.bits, idxd->reg_base + IDXD_GENCTRL_OFFSET);
40 }
41 
42 static void free_hw_descs(struct idxd_wq *wq)
43 {
44 	int i;
45 
46 	for (i = 0; i < wq->num_descs; i++)
47 		kfree(wq->hw_descs[i]);
48 
49 	kfree(wq->hw_descs);
50 }
51 
52 static int alloc_hw_descs(struct idxd_wq *wq, int num)
53 {
54 	struct device *dev = &wq->idxd->pdev->dev;
55 	int i;
56 	int node = dev_to_node(dev);
57 
58 	wq->hw_descs = kcalloc_node(num, sizeof(struct dsa_hw_desc *),
59 				    GFP_KERNEL, node);
60 	if (!wq->hw_descs)
61 		return -ENOMEM;
62 
63 	for (i = 0; i < num; i++) {
64 		wq->hw_descs[i] = kzalloc_node(sizeof(*wq->hw_descs[i]),
65 					       GFP_KERNEL, node);
66 		if (!wq->hw_descs[i]) {
67 			free_hw_descs(wq);
68 			return -ENOMEM;
69 		}
70 	}
71 
72 	return 0;
73 }
74 
75 static void free_descs(struct idxd_wq *wq)
76 {
77 	int i;
78 
79 	for (i = 0; i < wq->num_descs; i++)
80 		kfree(wq->descs[i]);
81 
82 	kfree(wq->descs);
83 }
84 
85 static int alloc_descs(struct idxd_wq *wq, int num)
86 {
87 	struct device *dev = &wq->idxd->pdev->dev;
88 	int i;
89 	int node = dev_to_node(dev);
90 
91 	wq->descs = kcalloc_node(num, sizeof(struct idxd_desc *),
92 				 GFP_KERNEL, node);
93 	if (!wq->descs)
94 		return -ENOMEM;
95 
96 	for (i = 0; i < num; i++) {
97 		wq->descs[i] = kzalloc_node(sizeof(*wq->descs[i]),
98 					    GFP_KERNEL, node);
99 		if (!wq->descs[i]) {
100 			free_descs(wq);
101 			return -ENOMEM;
102 		}
103 	}
104 
105 	return 0;
106 }
107 
108 /* WQ control bits */
109 int idxd_wq_alloc_resources(struct idxd_wq *wq)
110 {
111 	struct idxd_device *idxd = wq->idxd;
112 	struct device *dev = &idxd->pdev->dev;
113 	int rc, num_descs, i;
114 
115 	if (wq->type != IDXD_WQT_KERNEL)
116 		return 0;
117 
118 	num_descs = wq_dedicated(wq) ? wq->size : wq->threshold;
119 	wq->num_descs = num_descs;
120 
121 	rc = alloc_hw_descs(wq, num_descs);
122 	if (rc < 0)
123 		return rc;
124 
125 	wq->compls_size = num_descs * idxd->data->compl_size;
126 	wq->compls = dma_alloc_coherent(dev, wq->compls_size, &wq->compls_addr, GFP_KERNEL);
127 	if (!wq->compls) {
128 		rc = -ENOMEM;
129 		goto fail_alloc_compls;
130 	}
131 
132 	rc = alloc_descs(wq, num_descs);
133 	if (rc < 0)
134 		goto fail_alloc_descs;
135 
136 	rc = sbitmap_queue_init_node(&wq->sbq, num_descs, -1, false, GFP_KERNEL,
137 				     dev_to_node(dev));
138 	if (rc < 0)
139 		goto fail_sbitmap_init;
140 
141 	for (i = 0; i < num_descs; i++) {
142 		struct idxd_desc *desc = wq->descs[i];
143 
144 		desc->hw = wq->hw_descs[i];
145 		if (idxd->data->type == IDXD_TYPE_DSA)
146 			desc->completion = &wq->compls[i];
147 		else if (idxd->data->type == IDXD_TYPE_IAX)
148 			desc->iax_completion = &wq->iax_compls[i];
149 		desc->compl_dma = wq->compls_addr + idxd->data->compl_size * i;
150 		desc->id = i;
151 		desc->wq = wq;
152 		desc->cpu = -1;
153 	}
154 
155 	return 0;
156 
157  fail_sbitmap_init:
158 	free_descs(wq);
159  fail_alloc_descs:
160 	dma_free_coherent(dev, wq->compls_size, wq->compls, wq->compls_addr);
161  fail_alloc_compls:
162 	free_hw_descs(wq);
163 	return rc;
164 }
165 
166 void idxd_wq_free_resources(struct idxd_wq *wq)
167 {
168 	struct device *dev = &wq->idxd->pdev->dev;
169 
170 	if (wq->type != IDXD_WQT_KERNEL)
171 		return;
172 
173 	free_hw_descs(wq);
174 	free_descs(wq);
175 	dma_free_coherent(dev, wq->compls_size, wq->compls, wq->compls_addr);
176 	sbitmap_queue_free(&wq->sbq);
177 }
178 
179 int idxd_wq_enable(struct idxd_wq *wq)
180 {
181 	struct idxd_device *idxd = wq->idxd;
182 	struct device *dev = &idxd->pdev->dev;
183 	u32 status;
184 
185 	if (wq->state == IDXD_WQ_ENABLED) {
186 		dev_dbg(dev, "WQ %d already enabled\n", wq->id);
187 		return 0;
188 	}
189 
190 	idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_WQ, wq->id, &status);
191 
192 	if (status != IDXD_CMDSTS_SUCCESS &&
193 	    status != IDXD_CMDSTS_ERR_WQ_ENABLED) {
194 		dev_dbg(dev, "WQ enable failed: %#x\n", status);
195 		return -ENXIO;
196 	}
197 
198 	wq->state = IDXD_WQ_ENABLED;
199 	set_bit(wq->id, idxd->wq_enable_map);
200 	dev_dbg(dev, "WQ %d enabled\n", wq->id);
201 	return 0;
202 }
203 
204 int idxd_wq_disable(struct idxd_wq *wq, bool reset_config)
205 {
206 	struct idxd_device *idxd = wq->idxd;
207 	struct device *dev = &idxd->pdev->dev;
208 	u32 status, operand;
209 
210 	dev_dbg(dev, "Disabling WQ %d\n", wq->id);
211 
212 	if (wq->state != IDXD_WQ_ENABLED) {
213 		dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
214 		return 0;
215 	}
216 
217 	operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
218 	idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_WQ, operand, &status);
219 
220 	if (status != IDXD_CMDSTS_SUCCESS) {
221 		dev_dbg(dev, "WQ disable failed: %#x\n", status);
222 		return -ENXIO;
223 	}
224 
225 	if (reset_config)
226 		idxd_wq_disable_cleanup(wq);
227 	clear_bit(wq->id, idxd->wq_enable_map);
228 	wq->state = IDXD_WQ_DISABLED;
229 	dev_dbg(dev, "WQ %d disabled\n", wq->id);
230 	return 0;
231 }
232 
233 void idxd_wq_drain(struct idxd_wq *wq)
234 {
235 	struct idxd_device *idxd = wq->idxd;
236 	struct device *dev = &idxd->pdev->dev;
237 	u32 operand;
238 
239 	if (wq->state != IDXD_WQ_ENABLED) {
240 		dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
241 		return;
242 	}
243 
244 	dev_dbg(dev, "Draining WQ %d\n", wq->id);
245 	operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
246 	idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_WQ, operand, NULL);
247 }
248 
249 void idxd_wq_reset(struct idxd_wq *wq)
250 {
251 	struct idxd_device *idxd = wq->idxd;
252 	struct device *dev = &idxd->pdev->dev;
253 	u32 operand;
254 
255 	if (wq->state != IDXD_WQ_ENABLED) {
256 		dev_dbg(dev, "WQ %d in wrong state: %d\n", wq->id, wq->state);
257 		return;
258 	}
259 
260 	operand = BIT(wq->id % 16) | ((wq->id / 16) << 16);
261 	idxd_cmd_exec(idxd, IDXD_CMD_RESET_WQ, operand, NULL);
262 	idxd_wq_disable_cleanup(wq);
263 }
264 
265 int idxd_wq_map_portal(struct idxd_wq *wq)
266 {
267 	struct idxd_device *idxd = wq->idxd;
268 	struct pci_dev *pdev = idxd->pdev;
269 	struct device *dev = &pdev->dev;
270 	resource_size_t start;
271 
272 	start = pci_resource_start(pdev, IDXD_WQ_BAR);
273 	start += idxd_get_wq_portal_full_offset(wq->id, IDXD_PORTAL_LIMITED);
274 
275 	wq->portal = devm_ioremap(dev, start, IDXD_PORTAL_SIZE);
276 	if (!wq->portal)
277 		return -ENOMEM;
278 
279 	return 0;
280 }
281 
282 void idxd_wq_unmap_portal(struct idxd_wq *wq)
283 {
284 	struct device *dev = &wq->idxd->pdev->dev;
285 
286 	devm_iounmap(dev, wq->portal);
287 	wq->portal = NULL;
288 	wq->portal_offset = 0;
289 }
290 
291 void idxd_wqs_unmap_portal(struct idxd_device *idxd)
292 {
293 	int i;
294 
295 	for (i = 0; i < idxd->max_wqs; i++) {
296 		struct idxd_wq *wq = idxd->wqs[i];
297 
298 		if (wq->portal)
299 			idxd_wq_unmap_portal(wq);
300 	}
301 }
302 
303 static void __idxd_wq_set_priv_locked(struct idxd_wq *wq, int priv)
304 {
305 	struct idxd_device *idxd = wq->idxd;
306 	union wqcfg wqcfg;
307 	unsigned int offset;
308 
309 	offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PRIVL_IDX);
310 	spin_lock(&idxd->dev_lock);
311 	wqcfg.bits[WQCFG_PRIVL_IDX] = ioread32(idxd->reg_base + offset);
312 	wqcfg.priv = priv;
313 	wq->wqcfg->bits[WQCFG_PRIVL_IDX] = wqcfg.bits[WQCFG_PRIVL_IDX];
314 	iowrite32(wqcfg.bits[WQCFG_PRIVL_IDX], idxd->reg_base + offset);
315 	spin_unlock(&idxd->dev_lock);
316 }
317 
318 static void __idxd_wq_set_pasid_locked(struct idxd_wq *wq, int pasid)
319 {
320 	struct idxd_device *idxd = wq->idxd;
321 	union wqcfg wqcfg;
322 	unsigned int offset;
323 
324 	offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
325 	spin_lock(&idxd->dev_lock);
326 	wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
327 	wqcfg.pasid_en = 1;
328 	wqcfg.pasid = pasid;
329 	wq->wqcfg->bits[WQCFG_PASID_IDX] = wqcfg.bits[WQCFG_PASID_IDX];
330 	iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
331 	spin_unlock(&idxd->dev_lock);
332 }
333 
334 int idxd_wq_set_pasid(struct idxd_wq *wq, int pasid)
335 {
336 	int rc;
337 
338 	rc = idxd_wq_disable(wq, false);
339 	if (rc < 0)
340 		return rc;
341 
342 	__idxd_wq_set_pasid_locked(wq, pasid);
343 
344 	rc = idxd_wq_enable(wq);
345 	if (rc < 0)
346 		return rc;
347 
348 	return 0;
349 }
350 
351 int idxd_wq_disable_pasid(struct idxd_wq *wq)
352 {
353 	struct idxd_device *idxd = wq->idxd;
354 	int rc;
355 	union wqcfg wqcfg;
356 	unsigned int offset;
357 
358 	rc = idxd_wq_disable(wq, false);
359 	if (rc < 0)
360 		return rc;
361 
362 	offset = WQCFG_OFFSET(idxd, wq->id, WQCFG_PASID_IDX);
363 	spin_lock(&idxd->dev_lock);
364 	wqcfg.bits[WQCFG_PASID_IDX] = ioread32(idxd->reg_base + offset);
365 	wqcfg.pasid_en = 0;
366 	wqcfg.pasid = 0;
367 	iowrite32(wqcfg.bits[WQCFG_PASID_IDX], idxd->reg_base + offset);
368 	spin_unlock(&idxd->dev_lock);
369 
370 	rc = idxd_wq_enable(wq);
371 	if (rc < 0)
372 		return rc;
373 
374 	return 0;
375 }
376 
377 static void idxd_wq_disable_cleanup(struct idxd_wq *wq)
378 {
379 	struct idxd_device *idxd = wq->idxd;
380 
381 	lockdep_assert_held(&wq->wq_lock);
382 	wq->state = IDXD_WQ_DISABLED;
383 	memset(wq->wqcfg, 0, idxd->wqcfg_size);
384 	wq->type = IDXD_WQT_NONE;
385 	wq->threshold = 0;
386 	wq->priority = 0;
387 	wq->enqcmds_retries = IDXD_ENQCMDS_RETRIES;
388 	clear_bit(WQ_FLAG_DEDICATED, &wq->flags);
389 	clear_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags);
390 	clear_bit(WQ_FLAG_ATS_DISABLE, &wq->flags);
391 	memset(wq->name, 0, WQ_NAME_SIZE);
392 	wq->max_xfer_bytes = WQ_DEFAULT_MAX_XFER;
393 	wq->max_batch_size = WQ_DEFAULT_MAX_BATCH;
394 	if (wq->opcap_bmap)
395 		bitmap_copy(wq->opcap_bmap, idxd->opcap_bmap, IDXD_MAX_OPCAP_BITS);
396 }
397 
398 static void idxd_wq_device_reset_cleanup(struct idxd_wq *wq)
399 {
400 	lockdep_assert_held(&wq->wq_lock);
401 
402 	wq->size = 0;
403 	wq->group = NULL;
404 }
405 
406 static void idxd_wq_ref_release(struct percpu_ref *ref)
407 {
408 	struct idxd_wq *wq = container_of(ref, struct idxd_wq, wq_active);
409 
410 	complete(&wq->wq_dead);
411 }
412 
413 int idxd_wq_init_percpu_ref(struct idxd_wq *wq)
414 {
415 	int rc;
416 
417 	memset(&wq->wq_active, 0, sizeof(wq->wq_active));
418 	rc = percpu_ref_init(&wq->wq_active, idxd_wq_ref_release,
419 			     PERCPU_REF_ALLOW_REINIT, GFP_KERNEL);
420 	if (rc < 0)
421 		return rc;
422 	reinit_completion(&wq->wq_dead);
423 	reinit_completion(&wq->wq_resurrect);
424 	return 0;
425 }
426 
427 void __idxd_wq_quiesce(struct idxd_wq *wq)
428 {
429 	lockdep_assert_held(&wq->wq_lock);
430 	reinit_completion(&wq->wq_resurrect);
431 	percpu_ref_kill(&wq->wq_active);
432 	complete_all(&wq->wq_resurrect);
433 	wait_for_completion(&wq->wq_dead);
434 }
435 
436 void idxd_wq_quiesce(struct idxd_wq *wq)
437 {
438 	mutex_lock(&wq->wq_lock);
439 	__idxd_wq_quiesce(wq);
440 	mutex_unlock(&wq->wq_lock);
441 }
442 
443 /* Device control bits */
444 static inline bool idxd_is_enabled(struct idxd_device *idxd)
445 {
446 	union gensts_reg gensts;
447 
448 	gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
449 
450 	if (gensts.state == IDXD_DEVICE_STATE_ENABLED)
451 		return true;
452 	return false;
453 }
454 
455 static inline bool idxd_device_is_halted(struct idxd_device *idxd)
456 {
457 	union gensts_reg gensts;
458 
459 	gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
460 
461 	return (gensts.state == IDXD_DEVICE_STATE_HALT);
462 }
463 
464 /*
465  * This is function is only used for reset during probe and will
466  * poll for completion. Once the device is setup with interrupts,
467  * all commands will be done via interrupt completion.
468  */
469 int idxd_device_init_reset(struct idxd_device *idxd)
470 {
471 	struct device *dev = &idxd->pdev->dev;
472 	union idxd_command_reg cmd;
473 
474 	if (idxd_device_is_halted(idxd)) {
475 		dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
476 		return -ENXIO;
477 	}
478 
479 	memset(&cmd, 0, sizeof(cmd));
480 	cmd.cmd = IDXD_CMD_RESET_DEVICE;
481 	dev_dbg(dev, "%s: sending reset for init.\n", __func__);
482 	spin_lock(&idxd->cmd_lock);
483 	iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
484 
485 	while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) &
486 	       IDXD_CMDSTS_ACTIVE)
487 		cpu_relax();
488 	spin_unlock(&idxd->cmd_lock);
489 	return 0;
490 }
491 
492 static void idxd_cmd_exec(struct idxd_device *idxd, int cmd_code, u32 operand,
493 			  u32 *status)
494 {
495 	union idxd_command_reg cmd;
496 	DECLARE_COMPLETION_ONSTACK(done);
497 	u32 stat;
498 
499 	if (idxd_device_is_halted(idxd)) {
500 		dev_warn(&idxd->pdev->dev, "Device is HALTED!\n");
501 		if (status)
502 			*status = IDXD_CMDSTS_HW_ERR;
503 		return;
504 	}
505 
506 	memset(&cmd, 0, sizeof(cmd));
507 	cmd.cmd = cmd_code;
508 	cmd.operand = operand;
509 	cmd.int_req = 1;
510 
511 	spin_lock(&idxd->cmd_lock);
512 	wait_event_lock_irq(idxd->cmd_waitq,
513 			    !test_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags),
514 			    idxd->cmd_lock);
515 
516 	dev_dbg(&idxd->pdev->dev, "%s: sending cmd: %#x op: %#x\n",
517 		__func__, cmd_code, operand);
518 
519 	idxd->cmd_status = 0;
520 	__set_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
521 	idxd->cmd_done = &done;
522 	iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
523 
524 	/*
525 	 * After command submitted, release lock and go to sleep until
526 	 * the command completes via interrupt.
527 	 */
528 	spin_unlock(&idxd->cmd_lock);
529 	wait_for_completion(&done);
530 	stat = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
531 	spin_lock(&idxd->cmd_lock);
532 	if (status)
533 		*status = stat;
534 	idxd->cmd_status = stat & GENMASK(7, 0);
535 
536 	__clear_bit(IDXD_FLAG_CMD_RUNNING, &idxd->flags);
537 	/* Wake up other pending commands */
538 	wake_up(&idxd->cmd_waitq);
539 	spin_unlock(&idxd->cmd_lock);
540 }
541 
542 int idxd_device_enable(struct idxd_device *idxd)
543 {
544 	struct device *dev = &idxd->pdev->dev;
545 	u32 status;
546 
547 	if (idxd_is_enabled(idxd)) {
548 		dev_dbg(dev, "Device already enabled\n");
549 		return -ENXIO;
550 	}
551 
552 	idxd_cmd_exec(idxd, IDXD_CMD_ENABLE_DEVICE, 0, &status);
553 
554 	/* If the command is successful or if the device was enabled */
555 	if (status != IDXD_CMDSTS_SUCCESS &&
556 	    status != IDXD_CMDSTS_ERR_DEV_ENABLED) {
557 		dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
558 		return -ENXIO;
559 	}
560 
561 	idxd->state = IDXD_DEV_ENABLED;
562 	return 0;
563 }
564 
565 int idxd_device_disable(struct idxd_device *idxd)
566 {
567 	struct device *dev = &idxd->pdev->dev;
568 	u32 status;
569 
570 	if (!idxd_is_enabled(idxd)) {
571 		dev_dbg(dev, "Device is not enabled\n");
572 		return 0;
573 	}
574 
575 	idxd_cmd_exec(idxd, IDXD_CMD_DISABLE_DEVICE, 0, &status);
576 
577 	/* If the command is successful or if the device was disabled */
578 	if (status != IDXD_CMDSTS_SUCCESS &&
579 	    !(status & IDXD_CMDSTS_ERR_DIS_DEV_EN)) {
580 		dev_dbg(dev, "%s: err_code: %#x\n", __func__, status);
581 		return -ENXIO;
582 	}
583 
584 	idxd_device_clear_state(idxd);
585 	return 0;
586 }
587 
588 void idxd_device_reset(struct idxd_device *idxd)
589 {
590 	idxd_cmd_exec(idxd, IDXD_CMD_RESET_DEVICE, 0, NULL);
591 	idxd_device_clear_state(idxd);
592 	spin_lock(&idxd->dev_lock);
593 	idxd_unmask_error_interrupts(idxd);
594 	spin_unlock(&idxd->dev_lock);
595 }
596 
597 void idxd_device_drain_pasid(struct idxd_device *idxd, int pasid)
598 {
599 	struct device *dev = &idxd->pdev->dev;
600 	u32 operand;
601 
602 	operand = pasid;
603 	dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_DRAIN_PASID, operand);
604 	idxd_cmd_exec(idxd, IDXD_CMD_DRAIN_PASID, operand, NULL);
605 	dev_dbg(dev, "pasid %d drained\n", pasid);
606 }
607 
608 int idxd_device_request_int_handle(struct idxd_device *idxd, int idx, int *handle,
609 				   enum idxd_interrupt_type irq_type)
610 {
611 	struct device *dev = &idxd->pdev->dev;
612 	u32 operand, status;
613 
614 	if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_REQUEST_INT_HANDLE)))
615 		return -EOPNOTSUPP;
616 
617 	dev_dbg(dev, "get int handle, idx %d\n", idx);
618 
619 	operand = idx & GENMASK(15, 0);
620 	if (irq_type == IDXD_IRQ_IMS)
621 		operand |= CMD_INT_HANDLE_IMS;
622 
623 	dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_REQUEST_INT_HANDLE, operand);
624 
625 	idxd_cmd_exec(idxd, IDXD_CMD_REQUEST_INT_HANDLE, operand, &status);
626 
627 	if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
628 		dev_dbg(dev, "request int handle failed: %#x\n", status);
629 		return -ENXIO;
630 	}
631 
632 	*handle = (status >> IDXD_CMDSTS_RES_SHIFT) & GENMASK(15, 0);
633 
634 	dev_dbg(dev, "int handle acquired: %u\n", *handle);
635 	return 0;
636 }
637 
638 int idxd_device_release_int_handle(struct idxd_device *idxd, int handle,
639 				   enum idxd_interrupt_type irq_type)
640 {
641 	struct device *dev = &idxd->pdev->dev;
642 	u32 operand, status;
643 	union idxd_command_reg cmd;
644 
645 	if (!(idxd->hw.cmd_cap & BIT(IDXD_CMD_RELEASE_INT_HANDLE)))
646 		return -EOPNOTSUPP;
647 
648 	dev_dbg(dev, "release int handle, handle %d\n", handle);
649 
650 	memset(&cmd, 0, sizeof(cmd));
651 	operand = handle & GENMASK(15, 0);
652 
653 	if (irq_type == IDXD_IRQ_IMS)
654 		operand |= CMD_INT_HANDLE_IMS;
655 
656 	cmd.cmd = IDXD_CMD_RELEASE_INT_HANDLE;
657 	cmd.operand = operand;
658 
659 	dev_dbg(dev, "cmd: %u operand: %#x\n", IDXD_CMD_RELEASE_INT_HANDLE, operand);
660 
661 	spin_lock(&idxd->cmd_lock);
662 	iowrite32(cmd.bits, idxd->reg_base + IDXD_CMD_OFFSET);
663 
664 	while (ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET) & IDXD_CMDSTS_ACTIVE)
665 		cpu_relax();
666 	status = ioread32(idxd->reg_base + IDXD_CMDSTS_OFFSET);
667 	spin_unlock(&idxd->cmd_lock);
668 
669 	if ((status & IDXD_CMDSTS_ERR_MASK) != IDXD_CMDSTS_SUCCESS) {
670 		dev_dbg(dev, "release int handle failed: %#x\n", status);
671 		return -ENXIO;
672 	}
673 
674 	dev_dbg(dev, "int handle released.\n");
675 	return 0;
676 }
677 
678 /* Device configuration bits */
679 static void idxd_engines_clear_state(struct idxd_device *idxd)
680 {
681 	struct idxd_engine *engine;
682 	int i;
683 
684 	lockdep_assert_held(&idxd->dev_lock);
685 	for (i = 0; i < idxd->max_engines; i++) {
686 		engine = idxd->engines[i];
687 		engine->group = NULL;
688 	}
689 }
690 
691 static void idxd_groups_clear_state(struct idxd_device *idxd)
692 {
693 	struct idxd_group *group;
694 	int i;
695 
696 	lockdep_assert_held(&idxd->dev_lock);
697 	for (i = 0; i < idxd->max_groups; i++) {
698 		group = idxd->groups[i];
699 		memset(&group->grpcfg, 0, sizeof(group->grpcfg));
700 		group->num_engines = 0;
701 		group->num_wqs = 0;
702 		group->use_rdbuf_limit = false;
703 		group->rdbufs_allowed = 0;
704 		group->rdbufs_reserved = 0;
705 		if (idxd->hw.version < DEVICE_VERSION_2 && !tc_override) {
706 			group->tc_a = 1;
707 			group->tc_b = 1;
708 		} else {
709 			group->tc_a = -1;
710 			group->tc_b = -1;
711 		}
712 		group->desc_progress_limit = 0;
713 		group->batch_progress_limit = 0;
714 	}
715 }
716 
717 static void idxd_device_wqs_clear_state(struct idxd_device *idxd)
718 {
719 	int i;
720 
721 	for (i = 0; i < idxd->max_wqs; i++) {
722 		struct idxd_wq *wq = idxd->wqs[i];
723 
724 		mutex_lock(&wq->wq_lock);
725 		idxd_wq_disable_cleanup(wq);
726 		idxd_wq_device_reset_cleanup(wq);
727 		mutex_unlock(&wq->wq_lock);
728 	}
729 }
730 
731 void idxd_device_clear_state(struct idxd_device *idxd)
732 {
733 	if (!test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
734 		return;
735 
736 	idxd_device_wqs_clear_state(idxd);
737 	spin_lock(&idxd->dev_lock);
738 	idxd_groups_clear_state(idxd);
739 	idxd_engines_clear_state(idxd);
740 	idxd->state = IDXD_DEV_DISABLED;
741 	spin_unlock(&idxd->dev_lock);
742 }
743 
744 static void idxd_group_config_write(struct idxd_group *group)
745 {
746 	struct idxd_device *idxd = group->idxd;
747 	struct device *dev = &idxd->pdev->dev;
748 	int i;
749 	u32 grpcfg_offset;
750 
751 	dev_dbg(dev, "Writing group %d cfg registers\n", group->id);
752 
753 	/* setup GRPWQCFG */
754 	for (i = 0; i < GRPWQCFG_STRIDES; i++) {
755 		grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
756 		iowrite64(group->grpcfg.wqs[i], idxd->reg_base + grpcfg_offset);
757 		dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
758 			group->id, i, grpcfg_offset,
759 			ioread64(idxd->reg_base + grpcfg_offset));
760 	}
761 
762 	/* setup GRPENGCFG */
763 	grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
764 	iowrite64(group->grpcfg.engines, idxd->reg_base + grpcfg_offset);
765 	dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
766 		grpcfg_offset, ioread64(idxd->reg_base + grpcfg_offset));
767 
768 	/* setup GRPFLAGS */
769 	grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
770 	iowrite64(group->grpcfg.flags.bits, idxd->reg_base + grpcfg_offset);
771 	dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#llx\n",
772 		group->id, grpcfg_offset,
773 		ioread64(idxd->reg_base + grpcfg_offset));
774 }
775 
776 static int idxd_groups_config_write(struct idxd_device *idxd)
777 
778 {
779 	union gencfg_reg reg;
780 	int i;
781 	struct device *dev = &idxd->pdev->dev;
782 
783 	/* Setup bandwidth rdbuf limit */
784 	if (idxd->hw.gen_cap.config_en && idxd->rdbuf_limit) {
785 		reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
786 		reg.rdbuf_limit = idxd->rdbuf_limit;
787 		iowrite32(reg.bits, idxd->reg_base + IDXD_GENCFG_OFFSET);
788 	}
789 
790 	dev_dbg(dev, "GENCFG(%#x): %#x\n", IDXD_GENCFG_OFFSET,
791 		ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET));
792 
793 	for (i = 0; i < idxd->max_groups; i++) {
794 		struct idxd_group *group = idxd->groups[i];
795 
796 		idxd_group_config_write(group);
797 	}
798 
799 	return 0;
800 }
801 
802 static bool idxd_device_pasid_priv_enabled(struct idxd_device *idxd)
803 {
804 	struct pci_dev *pdev = idxd->pdev;
805 
806 	if (pdev->pasid_enabled && (pdev->pasid_features & PCI_PASID_CAP_PRIV))
807 		return true;
808 	return false;
809 }
810 
811 static int idxd_wq_config_write(struct idxd_wq *wq)
812 {
813 	struct idxd_device *idxd = wq->idxd;
814 	struct device *dev = &idxd->pdev->dev;
815 	u32 wq_offset;
816 	int i, n;
817 
818 	if (!wq->group)
819 		return 0;
820 
821 	/*
822 	 * Instead of memset the entire shadow copy of WQCFG, copy from the hardware after
823 	 * wq reset. This will copy back the sticky values that are present on some devices.
824 	 */
825 	for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
826 		wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
827 		wq->wqcfg->bits[i] |= ioread32(idxd->reg_base + wq_offset);
828 	}
829 
830 	if (wq->size == 0 && wq->type != IDXD_WQT_NONE)
831 		wq->size = WQ_DEFAULT_QUEUE_DEPTH;
832 
833 	/* byte 0-3 */
834 	wq->wqcfg->wq_size = wq->size;
835 
836 	/* bytes 4-7 */
837 	wq->wqcfg->wq_thresh = wq->threshold;
838 
839 	/* byte 8-11 */
840 	if (wq_dedicated(wq))
841 		wq->wqcfg->mode = 1;
842 
843 	/*
844 	 * The WQ priv bit is set depending on the WQ type. priv = 1 if the
845 	 * WQ type is kernel to indicate privileged access. This setting only
846 	 * matters for dedicated WQ. According to the DSA spec:
847 	 * If the WQ is in dedicated mode, WQ PASID Enable is 1, and the
848 	 * Privileged Mode Enable field of the PCI Express PASID capability
849 	 * is 0, this field must be 0.
850 	 *
851 	 * In the case of a dedicated kernel WQ that is not able to support
852 	 * the PASID cap, then the configuration will be rejected.
853 	 */
854 	if (wq_dedicated(wq) && wq->wqcfg->pasid_en &&
855 	    !idxd_device_pasid_priv_enabled(idxd) &&
856 	    wq->type == IDXD_WQT_KERNEL) {
857 		idxd->cmd_status = IDXD_SCMD_WQ_NO_PRIV;
858 		return -EOPNOTSUPP;
859 	}
860 
861 	wq->wqcfg->priority = wq->priority;
862 
863 	if (idxd->hw.gen_cap.block_on_fault &&
864 	    test_bit(WQ_FLAG_BLOCK_ON_FAULT, &wq->flags))
865 		wq->wqcfg->bof = 1;
866 
867 	if (idxd->hw.wq_cap.wq_ats_support)
868 		wq->wqcfg->wq_ats_disable = test_bit(WQ_FLAG_ATS_DISABLE, &wq->flags);
869 
870 	/* bytes 12-15 */
871 	wq->wqcfg->max_xfer_shift = ilog2(wq->max_xfer_bytes);
872 	wq->wqcfg->max_batch_shift = ilog2(wq->max_batch_size);
873 
874 	/* bytes 32-63 */
875 	if (idxd->hw.wq_cap.op_config && wq->opcap_bmap) {
876 		memset(wq->wqcfg->op_config, 0, IDXD_MAX_OPCAP_BITS / 8);
877 		for_each_set_bit(n, wq->opcap_bmap, IDXD_MAX_OPCAP_BITS) {
878 			int pos = n % BITS_PER_LONG_LONG;
879 			int idx = n / BITS_PER_LONG_LONG;
880 
881 			wq->wqcfg->op_config[idx] |= BIT(pos);
882 		}
883 	}
884 
885 	dev_dbg(dev, "WQ %d CFGs\n", wq->id);
886 	for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
887 		wq_offset = WQCFG_OFFSET(idxd, wq->id, i);
888 		iowrite32(wq->wqcfg->bits[i], idxd->reg_base + wq_offset);
889 		dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n",
890 			wq->id, i, wq_offset,
891 			ioread32(idxd->reg_base + wq_offset));
892 	}
893 
894 	return 0;
895 }
896 
897 static int idxd_wqs_config_write(struct idxd_device *idxd)
898 {
899 	int i, rc;
900 
901 	for (i = 0; i < idxd->max_wqs; i++) {
902 		struct idxd_wq *wq = idxd->wqs[i];
903 
904 		rc = idxd_wq_config_write(wq);
905 		if (rc < 0)
906 			return rc;
907 	}
908 
909 	return 0;
910 }
911 
912 static void idxd_group_flags_setup(struct idxd_device *idxd)
913 {
914 	int i;
915 
916 	/* TC-A 0 and TC-B 1 should be defaults */
917 	for (i = 0; i < idxd->max_groups; i++) {
918 		struct idxd_group *group = idxd->groups[i];
919 
920 		if (group->tc_a == -1)
921 			group->tc_a = group->grpcfg.flags.tc_a = 0;
922 		else
923 			group->grpcfg.flags.tc_a = group->tc_a;
924 		if (group->tc_b == -1)
925 			group->tc_b = group->grpcfg.flags.tc_b = 1;
926 		else
927 			group->grpcfg.flags.tc_b = group->tc_b;
928 		group->grpcfg.flags.use_rdbuf_limit = group->use_rdbuf_limit;
929 		group->grpcfg.flags.rdbufs_reserved = group->rdbufs_reserved;
930 		if (group->rdbufs_allowed)
931 			group->grpcfg.flags.rdbufs_allowed = group->rdbufs_allowed;
932 		else
933 			group->grpcfg.flags.rdbufs_allowed = idxd->max_rdbufs;
934 
935 		group->grpcfg.flags.desc_progress_limit = group->desc_progress_limit;
936 		group->grpcfg.flags.batch_progress_limit = group->batch_progress_limit;
937 	}
938 }
939 
940 static int idxd_engines_setup(struct idxd_device *idxd)
941 {
942 	int i, engines = 0;
943 	struct idxd_engine *eng;
944 	struct idxd_group *group;
945 
946 	for (i = 0; i < idxd->max_groups; i++) {
947 		group = idxd->groups[i];
948 		group->grpcfg.engines = 0;
949 	}
950 
951 	for (i = 0; i < idxd->max_engines; i++) {
952 		eng = idxd->engines[i];
953 		group = eng->group;
954 
955 		if (!group)
956 			continue;
957 
958 		group->grpcfg.engines |= BIT(eng->id);
959 		engines++;
960 	}
961 
962 	if (!engines)
963 		return -EINVAL;
964 
965 	return 0;
966 }
967 
968 static int idxd_wqs_setup(struct idxd_device *idxd)
969 {
970 	struct idxd_wq *wq;
971 	struct idxd_group *group;
972 	int i, j, configured = 0;
973 	struct device *dev = &idxd->pdev->dev;
974 
975 	for (i = 0; i < idxd->max_groups; i++) {
976 		group = idxd->groups[i];
977 		for (j = 0; j < 4; j++)
978 			group->grpcfg.wqs[j] = 0;
979 	}
980 
981 	for (i = 0; i < idxd->max_wqs; i++) {
982 		wq = idxd->wqs[i];
983 		group = wq->group;
984 
985 		if (!wq->group)
986 			continue;
987 
988 		if (wq_shared(wq) && !wq_shared_supported(wq)) {
989 			idxd->cmd_status = IDXD_SCMD_WQ_NO_SWQ_SUPPORT;
990 			dev_warn(dev, "No shared wq support but configured.\n");
991 			return -EINVAL;
992 		}
993 
994 		group->grpcfg.wqs[wq->id / 64] |= BIT(wq->id % 64);
995 		configured++;
996 	}
997 
998 	if (configured == 0) {
999 		idxd->cmd_status = IDXD_SCMD_WQ_NONE_CONFIGURED;
1000 		return -EINVAL;
1001 	}
1002 
1003 	return 0;
1004 }
1005 
1006 int idxd_device_config(struct idxd_device *idxd)
1007 {
1008 	int rc;
1009 
1010 	lockdep_assert_held(&idxd->dev_lock);
1011 	rc = idxd_wqs_setup(idxd);
1012 	if (rc < 0)
1013 		return rc;
1014 
1015 	rc = idxd_engines_setup(idxd);
1016 	if (rc < 0)
1017 		return rc;
1018 
1019 	idxd_group_flags_setup(idxd);
1020 
1021 	rc = idxd_wqs_config_write(idxd);
1022 	if (rc < 0)
1023 		return rc;
1024 
1025 	rc = idxd_groups_config_write(idxd);
1026 	if (rc < 0)
1027 		return rc;
1028 
1029 	return 0;
1030 }
1031 
1032 static int idxd_wq_load_config(struct idxd_wq *wq)
1033 {
1034 	struct idxd_device *idxd = wq->idxd;
1035 	struct device *dev = &idxd->pdev->dev;
1036 	int wqcfg_offset;
1037 	int i;
1038 
1039 	wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, 0);
1040 	memcpy_fromio(wq->wqcfg, idxd->reg_base + wqcfg_offset, idxd->wqcfg_size);
1041 
1042 	wq->size = wq->wqcfg->wq_size;
1043 	wq->threshold = wq->wqcfg->wq_thresh;
1044 
1045 	/* The driver does not support shared WQ mode in read-only config yet */
1046 	if (wq->wqcfg->mode == 0 || wq->wqcfg->pasid_en)
1047 		return -EOPNOTSUPP;
1048 
1049 	set_bit(WQ_FLAG_DEDICATED, &wq->flags);
1050 
1051 	wq->priority = wq->wqcfg->priority;
1052 
1053 	wq->max_xfer_bytes = 1ULL << wq->wqcfg->max_xfer_shift;
1054 	wq->max_batch_size = 1ULL << wq->wqcfg->max_batch_shift;
1055 
1056 	for (i = 0; i < WQCFG_STRIDES(idxd); i++) {
1057 		wqcfg_offset = WQCFG_OFFSET(idxd, wq->id, i);
1058 		dev_dbg(dev, "WQ[%d][%d][%#x]: %#x\n", wq->id, i, wqcfg_offset, wq->wqcfg->bits[i]);
1059 	}
1060 
1061 	return 0;
1062 }
1063 
1064 static void idxd_group_load_config(struct idxd_group *group)
1065 {
1066 	struct idxd_device *idxd = group->idxd;
1067 	struct device *dev = &idxd->pdev->dev;
1068 	int i, j, grpcfg_offset;
1069 
1070 	/*
1071 	 * Load WQS bit fields
1072 	 * Iterate through all 256 bits 64 bits at a time
1073 	 */
1074 	for (i = 0; i < GRPWQCFG_STRIDES; i++) {
1075 		struct idxd_wq *wq;
1076 
1077 		grpcfg_offset = GRPWQCFG_OFFSET(idxd, group->id, i);
1078 		group->grpcfg.wqs[i] = ioread64(idxd->reg_base + grpcfg_offset);
1079 		dev_dbg(dev, "GRPCFG wq[%d:%d: %#x]: %#llx\n",
1080 			group->id, i, grpcfg_offset, group->grpcfg.wqs[i]);
1081 
1082 		if (i * 64 >= idxd->max_wqs)
1083 			break;
1084 
1085 		/* Iterate through all 64 bits and check for wq set */
1086 		for (j = 0; j < 64; j++) {
1087 			int id = i * 64 + j;
1088 
1089 			/* No need to check beyond max wqs */
1090 			if (id >= idxd->max_wqs)
1091 				break;
1092 
1093 			/* Set group assignment for wq if wq bit is set */
1094 			if (group->grpcfg.wqs[i] & BIT(j)) {
1095 				wq = idxd->wqs[id];
1096 				wq->group = group;
1097 			}
1098 		}
1099 	}
1100 
1101 	grpcfg_offset = GRPENGCFG_OFFSET(idxd, group->id);
1102 	group->grpcfg.engines = ioread64(idxd->reg_base + grpcfg_offset);
1103 	dev_dbg(dev, "GRPCFG engs[%d: %#x]: %#llx\n", group->id,
1104 		grpcfg_offset, group->grpcfg.engines);
1105 
1106 	/* Iterate through all 64 bits to check engines set */
1107 	for (i = 0; i < 64; i++) {
1108 		if (i >= idxd->max_engines)
1109 			break;
1110 
1111 		if (group->grpcfg.engines & BIT(i)) {
1112 			struct idxd_engine *engine = idxd->engines[i];
1113 
1114 			engine->group = group;
1115 		}
1116 	}
1117 
1118 	grpcfg_offset = GRPFLGCFG_OFFSET(idxd, group->id);
1119 	group->grpcfg.flags.bits = ioread64(idxd->reg_base + grpcfg_offset);
1120 	dev_dbg(dev, "GRPFLAGS flags[%d: %#x]: %#llx\n",
1121 		group->id, grpcfg_offset, group->grpcfg.flags.bits);
1122 }
1123 
1124 int idxd_device_load_config(struct idxd_device *idxd)
1125 {
1126 	union gencfg_reg reg;
1127 	int i, rc;
1128 
1129 	reg.bits = ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET);
1130 	idxd->rdbuf_limit = reg.rdbuf_limit;
1131 
1132 	for (i = 0; i < idxd->max_groups; i++) {
1133 		struct idxd_group *group = idxd->groups[i];
1134 
1135 		idxd_group_load_config(group);
1136 	}
1137 
1138 	for (i = 0; i < idxd->max_wqs; i++) {
1139 		struct idxd_wq *wq = idxd->wqs[i];
1140 
1141 		rc = idxd_wq_load_config(wq);
1142 		if (rc < 0)
1143 			return rc;
1144 	}
1145 
1146 	return 0;
1147 }
1148 
1149 static void idxd_flush_pending_descs(struct idxd_irq_entry *ie)
1150 {
1151 	struct idxd_desc *desc, *itr;
1152 	struct llist_node *head;
1153 	LIST_HEAD(flist);
1154 	enum idxd_complete_type ctype;
1155 
1156 	spin_lock(&ie->list_lock);
1157 	head = llist_del_all(&ie->pending_llist);
1158 	if (head) {
1159 		llist_for_each_entry_safe(desc, itr, head, llnode)
1160 			list_add_tail(&desc->list, &ie->work_list);
1161 	}
1162 
1163 	list_for_each_entry_safe(desc, itr, &ie->work_list, list)
1164 		list_move_tail(&desc->list, &flist);
1165 	spin_unlock(&ie->list_lock);
1166 
1167 	list_for_each_entry_safe(desc, itr, &flist, list) {
1168 		list_del(&desc->list);
1169 		ctype = desc->completion->status ? IDXD_COMPLETE_NORMAL : IDXD_COMPLETE_ABORT;
1170 		idxd_dma_complete_txd(desc, ctype, true);
1171 	}
1172 }
1173 
1174 static void idxd_device_set_perm_entry(struct idxd_device *idxd,
1175 				       struct idxd_irq_entry *ie)
1176 {
1177 	union msix_perm mperm;
1178 
1179 	if (ie->pasid == INVALID_IOASID)
1180 		return;
1181 
1182 	mperm.bits = 0;
1183 	mperm.pasid = ie->pasid;
1184 	mperm.pasid_en = 1;
1185 	iowrite32(mperm.bits, idxd->reg_base + idxd->msix_perm_offset + ie->id * 8);
1186 }
1187 
1188 static void idxd_device_clear_perm_entry(struct idxd_device *idxd,
1189 					 struct idxd_irq_entry *ie)
1190 {
1191 	iowrite32(0, idxd->reg_base + idxd->msix_perm_offset + ie->id * 8);
1192 }
1193 
1194 void idxd_wq_free_irq(struct idxd_wq *wq)
1195 {
1196 	struct idxd_device *idxd = wq->idxd;
1197 	struct idxd_irq_entry *ie = &wq->ie;
1198 
1199 	if (wq->type != IDXD_WQT_KERNEL)
1200 		return;
1201 
1202 	free_irq(ie->vector, ie);
1203 	idxd_flush_pending_descs(ie);
1204 	if (idxd->request_int_handles)
1205 		idxd_device_release_int_handle(idxd, ie->int_handle, IDXD_IRQ_MSIX);
1206 	idxd_device_clear_perm_entry(idxd, ie);
1207 	ie->vector = -1;
1208 	ie->int_handle = INVALID_INT_HANDLE;
1209 	ie->pasid = INVALID_IOASID;
1210 }
1211 
1212 int idxd_wq_request_irq(struct idxd_wq *wq)
1213 {
1214 	struct idxd_device *idxd = wq->idxd;
1215 	struct pci_dev *pdev = idxd->pdev;
1216 	struct device *dev = &pdev->dev;
1217 	struct idxd_irq_entry *ie;
1218 	int rc;
1219 
1220 	if (wq->type != IDXD_WQT_KERNEL)
1221 		return 0;
1222 
1223 	ie = &wq->ie;
1224 	ie->vector = pci_irq_vector(pdev, ie->id);
1225 	ie->pasid = device_pasid_enabled(idxd) ? idxd->pasid : INVALID_IOASID;
1226 	idxd_device_set_perm_entry(idxd, ie);
1227 
1228 	rc = request_threaded_irq(ie->vector, NULL, idxd_wq_thread, 0, "idxd-portal", ie);
1229 	if (rc < 0) {
1230 		dev_err(dev, "Failed to request irq %d.\n", ie->vector);
1231 		goto err_irq;
1232 	}
1233 
1234 	if (idxd->request_int_handles) {
1235 		rc = idxd_device_request_int_handle(idxd, ie->id, &ie->int_handle,
1236 						    IDXD_IRQ_MSIX);
1237 		if (rc < 0)
1238 			goto err_int_handle;
1239 	} else {
1240 		ie->int_handle = ie->id;
1241 	}
1242 
1243 	return 0;
1244 
1245 err_int_handle:
1246 	ie->int_handle = INVALID_INT_HANDLE;
1247 	free_irq(ie->vector, ie);
1248 err_irq:
1249 	idxd_device_clear_perm_entry(idxd, ie);
1250 	ie->pasid = INVALID_IOASID;
1251 	return rc;
1252 }
1253 
1254 int drv_enable_wq(struct idxd_wq *wq)
1255 {
1256 	struct idxd_device *idxd = wq->idxd;
1257 	struct device *dev = &idxd->pdev->dev;
1258 	int rc = -ENXIO;
1259 
1260 	lockdep_assert_held(&wq->wq_lock);
1261 
1262 	if (idxd->state != IDXD_DEV_ENABLED) {
1263 		idxd->cmd_status = IDXD_SCMD_DEV_NOT_ENABLED;
1264 		goto err;
1265 	}
1266 
1267 	if (wq->state != IDXD_WQ_DISABLED) {
1268 		dev_dbg(dev, "wq %d already enabled.\n", wq->id);
1269 		idxd->cmd_status = IDXD_SCMD_WQ_ENABLED;
1270 		rc = -EBUSY;
1271 		goto err;
1272 	}
1273 
1274 	if (!wq->group) {
1275 		dev_dbg(dev, "wq %d not attached to group.\n", wq->id);
1276 		idxd->cmd_status = IDXD_SCMD_WQ_NO_GRP;
1277 		goto err;
1278 	}
1279 
1280 	if (strlen(wq->name) == 0) {
1281 		idxd->cmd_status = IDXD_SCMD_WQ_NO_NAME;
1282 		dev_dbg(dev, "wq %d name not set.\n", wq->id);
1283 		goto err;
1284 	}
1285 
1286 	/* Shared WQ checks */
1287 	if (wq_shared(wq)) {
1288 		if (!wq_shared_supported(wq)) {
1289 			idxd->cmd_status = IDXD_SCMD_WQ_NO_SVM;
1290 			dev_dbg(dev, "PASID not enabled and shared wq.\n");
1291 			goto err;
1292 		}
1293 		/*
1294 		 * Shared wq with the threshold set to 0 means the user
1295 		 * did not set the threshold or transitioned from a
1296 		 * dedicated wq but did not set threshold. A value
1297 		 * of 0 would effectively disable the shared wq. The
1298 		 * driver does not allow a value of 0 to be set for
1299 		 * threshold via sysfs.
1300 		 */
1301 		if (wq->threshold == 0) {
1302 			idxd->cmd_status = IDXD_SCMD_WQ_NO_THRESH;
1303 			dev_dbg(dev, "Shared wq and threshold 0.\n");
1304 			goto err;
1305 		}
1306 	}
1307 
1308 	/*
1309 	 * In the event that the WQ is configurable for pasid and priv bits.
1310 	 * For kernel wq, the driver should setup the pasid, pasid_en, and priv bit.
1311 	 * However, for non-kernel wq, the driver should only set the pasid_en bit for
1312 	 * shared wq. A dedicated wq that is not 'kernel' type will configure pasid and
1313 	 * pasid_en later on so there is no need to setup.
1314 	 */
1315 	if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags)) {
1316 		int priv = 0;
1317 
1318 		if (wq_pasid_enabled(wq)) {
1319 			if (is_idxd_wq_kernel(wq) || wq_shared(wq)) {
1320 				u32 pasid = wq_dedicated(wq) ? idxd->pasid : 0;
1321 
1322 				__idxd_wq_set_pasid_locked(wq, pasid);
1323 			}
1324 		}
1325 
1326 		if (is_idxd_wq_kernel(wq))
1327 			priv = 1;
1328 		__idxd_wq_set_priv_locked(wq, priv);
1329 	}
1330 
1331 	rc = 0;
1332 	spin_lock(&idxd->dev_lock);
1333 	if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1334 		rc = idxd_device_config(idxd);
1335 	spin_unlock(&idxd->dev_lock);
1336 	if (rc < 0) {
1337 		dev_dbg(dev, "Writing wq %d config failed: %d\n", wq->id, rc);
1338 		goto err;
1339 	}
1340 
1341 	rc = idxd_wq_enable(wq);
1342 	if (rc < 0) {
1343 		dev_dbg(dev, "wq %d enabling failed: %d\n", wq->id, rc);
1344 		goto err;
1345 	}
1346 
1347 	rc = idxd_wq_map_portal(wq);
1348 	if (rc < 0) {
1349 		idxd->cmd_status = IDXD_SCMD_WQ_PORTAL_ERR;
1350 		dev_dbg(dev, "wq %d portal mapping failed: %d\n", wq->id, rc);
1351 		goto err_map_portal;
1352 	}
1353 
1354 	wq->client_count = 0;
1355 
1356 	rc = idxd_wq_request_irq(wq);
1357 	if (rc < 0) {
1358 		idxd->cmd_status = IDXD_SCMD_WQ_IRQ_ERR;
1359 		dev_dbg(dev, "WQ %d irq setup failed: %d\n", wq->id, rc);
1360 		goto err_irq;
1361 	}
1362 
1363 	rc = idxd_wq_alloc_resources(wq);
1364 	if (rc < 0) {
1365 		idxd->cmd_status = IDXD_SCMD_WQ_RES_ALLOC_ERR;
1366 		dev_dbg(dev, "WQ resource alloc failed\n");
1367 		goto err_res_alloc;
1368 	}
1369 
1370 	rc = idxd_wq_init_percpu_ref(wq);
1371 	if (rc < 0) {
1372 		idxd->cmd_status = IDXD_SCMD_PERCPU_ERR;
1373 		dev_dbg(dev, "percpu_ref setup failed\n");
1374 		goto err_ref;
1375 	}
1376 
1377 	return 0;
1378 
1379 err_ref:
1380 	idxd_wq_free_resources(wq);
1381 err_res_alloc:
1382 	idxd_wq_free_irq(wq);
1383 err_irq:
1384 	idxd_wq_unmap_portal(wq);
1385 err_map_portal:
1386 	rc = idxd_wq_disable(wq, false);
1387 	if (rc < 0)
1388 		dev_dbg(dev, "wq %s disable failed\n", dev_name(wq_confdev(wq)));
1389 err:
1390 	return rc;
1391 }
1392 
1393 void drv_disable_wq(struct idxd_wq *wq)
1394 {
1395 	struct idxd_device *idxd = wq->idxd;
1396 	struct device *dev = &idxd->pdev->dev;
1397 
1398 	lockdep_assert_held(&wq->wq_lock);
1399 
1400 	if (idxd_wq_refcount(wq))
1401 		dev_warn(dev, "Clients has claim on wq %d: %d\n",
1402 			 wq->id, idxd_wq_refcount(wq));
1403 
1404 	idxd_wq_free_resources(wq);
1405 	idxd_wq_unmap_portal(wq);
1406 	idxd_wq_drain(wq);
1407 	idxd_wq_free_irq(wq);
1408 	idxd_wq_reset(wq);
1409 	percpu_ref_exit(&wq->wq_active);
1410 	wq->type = IDXD_WQT_NONE;
1411 	wq->client_count = 0;
1412 }
1413 
1414 int idxd_device_drv_probe(struct idxd_dev *idxd_dev)
1415 {
1416 	struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
1417 	int rc = 0;
1418 
1419 	/*
1420 	 * Device should be in disabled state for the idxd_drv to load. If it's in
1421 	 * enabled state, then the device was altered outside of driver's control.
1422 	 * If the state is in halted state, then we don't want to proceed.
1423 	 */
1424 	if (idxd->state != IDXD_DEV_DISABLED) {
1425 		idxd->cmd_status = IDXD_SCMD_DEV_ENABLED;
1426 		return -ENXIO;
1427 	}
1428 
1429 	/* Device configuration */
1430 	spin_lock(&idxd->dev_lock);
1431 	if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1432 		rc = idxd_device_config(idxd);
1433 	spin_unlock(&idxd->dev_lock);
1434 	if (rc < 0)
1435 		return -ENXIO;
1436 
1437 	/* Start device */
1438 	rc = idxd_device_enable(idxd);
1439 	if (rc < 0)
1440 		return rc;
1441 
1442 	/* Setup DMA device without channels */
1443 	rc = idxd_register_dma_device(idxd);
1444 	if (rc < 0) {
1445 		idxd_device_disable(idxd);
1446 		idxd->cmd_status = IDXD_SCMD_DEV_DMA_ERR;
1447 		return rc;
1448 	}
1449 
1450 	idxd->cmd_status = 0;
1451 	return 0;
1452 }
1453 
1454 void idxd_device_drv_remove(struct idxd_dev *idxd_dev)
1455 {
1456 	struct device *dev = &idxd_dev->conf_dev;
1457 	struct idxd_device *idxd = idxd_dev_to_idxd(idxd_dev);
1458 	int i;
1459 
1460 	for (i = 0; i < idxd->max_wqs; i++) {
1461 		struct idxd_wq *wq = idxd->wqs[i];
1462 		struct device *wq_dev = wq_confdev(wq);
1463 
1464 		if (wq->state == IDXD_WQ_DISABLED)
1465 			continue;
1466 		dev_warn(dev, "Active wq %d on disable %s.\n", i, dev_name(wq_dev));
1467 		device_release_driver(wq_dev);
1468 	}
1469 
1470 	idxd_unregister_dma_device(idxd);
1471 	idxd_device_disable(idxd);
1472 	if (test_bit(IDXD_FLAG_CONFIGURABLE, &idxd->flags))
1473 		idxd_device_reset(idxd);
1474 }
1475 
1476 static enum idxd_dev_type dev_types[] = {
1477 	IDXD_DEV_DSA,
1478 	IDXD_DEV_IAX,
1479 	IDXD_DEV_NONE,
1480 };
1481 
1482 struct idxd_device_driver idxd_drv = {
1483 	.type = dev_types,
1484 	.probe = idxd_device_drv_probe,
1485 	.remove = idxd_device_drv_remove,
1486 	.name = "idxd",
1487 };
1488 EXPORT_SYMBOL_GPL(idxd_drv);
1489