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>
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"
13 #include "registers.h"
17 IRQ_WORK_PROCESS_FAULT,
21 struct work_struct work;
23 struct idxd_device *idxd;
26 struct idxd_resubmit {
27 struct work_struct work;
28 struct idxd_desc *desc;
31 struct idxd_int_handle_revoke {
32 struct work_struct work;
33 struct idxd_device *idxd;
36 static void idxd_device_reinit(struct work_struct *work)
38 struct idxd_device *idxd = container_of(work, struct idxd_device, work);
39 struct device *dev = &idxd->pdev->dev;
42 idxd_device_reset(idxd);
43 rc = idxd_device_config(idxd);
47 rc = idxd_device_enable(idxd);
51 for (i = 0; i < idxd->max_wqs; i++) {
52 struct idxd_wq *wq = idxd->wqs[i];
54 if (wq->state == IDXD_WQ_ENABLED) {
55 rc = idxd_wq_enable(wq);
57 dev_warn(dev, "Unable to re-enable wq %s\n",
58 dev_name(wq_confdev(wq)));
66 idxd_device_clear_state(idxd);
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.
74 static void idxd_int_handle_revoke_drain(struct idxd_irq_entry *ie)
76 struct idxd_wq *wq = ie_to_wq(ie);
77 struct idxd_device *idxd = wq->idxd;
78 struct device *dev = &idxd->pdev->dev;
79 struct dsa_hw_desc desc = {};
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;
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);
94 * The wmb() makes sure that the descriptor is all there before we
98 if (wq_dedicated(wq)) {
99 iosubmit_cmds512(portal, &desc, 1);
101 rc = idxd_enqcmds(wq, portal, &desc);
102 /* This should not fail unless hardware failed. */
104 dev_warn(dev, "Failed to submit drain desc on wq %d\n", wq->id);
108 static void idxd_abort_invalid_int_handle_descs(struct idxd_irq_entry *ie)
111 struct idxd_desc *d, *t;
112 struct llist_node *head;
114 spin_lock(&ie->list_lock);
115 head = llist_del_all(&ie->pending_llist);
117 llist_for_each_entry_safe(d, t, head, llnode)
118 list_add_tail(&d->list, &ie->work_list);
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);
125 spin_unlock(&ie->list_lock);
127 list_for_each_entry_safe(d, t, &flist, list) {
129 idxd_dma_complete_txd(d, IDXD_COMPLETE_ABORT, true);
133 static void idxd_int_handle_revoke(struct work_struct *work)
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;
142 if (!idxd->request_int_handles) {
144 dev_warn(dev, "Unexpected int handle refresh interrupt.\n");
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.
157 for (i = 1; i < idxd->irq_cnt; i++) {
158 struct idxd_irq_entry *ie = idxd_get_ie(idxd, i);
159 struct idxd_wq *wq = ie_to_wq(ie);
161 if (ie->int_handle == INVALID_INT_HANDLE)
164 rc = idxd_device_request_int_handle(idxd, i, &new_handle, IDXD_IRQ_MSIX);
166 dev_warn(dev, "get int handle %d failed: %d\n", i, rc);
168 * Failed to acquire new interrupt handle. Kill the WQ
169 * and release all the pending submitters. The submitters will
170 * get error return code and handle appropriately.
172 ie->int_handle = INVALID_INT_HANDLE;
174 idxd_abort_invalid_int_handle_descs(ie);
178 /* No change in interrupt handle, nothing needs to be done */
179 if (ie->int_handle == new_handle)
182 if (wq->state != IDXD_WQ_ENABLED || wq->type != IDXD_WQT_KERNEL) {
184 * All the MSIX interrupts are allocated at once during probe.
185 * Therefore we need to update all interrupts even if the WQ
186 * isn't supporting interrupt operations.
188 ie->int_handle = new_handle;
192 mutex_lock(&wq->wq_lock);
193 reinit_completion(&wq->wq_resurrect);
195 /* Kill percpu_ref to pause additional descriptor submissions */
196 percpu_ref_kill(&wq->wq_active);
198 /* Wait for all submitters quiesce before we change interrupt handle */
199 wait_for_completion(&wq->wq_dead);
201 ie->int_handle = new_handle;
203 /* Revive percpu ref and wake up all the waiting submitters */
204 percpu_ref_reinit(&wq->wq_active);
205 complete_all(&wq->wq_resurrect);
206 mutex_unlock(&wq->wq_lock);
209 * The delay here is to wait for all possible MOVDIR64B that
210 * are issued before percpu_ref_kill() has happened to have
211 * reached the PCIe domain before the drain is issued. The driver
212 * needs to ensure that the drain descriptor issued does not pass
213 * all the other issued descriptors that contain the invalid
214 * interrupt handle in order to ensure that the drain descriptor
215 * interrupt will allow the cleanup of all the descriptors with
216 * invalid interrupt handle.
218 if (wq_dedicated(wq))
220 idxd_int_handle_revoke_drain(ie);
225 static int process_misc_interrupts(struct idxd_device *idxd, u32 cause)
227 struct device *dev = &idxd->pdev->dev;
228 union gensts_reg gensts;
233 if (cause & IDXD_INTC_HALT_STATE)
236 if (cause & IDXD_INTC_ERR) {
237 spin_lock(&idxd->dev_lock);
238 for (i = 0; i < 4; i++)
239 idxd->sw_err.bits[i] = ioread64(idxd->reg_base +
240 IDXD_SWERR_OFFSET + i * sizeof(u64));
242 iowrite64(idxd->sw_err.bits[0] & IDXD_SWERR_ACK,
243 idxd->reg_base + IDXD_SWERR_OFFSET);
245 if (idxd->sw_err.valid && idxd->sw_err.wq_idx_valid) {
246 int id = idxd->sw_err.wq_idx;
247 struct idxd_wq *wq = idxd->wqs[id];
249 if (wq->type == IDXD_WQT_USER)
250 wake_up_interruptible(&wq->err_queue);
254 for (i = 0; i < idxd->max_wqs; i++) {
255 struct idxd_wq *wq = idxd->wqs[i];
257 if (wq->type == IDXD_WQT_USER)
258 wake_up_interruptible(&wq->err_queue);
262 spin_unlock(&idxd->dev_lock);
263 val |= IDXD_INTC_ERR;
265 for (i = 0; i < 4; i++)
266 dev_warn(dev, "err[%d]: %#16.16llx\n",
267 i, idxd->sw_err.bits[i]);
271 if (cause & IDXD_INTC_INT_HANDLE_REVOKED) {
272 struct idxd_int_handle_revoke *revoke;
274 val |= IDXD_INTC_INT_HANDLE_REVOKED;
276 revoke = kzalloc(sizeof(*revoke), GFP_ATOMIC);
279 INIT_WORK(&revoke->work, idxd_int_handle_revoke);
280 queue_work(idxd->wq, &revoke->work);
283 dev_err(dev, "Failed to allocate work for int handle revoke\n");
284 idxd_wqs_quiesce(idxd);
288 if (cause & IDXD_INTC_CMD) {
289 val |= IDXD_INTC_CMD;
290 complete(idxd->cmd_done);
293 if (cause & IDXD_INTC_OCCUPY) {
294 /* Driver does not utilize occupancy interrupt */
295 val |= IDXD_INTC_OCCUPY;
298 if (cause & IDXD_INTC_PERFMON_OVFL) {
299 val |= IDXD_INTC_PERFMON_OVFL;
300 perfmon_counter_overflow(idxd);
305 dev_warn_once(dev, "Unexpected interrupt cause bits set: %#x\n",
312 gensts.bits = ioread32(idxd->reg_base + IDXD_GENSTATS_OFFSET);
313 if (gensts.state == IDXD_DEVICE_STATE_HALT) {
314 idxd->state = IDXD_DEV_HALTED;
315 if (gensts.reset_type == IDXD_DEVICE_RESET_SOFTWARE) {
317 * If we need a software reset, we will throw the work
318 * on a system workqueue in order to allow interrupts
319 * for the device command completions.
321 INIT_WORK(&idxd->work, idxd_device_reinit);
322 queue_work(idxd->wq, &idxd->work);
324 idxd->state = IDXD_DEV_HALTED;
325 idxd_wqs_quiesce(idxd);
326 idxd_wqs_unmap_portal(idxd);
327 spin_lock(&idxd->dev_lock);
328 idxd_device_clear_state(idxd);
329 dev_err(&idxd->pdev->dev,
330 "idxd halted, need %s.\n",
331 gensts.reset_type == IDXD_DEVICE_RESET_FLR ?
332 "FLR" : "system reset");
333 spin_unlock(&idxd->dev_lock);
341 irqreturn_t idxd_misc_thread(int vec, void *data)
343 struct idxd_irq_entry *irq_entry = data;
344 struct idxd_device *idxd = ie_to_idxd(irq_entry);
348 cause = ioread32(idxd->reg_base + IDXD_INTCAUSE_OFFSET);
350 iowrite32(cause, idxd->reg_base + IDXD_INTCAUSE_OFFSET);
353 rc = process_misc_interrupts(idxd, cause);
356 cause = ioread32(idxd->reg_base + IDXD_INTCAUSE_OFFSET);
358 iowrite32(cause, idxd->reg_base + IDXD_INTCAUSE_OFFSET);
364 static void idxd_int_handle_resubmit_work(struct work_struct *work)
366 struct idxd_resubmit *irw = container_of(work, struct idxd_resubmit, work);
367 struct idxd_desc *desc = irw->desc;
368 struct idxd_wq *wq = desc->wq;
371 desc->completion->status = 0;
372 rc = idxd_submit_desc(wq, desc);
374 dev_dbg(&wq->idxd->pdev->dev, "Failed to resubmit desc %d to wq %d.\n",
377 * If the error is not -EAGAIN, it means the submission failed due to wq
378 * has been killed instead of ENQCMDS failure. Here the driver needs to
379 * notify the submitter of the failure by reporting abort status.
381 * -EAGAIN comes from ENQCMDS failure. idxd_submit_desc() will handle the
385 desc->completion->status = IDXD_COMP_DESC_ABORT;
386 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, false);
388 idxd_free_desc(wq, desc);
393 bool idxd_queue_int_handle_resubmit(struct idxd_desc *desc)
395 struct idxd_wq *wq = desc->wq;
396 struct idxd_device *idxd = wq->idxd;
397 struct idxd_resubmit *irw;
399 irw = kzalloc(sizeof(*irw), GFP_KERNEL);
404 INIT_WORK(&irw->work, idxd_int_handle_resubmit_work);
405 queue_work(idxd->wq, &irw->work);
409 static void irq_process_pending_llist(struct idxd_irq_entry *irq_entry)
411 struct idxd_desc *desc, *t;
412 struct llist_node *head;
414 head = llist_del_all(&irq_entry->pending_llist);
418 llist_for_each_entry_safe(desc, t, head, llnode) {
419 u8 status = desc->completion->status & DSA_COMP_STATUS_MASK;
423 * Check against the original status as ABORT is software defined
424 * and 0xff, which DSA_COMP_STATUS_MASK can mask out.
426 if (unlikely(desc->completion->status == IDXD_COMP_DESC_ABORT)) {
427 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true);
431 idxd_dma_complete_txd(desc, IDXD_COMPLETE_NORMAL, true);
433 spin_lock(&irq_entry->list_lock);
434 list_add_tail(&desc->list,
435 &irq_entry->work_list);
436 spin_unlock(&irq_entry->list_lock);
441 static void irq_process_work_list(struct idxd_irq_entry *irq_entry)
444 struct idxd_desc *desc, *n;
447 * This lock protects list corruption from access of list outside of the irq handler
450 spin_lock(&irq_entry->list_lock);
451 if (list_empty(&irq_entry->work_list)) {
452 spin_unlock(&irq_entry->list_lock);
456 list_for_each_entry_safe(desc, n, &irq_entry->work_list, list) {
457 if (desc->completion->status) {
458 list_move_tail(&desc->list, &flist);
462 spin_unlock(&irq_entry->list_lock);
464 list_for_each_entry(desc, &flist, list) {
466 * Check against the original status as ABORT is software defined
467 * and 0xff, which DSA_COMP_STATUS_MASK can mask out.
469 if (unlikely(desc->completion->status == IDXD_COMP_DESC_ABORT)) {
470 idxd_dma_complete_txd(desc, IDXD_COMPLETE_ABORT, true);
474 idxd_dma_complete_txd(desc, IDXD_COMPLETE_NORMAL, true);
478 irqreturn_t idxd_wq_thread(int irq, void *data)
480 struct idxd_irq_entry *irq_entry = data;
483 * There are two lists we are processing. The pending_llist is where
484 * submmiter adds all the submitted descriptor after sending it to
485 * the workqueue. It's a lockless singly linked list. The work_list
486 * is the common linux double linked list. We are in a scenario of
487 * multiple producers and a single consumer. The producers are all
488 * the kernel submitters of descriptors, and the consumer is the
489 * kernel irq handler thread for the msix vector when using threaded
490 * irq. To work with the restrictions of llist to remain lockless,
491 * we are doing the following steps:
492 * 1. Iterate through the work_list and process any completed
493 * descriptor. Delete the completed entries during iteration.
494 * 2. llist_del_all() from the pending list.
495 * 3. Iterate through the llist that was deleted from the pending list
496 * and process the completed entries.
497 * 4. If the entry is still waiting on hardware, list_add_tail() to
500 irq_process_work_list(irq_entry);
501 irq_process_pending_llist(irq_entry);