Home | History | Annotate | Line # | Download | only in radeon
radeon_sync.c revision 1.2
      1 /*	$NetBSD: radeon_sync.c,v 1.2 2018/08/27 04:58:36 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 Knig <christian.koenig (at) amd.com>
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: radeon_sync.c,v 1.2 2018/08/27 04:58:36 riastradh Exp $");
     35 
     36 #include <drm/drmP.h>
     37 #include "radeon.h"
     38 #include "radeon_trace.h"
     39 
     40 /**
     41  * radeon_sync_create - zero init sync object
     42  *
     43  * @sync: sync object to initialize
     44  *
     45  * Just clear the sync object for now.
     46  */
     47 void radeon_sync_create(struct radeon_sync *sync)
     48 {
     49 	unsigned i;
     50 
     51 	for (i = 0; i < RADEON_NUM_SYNCS; ++i)
     52 		sync->semaphores[i] = NULL;
     53 
     54 	for (i = 0; i < RADEON_NUM_RINGS; ++i)
     55 		sync->sync_to[i] = NULL;
     56 
     57 	sync->last_vm_update = NULL;
     58 }
     59 
     60 /**
     61  * radeon_sync_fence - use the semaphore to sync to a fence
     62  *
     63  * @sync: sync object to add fence to
     64  * @fence: fence to sync to
     65  *
     66  * Sync to the fence using the semaphore objects
     67  */
     68 void radeon_sync_fence(struct radeon_sync *sync,
     69 		       struct radeon_fence *fence)
     70 {
     71 	struct radeon_fence *other;
     72 
     73 	if (!fence)
     74 		return;
     75 
     76 	other = sync->sync_to[fence->ring];
     77 	sync->sync_to[fence->ring] = radeon_fence_later(fence, other);
     78 
     79 	if (fence->is_vm_update) {
     80 		other = sync->last_vm_update;
     81 		sync->last_vm_update = radeon_fence_later(fence, other);
     82 	}
     83 }
     84 
     85 /**
     86  * radeon_sync_resv - use the semaphores to sync to a reservation object
     87  *
     88  * @sync: sync object to add fences from reservation object to
     89  * @resv: reservation object with embedded fence
     90  * @shared: true if we should only sync to the exclusive fence
     91  *
     92  * Sync to the fence using the semaphore objects
     93  */
     94 int radeon_sync_resv(struct radeon_device *rdev,
     95 		     struct radeon_sync *sync,
     96 		     struct reservation_object *resv,
     97 		     bool shared)
     98 {
     99 	struct reservation_object_list *flist;
    100 	struct fence *f;
    101 	struct radeon_fence *fence;
    102 	unsigned i;
    103 	int r = 0;
    104 
    105 	/* always sync to the exclusive fence */
    106 	f = reservation_object_get_excl(resv);
    107 	fence = f ? to_radeon_fence(f) : NULL;
    108 	if (fence && fence->rdev == rdev)
    109 		radeon_sync_fence(sync, fence);
    110 	else if (f)
    111 		r = fence_wait(f, true);
    112 
    113 	flist = reservation_object_get_list(resv);
    114 	if (shared || !flist || r)
    115 		return r;
    116 
    117 	for (i = 0; i < flist->shared_count; ++i) {
    118 		f = rcu_dereference_protected(flist->shared[i],
    119 					      reservation_object_held(resv));
    120 		fence = to_radeon_fence(f);
    121 		if (fence && fence->rdev == rdev)
    122 			radeon_sync_fence(sync, fence);
    123 		else
    124 			r = fence_wait(f, true);
    125 
    126 		if (r)
    127 			break;
    128 	}
    129 	return r;
    130 }
    131 
    132 /**
    133  * radeon_sync_rings - sync ring to all registered fences
    134  *
    135  * @rdev: radeon_device pointer
    136  * @sync: sync object to use
    137  * @ring: ring that needs sync
    138  *
    139  * Ensure that all registered fences are signaled before letting
    140  * the ring continue. The caller must hold the ring lock.
    141  */
    142 int radeon_sync_rings(struct radeon_device *rdev,
    143 		      struct radeon_sync *sync,
    144 		      int ring)
    145 {
    146 	unsigned count = 0;
    147 	int i, r;
    148 
    149 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    150 		struct radeon_fence *fence = sync->sync_to[i];
    151 		struct radeon_semaphore *semaphore;
    152 
    153 		/* check if we really need to sync */
    154 		if (!radeon_fence_need_sync(fence, ring))
    155 			continue;
    156 
    157 		/* prevent GPU deadlocks */
    158 		if (!rdev->ring[i].ready) {
    159 			dev_err(rdev->dev, "Syncing to a disabled ring!");
    160 			return -EINVAL;
    161 		}
    162 
    163 		if (count >= RADEON_NUM_SYNCS) {
    164 			/* not enough room, wait manually */
    165 			r = radeon_fence_wait(fence, false);
    166 			if (r)
    167 				return r;
    168 			continue;
    169 		}
    170 		r = radeon_semaphore_create(rdev, &semaphore);
    171 		if (r)
    172 			return r;
    173 
    174 		sync->semaphores[count++] = semaphore;
    175 
    176 		/* allocate enough space for sync command */
    177 		r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
    178 		if (r)
    179 			return r;
    180 
    181 		/* emit the signal semaphore */
    182 		if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) {
    183 			/* signaling wasn't successful wait manually */
    184 			radeon_ring_undo(&rdev->ring[i]);
    185 			r = radeon_fence_wait(fence, false);
    186 			if (r)
    187 				return r;
    188 			continue;
    189 		}
    190 
    191 		/* we assume caller has already allocated space on waiters ring */
    192 		if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
    193 			/* waiting wasn't successful wait manually */
    194 			radeon_ring_undo(&rdev->ring[i]);
    195 			r = radeon_fence_wait(fence, false);
    196 			if (r)
    197 				return r;
    198 			continue;
    199 		}
    200 
    201 		radeon_ring_commit(rdev, &rdev->ring[i], false);
    202 		radeon_fence_note_sync(fence, ring);
    203 	}
    204 
    205 	return 0;
    206 }
    207 
    208 /**
    209  * radeon_sync_free - free the sync object
    210  *
    211  * @rdev: radeon_device pointer
    212  * @sync: sync object to use
    213  * @fence: fence to use for the free
    214  *
    215  * Free the sync object by freeing all semaphores in it.
    216  */
    217 void radeon_sync_free(struct radeon_device *rdev,
    218 		      struct radeon_sync *sync,
    219 		      struct radeon_fence *fence)
    220 {
    221 	unsigned i;
    222 
    223 	for (i = 0; i < RADEON_NUM_SYNCS; ++i)
    224 		radeon_semaphore_free(rdev, &sync->semaphores[i], fence);
    225 }
    226