]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
drm/amdgpu: add zero timeout check in amdgpu_fence_wait_seq_timeout
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_fence.c
1 /*
2  * Copyright 2009 Jerome Glisse.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * The above copyright notice and this permission notice (including the
22  * next paragraph) shall be included in all copies or substantial portions
23  * of the Software.
24  *
25  */
26 /*
27  * Authors:
28  *    Jerome Glisse <[email protected]>
29  *    Dave Airlie
30  */
31 #include <linux/seq_file.h>
32 #include <linux/atomic.h>
33 #include <linux/wait.h>
34 #include <linux/kref.h>
35 #include <linux/slab.h>
36 #include <linux/firmware.h>
37 #include <drm/drmP.h>
38 #include "amdgpu.h"
39 #include "amdgpu_trace.h"
40
41 /*
42  * Fences
43  * Fences mark an event in the GPUs pipeline and are used
44  * for GPU/CPU synchronization.  When the fence is written,
45  * it is expected that all buffers associated with that fence
46  * are no longer in use by the associated ring on the GPU and
47  * that the the relevant GPU caches have been flushed.
48  */
49
50 /**
51  * amdgpu_fence_write - write a fence value
52  *
53  * @ring: ring the fence is associated with
54  * @seq: sequence number to write
55  *
56  * Writes a fence value to memory (all asics).
57  */
58 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
59 {
60         struct amdgpu_fence_driver *drv = &ring->fence_drv;
61
62         if (drv->cpu_addr)
63                 *drv->cpu_addr = cpu_to_le32(seq);
64 }
65
66 /**
67  * amdgpu_fence_read - read a fence value
68  *
69  * @ring: ring the fence is associated with
70  *
71  * Reads a fence value from memory (all asics).
72  * Returns the value of the fence read from memory.
73  */
74 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
75 {
76         struct amdgpu_fence_driver *drv = &ring->fence_drv;
77         u32 seq = 0;
78
79         if (drv->cpu_addr)
80                 seq = le32_to_cpu(*drv->cpu_addr);
81         else
82                 seq = lower_32_bits(atomic64_read(&drv->last_seq));
83
84         return seq;
85 }
86
87 /**
88  * amdgpu_fence_schedule_check - schedule lockup check
89  *
90  * @ring: pointer to struct amdgpu_ring
91  *
92  * Queues a delayed work item to check for lockups.
93  */
94 static void amdgpu_fence_schedule_check(struct amdgpu_ring *ring)
95 {
96         /*
97          * Do not reset the timer here with mod_delayed_work,
98          * this can livelock in an interaction with TTM delayed destroy.
99          */
100         queue_delayed_work(system_power_efficient_wq,
101                 &ring->fence_drv.lockup_work,
102                 AMDGPU_FENCE_JIFFIES_TIMEOUT);
103 }
104
105 /**
106  * amdgpu_fence_emit - emit a fence on the requested ring
107  *
108  * @ring: ring the fence is associated with
109  * @owner: creator of the fence
110  * @fence: amdgpu fence object
111  *
112  * Emits a fence command on the requested ring (all asics).
113  * Returns 0 on success, -ENOMEM on failure.
114  */
115 int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner,
116                       struct amdgpu_fence **fence)
117 {
118         struct amdgpu_device *adev = ring->adev;
119
120         /* we are protected by the ring emission mutex */
121         *fence = kmalloc(sizeof(struct amdgpu_fence), GFP_KERNEL);
122         if ((*fence) == NULL) {
123                 return -ENOMEM;
124         }
125         (*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx];
126         (*fence)->ring = ring;
127         (*fence)->owner = owner;
128         fence_init(&(*fence)->base, &amdgpu_fence_ops,
129                 &adev->fence_queue.lock, adev->fence_context + ring->idx,
130                 (*fence)->seq);
131         amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr, (*fence)->seq, false);
132         trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq);
133         return 0;
134 }
135
136 /**
137  * amdgpu_fence_check_signaled - callback from fence_queue
138  *
139  * this function is called with fence_queue lock held, which is also used
140  * for the fence locking itself, so unlocked variants are used for
141  * fence_signal, and remove_wait_queue.
142  */
143 static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
144 {
145         struct amdgpu_fence *fence;
146         struct amdgpu_device *adev;
147         u64 seq;
148         int ret;
149
150         fence = container_of(wait, struct amdgpu_fence, fence_wake);
151         adev = fence->ring->adev;
152
153         /*
154          * We cannot use amdgpu_fence_process here because we're already
155          * in the waitqueue, in a call from wake_up_all.
156          */
157         seq = atomic64_read(&fence->ring->fence_drv.last_seq);
158         if (seq >= fence->seq) {
159                 ret = fence_signal_locked(&fence->base);
160                 if (!ret)
161                         FENCE_TRACE(&fence->base, "signaled from irq context\n");
162                 else
163                         FENCE_TRACE(&fence->base, "was already signaled\n");
164
165                 amdgpu_irq_put(adev, fence->ring->fence_drv.irq_src,
166                                 fence->ring->fence_drv.irq_type);
167                 __remove_wait_queue(&adev->fence_queue, &fence->fence_wake);
168                 fence_put(&fence->base);
169         } else
170                 FENCE_TRACE(&fence->base, "pending\n");
171         return 0;
172 }
173
174 /**
175  * amdgpu_fence_activity - check for fence activity
176  *
177  * @ring: pointer to struct amdgpu_ring
178  *
179  * Checks the current fence value and calculates the last
180  * signalled fence value. Returns true if activity occured
181  * on the ring, and the fence_queue should be waken up.
182  */
183 static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
184 {
185         uint64_t seq, last_seq, last_emitted;
186         unsigned count_loop = 0;
187         bool wake = false;
188
189         /* Note there is a scenario here for an infinite loop but it's
190          * very unlikely to happen. For it to happen, the current polling
191          * process need to be interrupted by another process and another
192          * process needs to update the last_seq btw the atomic read and
193          * xchg of the current process.
194          *
195          * More over for this to go in infinite loop there need to be
196          * continuously new fence signaled ie amdgpu_fence_read needs
197          * to return a different value each time for both the currently
198          * polling process and the other process that xchg the last_seq
199          * btw atomic read and xchg of the current process. And the
200          * value the other process set as last seq must be higher than
201          * the seq value we just read. Which means that current process
202          * need to be interrupted after amdgpu_fence_read and before
203          * atomic xchg.
204          *
205          * To be even more safe we count the number of time we loop and
206          * we bail after 10 loop just accepting the fact that we might
207          * have temporarly set the last_seq not to the true real last
208          * seq but to an older one.
209          */
210         last_seq = atomic64_read(&ring->fence_drv.last_seq);
211         do {
212                 last_emitted = ring->fence_drv.sync_seq[ring->idx];
213                 seq = amdgpu_fence_read(ring);
214                 seq |= last_seq & 0xffffffff00000000LL;
215                 if (seq < last_seq) {
216                         seq &= 0xffffffff;
217                         seq |= last_emitted & 0xffffffff00000000LL;
218                 }
219
220                 if (seq <= last_seq || seq > last_emitted) {
221                         break;
222                 }
223                 /* If we loop over we don't want to return without
224                  * checking if a fence is signaled as it means that the
225                  * seq we just read is different from the previous on.
226                  */
227                 wake = true;
228                 last_seq = seq;
229                 if ((count_loop++) > 10) {
230                         /* We looped over too many time leave with the
231                          * fact that we might have set an older fence
232                          * seq then the current real last seq as signaled
233                          * by the hw.
234                          */
235                         break;
236                 }
237         } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
238
239         if (seq < last_emitted)
240                 amdgpu_fence_schedule_check(ring);
241
242         return wake;
243 }
244
245 /**
246  * amdgpu_fence_check_lockup - check for hardware lockup
247  *
248  * @work: delayed work item
249  *
250  * Checks for fence activity and if there is none probe
251  * the hardware if a lockup occured.
252  */
253 static void amdgpu_fence_check_lockup(struct work_struct *work)
254 {
255         struct amdgpu_fence_driver *fence_drv;
256         struct amdgpu_ring *ring;
257
258         fence_drv = container_of(work, struct amdgpu_fence_driver,
259                                 lockup_work.work);
260         ring = fence_drv->ring;
261
262         if (!down_read_trylock(&ring->adev->exclusive_lock)) {
263                 /* just reschedule the check if a reset is going on */
264                 amdgpu_fence_schedule_check(ring);
265                 return;
266         }
267
268         if (fence_drv->delayed_irq && ring->adev->ddev->irq_enabled) {
269                 fence_drv->delayed_irq = false;
270                 amdgpu_irq_update(ring->adev, fence_drv->irq_src,
271                                 fence_drv->irq_type);
272         }
273
274         if (amdgpu_fence_activity(ring))
275                 wake_up_all(&ring->adev->fence_queue);
276         else if (amdgpu_ring_is_lockup(ring)) {
277                 /* good news we believe it's a lockup */
278                 dev_warn(ring->adev->dev, "GPU lockup (current fence id "
279                         "0x%016llx last fence id 0x%016llx on ring %d)\n",
280                         (uint64_t)atomic64_read(&fence_drv->last_seq),
281                         fence_drv->sync_seq[ring->idx], ring->idx);
282
283                 /* remember that we need an reset */
284                 ring->adev->needs_reset = true;
285                 wake_up_all(&ring->adev->fence_queue);
286         }
287         up_read(&ring->adev->exclusive_lock);
288 }
289
290 /**
291  * amdgpu_fence_process - process a fence
292  *
293  * @adev: amdgpu_device pointer
294  * @ring: ring index the fence is associated with
295  *
296  * Checks the current fence value and wakes the fence queue
297  * if the sequence number has increased (all asics).
298  */
299 void amdgpu_fence_process(struct amdgpu_ring *ring)
300 {
301         uint64_t seq, last_seq, last_emitted;
302         unsigned count_loop = 0;
303         bool wake = false;
304
305         /* Note there is a scenario here for an infinite loop but it's
306          * very unlikely to happen. For it to happen, the current polling
307          * process need to be interrupted by another process and another
308          * process needs to update the last_seq btw the atomic read and
309          * xchg of the current process.
310          *
311          * More over for this to go in infinite loop there need to be
312          * continuously new fence signaled ie amdgpu_fence_read needs
313          * to return a different value each time for both the currently
314          * polling process and the other process that xchg the last_seq
315          * btw atomic read and xchg of the current process. And the
316          * value the other process set as last seq must be higher than
317          * the seq value we just read. Which means that current process
318          * need to be interrupted after amdgpu_fence_read and before
319          * atomic xchg.
320          *
321          * To be even more safe we count the number of time we loop and
322          * we bail after 10 loop just accepting the fact that we might
323          * have temporarly set the last_seq not to the true real last
324          * seq but to an older one.
325          */
326         last_seq = atomic64_read(&ring->fence_drv.last_seq);
327         do {
328                 last_emitted = ring->fence_drv.sync_seq[ring->idx];
329                 seq = amdgpu_fence_read(ring);
330                 seq |= last_seq & 0xffffffff00000000LL;
331                 if (seq < last_seq) {
332                         seq &= 0xffffffff;
333                         seq |= last_emitted & 0xffffffff00000000LL;
334                 }
335
336                 if (seq <= last_seq || seq > last_emitted) {
337                         break;
338                 }
339                 /* If we loop over we don't want to return without
340                  * checking if a fence is signaled as it means that the
341                  * seq we just read is different from the previous on.
342                  */
343                 wake = true;
344                 last_seq = seq;
345                 if ((count_loop++) > 10) {
346                         /* We looped over too many time leave with the
347                          * fact that we might have set an older fence
348                          * seq then the current real last seq as signaled
349                          * by the hw.
350                          */
351                         break;
352                 }
353         } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
354
355         if (wake)
356                 wake_up_all(&ring->adev->fence_queue);
357 }
358
359 /**
360  * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
361  *
362  * @ring: ring the fence is associated with
363  * @seq: sequence number
364  *
365  * Check if the last signaled fence sequnce number is >= the requested
366  * sequence number (all asics).
367  * Returns true if the fence has signaled (current fence value
368  * is >= requested value) or false if it has not (current fence
369  * value is < the requested value.  Helper function for
370  * amdgpu_fence_signaled().
371  */
372 static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
373 {
374         if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
375                 return true;
376
377         /* poll new last sequence at least once */
378         amdgpu_fence_process(ring);
379         if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
380                 return true;
381
382         return false;
383 }
384
385 static bool amdgpu_fence_is_signaled(struct fence *f)
386 {
387         struct amdgpu_fence *fence = to_amdgpu_fence(f);
388         struct amdgpu_ring *ring = fence->ring;
389         struct amdgpu_device *adev = ring->adev;
390
391         if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
392                 return true;
393
394         if (down_read_trylock(&adev->exclusive_lock)) {
395                 amdgpu_fence_process(ring);
396                 up_read(&adev->exclusive_lock);
397
398                 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
399                         return true;
400         }
401         return false;
402 }
403
404 /**
405  * amdgpu_fence_enable_signaling - enable signalling on fence
406  * @fence: fence
407  *
408  * This function is called with fence_queue lock held, and adds a callback
409  * to fence_queue that checks if this fence is signaled, and if so it
410  * signals the fence and removes itself.
411  */
412 static bool amdgpu_fence_enable_signaling(struct fence *f)
413 {
414         struct amdgpu_fence *fence = to_amdgpu_fence(f);
415         struct amdgpu_ring *ring = fence->ring;
416         struct amdgpu_device *adev = ring->adev;
417
418         if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
419                 return false;
420
421         if (down_read_trylock(&adev->exclusive_lock)) {
422                 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
423                         ring->fence_drv.irq_type);
424                 if (amdgpu_fence_activity(ring))
425                         wake_up_all_locked(&adev->fence_queue);
426
427                 /* did fence get signaled after we enabled the sw irq? */
428                 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) {
429                         amdgpu_irq_put(adev, ring->fence_drv.irq_src,
430                                 ring->fence_drv.irq_type);
431                         up_read(&adev->exclusive_lock);
432                         return false;
433                 }
434
435                 up_read(&adev->exclusive_lock);
436         } else {
437                 /* we're probably in a lockup, lets not fiddle too much */
438                 if (amdgpu_irq_get_delayed(adev, ring->fence_drv.irq_src,
439                         ring->fence_drv.irq_type))
440                         ring->fence_drv.delayed_irq = true;
441                 amdgpu_fence_schedule_check(ring);
442         }
443
444         fence->fence_wake.flags = 0;
445         fence->fence_wake.private = NULL;
446         fence->fence_wake.func = amdgpu_fence_check_signaled;
447         __add_wait_queue(&adev->fence_queue, &fence->fence_wake);
448         fence_get(f);
449         FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
450         return true;
451 }
452
453 /**
454  * amdgpu_fence_signaled - check if a fence has signaled
455  *
456  * @fence: amdgpu fence object
457  *
458  * Check if the requested fence has signaled (all asics).
459  * Returns true if the fence has signaled or false if it has not.
460  */
461 bool amdgpu_fence_signaled(struct amdgpu_fence *fence)
462 {
463         if (!fence)
464                 return true;
465
466         if (amdgpu_fence_seq_signaled(fence->ring, fence->seq)) {
467                 if (!fence_signal(&fence->base))
468                         FENCE_TRACE(&fence->base, "signaled from amdgpu_fence_signaled\n");
469                 return true;
470         }
471
472         return false;
473 }
474
475 /**
476  * amdgpu_fence_any_seq_signaled - check if any sequence number is signaled
477  *
478  * @adev: amdgpu device pointer
479  * @seq: sequence numbers
480  *
481  * Check if the last signaled fence sequnce number is >= the requested
482  * sequence number (all asics).
483  * Returns true if any has signaled (current value is >= requested value)
484  * or false if it has not. Helper function for amdgpu_fence_wait_seq.
485  */
486 static bool amdgpu_fence_any_seq_signaled(struct amdgpu_device *adev, u64 *seq)
487 {
488         unsigned i;
489
490         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
491                 if (!adev->rings[i] || !seq[i])
492                         continue;
493
494                 if (amdgpu_fence_seq_signaled(adev->rings[i], seq[i]))
495                         return true;
496         }
497
498         return false;
499 }
500
501 /**
502  * amdgpu_fence_wait_seq_timeout - wait for a specific sequence numbers
503  *
504  * @adev: amdgpu device pointer
505  * @target_seq: sequence number(s) we want to wait for
506  * @intr: use interruptable sleep
507  * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
508  *
509  * Wait for the requested sequence number(s) to be written by any ring
510  * (all asics).  Sequnce number array is indexed by ring id.
511  * @intr selects whether to use interruptable (true) or non-interruptable
512  * (false) sleep when waiting for the sequence number.  Helper function
513  * for amdgpu_fence_wait_*().
514  * Returns remaining time if the sequence number has passed, 0 when
515  * the wait timeout, or an error for all other cases.
516  * -EDEADLK is returned when a GPU lockup has been detected.
517  */
518 long amdgpu_fence_wait_seq_timeout(struct amdgpu_device *adev, u64 *target_seq,
519                                    bool intr, long timeout)
520 {
521         uint64_t last_seq[AMDGPU_MAX_RINGS];
522         bool signaled;
523         int i, r;
524
525         if (timeout == 0) {
526                 return amdgpu_fence_any_seq_signaled(adev, target_seq);
527         }
528
529         while (!amdgpu_fence_any_seq_signaled(adev, target_seq)) {
530
531                 /* Save current sequence values, used to check for GPU lockups */
532                 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
533                         struct amdgpu_ring *ring = adev->rings[i];
534
535                         if (!ring || !target_seq[i])
536                                 continue;
537
538                         last_seq[i] = atomic64_read(&ring->fence_drv.last_seq);
539                         trace_amdgpu_fence_wait_begin(adev->ddev, i, target_seq[i]);
540                         amdgpu_irq_get(adev, ring->fence_drv.irq_src,
541                                        ring->fence_drv.irq_type);
542                 }
543
544                 if (intr) {
545                         r = wait_event_interruptible_timeout(adev->fence_queue, (
546                                 (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
547                                  || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
548                 } else {
549                         r = wait_event_timeout(adev->fence_queue, (
550                                 (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
551                                  || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
552                 }
553
554                 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
555                         struct amdgpu_ring *ring = adev->rings[i];
556
557                         if (!ring || !target_seq[i])
558                                 continue;
559
560                         amdgpu_irq_put(adev, ring->fence_drv.irq_src,
561                                        ring->fence_drv.irq_type);
562                         trace_amdgpu_fence_wait_end(adev->ddev, i, target_seq[i]);
563                 }
564
565                 if (unlikely(r < 0))
566                         return r;
567
568                 if (unlikely(!signaled)) {
569
570                         if (adev->needs_reset)
571                                 return -EDEADLK;
572
573                         /* we were interrupted for some reason and fence
574                          * isn't signaled yet, resume waiting */
575                         if (r)
576                                 continue;
577
578                         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
579                                 struct amdgpu_ring *ring = adev->rings[i];
580
581                                 if (!ring || !target_seq[i])
582                                         continue;
583
584                                 if (last_seq[i] != atomic64_read(&ring->fence_drv.last_seq))
585                                         break;
586                         }
587
588                         if (i != AMDGPU_MAX_RINGS)
589                                 continue;
590
591                         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
592                                 if (!adev->rings[i] || !target_seq[i])
593                                         continue;
594
595                                 if (amdgpu_ring_is_lockup(adev->rings[i]))
596                                         break;
597                         }
598
599                         if (i < AMDGPU_MAX_RINGS) {
600                                 /* good news we believe it's a lockup */
601                                 dev_warn(adev->dev, "GPU lockup (waiting for "
602                                          "0x%016llx last fence id 0x%016llx on"
603                                          " ring %d)\n",
604                                          target_seq[i], last_seq[i], i);
605
606                                 /* remember that we need an reset */
607                                 adev->needs_reset = true;
608                                 wake_up_all(&adev->fence_queue);
609                                 return -EDEADLK;
610                         }
611
612                         if (timeout < MAX_SCHEDULE_TIMEOUT) {
613                                 timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
614                                 if (timeout <= 0) {
615                                         return 0;
616                                 }
617                         }
618                 }
619         }
620         return timeout;
621 }
622
623 /**
624  * amdgpu_fence_wait - wait for a fence to signal
625  *
626  * @fence: amdgpu fence object
627  * @intr: use interruptable sleep
628  *
629  * Wait for the requested fence to signal (all asics).
630  * @intr selects whether to use interruptable (true) or non-interruptable
631  * (false) sleep when waiting for the fence.
632  * Returns 0 if the fence has passed, error for all other cases.
633  */
634 int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr)
635 {
636         uint64_t seq[AMDGPU_MAX_RINGS] = {};
637         long r;
638
639         seq[fence->ring->idx] = fence->seq;
640         r = amdgpu_fence_wait_seq_timeout(fence->ring->adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
641         if (r < 0) {
642                 return r;
643         }
644
645         r = fence_signal(&fence->base);
646         if (!r)
647                 FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
648         return 0;
649 }
650
651 /**
652  * amdgpu_fence_wait_any - wait for a fence to signal on any ring
653  *
654  * @adev: amdgpu device pointer
655  * @fences: amdgpu fence object(s)
656  * @intr: use interruptable sleep
657  *
658  * Wait for any requested fence to signal (all asics).  Fence
659  * array is indexed by ring id.  @intr selects whether to use
660  * interruptable (true) or non-interruptable (false) sleep when
661  * waiting for the fences. Used by the suballocator.
662  * Returns 0 if any fence has passed, error for all other cases.
663  */
664 int amdgpu_fence_wait_any(struct amdgpu_device *adev,
665                           struct amdgpu_fence **fences,
666                           bool intr)
667 {
668         uint64_t seq[AMDGPU_MAX_RINGS];
669         unsigned i, num_rings = 0;
670         long r;
671
672         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
673                 seq[i] = 0;
674
675                 if (!fences[i]) {
676                         continue;
677                 }
678
679                 seq[i] = fences[i]->seq;
680                 ++num_rings;
681         }
682
683         /* nothing to wait for ? */
684         if (num_rings == 0)
685                 return -ENOENT;
686
687         r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
688         if (r < 0) {
689                 return r;
690         }
691         return 0;
692 }
693
694 /**
695  * amdgpu_fence_wait_next - wait for the next fence to signal
696  *
697  * @adev: amdgpu device pointer
698  * @ring: ring index the fence is associated with
699  *
700  * Wait for the next fence on the requested ring to signal (all asics).
701  * Returns 0 if the next fence has passed, error for all other cases.
702  * Caller must hold ring lock.
703  */
704 int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
705 {
706         uint64_t seq[AMDGPU_MAX_RINGS] = {};
707         long r;
708
709         seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
710         if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) {
711                 /* nothing to wait for, last_seq is
712                    already the last emited fence */
713                 return -ENOENT;
714         }
715         r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT);
716         if (r < 0)
717                 return r;
718         return 0;
719 }
720
721 /**
722  * amdgpu_fence_wait_empty - wait for all fences to signal
723  *
724  * @adev: amdgpu device pointer
725  * @ring: ring index the fence is associated with
726  *
727  * Wait for all fences on the requested ring to signal (all asics).
728  * Returns 0 if the fences have passed, error for all other cases.
729  * Caller must hold ring lock.
730  */
731 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
732 {
733         struct amdgpu_device *adev = ring->adev;
734         uint64_t seq[AMDGPU_MAX_RINGS] = {};
735         long r;
736
737         seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx];
738         if (!seq[ring->idx])
739                 return 0;
740
741         r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT);
742         if (r < 0) {
743                 if (r == -EDEADLK)
744                         return -EDEADLK;
745
746                 dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
747                         ring->idx, r);
748         }
749         return 0;
750 }
751
752 /**
753  * amdgpu_fence_ref - take a ref on a fence
754  *
755  * @fence: amdgpu fence object
756  *
757  * Take a reference on a fence (all asics).
758  * Returns the fence.
759  */
760 struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
761 {
762         fence_get(&fence->base);
763         return fence;
764 }
765
766 /**
767  * amdgpu_fence_unref - remove a ref on a fence
768  *
769  * @fence: amdgpu fence object
770  *
771  * Remove a reference on a fence (all asics).
772  */
773 void amdgpu_fence_unref(struct amdgpu_fence **fence)
774 {
775         struct amdgpu_fence *tmp = *fence;
776
777         *fence = NULL;
778         if (tmp)
779                 fence_put(&tmp->base);
780 }
781
782 /**
783  * amdgpu_fence_count_emitted - get the count of emitted fences
784  *
785  * @ring: ring the fence is associated with
786  *
787  * Get the number of fences emitted on the requested ring (all asics).
788  * Returns the number of emitted fences on the ring.  Used by the
789  * dynpm code to ring track activity.
790  */
791 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
792 {
793         uint64_t emitted;
794
795         /* We are not protected by ring lock when reading the last sequence
796          * but it's ok to report slightly wrong fence count here.
797          */
798         amdgpu_fence_process(ring);
799         emitted = ring->fence_drv.sync_seq[ring->idx]
800                 - atomic64_read(&ring->fence_drv.last_seq);
801         /* to avoid 32bits warp around */
802         if (emitted > 0x10000000)
803                 emitted = 0x10000000;
804
805         return (unsigned)emitted;
806 }
807
808 /**
809  * amdgpu_fence_need_sync - do we need a semaphore
810  *
811  * @fence: amdgpu fence object
812  * @dst_ring: which ring to check against
813  *
814  * Check if the fence needs to be synced against another ring
815  * (all asics).  If so, we need to emit a semaphore.
816  * Returns true if we need to sync with another ring, false if
817  * not.
818  */
819 bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
820                             struct amdgpu_ring *dst_ring)
821 {
822         struct amdgpu_fence_driver *fdrv;
823
824         if (!fence)
825                 return false;
826
827         if (fence->ring == dst_ring)
828                 return false;
829
830         /* we are protected by the ring mutex */
831         fdrv = &dst_ring->fence_drv;
832         if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
833                 return false;
834
835         return true;
836 }
837
838 /**
839  * amdgpu_fence_note_sync - record the sync point
840  *
841  * @fence: amdgpu fence object
842  * @dst_ring: which ring to check against
843  *
844  * Note the sequence number at which point the fence will
845  * be synced with the requested ring (all asics).
846  */
847 void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
848                             struct amdgpu_ring *dst_ring)
849 {
850         struct amdgpu_fence_driver *dst, *src;
851         unsigned i;
852
853         if (!fence)
854                 return;
855
856         if (fence->ring == dst_ring)
857                 return;
858
859         /* we are protected by the ring mutex */
860         src = &fence->ring->fence_drv;
861         dst = &dst_ring->fence_drv;
862         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
863                 if (i == dst_ring->idx)
864                         continue;
865
866                 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
867         }
868 }
869
870 /**
871  * amdgpu_fence_driver_start_ring - make the fence driver
872  * ready for use on the requested ring.
873  *
874  * @ring: ring to start the fence driver on
875  * @irq_src: interrupt source to use for this ring
876  * @irq_type: interrupt type to use for this ring
877  *
878  * Make the fence driver ready for processing (all asics).
879  * Not all asics have all rings, so each asic will only
880  * start the fence driver on the rings it has.
881  * Returns 0 for success, errors for failure.
882  */
883 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
884                                    struct amdgpu_irq_src *irq_src,
885                                    unsigned irq_type)
886 {
887         struct amdgpu_device *adev = ring->adev;
888         uint64_t index;
889
890         if (ring != &adev->uvd.ring) {
891                 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
892                 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
893         } else {
894                 /* put fence directly behind firmware */
895                 index = ALIGN(adev->uvd.fw->size, 8);
896                 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
897                 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
898         }
899         amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
900         ring->fence_drv.initialized = true;
901         ring->fence_drv.irq_src = irq_src;
902         ring->fence_drv.irq_type = irq_type;
903         dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
904                  "cpu addr 0x%p\n", ring->idx,
905                  ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
906         return 0;
907 }
908
909 /**
910  * amdgpu_fence_driver_init_ring - init the fence driver
911  * for the requested ring.
912  *
913  * @ring: ring to init the fence driver on
914  *
915  * Init the fence driver for the requested ring (all asics).
916  * Helper function for amdgpu_fence_driver_init().
917  */
918 void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
919 {
920         int i;
921
922         ring->fence_drv.cpu_addr = NULL;
923         ring->fence_drv.gpu_addr = 0;
924         for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
925                 ring->fence_drv.sync_seq[i] = 0;
926
927         atomic64_set(&ring->fence_drv.last_seq, 0);
928         ring->fence_drv.initialized = false;
929
930         INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
931                         amdgpu_fence_check_lockup);
932         ring->fence_drv.ring = ring;
933 }
934
935 /**
936  * amdgpu_fence_driver_init - init the fence driver
937  * for all possible rings.
938  *
939  * @adev: amdgpu device pointer
940  *
941  * Init the fence driver for all possible rings (all asics).
942  * Not all asics have all rings, so each asic will only
943  * start the fence driver on the rings it has using
944  * amdgpu_fence_driver_start_ring().
945  * Returns 0 for success.
946  */
947 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
948 {
949         init_waitqueue_head(&adev->fence_queue);
950         if (amdgpu_debugfs_fence_init(adev))
951                 dev_err(adev->dev, "fence debugfs file creation failed\n");
952
953         return 0;
954 }
955
956 /**
957  * amdgpu_fence_driver_fini - tear down the fence driver
958  * for all possible rings.
959  *
960  * @adev: amdgpu device pointer
961  *
962  * Tear down the fence driver for all possible rings (all asics).
963  */
964 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
965 {
966         int i, r;
967
968         mutex_lock(&adev->ring_lock);
969         for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
970                 struct amdgpu_ring *ring = adev->rings[i];
971                 if (!ring || !ring->fence_drv.initialized)
972                         continue;
973                 r = amdgpu_fence_wait_empty(ring);
974                 if (r) {
975                         /* no need to trigger GPU reset as we are unloading */
976                         amdgpu_fence_driver_force_completion(adev);
977                 }
978                 wake_up_all(&adev->fence_queue);
979                 ring->fence_drv.initialized = false;
980         }
981         mutex_unlock(&adev->ring_lock);
982 }
983
984 /**
985  * amdgpu_fence_driver_force_completion - force all fence waiter to complete
986  *
987  * @adev: amdgpu device pointer
988  *
989  * In case of GPU reset failure make sure no process keep waiting on fence
990  * that will never complete.
991  */
992 void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
993 {
994         int i;
995
996         for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
997                 struct amdgpu_ring *ring = adev->rings[i];
998                 if (!ring || !ring->fence_drv.initialized)
999                         continue;
1000
1001                 amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
1002         }
1003 }
1004
1005
1006 /*
1007  * Fence debugfs
1008  */
1009 #if defined(CONFIG_DEBUG_FS)
1010 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
1011 {
1012         struct drm_info_node *node = (struct drm_info_node *)m->private;
1013         struct drm_device *dev = node->minor->dev;
1014         struct amdgpu_device *adev = dev->dev_private;
1015         int i, j;
1016
1017         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1018                 struct amdgpu_ring *ring = adev->rings[i];
1019                 if (!ring || !ring->fence_drv.initialized)
1020                         continue;
1021
1022                 amdgpu_fence_process(ring);
1023
1024                 seq_printf(m, "--- ring %d ---\n", i);
1025                 seq_printf(m, "Last signaled fence 0x%016llx\n",
1026                            (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
1027                 seq_printf(m, "Last emitted        0x%016llx\n",
1028                            ring->fence_drv.sync_seq[i]);
1029
1030                 for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
1031                         struct amdgpu_ring *other = adev->rings[j];
1032                         if (i != j && other && other->fence_drv.initialized)
1033                                 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
1034                                            j, ring->fence_drv.sync_seq[j]);
1035                 }
1036         }
1037         return 0;
1038 }
1039
1040 static struct drm_info_list amdgpu_debugfs_fence_list[] = {
1041         {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
1042 };
1043 #endif
1044
1045 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
1046 {
1047 #if defined(CONFIG_DEBUG_FS)
1048         return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
1049 #else
1050         return 0;
1051 #endif
1052 }
1053
1054 static const char *amdgpu_fence_get_driver_name(struct fence *fence)
1055 {
1056         return "amdgpu";
1057 }
1058
1059 static const char *amdgpu_fence_get_timeline_name(struct fence *f)
1060 {
1061         struct amdgpu_fence *fence = to_amdgpu_fence(f);
1062         return (const char *)fence->ring->name;
1063 }
1064
1065 static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
1066 {
1067         return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
1068 }
1069
1070 struct amdgpu_wait_cb {
1071         struct fence_cb base;
1072         struct task_struct *task;
1073 };
1074
1075 static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
1076 {
1077         struct amdgpu_wait_cb *wait =
1078                 container_of(cb, struct amdgpu_wait_cb, base);
1079         wake_up_process(wait->task);
1080 }
1081
1082 static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
1083                                              signed long t)
1084 {
1085         struct amdgpu_fence *fence = to_amdgpu_fence(f);
1086         struct amdgpu_device *adev = fence->ring->adev;
1087         struct amdgpu_wait_cb cb;
1088
1089         cb.task = current;
1090
1091         if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb))
1092                 return t;
1093
1094         while (t > 0) {
1095                 if (intr)
1096                         set_current_state(TASK_INTERRUPTIBLE);
1097                 else
1098                         set_current_state(TASK_UNINTERRUPTIBLE);
1099
1100                 /*
1101                  * amdgpu_test_signaled must be called after
1102                  * set_current_state to prevent a race with wake_up_process
1103                  */
1104                 if (amdgpu_test_signaled(fence))
1105                         break;
1106
1107                 if (adev->needs_reset) {
1108                         t = -EDEADLK;
1109                         break;
1110                 }
1111
1112                 t = schedule_timeout(t);
1113
1114                 if (t > 0 && intr && signal_pending(current))
1115                         t = -ERESTARTSYS;
1116         }
1117
1118         __set_current_state(TASK_RUNNING);
1119         fence_remove_callback(f, &cb.base);
1120
1121         return t;
1122 }
1123
1124 const struct fence_ops amdgpu_fence_ops = {
1125         .get_driver_name = amdgpu_fence_get_driver_name,
1126         .get_timeline_name = amdgpu_fence_get_timeline_name,
1127         .enable_signaling = amdgpu_fence_enable_signaling,
1128         .signaled = amdgpu_fence_is_signaled,
1129         .wait = amdgpu_fence_default_wait,
1130         .release = NULL,
1131 };
This page took 0.103648 seconds and 4 git commands to generate.