radeon_semaphore.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. int radeon_semaphore_create(struct radeon_device *rdev,
  33. struct radeon_semaphore **semaphore)
  34. {
  35. int r;
  36. *semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
  37. if (*semaphore == NULL) {
  38. return -ENOMEM;
  39. }
  40. r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo,
  41. &(*semaphore)->sa_bo, 8, 8, true);
  42. if (r) {
  43. kfree(*semaphore);
  44. *semaphore = NULL;
  45. return r;
  46. }
  47. (*semaphore)->waiters = 0;
  48. (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
  49. *((uint64_t*)radeon_sa_bo_cpu_addr((*semaphore)->sa_bo)) = 0;
  50. return 0;
  51. }
  52. void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
  53. struct radeon_semaphore *semaphore)
  54. {
  55. --semaphore->waiters;
  56. radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, false);
  57. }
  58. void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
  59. struct radeon_semaphore *semaphore)
  60. {
  61. ++semaphore->waiters;
  62. radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true);
  63. }
  64. /* caller must hold ring lock */
  65. int radeon_semaphore_sync_rings(struct radeon_device *rdev,
  66. struct radeon_semaphore *semaphore,
  67. int signaler, int waiter)
  68. {
  69. int r;
  70. /* no need to signal and wait on the same ring */
  71. if (signaler == waiter) {
  72. return 0;
  73. }
  74. /* prevent GPU deadlocks */
  75. if (!rdev->ring[signaler].ready) {
  76. dev_err(rdev->dev, "Trying to sync to a disabled ring!");
  77. return -EINVAL;
  78. }
  79. r = radeon_ring_alloc(rdev, &rdev->ring[signaler], 8);
  80. if (r) {
  81. return r;
  82. }
  83. radeon_semaphore_emit_signal(rdev, signaler, semaphore);
  84. radeon_ring_commit(rdev, &rdev->ring[signaler]);
  85. /* we assume caller has already allocated space on waiters ring */
  86. radeon_semaphore_emit_wait(rdev, waiter, semaphore);
  87. /* for debugging lockup only, used by sysfs debug files */
  88. rdev->ring[signaler].last_semaphore_signal_addr = semaphore->gpu_addr;
  89. rdev->ring[waiter].last_semaphore_wait_addr = semaphore->gpu_addr;
  90. return 0;
  91. }
  92. void radeon_semaphore_free(struct radeon_device *rdev,
  93. struct radeon_semaphore **semaphore,
  94. struct radeon_fence *fence)
  95. {
  96. if (semaphore == NULL || *semaphore == NULL) {
  97. return;
  98. }
  99. if ((*semaphore)->waiters > 0) {
  100. dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
  101. " hardware lockup imminent!\n", *semaphore);
  102. }
  103. radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
  104. kfree(*semaphore);
  105. *semaphore = NULL;
  106. }