xref: /linux/drivers/dma/idxd/irq.c (revision 58fe107660483138a7a77acd673b911016e4ad31)
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/delay.h>
10 #include <uapi/linux/idxd.h>
11 #include "../dmaengine.h"
12 #include "idxd.h"
13 #include "registers.h"
14 
15 enum irq_work_type {
16 	IRQ_WORK_NORMAL = 0,
17 	IRQ_WORK_PROCESS_FAULT,
18 };
19 
20 struct idxd_fault {
21 	struct work_struct work;
22 	u64 addr;
23 	struct idxd_device *idxd;
24 };
25 
26 struct idxd_resubmit {
27 	struct work_struct work;
28 	struct idxd_desc *desc;
29 };
30 
31 struct idxd_int_handle_revoke {
32 	struct work_struct work;
33 	struct idxd_device *idxd;
34 };
35 
36 static void idxd_device_reinit(struct work_struct *work)
37 {
38 	struct idxd_device *idxd = container_of(work, struct idxd_device, work);
39 	struct device *dev = &idxd->pdev->dev;
40 	int rc, i;
41 
42 	idxd_device_reset(idxd);
43 	rc = idxd_device_config(idxd);
44 	if (rc < 0)
45 		goto out;
46 
47 	rc = idxd_device_enable(idxd);
48 	if (rc < 0)
49 		goto out;
50 
51 	for (i = 0; i < idxd->max_wqs; i++) {
52 		struct idxd_wq *wq = idxd->wqs[i];
53 
54 		if (wq->state == IDXD_WQ_ENABLED) {
55 			rc = idxd_wq_enable(wq);
56 			if (rc < 0) {
57 				dev_warn(dev, "Unable to re-enable wq %s\n",
58 					 dev_name(wq_confdev(wq)));
59 			}
60 		}
61 	}
62 
63 	return;
64 
65  out:
66 	idxd_device_clear_state(idxd);
67 }
68 
69 /*
70  * The function sends a drain descriptor for the interrupt handle. The drain ensures
71  * all descriptors with this interrupt handle is flushed and the interrupt
72  * will allow the cleanup of the outstanding descriptors.
73  */
74 static void idxd_int_handle_revoke_drain(struct idxd_irq_entry *ie)
75 {
76 	struct idxd_wq *wq = ie->wq;
77 	struct idxd_device *idxd = ie->idxd;
78 	struct device *dev = &idxd->pdev->dev;
79 	struct dsa_hw_desc desc = {};
80 	void __iomem *portal;
81 	int rc;
82 
83 	/* Issue a simple drain operation with interrupt but no completion record */
84 	desc.flags = IDXD_OP_FLAG_RCI;
85 	desc.opcode = DSA_OPCODE_DRAIN;
86 	desc.priv = 1;
87 
88 	if (ie->pasid != INVALID_IOASID)
89 		desc.pasid = ie->pasid;
90 	desc.int_handle = ie->int_handle;
91 	portal = idxd_wq_portal_addr(wq);
92 
93 	/*
94 	 * The wmb() makes sure that the descriptor is all there before we
95 	 * issue.
96 	 */
97 	wmb();
98 	if (wq_dedicated(wq)) {
99 		iosubmit_cmds512(portal, &desc, 1);
100 	} else {
101 		rc = enqcmds(portal, &desc);
102 		/* This should not fail unless hardware failed. */
103 		if (rc < 0)
104 			dev_warn(dev, "Failed to submit drain desc on wq %d\n", wq->id);
105 	}
106 }
107 
108 static void idxd_abort_invalid_int_handle_descs(struct idxd_irq_entry *ie)
109 {
110 	LIST_HEAD(flist);
111 	struct idxd_desc *d, *t;
112 	struct llist_node *head;
113 
114 	spin_lock(&ie->list_lock);
115 	head = llist_del_all(&ie->pending_llist);
116 	if (head) {
117 		llist_for_each_entry_safe(d, t, head, llnode)
118 			list_add_tail(&d->list, &ie->work_list);
119 	}
120 
121 	list_for_each_entry_safe(d, t, &ie->work_list, list) {
122 		if (d->completion->status == DSA_COMP_INT_HANDLE_INVAL)
123 			list_move_tail(&d->list, &flist);
124 	}
125 	spin_unlock(&ie->list_lock);
126 
127 	list_for_each_entry_safe(d, t, &flist, list) {
128 		list_del(&d->list);
129 		idxd_dma_complete_txd(d, IDXD_COMPLETE_ABORT, true);
130 	}
131 }
132 
133 static void idxd_int_handle_revoke(struct work_struct *work)
134 {
135 	struct idxd_int_handle_revoke *revoke =
136 		container_of(work, struct idxd_int_handle_revoke, work);
137 	struct idxd_device *idxd = revoke->idxd;
138 	struct pci_dev *pdev = idxd->pdev;
139 	struct device *dev = &pdev->dev;
140 	int i, new_handle, rc;
141 
142 	if (!idxd->request_int_handles) {
143 		kfree(revoke);
144 		dev_warn(dev, "Unexpected int handle refresh interrupt.\n");
145 		return;
146 	}
147 
148 	/*
149 	 * The loop attempts to acquire new interrupt handle for all interrupt
150 	 * vectors that supports a handle. If a new interrupt handle is acquired and the
151 	 * wq is kernel type, the driver will kill the percpu_ref to pause all
152 	 * ongoing descriptor submissions. The interrupt handle is then changed.
153 	 * After change, the percpu_ref is revived and all the pending submissions
154 	 * are woken to try again. A drain is sent to for the interrupt handle
155 	 * at the end to make sure all invalid int handle descriptors are processed.
156 	 */
157 	for (i = 1; i < idxd->irq_cnt; i++) {
158 		struct idxd_irq_entry *ie = &idxd->irq_entries[i];
159 		struct idxd_wq *wq = ie->wq;
160 
161 		rc = idxd_device_request_int_handle(idxd, i, &new_handle, IDXD_IRQ_MSIX);
162 		if (rc < 0) {
163 			dev_warn(dev, "get int handle %d failed: %d\n", i, rc);
164 			/*
165 			 * Failed to acquire new interrupt handle. Kill the WQ
166 			 * and release all the pending submitters. The submitters will
167 			 * get error return code and handle appropriately.
168 			 */
169 			ie->int_handle = INVALID_INT_HANDLE;
170 			idxd_wq_quiesce(wq);
171 			idxd_abort_invalid_int_handle_descs(ie);
172 			continue;
173 		}
174 
175 		/* No change in interrupt handle, nothing needs to be done */
176 		if (ie->int_handle == new_handle)
177 			continue;
178 
179 		if (wq->state != IDXD_WQ_ENABLED || wq->type != IDXD_WQT_KERNEL) {
180 			/*
181 			 * All the MSIX interrupts are allocated at once during probe.
182 			 * Therefore we need to update all interrupts even if the WQ
183 			 * isn't supporting interrupt operations.
184 			 */
185 			ie->int_handle = new_handle;
186 			continue;
187 		}
188 
189 		mutex_lock(&wq->wq_lock);
190 		reinit_completion(&wq->wq_resurrect);
191 
192 		/* Kill percpu_ref to pause additional descriptor submissions */
193 		percpu_ref_kill(&wq->wq_active);
194 
195 		/* Wait for all submitters quiesce before we change interrupt handle */
196 		wait_for_completion(&wq->wq_dead);
197 
198 		ie->int_handle = new_handle;
199 
200 		/* Revive percpu ref and wake up all the waiting submitters */
201 		percpu_ref_reinit(&wq->wq_active);
202 		complete_all(&wq->wq_resurrect);
203 		mutex_unlock(&wq->wq_lock);
204 
205 		/*
206 		 * The delay here is to wait for all possible MOVDIR64B that
207 		 * are issued before percpu_ref_kill() has happened to have
208 		 * reached the PCIe domain before the drain is issued. The driver
209 		 * needs to ensure that the drain descriptor issued does not pass
210 		 * all the other issued descriptors that contain the invalid
211 		 * interrupt handle in order to ensure that the drain descriptor
212 		 * interrupt will allow the cleanup of all the descriptors with
213 		 * invalid interrupt handle.
214 		 */
215 		if (wq_dedicated(wq))
216 			udelay(100);
217 		idxd_int_handle_revoke_drain(ie);
218 	}
219 	kfree(revoke);
220 }
221 
222 static int process_misc_interrupts(struct idxd_device *idxd, u32 cause)
223 {
224 	struct device *dev = &idxd->pdev->dev;
225 	union gensts_reg gensts;
226 	u32 val = 0;
227 	int i;
228 	bool err = false;
229 
230 	if (cause & IDXD_INTC_HALT_STATE)
231 		goto halt;
232 
233 	if (cause & IDXD_INTC_ERR) {
234 		spin_lock(&idxd->dev_lock);
235 		for (i = 0; i < 4; i++)
236 			idxd->sw_err.bits[i] = ioread64(idxd->reg_base +
237 					IDXD_SWERR_OFFSET + i * sizeof(u64));
238 
239 		iowrite64(idxd->sw_err.bits[0] & IDXD_SWERR_ACK,
240 			  idxd->reg_base + IDXD_SWERR_OFFSET);
241 
242 		if (idxd->sw_err.valid && idxd->sw_err.wq_idx_valid) {
243 			int id = idxd->sw_err.wq_idx;
244 			struct idxd_wq *wq = idxd->wqs[id];
245 
246 			if (wq->type == IDXD_WQT_USER)
247 				wake_up_interruptible(&wq->err_queue);
248 		} else {
249 			int i;
250 
251 			for (i = 0; i < idxd->max_wqs; i++) {
252 				struct idxd_wq *wq = idxd->wqs[i];
253 
254 				if (wq->type == IDXD_WQT_USER)
255 					wake_up_interruptible(&wq->err_queue);
256 			}
257 		}
258 
259 		spin_unlock(&idxd->dev_lock);
260 		val |= IDXD_INTC_ERR;
261 
262 		for (i = 0; i < 4; i++)
263 			dev_warn(dev, "err[%d]: %#16.16llx\n",
264 				 i, idxd->sw_err.bits[i]);
265 		err = true;
266 	}
267 
268 	if (cause & IDXD_INTC_INT_HANDLE_REVOKED) {
269 		struct idxd_int_handle_revoke *revoke;
270 
271 		val |= IDXD_INTC_INT_HANDLE_REVOKED;
272 
273 		revoke = kzalloc(sizeof(*revoke), GFP_ATOMIC);
274 		if (revoke) {
275 			revoke->idxd = idxd;
276 			INIT_WORK(&revoke->work, idxd_int_handle_revoke);
277 			queue_work(idxd->wq, &revoke->work);
278 
279 		} else {
280 			dev_err(dev, "Failed to allocate work for int handle revoke\n");
281 			idxd_wqs_quiesce(idxd);
282 		}
283 	}
284 
285 	if (cause & IDXD_INTC_CMD) {
286 		val |= IDXD_INTC_CMD;
287 		complete(idxd->cmd_done);
288 	}
289 
290 	if (cause & IDXD_INTC_OCCUPY) {
291 		/* Driver does not utilize occupancy interrupt */
292 		val |= IDXD_INTC_OCCUPY;
293 	}
294 
295 	if (cause & IDXD_INTC_PERFMON_OVFL) {
296 		val |= IDXD_INTC_PERFMON_OVFL;
297 		perfmon_counter_overflow(idxd);
298 	}
299 
300 	val ^= cause;
301 	if (val)
302 		dev_warn_once(dev, "Unexpected interrupt cause bits set: %#x\n",
303 			      val);
304 
305 	if (!err)
306 		return 0;
307 
308 halt:
309 	gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
310 	if (gensts.state == IDXD_DEVICE_STATE_HALT) {
311 		idxd->state = IDXD_DEV_HALTED;
312 		if (gensts.reset_type == IDXD_DEVICE_RESET_SOFTWARE) {
313 			/*
314 			 * If we need a software reset, we will throw the work
315 			 * on a system workqueue in order to allow interrupts
316 			 * for the device command completions.
317 			 */
318 			INIT_WORK(&idxd->work, idxd_device_reinit);
319 			queue_work(idxd->wq, &idxd->work);
320 		} else {
321 			spin_lock(&idxd->dev_lock);
322 			idxd->state = IDXD_DEV_HALTED;
323 			idxd_wqs_quiesce(idxd);
324 			idxd_wqs_unmap_portal(idxd);
325 			idxd_device_clear_state(idxd);
326 			dev_err(&idxd->pdev->dev,
327 				"idxd halted, need %s.\n",
328 				gensts.reset_type == IDXD_DEVICE_RESET_FLR ?
329 				"FLR" : "system reset");
330 			spin_unlock(&idxd->dev_lock);
331 			return -ENXIO;
332 		}
333 	}
334 
335 	return 0;
336 }
337 
338 irqreturn_t idxd_misc_thread(int vec, void *data)
339 {
340 	struct idxd_irq_entry *irq_entry = data;
341 	struct idxd_device *idxd = irq_entry->idxd;
342 	int rc;
343 	u32 cause;
344 
345 	cause = ioread32(idxd->reg_base + IDXD_INTCAUSE_OFFSET);
346 	if (cause)
347 		iowrite32(cause, idxd->reg_base + IDXD_INTCAUSE_OFFSET);
348 
349 	while (cause) {
350 		rc = process_misc_interrupts(idxd, cause);
351 		if (rc < 0)
352 			break;
353 		cause = ioread32(idxd->reg_base + IDXD_INTCAUSE_OFFSET);
354 		if (cause)
355 			iowrite32(cause, idxd->reg_base + IDXD_INTCAUSE_OFFSET);
356 	}
357 
358 	return IRQ_HANDLED;
359 }
360 
361 static void idxd_int_handle_resubmit_work(struct work_struct *work)
362 {
363 	struct idxd_resubmit *irw = container_of(work, struct idxd_resubmit, work);
364 	struct idxd_desc *desc = irw->desc;
365 	struct idxd_wq *wq = desc->wq;
366 	int rc;
367 
368 	desc->completion->status = 0;
369 	rc = idxd_submit_desc(wq, desc);
370 	if (rc < 0) {
371 		dev_dbg(&wq->idxd->pdev->dev, "Failed to resubmit desc %d to wq %d.\n",
372 			desc->id, wq->id);
373 		/*
374 		 * If the error is not -EAGAIN, it means the submission failed due to wq
375 		 * has been killed instead of ENQCMDS failure. Here the driver needs to
376 		 * notify the submitter of the failure by reporting abort status.
377 		 *
378 		 * -EAGAIN comes from ENQCMDS failure. idxd_submit_desc() will handle the
379 		 * abort.
380 		 */
381 		if (rc != -EAGAIN) {
382 			desc->completion->status = IDXD_COMP_DESC_ABORT;
383 			idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, false);
384 		}
385 		idxd_free_desc(wq, desc);
386 	}
387 	kfree(irw);
388 }
389 
390 bool idxd_queue_int_handle_resubmit(struct idxd_desc *desc)
391 {
392 	struct idxd_wq *wq = desc->wq;
393 	struct idxd_device *idxd = wq->idxd;
394 	struct idxd_resubmit *irw;
395 
396 	irw = kzalloc(sizeof(*irw), GFP_KERNEL);
397 	if (!irw)
398 		return false;
399 
400 	irw->desc = desc;
401 	INIT_WORK(&irw->work, idxd_int_handle_resubmit_work);
402 	queue_work(idxd->wq, &irw->work);
403 	return true;
404 }
405 
406 static void irq_process_pending_llist(struct idxd_irq_entry *irq_entry)
407 {
408 	struct idxd_desc *desc, *t;
409 	struct llist_node *head;
410 
411 	head = llist_del_all(&irq_entry->pending_llist);
412 	if (!head)
413 		return;
414 
415 	llist_for_each_entry_safe(desc, t, head, llnode) {
416 		u8 status = desc->completion->status & DSA_COMP_STATUS_MASK;
417 
418 		if (status) {
419 			/*
420 			 * Check against the original status as ABORT is software defined
421 			 * and 0xff, which DSA_COMP_STATUS_MASK can mask out.
422 			 */
423 			if (unlikely(desc->completion->status == IDXD_COMP_DESC_ABORT)) {
424 				idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true);
425 				continue;
426 			}
427 
428 			idxd_dma_complete_txd(desc, IDXD_COMPLETE_NORMAL, true);
429 		} else {
430 			spin_lock(&irq_entry->list_lock);
431 			list_add_tail(&desc->list,
432 				      &irq_entry->work_list);
433 			spin_unlock(&irq_entry->list_lock);
434 		}
435 	}
436 }
437 
438 static void irq_process_work_list(struct idxd_irq_entry *irq_entry)
439 {
440 	LIST_HEAD(flist);
441 	struct idxd_desc *desc, *n;
442 
443 	/*
444 	 * This lock protects list corruption from access of list outside of the irq handler
445 	 * thread.
446 	 */
447 	spin_lock(&irq_entry->list_lock);
448 	if (list_empty(&irq_entry->work_list)) {
449 		spin_unlock(&irq_entry->list_lock);
450 		return;
451 	}
452 
453 	list_for_each_entry_safe(desc, n, &irq_entry->work_list, list) {
454 		if (desc->completion->status) {
455 			list_move_tail(&desc->list, &flist);
456 		}
457 	}
458 
459 	spin_unlock(&irq_entry->list_lock);
460 
461 	list_for_each_entry(desc, &flist, list) {
462 		/*
463 		 * Check against the original status as ABORT is software defined
464 		 * and 0xff, which DSA_COMP_STATUS_MASK can mask out.
465 		 */
466 		if (unlikely(desc->completion->status == IDXD_COMP_DESC_ABORT)) {
467 			idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true);
468 			continue;
469 		}
470 
471 		idxd_dma_complete_txd(desc, IDXD_COMPLETE_NORMAL, true);
472 	}
473 }
474 
475 irqreturn_t idxd_wq_thread(int irq, void *data)
476 {
477 	struct idxd_irq_entry *irq_entry = data;
478 
479 	/*
480 	 * There are two lists we are processing. The pending_llist is where
481 	 * submmiter adds all the submitted descriptor after sending it to
482 	 * the workqueue. It's a lockless singly linked list. The work_list
483 	 * is the common linux double linked list. We are in a scenario of
484 	 * multiple producers and a single consumer. The producers are all
485 	 * the kernel submitters of descriptors, and the consumer is the
486 	 * kernel irq handler thread for the msix vector when using threaded
487 	 * irq. To work with the restrictions of llist to remain lockless,
488 	 * we are doing the following steps:
489 	 * 1. Iterate through the work_list and process any completed
490 	 *    descriptor. Delete the completed entries during iteration.
491 	 * 2. llist_del_all() from the pending list.
492 	 * 3. Iterate through the llist that was deleted from the pending list
493 	 *    and process the completed entries.
494 	 * 4. If the entry is still waiting on hardware, list_add_tail() to
495 	 *    the work_list.
496 	 */
497 	irq_process_work_list(irq_entry);
498 	irq_process_pending_llist(irq_entry);
499 
500 	return IRQ_HANDLED;
501 }
502