xref: /dragonfly/sys/dev/drm/radeon/radeon_semaphore.c (revision d78d3a2272f5ecf9e0b570e362128240417a1b85)
1 /*
2  * Copyright 2011 Christian König.
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  *    Christian König <deathsimple@vodafone.de>
29  */
30 #include <drm/drmP.h>
31 #include "radeon.h"
32 #ifdef TRACE_TODO
33 #include "radeon_trace.h"
34 #endif
35 
radeon_semaphore_create(struct radeon_device * rdev,struct radeon_semaphore ** semaphore)36 int radeon_semaphore_create(struct radeon_device *rdev,
37                                   struct radeon_semaphore **semaphore)
38 {
39           int r;
40 
41           *semaphore = kmalloc(sizeof(struct radeon_semaphore), M_DRM, GFP_KERNEL);
42           if (*semaphore == NULL) {
43                     return -ENOMEM;
44           }
45           r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo,
46                                    &(*semaphore)->sa_bo, 8, 8);
47           if (r) {
48                     kfree(*semaphore);
49                     *semaphore = NULL;
50                     return r;
51           }
52           (*semaphore)->waiters = 0;
53           (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
54 
55           *((uint64_t *)radeon_sa_bo_cpu_addr((*semaphore)->sa_bo)) = 0;
56 
57           return 0;
58 }
59 
radeon_semaphore_emit_signal(struct radeon_device * rdev,int ridx,struct radeon_semaphore * semaphore)60 bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx,
61                                           struct radeon_semaphore *semaphore)
62 {
63           struct radeon_ring *ring = &rdev->ring[ridx];
64 
65 #ifdef TRACE_TODO
66           trace_radeon_semaphore_signale(ridx, semaphore);
67 #endif
68 
69           if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) {
70                     --semaphore->waiters;
71 
72                     /* for debugging lockup only, used by sysfs debug files */
73                     ring->last_semaphore_signal_addr = semaphore->gpu_addr;
74                     return true;
75           }
76           return false;
77 }
78 
radeon_semaphore_emit_wait(struct radeon_device * rdev,int ridx,struct radeon_semaphore * semaphore)79 bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx,
80                                         struct radeon_semaphore *semaphore)
81 {
82           struct radeon_ring *ring = &rdev->ring[ridx];
83 
84 #ifdef TRACE_TODO
85           trace_radeon_semaphore_wait(ridx, semaphore);
86 #endif
87 
88           if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) {
89                     ++semaphore->waiters;
90 
91                     /* for debugging lockup only, used by sysfs debug files */
92                     ring->last_semaphore_wait_addr = semaphore->gpu_addr;
93                     return true;
94           }
95           return false;
96 }
97 
radeon_semaphore_free(struct radeon_device * rdev,struct radeon_semaphore ** semaphore,struct radeon_fence * fence)98 void radeon_semaphore_free(struct radeon_device *rdev,
99                                  struct radeon_semaphore **semaphore,
100                                  struct radeon_fence *fence)
101 {
102           if (semaphore == NULL || *semaphore == NULL) {
103                     return;
104           }
105           if ((*semaphore)->waiters > 0) {
106                     dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
107                               " hardware lockup imminent!\n", *semaphore);
108           }
109           radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
110           kfree(*semaphore);
111           *semaphore = NULL;
112 }
113