18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc. 38c2ecf20Sopenharmony_ci * All Rights Reserved. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 68c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the 78c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 88c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 98c2ecf20Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 108c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 118c2ecf20Sopenharmony_ci * the following conditions: 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 148c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 158c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 168c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 178c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 188c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 198c2ecf20Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the 228c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 238c2ecf20Sopenharmony_ci * of the Software. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_ci/* 278c2ecf20Sopenharmony_ci * Authors: 288c2ecf20Sopenharmony_ci * Christian König <christian.koenig@amd.com> 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include "radeon.h" 328c2ecf20Sopenharmony_ci#include "radeon_trace.h" 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/** 358c2ecf20Sopenharmony_ci * radeon_sync_create - zero init sync object 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * @sync: sync object to initialize 388c2ecf20Sopenharmony_ci * 398c2ecf20Sopenharmony_ci * Just clear the sync object for now. 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_civoid radeon_sync_create(struct radeon_sync *sync) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci unsigned i; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci for (i = 0; i < RADEON_NUM_SYNCS; ++i) 468c2ecf20Sopenharmony_ci sync->semaphores[i] = NULL; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci for (i = 0; i < RADEON_NUM_RINGS; ++i) 498c2ecf20Sopenharmony_ci sync->sync_to[i] = NULL; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci sync->last_vm_update = NULL; 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/** 558c2ecf20Sopenharmony_ci * radeon_sync_fence - use the semaphore to sync to a fence 568c2ecf20Sopenharmony_ci * 578c2ecf20Sopenharmony_ci * @sync: sync object to add fence to 588c2ecf20Sopenharmony_ci * @fence: fence to sync to 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci * Sync to the fence using the semaphore objects 618c2ecf20Sopenharmony_ci */ 628c2ecf20Sopenharmony_civoid radeon_sync_fence(struct radeon_sync *sync, 638c2ecf20Sopenharmony_ci struct radeon_fence *fence) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci struct radeon_fence *other; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci if (!fence) 688c2ecf20Sopenharmony_ci return; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci other = sync->sync_to[fence->ring]; 718c2ecf20Sopenharmony_ci sync->sync_to[fence->ring] = radeon_fence_later(fence, other); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci if (fence->is_vm_update) { 748c2ecf20Sopenharmony_ci other = sync->last_vm_update; 758c2ecf20Sopenharmony_ci sync->last_vm_update = radeon_fence_later(fence, other); 768c2ecf20Sopenharmony_ci } 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci/** 808c2ecf20Sopenharmony_ci * radeon_sync_resv - use the semaphores to sync to a reservation object 818c2ecf20Sopenharmony_ci * 828c2ecf20Sopenharmony_ci * @sync: sync object to add fences from reservation object to 838c2ecf20Sopenharmony_ci * @resv: reservation object with embedded fence 848c2ecf20Sopenharmony_ci * @shared: true if we should only sync to the exclusive fence 858c2ecf20Sopenharmony_ci * 868c2ecf20Sopenharmony_ci * Sync to the fence using the semaphore objects 878c2ecf20Sopenharmony_ci */ 888c2ecf20Sopenharmony_ciint radeon_sync_resv(struct radeon_device *rdev, 898c2ecf20Sopenharmony_ci struct radeon_sync *sync, 908c2ecf20Sopenharmony_ci struct dma_resv *resv, 918c2ecf20Sopenharmony_ci bool shared) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci struct dma_resv_list *flist; 948c2ecf20Sopenharmony_ci struct dma_fence *f; 958c2ecf20Sopenharmony_ci struct radeon_fence *fence; 968c2ecf20Sopenharmony_ci unsigned i; 978c2ecf20Sopenharmony_ci int r = 0; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci /* always sync to the exclusive fence */ 1008c2ecf20Sopenharmony_ci f = dma_resv_get_excl(resv); 1018c2ecf20Sopenharmony_ci fence = f ? to_radeon_fence(f) : NULL; 1028c2ecf20Sopenharmony_ci if (fence && fence->rdev == rdev) 1038c2ecf20Sopenharmony_ci radeon_sync_fence(sync, fence); 1048c2ecf20Sopenharmony_ci else if (f) 1058c2ecf20Sopenharmony_ci r = dma_fence_wait(f, true); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci flist = dma_resv_get_list(resv); 1088c2ecf20Sopenharmony_ci if (shared || !flist || r) 1098c2ecf20Sopenharmony_ci return r; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci for (i = 0; i < flist->shared_count; ++i) { 1128c2ecf20Sopenharmony_ci f = rcu_dereference_protected(flist->shared[i], 1138c2ecf20Sopenharmony_ci dma_resv_held(resv)); 1148c2ecf20Sopenharmony_ci fence = to_radeon_fence(f); 1158c2ecf20Sopenharmony_ci if (fence && fence->rdev == rdev) 1168c2ecf20Sopenharmony_ci radeon_sync_fence(sync, fence); 1178c2ecf20Sopenharmony_ci else 1188c2ecf20Sopenharmony_ci r = dma_fence_wait(f, true); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (r) 1218c2ecf20Sopenharmony_ci break; 1228c2ecf20Sopenharmony_ci } 1238c2ecf20Sopenharmony_ci return r; 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/** 1278c2ecf20Sopenharmony_ci * radeon_sync_rings - sync ring to all registered fences 1288c2ecf20Sopenharmony_ci * 1298c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer 1308c2ecf20Sopenharmony_ci * @sync: sync object to use 1318c2ecf20Sopenharmony_ci * @ring: ring that needs sync 1328c2ecf20Sopenharmony_ci * 1338c2ecf20Sopenharmony_ci * Ensure that all registered fences are signaled before letting 1348c2ecf20Sopenharmony_ci * the ring continue. The caller must hold the ring lock. 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_ciint radeon_sync_rings(struct radeon_device *rdev, 1378c2ecf20Sopenharmony_ci struct radeon_sync *sync, 1388c2ecf20Sopenharmony_ci int ring) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci unsigned count = 0; 1418c2ecf20Sopenharmony_ci int i, r; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci for (i = 0; i < RADEON_NUM_RINGS; ++i) { 1448c2ecf20Sopenharmony_ci struct radeon_fence *fence = sync->sync_to[i]; 1458c2ecf20Sopenharmony_ci struct radeon_semaphore *semaphore; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci /* check if we really need to sync */ 1488c2ecf20Sopenharmony_ci if (!radeon_fence_need_sync(fence, ring)) 1498c2ecf20Sopenharmony_ci continue; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* prevent GPU deadlocks */ 1528c2ecf20Sopenharmony_ci if (!rdev->ring[i].ready) { 1538c2ecf20Sopenharmony_ci dev_err(rdev->dev, "Syncing to a disabled ring!"); 1548c2ecf20Sopenharmony_ci return -EINVAL; 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci if (count >= RADEON_NUM_SYNCS) { 1588c2ecf20Sopenharmony_ci /* not enough room, wait manually */ 1598c2ecf20Sopenharmony_ci r = radeon_fence_wait(fence, false); 1608c2ecf20Sopenharmony_ci if (r) 1618c2ecf20Sopenharmony_ci return r; 1628c2ecf20Sopenharmony_ci continue; 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci r = radeon_semaphore_create(rdev, &semaphore); 1658c2ecf20Sopenharmony_ci if (r) 1668c2ecf20Sopenharmony_ci return r; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci sync->semaphores[count++] = semaphore; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci /* allocate enough space for sync command */ 1718c2ecf20Sopenharmony_ci r = radeon_ring_alloc(rdev, &rdev->ring[i], 16); 1728c2ecf20Sopenharmony_ci if (r) 1738c2ecf20Sopenharmony_ci return r; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci /* emit the signal semaphore */ 1768c2ecf20Sopenharmony_ci if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) { 1778c2ecf20Sopenharmony_ci /* signaling wasn't successful wait manually */ 1788c2ecf20Sopenharmony_ci radeon_ring_undo(&rdev->ring[i]); 1798c2ecf20Sopenharmony_ci r = radeon_fence_wait(fence, false); 1808c2ecf20Sopenharmony_ci if (r) 1818c2ecf20Sopenharmony_ci return r; 1828c2ecf20Sopenharmony_ci continue; 1838c2ecf20Sopenharmony_ci } 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci /* we assume caller has already allocated space on waiters ring */ 1868c2ecf20Sopenharmony_ci if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) { 1878c2ecf20Sopenharmony_ci /* waiting wasn't successful wait manually */ 1888c2ecf20Sopenharmony_ci radeon_ring_undo(&rdev->ring[i]); 1898c2ecf20Sopenharmony_ci r = radeon_fence_wait(fence, false); 1908c2ecf20Sopenharmony_ci if (r) 1918c2ecf20Sopenharmony_ci return r; 1928c2ecf20Sopenharmony_ci continue; 1938c2ecf20Sopenharmony_ci } 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci radeon_ring_commit(rdev, &rdev->ring[i], false); 1968c2ecf20Sopenharmony_ci radeon_fence_note_sync(fence, ring); 1978c2ecf20Sopenharmony_ci } 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci return 0; 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci/** 2038c2ecf20Sopenharmony_ci * radeon_sync_free - free the sync object 2048c2ecf20Sopenharmony_ci * 2058c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer 2068c2ecf20Sopenharmony_ci * @sync: sync object to use 2078c2ecf20Sopenharmony_ci * @fence: fence to use for the free 2088c2ecf20Sopenharmony_ci * 2098c2ecf20Sopenharmony_ci * Free the sync object by freeing all semaphores in it. 2108c2ecf20Sopenharmony_ci */ 2118c2ecf20Sopenharmony_civoid radeon_sync_free(struct radeon_device *rdev, 2128c2ecf20Sopenharmony_ci struct radeon_sync *sync, 2138c2ecf20Sopenharmony_ci struct radeon_fence *fence) 2148c2ecf20Sopenharmony_ci{ 2158c2ecf20Sopenharmony_ci unsigned i; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci for (i = 0; i < RADEON_NUM_SYNCS; ++i) 2188c2ecf20Sopenharmony_ci radeon_semaphore_free(rdev, &sync->semaphores[i], fence); 2198c2ecf20Sopenharmony_ci} 220