xref: /dragonfly/sys/dev/drm/radeon/radeon_sync.c (revision 6559babb)
17dcf36dcSFrançois Tigeot /*
27dcf36dcSFrançois Tigeot  * Copyright 2014 Advanced Micro Devices, Inc.
37dcf36dcSFrançois Tigeot  * All Rights Reserved.
47dcf36dcSFrançois Tigeot  *
57dcf36dcSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
67dcf36dcSFrançois Tigeot  * copy of this software and associated documentation files (the
77dcf36dcSFrançois Tigeot  * "Software"), to deal in the Software without restriction, including
87dcf36dcSFrançois Tigeot  * without limitation the rights to use, copy, modify, merge, publish,
97dcf36dcSFrançois Tigeot  * distribute, sub license, and/or sell copies of the Software, and to
107dcf36dcSFrançois Tigeot  * permit persons to whom the Software is furnished to do so, subject to
117dcf36dcSFrançois Tigeot  * the following conditions:
127dcf36dcSFrançois Tigeot  *
137dcf36dcSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
147dcf36dcSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
157dcf36dcSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
167dcf36dcSFrançois Tigeot  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
177dcf36dcSFrançois Tigeot  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
187dcf36dcSFrançois Tigeot  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
197dcf36dcSFrançois Tigeot  * USE OR OTHER DEALINGS IN THE SOFTWARE.
207dcf36dcSFrançois Tigeot  *
217dcf36dcSFrançois Tigeot  * The above copyright notice and this permission notice (including the
227dcf36dcSFrançois Tigeot  * next paragraph) shall be included in all copies or substantial portions
237dcf36dcSFrançois Tigeot  * of the Software.
247dcf36dcSFrançois Tigeot  *
257dcf36dcSFrançois Tigeot  */
267dcf36dcSFrançois Tigeot /*
277dcf36dcSFrançois Tigeot  * Authors:
287dcf36dcSFrançois Tigeot  *    Christian König <christian.koenig@amd.com>
297dcf36dcSFrançois Tigeot  */
307dcf36dcSFrançois Tigeot 
317dcf36dcSFrançois Tigeot #include <drm/drmP.h>
327dcf36dcSFrançois Tigeot #include "radeon.h"
337dcf36dcSFrançois Tigeot #include "radeon_trace.h"
347dcf36dcSFrançois Tigeot 
357dcf36dcSFrançois Tigeot /**
367dcf36dcSFrançois Tigeot  * radeon_sync_create - zero init sync object
377dcf36dcSFrançois Tigeot  *
387dcf36dcSFrançois Tigeot  * @sync: sync object to initialize
397dcf36dcSFrançois Tigeot  *
407dcf36dcSFrançois Tigeot  * Just clear the sync object for now.
417dcf36dcSFrançois Tigeot  */
radeon_sync_create(struct radeon_sync * sync)427dcf36dcSFrançois Tigeot void radeon_sync_create(struct radeon_sync *sync)
437dcf36dcSFrançois Tigeot {
447dcf36dcSFrançois Tigeot 	unsigned i;
457dcf36dcSFrançois Tigeot 
467dcf36dcSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_SYNCS; ++i)
477dcf36dcSFrançois Tigeot 		sync->semaphores[i] = NULL;
487dcf36dcSFrançois Tigeot 
497dcf36dcSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i)
507dcf36dcSFrançois Tigeot 		sync->sync_to[i] = NULL;
517dcf36dcSFrançois Tigeot 
527dcf36dcSFrançois Tigeot 	sync->last_vm_update = NULL;
537dcf36dcSFrançois Tigeot }
547dcf36dcSFrançois Tigeot 
557dcf36dcSFrançois Tigeot /**
567dcf36dcSFrançois Tigeot  * radeon_sync_fence - use the semaphore to sync to a fence
577dcf36dcSFrançois Tigeot  *
587dcf36dcSFrançois Tigeot  * @sync: sync object to add fence to
597dcf36dcSFrançois Tigeot  * @fence: fence to sync to
607dcf36dcSFrançois Tigeot  *
617dcf36dcSFrançois Tigeot  * Sync to the fence using the semaphore objects
627dcf36dcSFrançois Tigeot  */
radeon_sync_fence(struct radeon_sync * sync,struct radeon_fence * fence)637dcf36dcSFrançois Tigeot void radeon_sync_fence(struct radeon_sync *sync,
647dcf36dcSFrançois Tigeot 		       struct radeon_fence *fence)
657dcf36dcSFrançois Tigeot {
667dcf36dcSFrançois Tigeot 	struct radeon_fence *other;
677dcf36dcSFrançois Tigeot 
687dcf36dcSFrançois Tigeot 	if (!fence)
697dcf36dcSFrançois Tigeot 		return;
707dcf36dcSFrançois Tigeot 
717dcf36dcSFrançois Tigeot 	other = sync->sync_to[fence->ring];
727dcf36dcSFrançois Tigeot 	sync->sync_to[fence->ring] = radeon_fence_later(fence, other);
737dcf36dcSFrançois Tigeot 
747dcf36dcSFrançois Tigeot 	if (fence->is_vm_update) {
757dcf36dcSFrançois Tigeot 		other = sync->last_vm_update;
767dcf36dcSFrançois Tigeot 		sync->last_vm_update = radeon_fence_later(fence, other);
777dcf36dcSFrançois Tigeot 	}
787dcf36dcSFrançois Tigeot }
797dcf36dcSFrançois Tigeot 
807dcf36dcSFrançois Tigeot /**
817dcf36dcSFrançois Tigeot  * radeon_sync_resv - use the semaphores to sync to a reservation object
827dcf36dcSFrançois Tigeot  *
837dcf36dcSFrançois Tigeot  * @sync: sync object to add fences from reservation object to
847dcf36dcSFrançois Tigeot  * @resv: reservation object with embedded fence
857dcf36dcSFrançois Tigeot  * @shared: true if we should only sync to the exclusive fence
867dcf36dcSFrançois Tigeot  *
877dcf36dcSFrançois Tigeot  * Sync to the fence using the semaphore objects
887dcf36dcSFrançois Tigeot  */
radeon_sync_resv(struct radeon_device * rdev,struct radeon_sync * sync,struct reservation_object * resv,bool shared)897dcf36dcSFrançois Tigeot int radeon_sync_resv(struct radeon_device *rdev,
907dcf36dcSFrançois Tigeot 		     struct radeon_sync *sync,
917dcf36dcSFrançois Tigeot 		     struct reservation_object *resv,
927dcf36dcSFrançois Tigeot 		     bool shared)
937dcf36dcSFrançois Tigeot {
947dcf36dcSFrançois Tigeot 	struct reservation_object_list *flist;
95*6559babbSFrançois Tigeot 	struct dma_fence *f;
967dcf36dcSFrançois Tigeot 	struct radeon_fence *fence;
977dcf36dcSFrançois Tigeot 	unsigned i;
987dcf36dcSFrançois Tigeot 	int r = 0;
997dcf36dcSFrançois Tigeot 
1007dcf36dcSFrançois Tigeot 	/* always sync to the exclusive fence */
1017dcf36dcSFrançois Tigeot 	f = reservation_object_get_excl(resv);
1027dcf36dcSFrançois Tigeot 	fence = f ? to_radeon_fence(f) : NULL;
1037dcf36dcSFrançois Tigeot 	if (fence && fence->rdev == rdev)
1047dcf36dcSFrançois Tigeot 		radeon_sync_fence(sync, fence);
1057dcf36dcSFrançois Tigeot 	else if (f)
106*6559babbSFrançois Tigeot 		r = dma_fence_wait(f, true);
1077dcf36dcSFrançois Tigeot 
1087dcf36dcSFrançois Tigeot 	flist = reservation_object_get_list(resv);
1097dcf36dcSFrançois Tigeot 	if (shared || !flist || r)
1107dcf36dcSFrançois Tigeot 		return r;
1117dcf36dcSFrançois Tigeot 
1127dcf36dcSFrançois Tigeot 	for (i = 0; i < flist->shared_count; ++i) {
1137dcf36dcSFrançois Tigeot 		f = rcu_dereference_protected(flist->shared[i],
1147dcf36dcSFrançois Tigeot 					      reservation_object_held(resv));
1157dcf36dcSFrançois Tigeot 		fence = to_radeon_fence(f);
1167dcf36dcSFrançois Tigeot 		if (fence && fence->rdev == rdev)
1177dcf36dcSFrançois Tigeot 			radeon_sync_fence(sync, fence);
1187dcf36dcSFrançois Tigeot 		else
119*6559babbSFrançois Tigeot 			r = dma_fence_wait(f, true);
1207dcf36dcSFrançois Tigeot 
1217dcf36dcSFrançois Tigeot 		if (r)
1227dcf36dcSFrançois Tigeot 			break;
1237dcf36dcSFrançois Tigeot 	}
1247dcf36dcSFrançois Tigeot 	return r;
1257dcf36dcSFrançois Tigeot }
1267dcf36dcSFrançois Tigeot 
1277dcf36dcSFrançois Tigeot /**
1287dcf36dcSFrançois Tigeot  * radeon_sync_rings - sync ring to all registered fences
1297dcf36dcSFrançois Tigeot  *
1307dcf36dcSFrançois Tigeot  * @rdev: radeon_device pointer
1317dcf36dcSFrançois Tigeot  * @sync: sync object to use
1327dcf36dcSFrançois Tigeot  * @ring: ring that needs sync
1337dcf36dcSFrançois Tigeot  *
1347dcf36dcSFrançois Tigeot  * Ensure that all registered fences are signaled before letting
1357dcf36dcSFrançois Tigeot  * the ring continue. The caller must hold the ring lock.
1367dcf36dcSFrançois Tigeot  */
radeon_sync_rings(struct radeon_device * rdev,struct radeon_sync * sync,int ring)1377dcf36dcSFrançois Tigeot int radeon_sync_rings(struct radeon_device *rdev,
1387dcf36dcSFrançois Tigeot 		      struct radeon_sync *sync,
1397dcf36dcSFrançois Tigeot 		      int ring)
1407dcf36dcSFrançois Tigeot {
1417dcf36dcSFrançois Tigeot 	unsigned count = 0;
1427dcf36dcSFrançois Tigeot 	int i, r;
1437dcf36dcSFrançois Tigeot 
1447dcf36dcSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
1457dcf36dcSFrançois Tigeot 		struct radeon_fence *fence = sync->sync_to[i];
1467dcf36dcSFrançois Tigeot 		struct radeon_semaphore *semaphore;
1477dcf36dcSFrançois Tigeot 
1487dcf36dcSFrançois Tigeot 		/* check if we really need to sync */
1497dcf36dcSFrançois Tigeot 		if (!radeon_fence_need_sync(fence, ring))
1507dcf36dcSFrançois Tigeot 			continue;
1517dcf36dcSFrançois Tigeot 
1527dcf36dcSFrançois Tigeot 		/* prevent GPU deadlocks */
1537dcf36dcSFrançois Tigeot 		if (!rdev->ring[i].ready) {
1547dcf36dcSFrançois Tigeot 			dev_err(rdev->dev, "Syncing to a disabled ring!");
1557dcf36dcSFrançois Tigeot 			return -EINVAL;
1567dcf36dcSFrançois Tigeot 		}
1577dcf36dcSFrançois Tigeot 
1587dcf36dcSFrançois Tigeot 		if (count >= RADEON_NUM_SYNCS) {
1597dcf36dcSFrançois Tigeot 			/* not enough room, wait manually */
1607dcf36dcSFrançois Tigeot 			r = radeon_fence_wait(fence, false);
1617dcf36dcSFrançois Tigeot 			if (r)
1627dcf36dcSFrançois Tigeot 				return r;
1637dcf36dcSFrançois Tigeot 			continue;
1647dcf36dcSFrançois Tigeot 		}
1657dcf36dcSFrançois Tigeot 		r = radeon_semaphore_create(rdev, &semaphore);
1667dcf36dcSFrançois Tigeot 		if (r)
1677dcf36dcSFrançois Tigeot 			return r;
1687dcf36dcSFrançois Tigeot 
1697dcf36dcSFrançois Tigeot 		sync->semaphores[count++] = semaphore;
1707dcf36dcSFrançois Tigeot 
1717dcf36dcSFrançois Tigeot 		/* allocate enough space for sync command */
1727dcf36dcSFrançois Tigeot 		r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
1737dcf36dcSFrançois Tigeot 		if (r)
1747dcf36dcSFrançois Tigeot 			return r;
1757dcf36dcSFrançois Tigeot 
1767dcf36dcSFrançois Tigeot 		/* emit the signal semaphore */
1777dcf36dcSFrançois Tigeot 		if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) {
1787dcf36dcSFrançois Tigeot 			/* signaling wasn't successful wait manually */
1797dcf36dcSFrançois Tigeot 			radeon_ring_undo(&rdev->ring[i]);
1807dcf36dcSFrançois Tigeot 			r = radeon_fence_wait(fence, false);
1817dcf36dcSFrançois Tigeot 			if (r)
1827dcf36dcSFrançois Tigeot 				return r;
1837dcf36dcSFrançois Tigeot 			continue;
1847dcf36dcSFrançois Tigeot 		}
1857dcf36dcSFrançois Tigeot 
1867dcf36dcSFrançois Tigeot 		/* we assume caller has already allocated space on waiters ring */
1877dcf36dcSFrançois Tigeot 		if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
1887dcf36dcSFrançois Tigeot 			/* waiting wasn't successful wait manually */
1897dcf36dcSFrançois Tigeot 			radeon_ring_undo(&rdev->ring[i]);
1907dcf36dcSFrançois Tigeot 			r = radeon_fence_wait(fence, false);
1917dcf36dcSFrançois Tigeot 			if (r)
1927dcf36dcSFrançois Tigeot 				return r;
1937dcf36dcSFrançois Tigeot 			continue;
1947dcf36dcSFrançois Tigeot 		}
1957dcf36dcSFrançois Tigeot 
1967dcf36dcSFrançois Tigeot 		radeon_ring_commit(rdev, &rdev->ring[i], false);
1977dcf36dcSFrançois Tigeot 		radeon_fence_note_sync(fence, ring);
1987dcf36dcSFrançois Tigeot 	}
1997dcf36dcSFrançois Tigeot 
2007dcf36dcSFrançois Tigeot 	return 0;
2017dcf36dcSFrançois Tigeot }
2027dcf36dcSFrançois Tigeot 
2037dcf36dcSFrançois Tigeot /**
2047dcf36dcSFrançois Tigeot  * radeon_sync_free - free the sync object
2057dcf36dcSFrançois Tigeot  *
2067dcf36dcSFrançois Tigeot  * @rdev: radeon_device pointer
2077dcf36dcSFrançois Tigeot  * @sync: sync object to use
2087dcf36dcSFrançois Tigeot  * @fence: fence to use for the free
2097dcf36dcSFrançois Tigeot  *
2107dcf36dcSFrançois Tigeot  * Free the sync object by freeing all semaphores in it.
2117dcf36dcSFrançois Tigeot  */
radeon_sync_free(struct radeon_device * rdev,struct radeon_sync * sync,struct radeon_fence * fence)2127dcf36dcSFrançois Tigeot void radeon_sync_free(struct radeon_device *rdev,
2137dcf36dcSFrançois Tigeot 		      struct radeon_sync *sync,
2147dcf36dcSFrançois Tigeot 		      struct radeon_fence *fence)
2157dcf36dcSFrançois Tigeot {
2167dcf36dcSFrançois Tigeot 	unsigned i;
2177dcf36dcSFrançois Tigeot 
2187dcf36dcSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_SYNCS; ++i)
2197dcf36dcSFrançois Tigeot 		radeon_semaphore_free(rdev, &sync->semaphores[i], fence);
2207dcf36dcSFrançois Tigeot }
221