Commit | Line | Data |
---|---|---|
15d3332f CK |
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 "drmP.h" | |
31 | #include "drm.h" | |
32 | #include "radeon.h" | |
33 | ||
c1341e52 | 34 | static int radeon_semaphore_add_bo(struct radeon_device *rdev) |
15d3332f | 35 | { |
c1341e52 | 36 | struct radeon_semaphore_bo *bo; |
15d3332f CK |
37 | unsigned long irq_flags; |
38 | uint64_t gpu_addr; | |
c1341e52 JG |
39 | uint32_t *cpu_ptr; |
40 | int r, i; | |
15d3332f | 41 | |
c1341e52 JG |
42 | bo = kmalloc(sizeof(struct radeon_semaphore_bo), GFP_KERNEL); |
43 | if (bo == NULL) { | |
44 | return -ENOMEM; | |
15d3332f | 45 | } |
c1341e52 JG |
46 | INIT_LIST_HEAD(&bo->free); |
47 | INIT_LIST_HEAD(&bo->list); | |
48 | bo->nused = 0; | |
15d3332f | 49 | |
c1341e52 | 50 | r = radeon_ib_get(rdev, 0, &bo->ib, RADEON_SEMAPHORE_BO_SIZE); |
15d3332f | 51 | if (r) { |
c1341e52 JG |
52 | dev_err(rdev->dev, "failed to get a bo after 5 retry\n"); |
53 | kfree(bo); | |
15d3332f CK |
54 | return r; |
55 | } | |
2e0d9910 CK |
56 | gpu_addr = radeon_sa_bo_gpu_addr(bo->ib->sa_bo); |
57 | cpu_ptr = radeon_sa_bo_cpu_addr(bo->ib->sa_bo); | |
c1341e52 JG |
58 | for (i = 0; i < (RADEON_SEMAPHORE_BO_SIZE/8); i++) { |
59 | bo->semaphores[i].gpu_addr = gpu_addr; | |
60 | bo->semaphores[i].cpu_ptr = cpu_ptr; | |
61 | bo->semaphores[i].bo = bo; | |
62 | list_add_tail(&bo->semaphores[i].list, &bo->free); | |
15d3332f | 63 | gpu_addr += 8; |
c1341e52 | 64 | cpu_ptr += 2; |
15d3332f | 65 | } |
15d3332f | 66 | write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags); |
c1341e52 | 67 | list_add_tail(&bo->list, &rdev->semaphore_drv.bo); |
15d3332f | 68 | write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags); |
c1341e52 JG |
69 | return 0; |
70 | } | |
15d3332f | 71 | |
c1341e52 JG |
72 | static void radeon_semaphore_del_bo_locked(struct radeon_device *rdev, |
73 | struct radeon_semaphore_bo *bo) | |
74 | { | |
557017a0 | 75 | radeon_sa_bo_free(rdev, &bo->ib->sa_bo, NULL); |
c1341e52 JG |
76 | radeon_fence_unref(&bo->ib->fence); |
77 | list_del(&bo->list); | |
78 | kfree(bo); | |
79 | } | |
15d3332f | 80 | |
c1341e52 JG |
81 | void radeon_semaphore_shrink_locked(struct radeon_device *rdev) |
82 | { | |
83 | struct radeon_semaphore_bo *bo, *n; | |
84 | ||
85 | if (list_empty(&rdev->semaphore_drv.bo)) { | |
86 | return; | |
87 | } | |
88 | /* only shrink if first bo has free semaphore */ | |
89 | bo = list_first_entry(&rdev->semaphore_drv.bo, struct radeon_semaphore_bo, list); | |
90 | if (list_empty(&bo->free)) { | |
91 | return; | |
92 | } | |
93 | list_for_each_entry_safe_continue(bo, n, &rdev->semaphore_drv.bo, list) { | |
94 | if (bo->nused) | |
95 | continue; | |
96 | radeon_semaphore_del_bo_locked(rdev, bo); | |
97 | } | |
15d3332f CK |
98 | } |
99 | ||
100 | int radeon_semaphore_create(struct radeon_device *rdev, | |
101 | struct radeon_semaphore **semaphore) | |
102 | { | |
c1341e52 | 103 | struct radeon_semaphore_bo *bo; |
15d3332f | 104 | unsigned long irq_flags; |
c1341e52 JG |
105 | bool do_retry = true; |
106 | int r; | |
15d3332f | 107 | |
c1341e52 JG |
108 | retry: |
109 | *semaphore = NULL; | |
15d3332f | 110 | write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags); |
c1341e52 JG |
111 | list_for_each_entry(bo, &rdev->semaphore_drv.bo, list) { |
112 | if (list_empty(&bo->free)) | |
113 | continue; | |
114 | *semaphore = list_first_entry(&bo->free, struct radeon_semaphore, list); | |
115 | (*semaphore)->cpu_ptr[0] = 0; | |
116 | (*semaphore)->cpu_ptr[1] = 0; | |
117 | list_del(&(*semaphore)->list); | |
118 | bo->nused++; | |
119 | break; | |
15d3332f | 120 | } |
c1341e52 | 121 | write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags); |
15d3332f | 122 | |
c1341e52 JG |
123 | if (*semaphore == NULL) { |
124 | if (do_retry) { | |
125 | do_retry = false; | |
126 | r = radeon_semaphore_add_bo(rdev); | |
127 | if (r) | |
128 | return r; | |
129 | goto retry; | |
130 | } | |
131 | return -ENOMEM; | |
132 | } | |
15d3332f | 133 | |
15d3332f CK |
134 | return 0; |
135 | } | |
136 | ||
137 | void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring, | |
138 | struct radeon_semaphore *semaphore) | |
139 | { | |
e32eb50d | 140 | radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, false); |
15d3332f CK |
141 | } |
142 | ||
143 | void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring, | |
144 | struct radeon_semaphore *semaphore) | |
145 | { | |
e32eb50d | 146 | radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true); |
15d3332f CK |
147 | } |
148 | ||
8f676c4c CK |
149 | int radeon_semaphore_sync_rings(struct radeon_device *rdev, |
150 | struct radeon_semaphore *semaphore, | |
151 | bool sync_to[RADEON_NUM_RINGS], | |
152 | int dst_ring) | |
153 | { | |
d6999bc7 | 154 | int i = 0, r; |
8f676c4c | 155 | |
d6999bc7 CK |
156 | mutex_lock(&rdev->ring_lock); |
157 | r = radeon_ring_alloc(rdev, &rdev->ring[dst_ring], RADEON_NUM_RINGS * 8); | |
158 | if (r) { | |
159 | goto error; | |
160 | } | |
8f676c4c | 161 | |
d6999bc7 CK |
162 | for (i = 0; i < RADEON_NUM_RINGS; ++i) { |
163 | /* no need to sync to our own or unused rings */ | |
164 | if (!sync_to[i] || i == dst_ring) | |
8f676c4c CK |
165 | continue; |
166 | ||
167 | /* prevent GPU deadlocks */ | |
168 | if (!rdev->ring[i].ready) { | |
169 | dev_err(rdev->dev, "Trying to sync to a disabled ring!"); | |
170 | r = -EINVAL; | |
171 | goto error; | |
172 | } | |
173 | ||
d6999bc7 CK |
174 | r = radeon_ring_alloc(rdev, &rdev->ring[i], 8); |
175 | if (r) { | |
8f676c4c | 176 | goto error; |
d6999bc7 | 177 | } |
8f676c4c CK |
178 | |
179 | radeon_semaphore_emit_signal(rdev, i, semaphore); | |
180 | radeon_semaphore_emit_wait(rdev, dst_ring, semaphore); | |
8f676c4c | 181 | |
d6999bc7 | 182 | radeon_ring_commit(rdev, &rdev->ring[i]); |
8f676c4c CK |
183 | } |
184 | ||
d6999bc7 CK |
185 | radeon_ring_commit(rdev, &rdev->ring[dst_ring]); |
186 | mutex_unlock(&rdev->ring_lock); | |
187 | ||
8f676c4c CK |
188 | return 0; |
189 | ||
190 | error: | |
191 | /* unlock all locks taken so far */ | |
192 | for (--i; i >= 0; --i) { | |
193 | if (sync_to[i] || i == dst_ring) { | |
d6999bc7 | 194 | radeon_ring_undo(&rdev->ring[i]); |
8f676c4c CK |
195 | } |
196 | } | |
d6999bc7 CK |
197 | radeon_ring_undo(&rdev->ring[dst_ring]); |
198 | mutex_unlock(&rdev->ring_lock); | |
8f676c4c CK |
199 | return r; |
200 | } | |
201 | ||
15d3332f | 202 | void radeon_semaphore_free(struct radeon_device *rdev, |
c1341e52 | 203 | struct radeon_semaphore *semaphore) |
15d3332f CK |
204 | { |
205 | unsigned long irq_flags; | |
206 | ||
207 | write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags); | |
c1341e52 JG |
208 | semaphore->bo->nused--; |
209 | list_add_tail(&semaphore->list, &semaphore->bo->free); | |
210 | radeon_semaphore_shrink_locked(rdev); | |
15d3332f CK |
211 | write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags); |
212 | } | |
213 | ||
214 | void radeon_semaphore_driver_fini(struct radeon_device *rdev) | |
215 | { | |
c1341e52 | 216 | struct radeon_semaphore_bo *bo, *n; |
15d3332f CK |
217 | unsigned long irq_flags; |
218 | ||
15d3332f | 219 | write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags); |
c1341e52 JG |
220 | /* we force to free everything */ |
221 | list_for_each_entry_safe(bo, n, &rdev->semaphore_drv.bo, list) { | |
222 | if (!list_empty(&bo->free)) { | |
223 | dev_err(rdev->dev, "still in use semaphore\n"); | |
224 | } | |
225 | radeon_semaphore_del_bo_locked(rdev, bo); | |
15d3332f | 226 | } |
15d3332f | 227 | write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags); |
15d3332f | 228 | } |