Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2014 Advanced Micro Devices, Inc.
0003  * All Rights Reserved.
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a
0006  * copy of this software and associated documentation files (the
0007  * "Software"), to deal in the Software without restriction, including
0008  * without limitation the rights to use, copy, modify, merge, publish,
0009  * distribute, sub license, and/or sell copies of the Software, and to
0010  * permit persons to whom the Software is furnished to do so, subject to
0011  * the following conditions:
0012  *
0013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0014  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0015  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0016  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
0017  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0018  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0019  * USE OR OTHER DEALINGS IN THE SOFTWARE.
0020  *
0021  * The above copyright notice and this permission notice (including the
0022  * next paragraph) shall be included in all copies or substantial portions
0023  * of the Software.
0024  *
0025  */
0026 /*
0027  * Authors:
0028  *    Christian König <christian.koenig@amd.com>
0029  */
0030 
0031 #include "radeon.h"
0032 #include "radeon_trace.h"
0033 
0034 /**
0035  * radeon_sync_create - zero init sync object
0036  *
0037  * @sync: sync object to initialize
0038  *
0039  * Just clear the sync object for now.
0040  */
0041 void radeon_sync_create(struct radeon_sync *sync)
0042 {
0043     unsigned i;
0044 
0045     for (i = 0; i < RADEON_NUM_SYNCS; ++i)
0046         sync->semaphores[i] = NULL;
0047 
0048     for (i = 0; i < RADEON_NUM_RINGS; ++i)
0049         sync->sync_to[i] = NULL;
0050 
0051     sync->last_vm_update = NULL;
0052 }
0053 
0054 /**
0055  * radeon_sync_fence - use the semaphore to sync to a fence
0056  *
0057  * @sync: sync object to add fence to
0058  * @fence: fence to sync to
0059  *
0060  * Sync to the fence using the semaphore objects
0061  */
0062 void radeon_sync_fence(struct radeon_sync *sync,
0063                struct radeon_fence *fence)
0064 {
0065     struct radeon_fence *other;
0066 
0067     if (!fence)
0068         return;
0069 
0070     other = sync->sync_to[fence->ring];
0071     sync->sync_to[fence->ring] = radeon_fence_later(fence, other);
0072 
0073     if (fence->is_vm_update) {
0074         other = sync->last_vm_update;
0075         sync->last_vm_update = radeon_fence_later(fence, other);
0076     }
0077 }
0078 
0079 /**
0080  * radeon_sync_resv - use the semaphores to sync to a reservation object
0081  *
0082  * @rdev: radeon_device pointer
0083  * @sync: sync object to add fences from reservation object to
0084  * @resv: reservation object with embedded fence
0085  * @shared: true if we should only sync to the exclusive fence
0086  *
0087  * Sync to the fence using the semaphore objects
0088  */
0089 int radeon_sync_resv(struct radeon_device *rdev,
0090              struct radeon_sync *sync,
0091              struct dma_resv *resv,
0092              bool shared)
0093 {
0094     struct dma_resv_iter cursor;
0095     struct radeon_fence *fence;
0096     struct dma_fence *f;
0097     int r = 0;
0098 
0099     dma_resv_for_each_fence(&cursor, resv, dma_resv_usage_rw(!shared), f) {
0100         fence = to_radeon_fence(f);
0101         if (fence && fence->rdev == rdev)
0102             radeon_sync_fence(sync, fence);
0103         else
0104             r = dma_fence_wait(f, true);
0105         if (r)
0106             break;
0107     }
0108     return r;
0109 }
0110 
0111 /**
0112  * radeon_sync_rings - sync ring to all registered fences
0113  *
0114  * @rdev: radeon_device pointer
0115  * @sync: sync object to use
0116  * @ring: ring that needs sync
0117  *
0118  * Ensure that all registered fences are signaled before letting
0119  * the ring continue. The caller must hold the ring lock.
0120  */
0121 int radeon_sync_rings(struct radeon_device *rdev,
0122               struct radeon_sync *sync,
0123               int ring)
0124 {
0125     unsigned count = 0;
0126     int i, r;
0127 
0128     for (i = 0; i < RADEON_NUM_RINGS; ++i) {
0129         struct radeon_fence *fence = sync->sync_to[i];
0130         struct radeon_semaphore *semaphore;
0131 
0132         /* check if we really need to sync */
0133         if (!radeon_fence_need_sync(fence, ring))
0134             continue;
0135 
0136         /* prevent GPU deadlocks */
0137         if (!rdev->ring[i].ready) {
0138             dev_err(rdev->dev, "Syncing to a disabled ring!");
0139             return -EINVAL;
0140         }
0141 
0142         if (count >= RADEON_NUM_SYNCS) {
0143             /* not enough room, wait manually */
0144             r = radeon_fence_wait(fence, false);
0145             if (r)
0146                 return r;
0147             continue;
0148         }
0149         r = radeon_semaphore_create(rdev, &semaphore);
0150         if (r)
0151             return r;
0152 
0153         sync->semaphores[count++] = semaphore;
0154 
0155         /* allocate enough space for sync command */
0156         r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
0157         if (r)
0158             return r;
0159 
0160         /* emit the signal semaphore */
0161         if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) {
0162             /* signaling wasn't successful wait manually */
0163             radeon_ring_undo(&rdev->ring[i]);
0164             r = radeon_fence_wait(fence, false);
0165             if (r)
0166                 return r;
0167             continue;
0168         }
0169 
0170         /* we assume caller has already allocated space on waiters ring */
0171         if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
0172             /* waiting wasn't successful wait manually */
0173             radeon_ring_undo(&rdev->ring[i]);
0174             r = radeon_fence_wait(fence, false);
0175             if (r)
0176                 return r;
0177             continue;
0178         }
0179 
0180         radeon_ring_commit(rdev, &rdev->ring[i], false);
0181         radeon_fence_note_sync(fence, ring);
0182     }
0183 
0184     return 0;
0185 }
0186 
0187 /**
0188  * radeon_sync_free - free the sync object
0189  *
0190  * @rdev: radeon_device pointer
0191  * @sync: sync object to use
0192  * @fence: fence to use for the free
0193  *
0194  * Free the sync object by freeing all semaphores in it.
0195  */
0196 void radeon_sync_free(struct radeon_device *rdev,
0197               struct radeon_sync *sync,
0198               struct radeon_fence *fence)
0199 {
0200     unsigned i;
0201 
0202     for (i = 0; i < RADEON_NUM_SYNCS; ++i)
0203         radeon_semaphore_free(rdev, &sync->semaphores[i], fence);
0204 }