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