docs: Fix empty parallelism argument
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / gt / intel_breadcrumbs.c
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24
25 #include <linux/kthread.h>
26 #include <trace/events/dma_fence.h>
27 #include <uapi/linux/sched/types.h>
28
29 #include "i915_drv.h"
30 #include "i915_trace.h"
31 #include "intel_gt_pm.h"
32 #include "intel_gt_requests.h"
33
34 static void irq_enable(struct intel_engine_cs *engine)
35 {
36         if (!engine->irq_enable)
37                 return;
38
39         /* Caller disables interrupts */
40         spin_lock(&engine->gt->irq_lock);
41         engine->irq_enable(engine);
42         spin_unlock(&engine->gt->irq_lock);
43 }
44
45 static void irq_disable(struct intel_engine_cs *engine)
46 {
47         if (!engine->irq_disable)
48                 return;
49
50         /* Caller disables interrupts */
51         spin_lock(&engine->gt->irq_lock);
52         engine->irq_disable(engine);
53         spin_unlock(&engine->gt->irq_lock);
54 }
55
56 static void __intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
57 {
58         struct intel_engine_cs *engine =
59                 container_of(b, struct intel_engine_cs, breadcrumbs);
60
61         lockdep_assert_held(&b->irq_lock);
62
63         GEM_BUG_ON(!b->irq_enabled);
64         if (!--b->irq_enabled)
65                 irq_disable(engine);
66
67         b->irq_armed = false;
68         intel_gt_pm_put_async(engine->gt);
69 }
70
71 void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
72 {
73         struct intel_breadcrumbs *b = &engine->breadcrumbs;
74         unsigned long flags;
75
76         if (!b->irq_armed)
77                 return;
78
79         spin_lock_irqsave(&b->irq_lock, flags);
80         if (b->irq_armed)
81                 __intel_breadcrumbs_disarm_irq(b);
82         spin_unlock_irqrestore(&b->irq_lock, flags);
83 }
84
85 static inline bool __request_completed(const struct i915_request *rq)
86 {
87         return i915_seqno_passed(__hwsp_seqno(rq), rq->fence.seqno);
88 }
89
90 __maybe_unused static bool
91 check_signal_order(struct intel_context *ce, struct i915_request *rq)
92 {
93         if (!list_is_last(&rq->signal_link, &ce->signals) &&
94             i915_seqno_passed(rq->fence.seqno,
95                               list_next_entry(rq, signal_link)->fence.seqno))
96                 return false;
97
98         if (!list_is_first(&rq->signal_link, &ce->signals) &&
99             i915_seqno_passed(list_prev_entry(rq, signal_link)->fence.seqno,
100                               rq->fence.seqno))
101                 return false;
102
103         return true;
104 }
105
106 static bool
107 __dma_fence_signal(struct dma_fence *fence)
108 {
109         return !test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
110 }
111
112 static void
113 __dma_fence_signal__timestamp(struct dma_fence *fence, ktime_t timestamp)
114 {
115         fence->timestamp = timestamp;
116         set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
117         trace_dma_fence_signaled(fence);
118 }
119
120 static void
121 __dma_fence_signal__notify(struct dma_fence *fence,
122                            const struct list_head *list)
123 {
124         struct dma_fence_cb *cur, *tmp;
125
126         lockdep_assert_held(fence->lock);
127
128         list_for_each_entry_safe(cur, tmp, list, node) {
129                 INIT_LIST_HEAD(&cur->node);
130                 cur->func(fence, cur);
131         }
132 }
133
134 static void add_retire(struct intel_breadcrumbs *b, struct intel_timeline *tl)
135 {
136         struct intel_engine_cs *engine =
137                 container_of(b, struct intel_engine_cs, breadcrumbs);
138
139         intel_engine_add_retire(engine, tl);
140 }
141
142 static void signal_irq_work(struct irq_work *work)
143 {
144         struct intel_breadcrumbs *b = container_of(work, typeof(*b), irq_work);
145         const ktime_t timestamp = ktime_get();
146         struct intel_context *ce, *cn;
147         struct list_head *pos, *next;
148         LIST_HEAD(signal);
149
150         spin_lock(&b->irq_lock);
151
152         if (b->irq_armed && list_empty(&b->signalers))
153                 __intel_breadcrumbs_disarm_irq(b);
154
155         list_for_each_entry_safe(ce, cn, &b->signalers, signal_link) {
156                 GEM_BUG_ON(list_empty(&ce->signals));
157
158                 list_for_each_safe(pos, next, &ce->signals) {
159                         struct i915_request *rq =
160                                 list_entry(pos, typeof(*rq), signal_link);
161
162                         GEM_BUG_ON(!check_signal_order(ce, rq));
163
164                         if (!__request_completed(rq))
165                                 break;
166
167                         GEM_BUG_ON(!test_bit(I915_FENCE_FLAG_SIGNAL,
168                                              &rq->fence.flags));
169                         clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
170
171                         if (!__dma_fence_signal(&rq->fence))
172                                 continue;
173
174                         /*
175                          * Queue for execution after dropping the signaling
176                          * spinlock as the callback chain may end up adding
177                          * more signalers to the same context or engine.
178                          */
179                         i915_request_get(rq);
180                         list_add_tail(&rq->signal_link, &signal);
181                 }
182
183                 /*
184                  * We process the list deletion in bulk, only using a list_add
185                  * (not list_move) above but keeping the status of
186                  * rq->signal_link known with the I915_FENCE_FLAG_SIGNAL bit.
187                  */
188                 if (!list_is_first(pos, &ce->signals)) {
189                         /* Advance the list to the first incomplete request */
190                         __list_del_many(&ce->signals, pos);
191                         if (&ce->signals == pos) { /* now empty */
192                                 list_del_init(&ce->signal_link);
193                                 add_retire(b, ce->timeline);
194                         }
195                 }
196         }
197
198         spin_unlock(&b->irq_lock);
199
200         list_for_each_safe(pos, next, &signal) {
201                 struct i915_request *rq =
202                         list_entry(pos, typeof(*rq), signal_link);
203                 struct list_head cb_list;
204
205                 spin_lock(&rq->lock);
206                 list_replace(&rq->fence.cb_list, &cb_list);
207                 __dma_fence_signal__timestamp(&rq->fence, timestamp);
208                 __dma_fence_signal__notify(&rq->fence, &cb_list);
209                 spin_unlock(&rq->lock);
210
211                 i915_request_put(rq);
212         }
213 }
214
215 static bool __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
216 {
217         struct intel_engine_cs *engine =
218                 container_of(b, struct intel_engine_cs, breadcrumbs);
219
220         lockdep_assert_held(&b->irq_lock);
221         if (b->irq_armed)
222                 return true;
223
224         if (!intel_gt_pm_get_if_awake(engine->gt))
225                 return false;
226
227         /*
228          * The breadcrumb irq will be disarmed on the interrupt after the
229          * waiters are signaled. This gives us a single interrupt window in
230          * which we can add a new waiter and avoid the cost of re-enabling
231          * the irq.
232          */
233         b->irq_armed = true;
234
235         /*
236          * Since we are waiting on a request, the GPU should be busy
237          * and should have its own rpm reference. This is tracked
238          * by i915->gt.awake, we can forgo holding our own wakref
239          * for the interrupt as before i915->gt.awake is released (when
240          * the driver is idle) we disarm the breadcrumbs.
241          */
242
243         if (!b->irq_enabled++)
244                 irq_enable(engine);
245
246         return true;
247 }
248
249 void intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
250 {
251         struct intel_breadcrumbs *b = &engine->breadcrumbs;
252
253         spin_lock_init(&b->irq_lock);
254         INIT_LIST_HEAD(&b->signalers);
255
256         init_irq_work(&b->irq_work, signal_irq_work);
257 }
258
259 void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
260 {
261         struct intel_breadcrumbs *b = &engine->breadcrumbs;
262         unsigned long flags;
263
264         spin_lock_irqsave(&b->irq_lock, flags);
265
266         if (b->irq_enabled)
267                 irq_enable(engine);
268         else
269                 irq_disable(engine);
270
271         spin_unlock_irqrestore(&b->irq_lock, flags);
272 }
273
274 void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
275 {
276 }
277
278 bool i915_request_enable_breadcrumb(struct i915_request *rq)
279 {
280         lockdep_assert_held(&rq->lock);
281
282         if (test_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags)) {
283                 struct intel_breadcrumbs *b = &rq->engine->breadcrumbs;
284                 struct intel_context *ce = rq->context;
285                 struct list_head *pos;
286
287                 spin_lock(&b->irq_lock);
288                 GEM_BUG_ON(test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags));
289
290                 if (!__intel_breadcrumbs_arm_irq(b))
291                         goto unlock;
292
293                 /*
294                  * We keep the seqno in retirement order, so we can break
295                  * inside intel_engine_signal_breadcrumbs as soon as we've
296                  * passed the last completed request (or seen a request that
297                  * hasn't event started). We could walk the timeline->requests,
298                  * but keeping a separate signalers_list has the advantage of
299                  * hopefully being much smaller than the full list and so
300                  * provides faster iteration and detection when there are no
301                  * more interrupts required for this context.
302                  *
303                  * We typically expect to add new signalers in order, so we
304                  * start looking for our insertion point from the tail of
305                  * the list.
306                  */
307                 list_for_each_prev(pos, &ce->signals) {
308                         struct i915_request *it =
309                                 list_entry(pos, typeof(*it), signal_link);
310
311                         if (i915_seqno_passed(rq->fence.seqno, it->fence.seqno))
312                                 break;
313                 }
314                 list_add(&rq->signal_link, pos);
315                 if (pos == &ce->signals) /* catch transitions from empty list */
316                         list_move_tail(&ce->signal_link, &b->signalers);
317                 GEM_BUG_ON(!check_signal_order(ce, rq));
318
319                 set_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
320 unlock:
321                 spin_unlock(&b->irq_lock);
322         }
323
324         return !__request_completed(rq);
325 }
326
327 void i915_request_cancel_breadcrumb(struct i915_request *rq)
328 {
329         struct intel_breadcrumbs *b = &rq->engine->breadcrumbs;
330
331         lockdep_assert_held(&rq->lock);
332
333         /*
334          * We must wait for b->irq_lock so that we know the interrupt handler
335          * has released its reference to the intel_context and has completed
336          * the DMA_FENCE_FLAG_SIGNALED_BIT/I915_FENCE_FLAG_SIGNAL dance (if
337          * required).
338          */
339         spin_lock(&b->irq_lock);
340         if (test_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags)) {
341                 struct intel_context *ce = rq->context;
342
343                 list_del(&rq->signal_link);
344                 if (list_empty(&ce->signals))
345                         list_del_init(&ce->signal_link);
346
347                 clear_bit(I915_FENCE_FLAG_SIGNAL, &rq->fence.flags);
348         }
349         spin_unlock(&b->irq_lock);
350 }
351
352 void intel_engine_print_breadcrumbs(struct intel_engine_cs *engine,
353                                     struct drm_printer *p)
354 {
355         struct intel_breadcrumbs *b = &engine->breadcrumbs;
356         struct intel_context *ce;
357         struct i915_request *rq;
358
359         if (list_empty(&b->signalers))
360                 return;
361
362         drm_printf(p, "Signals:\n");
363
364         spin_lock_irq(&b->irq_lock);
365         list_for_each_entry(ce, &b->signalers, signal_link) {
366                 list_for_each_entry(rq, &ce->signals, signal_link) {
367                         drm_printf(p, "\t[%llx:%llx%s] @ %dms\n",
368                                    rq->fence.context, rq->fence.seqno,
369                                    i915_request_completed(rq) ? "!" :
370                                    i915_request_started(rq) ? "*" :
371                                    "",
372                                    jiffies_to_msecs(jiffies - rq->emitted_jiffies));
373                 }
374         }
375         spin_unlock_irq(&b->irq_lock);
376 }