162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc. 362306a36Sopenharmony_ci * All Rights Reserved. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 662306a36Sopenharmony_ci * copy of this software and associated documentation files (the 762306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 862306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 962306a36Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 1062306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 1162306a36Sopenharmony_ci * the following conditions: 1262306a36Sopenharmony_ci * 1362306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1462306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1562306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 1662306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 1762306a36Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 1862306a36Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 1962306a36Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 2062306a36Sopenharmony_ci * 2162306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the 2262306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 2362306a36Sopenharmony_ci * of the Software. 2462306a36Sopenharmony_ci * 2562306a36Sopenharmony_ci */ 2662306a36Sopenharmony_ci/* 2762306a36Sopenharmony_ci * Authors: 2862306a36Sopenharmony_ci * Christian König <christian.koenig@amd.com> 2962306a36Sopenharmony_ci */ 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci#include "radeon.h" 3262306a36Sopenharmony_ci#include "radeon_trace.h" 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci/** 3562306a36Sopenharmony_ci * radeon_sync_create - zero init sync object 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * @sync: sync object to initialize 3862306a36Sopenharmony_ci * 3962306a36Sopenharmony_ci * Just clear the sync object for now. 4062306a36Sopenharmony_ci */ 4162306a36Sopenharmony_civoid radeon_sync_create(struct radeon_sync *sync) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci unsigned i; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci for (i = 0; i < RADEON_NUM_SYNCS; ++i) 4662306a36Sopenharmony_ci sync->semaphores[i] = NULL; 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci for (i = 0; i < RADEON_NUM_RINGS; ++i) 4962306a36Sopenharmony_ci sync->sync_to[i] = NULL; 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci sync->last_vm_update = NULL; 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci/** 5562306a36Sopenharmony_ci * radeon_sync_fence - use the semaphore to sync to a fence 5662306a36Sopenharmony_ci * 5762306a36Sopenharmony_ci * @sync: sync object to add fence to 5862306a36Sopenharmony_ci * @fence: fence to sync to 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci * Sync to the fence using the semaphore objects 6162306a36Sopenharmony_ci */ 6262306a36Sopenharmony_civoid radeon_sync_fence(struct radeon_sync *sync, 6362306a36Sopenharmony_ci struct radeon_fence *fence) 6462306a36Sopenharmony_ci{ 6562306a36Sopenharmony_ci struct radeon_fence *other; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci if (!fence) 6862306a36Sopenharmony_ci return; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci other = sync->sync_to[fence->ring]; 7162306a36Sopenharmony_ci sync->sync_to[fence->ring] = radeon_fence_later(fence, other); 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci if (fence->is_vm_update) { 7462306a36Sopenharmony_ci other = sync->last_vm_update; 7562306a36Sopenharmony_ci sync->last_vm_update = radeon_fence_later(fence, other); 7662306a36Sopenharmony_ci } 7762306a36Sopenharmony_ci} 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci/** 8062306a36Sopenharmony_ci * radeon_sync_resv - use the semaphores to sync to a reservation object 8162306a36Sopenharmony_ci * 8262306a36Sopenharmony_ci * @rdev: radeon_device pointer 8362306a36Sopenharmony_ci * @sync: sync object to add fences from reservation object to 8462306a36Sopenharmony_ci * @resv: reservation object with embedded fence 8562306a36Sopenharmony_ci * @shared: true if we should only sync to the exclusive fence 8662306a36Sopenharmony_ci * 8762306a36Sopenharmony_ci * Sync to the fence using the semaphore objects 8862306a36Sopenharmony_ci */ 8962306a36Sopenharmony_ciint radeon_sync_resv(struct radeon_device *rdev, 9062306a36Sopenharmony_ci struct radeon_sync *sync, 9162306a36Sopenharmony_ci struct dma_resv *resv, 9262306a36Sopenharmony_ci bool shared) 9362306a36Sopenharmony_ci{ 9462306a36Sopenharmony_ci struct dma_resv_iter cursor; 9562306a36Sopenharmony_ci struct radeon_fence *fence; 9662306a36Sopenharmony_ci struct dma_fence *f; 9762306a36Sopenharmony_ci int r = 0; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci dma_resv_for_each_fence(&cursor, resv, dma_resv_usage_rw(!shared), f) { 10062306a36Sopenharmony_ci fence = to_radeon_fence(f); 10162306a36Sopenharmony_ci if (fence && fence->rdev == rdev) 10262306a36Sopenharmony_ci radeon_sync_fence(sync, fence); 10362306a36Sopenharmony_ci else 10462306a36Sopenharmony_ci r = dma_fence_wait(f, true); 10562306a36Sopenharmony_ci if (r) 10662306a36Sopenharmony_ci break; 10762306a36Sopenharmony_ci } 10862306a36Sopenharmony_ci return r; 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci/** 11262306a36Sopenharmony_ci * radeon_sync_rings - sync ring to all registered fences 11362306a36Sopenharmony_ci * 11462306a36Sopenharmony_ci * @rdev: radeon_device pointer 11562306a36Sopenharmony_ci * @sync: sync object to use 11662306a36Sopenharmony_ci * @ring: ring that needs sync 11762306a36Sopenharmony_ci * 11862306a36Sopenharmony_ci * Ensure that all registered fences are signaled before letting 11962306a36Sopenharmony_ci * the ring continue. The caller must hold the ring lock. 12062306a36Sopenharmony_ci */ 12162306a36Sopenharmony_ciint radeon_sync_rings(struct radeon_device *rdev, 12262306a36Sopenharmony_ci struct radeon_sync *sync, 12362306a36Sopenharmony_ci int ring) 12462306a36Sopenharmony_ci{ 12562306a36Sopenharmony_ci unsigned count = 0; 12662306a36Sopenharmony_ci int i, r; 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci for (i = 0; i < RADEON_NUM_RINGS; ++i) { 12962306a36Sopenharmony_ci struct radeon_fence *fence = sync->sync_to[i]; 13062306a36Sopenharmony_ci struct radeon_semaphore *semaphore; 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci /* check if we really need to sync */ 13362306a36Sopenharmony_ci if (!radeon_fence_need_sync(fence, ring)) 13462306a36Sopenharmony_ci continue; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci /* prevent GPU deadlocks */ 13762306a36Sopenharmony_ci if (!rdev->ring[i].ready) { 13862306a36Sopenharmony_ci dev_err(rdev->dev, "Syncing to a disabled ring!"); 13962306a36Sopenharmony_ci return -EINVAL; 14062306a36Sopenharmony_ci } 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci if (count >= RADEON_NUM_SYNCS) { 14362306a36Sopenharmony_ci /* not enough room, wait manually */ 14462306a36Sopenharmony_ci r = radeon_fence_wait(fence, false); 14562306a36Sopenharmony_ci if (r) 14662306a36Sopenharmony_ci return r; 14762306a36Sopenharmony_ci continue; 14862306a36Sopenharmony_ci } 14962306a36Sopenharmony_ci r = radeon_semaphore_create(rdev, &semaphore); 15062306a36Sopenharmony_ci if (r) 15162306a36Sopenharmony_ci return r; 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci sync->semaphores[count++] = semaphore; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci /* allocate enough space for sync command */ 15662306a36Sopenharmony_ci r = radeon_ring_alloc(rdev, &rdev->ring[i], 16); 15762306a36Sopenharmony_ci if (r) 15862306a36Sopenharmony_ci return r; 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci /* emit the signal semaphore */ 16162306a36Sopenharmony_ci if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) { 16262306a36Sopenharmony_ci /* signaling wasn't successful wait manually */ 16362306a36Sopenharmony_ci radeon_ring_undo(&rdev->ring[i]); 16462306a36Sopenharmony_ci r = radeon_fence_wait(fence, false); 16562306a36Sopenharmony_ci if (r) 16662306a36Sopenharmony_ci return r; 16762306a36Sopenharmony_ci continue; 16862306a36Sopenharmony_ci } 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci /* we assume caller has already allocated space on waiters ring */ 17162306a36Sopenharmony_ci if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) { 17262306a36Sopenharmony_ci /* waiting wasn't successful wait manually */ 17362306a36Sopenharmony_ci radeon_ring_undo(&rdev->ring[i]); 17462306a36Sopenharmony_ci r = radeon_fence_wait(fence, false); 17562306a36Sopenharmony_ci if (r) 17662306a36Sopenharmony_ci return r; 17762306a36Sopenharmony_ci continue; 17862306a36Sopenharmony_ci } 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci radeon_ring_commit(rdev, &rdev->ring[i], false); 18162306a36Sopenharmony_ci radeon_fence_note_sync(fence, ring); 18262306a36Sopenharmony_ci } 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci return 0; 18562306a36Sopenharmony_ci} 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci/** 18862306a36Sopenharmony_ci * radeon_sync_free - free the sync object 18962306a36Sopenharmony_ci * 19062306a36Sopenharmony_ci * @rdev: radeon_device pointer 19162306a36Sopenharmony_ci * @sync: sync object to use 19262306a36Sopenharmony_ci * @fence: fence to use for the free 19362306a36Sopenharmony_ci * 19462306a36Sopenharmony_ci * Free the sync object by freeing all semaphores in it. 19562306a36Sopenharmony_ci */ 19662306a36Sopenharmony_civoid radeon_sync_free(struct radeon_device *rdev, 19762306a36Sopenharmony_ci struct radeon_sync *sync, 19862306a36Sopenharmony_ci struct radeon_fence *fence) 19962306a36Sopenharmony_ci{ 20062306a36Sopenharmony_ci unsigned i; 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci for (i = 0; i < RADEON_NUM_SYNCS; ++i) 20362306a36Sopenharmony_ci radeon_semaphore_free(rdev, &sync->semaphores[i], fence); 20462306a36Sopenharmony_ci} 205