xref: /openbmc/linux/drivers/dma/idxd/submit.c (revision 56fc39f5)
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 <uapi/linux/idxd.h>
8 #include "idxd.h"
9 #include "registers.h"
10 
11 static struct idxd_desc *__get_desc(struct idxd_wq *wq, int idx, int cpu)
12 {
13 	struct idxd_desc *desc;
14 	struct idxd_device *idxd = wq->idxd;
15 
16 	desc = wq->descs[idx];
17 	memset(desc->hw, 0, sizeof(struct dsa_hw_desc));
18 	memset(desc->completion, 0, idxd->data->compl_size);
19 	desc->cpu = cpu;
20 
21 	if (device_pasid_enabled(idxd))
22 		desc->hw->pasid = idxd->pasid;
23 
24 	return desc;
25 }
26 
27 struct idxd_desc *idxd_alloc_desc(struct idxd_wq *wq, enum idxd_op_type optype)
28 {
29 	int cpu, idx;
30 	struct idxd_device *idxd = wq->idxd;
31 	DEFINE_SBQ_WAIT(wait);
32 	struct sbq_wait_state *ws;
33 	struct sbitmap_queue *sbq;
34 
35 	if (idxd->state != IDXD_DEV_ENABLED)
36 		return ERR_PTR(-EIO);
37 
38 	sbq = &wq->sbq;
39 	idx = sbitmap_queue_get(sbq, &cpu);
40 	if (idx < 0) {
41 		if (optype == IDXD_OP_NONBLOCK)
42 			return ERR_PTR(-EAGAIN);
43 	} else {
44 		return __get_desc(wq, idx, cpu);
45 	}
46 
47 	ws = &sbq->ws[0];
48 	for (;;) {
49 		sbitmap_prepare_to_wait(sbq, ws, &wait, TASK_INTERRUPTIBLE);
50 		if (signal_pending_state(TASK_INTERRUPTIBLE, current))
51 			break;
52 		idx = sbitmap_queue_get(sbq, &cpu);
53 		if (idx >= 0)
54 			break;
55 		schedule();
56 	}
57 
58 	sbitmap_finish_wait(sbq, ws, &wait);
59 	if (idx < 0)
60 		return ERR_PTR(-EAGAIN);
61 
62 	return __get_desc(wq, idx, cpu);
63 }
64 
65 void idxd_free_desc(struct idxd_wq *wq, struct idxd_desc *desc)
66 {
67 	int cpu = desc->cpu;
68 
69 	desc->cpu = -1;
70 	sbitmap_queue_clear(&wq->sbq, desc->id, cpu);
71 }
72 
73 static struct idxd_desc *list_abort_desc(struct idxd_wq *wq, struct idxd_irq_entry *ie,
74 					 struct idxd_desc *desc)
75 {
76 	struct idxd_desc *d, *n;
77 
78 	lockdep_assert_held(&ie->list_lock);
79 	list_for_each_entry_safe(d, n, &ie->work_list, list) {
80 		if (d == desc) {
81 			list_del(&d->list);
82 			return d;
83 		}
84 	}
85 
86 	/*
87 	 * At this point, the desc needs to be aborted is held by the completion
88 	 * handler where it has taken it off the pending list but has not added to the
89 	 * work list. It will be cleaned up by the interrupt handler when it sees the
90 	 * IDXD_COMP_DESC_ABORT for completion status.
91 	 */
92 	return NULL;
93 }
94 
95 static void llist_abort_desc(struct idxd_wq *wq, struct idxd_irq_entry *ie,
96 			     struct idxd_desc *desc)
97 {
98 	struct idxd_desc *d, *t, *found = NULL;
99 	struct llist_node *head;
100 
101 	desc->completion->status = IDXD_COMP_DESC_ABORT;
102 	/*
103 	 * Grab the list lock so it will block the irq thread handler. This allows the
104 	 * abort code to locate the descriptor need to be aborted.
105 	 */
106 	spin_lock(&ie->list_lock);
107 	head = llist_del_all(&ie->pending_llist);
108 	if (head) {
109 		llist_for_each_entry_safe(d, t, head, llnode) {
110 			if (d == desc) {
111 				found = desc;
112 				continue;
113 			}
114 			list_add_tail(&desc->list, &ie->work_list);
115 		}
116 	}
117 
118 	if (!found)
119 		found = list_abort_desc(wq, ie, desc);
120 	spin_unlock(&ie->list_lock);
121 
122 	if (found)
123 		idxd_dma_complete_txd(found, IDXD_COMPLETE_ABORT, false);
124 }
125 
126 int idxd_submit_desc(struct idxd_wq *wq, struct idxd_desc *desc)
127 {
128 	struct idxd_device *idxd = wq->idxd;
129 	struct idxd_irq_entry *ie = NULL;
130 	u32 desc_flags = desc->hw->flags;
131 	void __iomem *portal;
132 	int rc;
133 
134 	if (idxd->state != IDXD_DEV_ENABLED)
135 		return -EIO;
136 
137 	if (!percpu_ref_tryget_live(&wq->wq_active)) {
138 		wait_for_completion(&wq->wq_resurrect);
139 		if (!percpu_ref_tryget_live(&wq->wq_active))
140 			return -ENXIO;
141 	}
142 
143 	portal = idxd_wq_portal_addr(wq);
144 
145 	/*
146 	 * The wmb() flushes writes to coherent DMA data before
147 	 * possibly triggering a DMA read. The wmb() is necessary
148 	 * even on UP because the recipient is a device.
149 	 */
150 	wmb();
151 
152 	/*
153 	 * Pending the descriptor to the lockless list for the irq_entry
154 	 * that we designated the descriptor to.
155 	 */
156 	if (desc_flags & IDXD_OP_FLAG_RCI) {
157 		ie = wq->ie;
158 		if (ie->int_handle == INVALID_INT_HANDLE)
159 			desc->hw->int_handle = ie->id;
160 		else
161 			desc->hw->int_handle = ie->int_handle;
162 
163 		llist_add(&desc->llnode, &ie->pending_llist);
164 	}
165 
166 	if (wq_dedicated(wq)) {
167 		iosubmit_cmds512(portal, desc->hw, 1);
168 	} else {
169 		/*
170 		 * It's not likely that we would receive queue full rejection
171 		 * since the descriptor allocation gates at wq size. If we
172 		 * receive a -EAGAIN, that means something went wrong such as the
173 		 * device is not accepting descriptor at all.
174 		 */
175 		rc = enqcmds(portal, desc->hw);
176 		if (rc < 0) {
177 			percpu_ref_put(&wq->wq_active);
178 			/* abort operation frees the descriptor */
179 			if (ie)
180 				llist_abort_desc(wq, ie, desc);
181 			return rc;
182 		}
183 	}
184 
185 	percpu_ref_put(&wq->wq_active);
186 	return 0;
187 }
188