radeon_sync.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Christian König <christian.koenig@amd.com>
  29. */
  30. #include <drm/drmP.h>
  31. #include "radeon.h"
  32. #include "radeon_trace.h"
  33. /**
  34. * radeon_sync_create - zero init sync object
  35. *
  36. * @sync: sync object to initialize
  37. *
  38. * Just clear the sync object for now.
  39. */
  40. void radeon_sync_create(struct radeon_sync *sync)
  41. {
  42. unsigned i;
  43. for (i = 0; i < RADEON_NUM_SYNCS; ++i)
  44. sync->semaphores[i] = NULL;
  45. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  46. sync->sync_to[i] = NULL;
  47. sync->last_vm_update = NULL;
  48. }
  49. /**
  50. * radeon_sync_fence - use the semaphore to sync to a fence
  51. *
  52. * @sync: sync object to add fence to
  53. * @fence: fence to sync to
  54. *
  55. * Sync to the fence using the semaphore objects
  56. */
  57. void radeon_sync_fence(struct radeon_sync *sync,
  58. struct radeon_fence *fence)
  59. {
  60. struct radeon_fence *other;
  61. if (!fence)
  62. return;
  63. other = sync->sync_to[fence->ring];
  64. sync->sync_to[fence->ring] = radeon_fence_later(fence, other);
  65. if (fence->is_vm_update) {
  66. other = sync->last_vm_update;
  67. sync->last_vm_update = radeon_fence_later(fence, other);
  68. }
  69. }
  70. /**
  71. * radeon_sync_resv - use the semaphores to sync to a reservation object
  72. *
  73. * @sync: sync object to add fences from reservation object to
  74. * @resv: reservation object with embedded fence
  75. * @shared: true if we should only sync to the exclusive fence
  76. *
  77. * Sync to the fence using the semaphore objects
  78. */
  79. int radeon_sync_resv(struct radeon_device *rdev,
  80. struct radeon_sync *sync,
  81. struct reservation_object *resv,
  82. bool shared)
  83. {
  84. struct reservation_object_list *flist;
  85. struct fence *f;
  86. struct radeon_fence *fence;
  87. unsigned i;
  88. int r = 0;
  89. /* always sync to the exclusive fence */
  90. f = reservation_object_get_excl(resv);
  91. fence = f ? to_radeon_fence(f) : NULL;
  92. if (fence && fence->rdev == rdev)
  93. radeon_sync_fence(sync, fence);
  94. else if (f)
  95. r = fence_wait(f, true);
  96. flist = reservation_object_get_list(resv);
  97. if (shared || !flist || r)
  98. return r;
  99. for (i = 0; i < flist->shared_count; ++i) {
  100. f = rcu_dereference_protected(flist->shared[i],
  101. reservation_object_held(resv));
  102. fence = to_radeon_fence(f);
  103. if (fence && fence->rdev == rdev)
  104. radeon_sync_fence(sync, fence);
  105. else
  106. r = fence_wait(f, true);
  107. if (r)
  108. break;
  109. }
  110. return r;
  111. }
  112. /**
  113. * radeon_sync_rings - sync ring to all registered fences
  114. *
  115. * @rdev: radeon_device pointer
  116. * @sync: sync object to use
  117. * @ring: ring that needs sync
  118. *
  119. * Ensure that all registered fences are signaled before letting
  120. * the ring continue. The caller must hold the ring lock.
  121. */
  122. int radeon_sync_rings(struct radeon_device *rdev,
  123. struct radeon_sync *sync,
  124. int ring)
  125. {
  126. unsigned count = 0;
  127. int i, r;
  128. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  129. struct radeon_fence *fence = sync->sync_to[i];
  130. struct radeon_semaphore *semaphore;
  131. /* check if we really need to sync */
  132. if (!radeon_fence_need_sync(fence, ring))
  133. continue;
  134. /* prevent GPU deadlocks */
  135. if (!rdev->ring[i].ready) {
  136. dev_err(rdev->dev, "Syncing to a disabled ring!");
  137. return -EINVAL;
  138. }
  139. if (count >= RADEON_NUM_SYNCS) {
  140. /* not enough room, wait manually */
  141. r = radeon_fence_wait(fence, false);
  142. if (r)
  143. return r;
  144. continue;
  145. }
  146. r = radeon_semaphore_create(rdev, &semaphore);
  147. if (r)
  148. return r;
  149. sync->semaphores[count++] = semaphore;
  150. /* allocate enough space for sync command */
  151. r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
  152. if (r)
  153. return r;
  154. /* emit the signal semaphore */
  155. if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) {
  156. /* signaling wasn't successful wait manually */
  157. radeon_ring_undo(&rdev->ring[i]);
  158. r = radeon_fence_wait(fence, false);
  159. if (r)
  160. return r;
  161. continue;
  162. }
  163. /* we assume caller has already allocated space on waiters ring */
  164. if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
  165. /* waiting wasn't successful wait manually */
  166. radeon_ring_undo(&rdev->ring[i]);
  167. r = radeon_fence_wait(fence, false);
  168. if (r)
  169. return r;
  170. continue;
  171. }
  172. radeon_ring_commit(rdev, &rdev->ring[i], false);
  173. radeon_fence_note_sync(fence, ring);
  174. }
  175. return 0;
  176. }
  177. /**
  178. * radeon_sync_free - free the sync object
  179. *
  180. * @rdev: radeon_device pointer
  181. * @sync: sync object to use
  182. * @fence: fence to use for the free
  183. *
  184. * Free the sync object by freeing all semaphores in it.
  185. */
  186. void radeon_sync_free(struct radeon_device *rdev,
  187. struct radeon_sync *sync,
  188. struct radeon_fence *fence)
  189. {
  190. unsigned i;
  191. for (i = 0; i < RADEON_NUM_SYNCS; ++i)
  192. radeon_semaphore_free(rdev, &sync->semaphores[i], fence);
  193. }