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