radeon_semaphore.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright 2011 Christian König.
  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 <deathsimple@vodafone.de>
  29. */
  30. #include <drm/drmP.h>
  31. #include "radeon.h"
  32. #include "radeon_trace.h"
  33. int radeon_semaphore_create(struct radeon_device *rdev,
  34. struct radeon_semaphore **semaphore)
  35. {
  36. uint64_t *cpu_addr;
  37. int i, r;
  38. *semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
  39. if (*semaphore == NULL) {
  40. return -ENOMEM;
  41. }
  42. r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &(*semaphore)->sa_bo,
  43. 8 * RADEON_NUM_SYNCS, 8);
  44. if (r) {
  45. kfree(*semaphore);
  46. *semaphore = NULL;
  47. return r;
  48. }
  49. (*semaphore)->waiters = 0;
  50. (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
  51. cpu_addr = radeon_sa_bo_cpu_addr((*semaphore)->sa_bo);
  52. for (i = 0; i < RADEON_NUM_SYNCS; ++i)
  53. cpu_addr[i] = 0;
  54. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  55. (*semaphore)->sync_to[i] = NULL;
  56. return 0;
  57. }
  58. bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx,
  59. struct radeon_semaphore *semaphore)
  60. {
  61. struct radeon_ring *ring = &rdev->ring[ridx];
  62. trace_radeon_semaphore_signale(ridx, semaphore);
  63. if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) {
  64. --semaphore->waiters;
  65. /* for debugging lockup only, used by sysfs debug files */
  66. ring->last_semaphore_signal_addr = semaphore->gpu_addr;
  67. return true;
  68. }
  69. return false;
  70. }
  71. bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx,
  72. struct radeon_semaphore *semaphore)
  73. {
  74. struct radeon_ring *ring = &rdev->ring[ridx];
  75. trace_radeon_semaphore_wait(ridx, semaphore);
  76. if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) {
  77. ++semaphore->waiters;
  78. /* for debugging lockup only, used by sysfs debug files */
  79. ring->last_semaphore_wait_addr = semaphore->gpu_addr;
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * radeon_semaphore_sync_fence - use the semaphore to sync to a fence
  86. *
  87. * @semaphore: semaphore object to add fence to
  88. * @fence: fence to sync to
  89. *
  90. * Sync to the fence using this semaphore object
  91. */
  92. void radeon_semaphore_sync_fence(struct radeon_semaphore *semaphore,
  93. struct radeon_fence *fence)
  94. {
  95. struct radeon_fence *other;
  96. if (!fence)
  97. return;
  98. other = semaphore->sync_to[fence->ring];
  99. semaphore->sync_to[fence->ring] = radeon_fence_later(fence, other);
  100. }
  101. /**
  102. * radeon_semaphore_sync_to - use the semaphore to sync to a reservation object
  103. *
  104. * @sema: semaphore object to add fence from reservation object to
  105. * @resv: reservation object with embedded fence
  106. * @shared: true if we should onyl sync to the exclusive fence
  107. *
  108. * Sync to the fence using this semaphore object
  109. */
  110. int radeon_semaphore_sync_resv(struct radeon_device *rdev,
  111. struct radeon_semaphore *sema,
  112. struct reservation_object *resv,
  113. bool shared)
  114. {
  115. struct reservation_object_list *flist;
  116. struct fence *f;
  117. struct radeon_fence *fence;
  118. unsigned i;
  119. int r = 0;
  120. /* always sync to the exclusive fence */
  121. f = reservation_object_get_excl(resv);
  122. fence = f ? to_radeon_fence(f) : NULL;
  123. if (fence && fence->rdev == rdev)
  124. radeon_semaphore_sync_fence(sema, fence);
  125. else if (f)
  126. r = fence_wait(f, true);
  127. flist = reservation_object_get_list(resv);
  128. if (shared || !flist || r)
  129. return r;
  130. for (i = 0; i < flist->shared_count; ++i) {
  131. f = rcu_dereference_protected(flist->shared[i],
  132. reservation_object_held(resv));
  133. fence = to_radeon_fence(f);
  134. if (fence && fence->rdev == rdev)
  135. radeon_semaphore_sync_fence(sema, fence);
  136. else
  137. r = fence_wait(f, true);
  138. if (r)
  139. break;
  140. }
  141. return r;
  142. }
  143. /**
  144. * radeon_semaphore_sync_rings - sync ring to all registered fences
  145. *
  146. * @rdev: radeon_device pointer
  147. * @semaphore: semaphore object to use for sync
  148. * @ring: ring that needs sync
  149. *
  150. * Ensure that all registered fences are signaled before letting
  151. * the ring continue. The caller must hold the ring lock.
  152. */
  153. int radeon_semaphore_sync_rings(struct radeon_device *rdev,
  154. struct radeon_semaphore *semaphore,
  155. int ring)
  156. {
  157. unsigned count = 0;
  158. int i, r;
  159. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  160. struct radeon_fence *fence = semaphore->sync_to[i];
  161. /* check if we really need to sync */
  162. if (!radeon_fence_need_sync(fence, ring))
  163. continue;
  164. /* prevent GPU deadlocks */
  165. if (!rdev->ring[i].ready) {
  166. dev_err(rdev->dev, "Syncing to a disabled ring!");
  167. return -EINVAL;
  168. }
  169. if (++count > RADEON_NUM_SYNCS) {
  170. /* not enough room, wait manually */
  171. r = radeon_fence_wait(fence, false);
  172. if (r)
  173. return r;
  174. continue;
  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. /* we assume caller has already allocated space on waiters ring */
  191. if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
  192. /* waiting wasn't successful wait manually */
  193. radeon_ring_undo(&rdev->ring[i]);
  194. r = radeon_fence_wait(fence, false);
  195. if (r)
  196. return r;
  197. continue;
  198. }
  199. radeon_ring_commit(rdev, &rdev->ring[i], false);
  200. radeon_fence_note_sync(fence, ring);
  201. semaphore->gpu_addr += 8;
  202. }
  203. return 0;
  204. }
  205. void radeon_semaphore_free(struct radeon_device *rdev,
  206. struct radeon_semaphore **semaphore,
  207. struct radeon_fence *fence)
  208. {
  209. if (semaphore == NULL || *semaphore == NULL) {
  210. return;
  211. }
  212. if ((*semaphore)->waiters > 0) {
  213. dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
  214. " hardware lockup imminent!\n", *semaphore);
  215. }
  216. radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
  217. kfree(*semaphore);
  218. *semaphore = NULL;
  219. }