1 /*	$NetBSD: radeon_sync.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2014 Advanced Micro Devices, Inc.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * The above copyright notice and this permission notice (including the
24  * next paragraph) shall be included in all copies or substantial portions
25  * of the Software.
26  *
27  */
28 /*
29  * Authors:
30  *    Christian König <christian.koenig@amd.com>
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: radeon_sync.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $");
35 
36 #include "radeon.h"
37 #include "radeon_trace.h"
38 
39 /**
40  * radeon_sync_create - zero init sync object
41  *
42  * @sync: sync object to initialize
43  *
44  * Just clear the sync object for now.
45  */
radeon_sync_create(struct radeon_sync * sync)46 void radeon_sync_create(struct radeon_sync *sync)
47 {
48 	unsigned i;
49 
50 	for (i = 0; i < RADEON_NUM_SYNCS; ++i)
51 		sync->semaphores[i] = NULL;
52 
53 	for (i = 0; i < RADEON_NUM_RINGS; ++i)
54 		sync->sync_to[i] = NULL;
55 
56 	sync->last_vm_update = NULL;
57 }
58 
59 /**
60  * radeon_sync_fence - use the semaphore to sync to a fence
61  *
62  * @sync: sync object to add fence to
63  * @fence: fence to sync to
64  *
65  * Sync to the fence using the semaphore objects
66  */
radeon_sync_fence(struct radeon_sync * sync,struct radeon_fence * fence)67 void radeon_sync_fence(struct radeon_sync *sync,
68 		       struct radeon_fence *fence)
69 {
70 	struct radeon_fence *other;
71 
72 	if (!fence)
73 		return;
74 
75 	other = sync->sync_to[fence->ring];
76 	sync->sync_to[fence->ring] = radeon_fence_later(fence, other);
77 
78 	if (fence->is_vm_update) {
79 		other = sync->last_vm_update;
80 		sync->last_vm_update = radeon_fence_later(fence, other);
81 	}
82 }
83 
84 /**
85  * radeon_sync_resv - use the semaphores to sync to a reservation object
86  *
87  * @sync: sync object to add fences from reservation object to
88  * @resv: reservation object with embedded fence
89  * @shared: true if we should only sync to the exclusive fence
90  *
91  * Sync to the fence using the semaphore objects
92  */
radeon_sync_resv(struct radeon_device * rdev,struct radeon_sync * sync,struct dma_resv * resv,bool shared)93 int radeon_sync_resv(struct radeon_device *rdev,
94 		     struct radeon_sync *sync,
95 		     struct dma_resv *resv,
96 		     bool shared)
97 {
98 	struct dma_resv_list *flist;
99 	struct dma_fence *f;
100 	struct radeon_fence *fence;
101 	unsigned i;
102 	int r = 0;
103 
104 	/* always sync to the exclusive fence */
105 	f = dma_resv_get_excl(resv);
106 	fence = f ? to_radeon_fence(f) : NULL;
107 	if (fence && fence->rdev == rdev)
108 		radeon_sync_fence(sync, fence);
109 	else if (f)
110 		r = dma_fence_wait(f, true);
111 
112 	flist = dma_resv_get_list(resv);
113 	if (shared || !flist || r)
114 		return r;
115 
116 	for (i = 0; i < flist->shared_count; ++i) {
117 		f = rcu_dereference_protected(flist->shared[i],
118 					      dma_resv_held(resv));
119 		fence = to_radeon_fence(f);
120 		if (fence && fence->rdev == rdev)
121 			radeon_sync_fence(sync, fence);
122 		else
123 			r = dma_fence_wait(f, true);
124 
125 		if (r)
126 			break;
127 	}
128 	return r;
129 }
130 
131 /**
132  * radeon_sync_rings - sync ring to all registered fences
133  *
134  * @rdev: radeon_device pointer
135  * @sync: sync object to use
136  * @ring: ring that needs sync
137  *
138  * Ensure that all registered fences are signaled before letting
139  * the ring continue. The caller must hold the ring lock.
140  */
radeon_sync_rings(struct radeon_device * rdev,struct radeon_sync * sync,int ring)141 int radeon_sync_rings(struct radeon_device *rdev,
142 		      struct radeon_sync *sync,
143 		      int ring)
144 {
145 	unsigned count = 0;
146 	int i, r;
147 
148 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
149 		struct radeon_fence *fence = sync->sync_to[i];
150 		struct radeon_semaphore *semaphore;
151 
152 		/* check if we really need to sync */
153 		if (!radeon_fence_need_sync(fence, ring))
154 			continue;
155 
156 		/* prevent GPU deadlocks */
157 		if (!rdev->ring[i].ready) {
158 			dev_err(rdev->dev, "Syncing to a disabled ring!");
159 			return -EINVAL;
160 		}
161 
162 		if (count >= RADEON_NUM_SYNCS) {
163 			/* not enough room, wait manually */
164 			r = radeon_fence_wait(fence, false);
165 			if (r)
166 				return r;
167 			continue;
168 		}
169 		r = radeon_semaphore_create(rdev, &semaphore);
170 		if (r)
171 			return r;
172 
173 		sync->semaphores[count++] = semaphore;
174 
175 		/* allocate enough space for sync command */
176 		r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
177 		if (r)
178 			return r;
179 
180 		/* emit the signal semaphore */
181 		if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) {
182 			/* signaling wasn't successful wait manually */
183 			radeon_ring_undo(&rdev->ring[i]);
184 			r = radeon_fence_wait(fence, false);
185 			if (r)
186 				return r;
187 			continue;
188 		}
189 
190 		/* we assume caller has already allocated space on waiters ring */
191 		if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
192 			/* waiting wasn't successful wait manually */
193 			radeon_ring_undo(&rdev->ring[i]);
194 			r = radeon_fence_wait(fence, false);
195 			if (r)
196 				return r;
197 			continue;
198 		}
199 
200 		radeon_ring_commit(rdev, &rdev->ring[i], false);
201 		radeon_fence_note_sync(fence, ring);
202 	}
203 
204 	return 0;
205 }
206 
207 /**
208  * radeon_sync_free - free the sync object
209  *
210  * @rdev: radeon_device pointer
211  * @sync: sync object to use
212  * @fence: fence to use for the free
213  *
214  * Free the sync object by freeing all semaphores in it.
215  */
radeon_sync_free(struct radeon_device * rdev,struct radeon_sync * sync,struct radeon_fence * fence)216 void radeon_sync_free(struct radeon_device *rdev,
217 		      struct radeon_sync *sync,
218 		      struct radeon_fence *fence)
219 {
220 	unsigned i;
221 
222 	for (i = 0; i < RADEON_NUM_SYNCS; ++i)
223 		radeon_semaphore_free(rdev, &sync->semaphores[i], fence);
224 }
225