radeon_semaphore.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "drmP.h"
  31. #include "drm.h"
  32. #include "radeon.h"
  33. static int allocate_semaphores(struct radeon_device *rdev)
  34. {
  35. const unsigned long bo_size = PAGE_SIZE * 4;
  36. struct radeon_bo *bo;
  37. struct list_head new_entrys;
  38. unsigned long irq_flags;
  39. uint64_t gpu_addr;
  40. void *map;
  41. int i, r;
  42. r = radeon_bo_create(rdev, bo_size, RADEON_GPU_PAGE_SIZE, true,
  43. RADEON_GEM_DOMAIN_GTT, &bo);
  44. if (r) {
  45. dev_err(rdev->dev, "(%d) failed to allocate semaphore bo\n", r);
  46. return r;
  47. }
  48. r = radeon_bo_reserve(bo, false);
  49. if (r) {
  50. radeon_bo_unref(&bo);
  51. dev_err(rdev->dev, "(%d) failed to reserve semaphore bo\n", r);
  52. return r;
  53. }
  54. r = radeon_bo_kmap(bo, &map);
  55. if (r) {
  56. radeon_bo_unreserve(bo);
  57. radeon_bo_unref(&bo);
  58. dev_err(rdev->dev, "(%d) semaphore map failed\n", r);
  59. return r;
  60. }
  61. memset(map, 0, bo_size);
  62. radeon_bo_kunmap(bo);
  63. r = radeon_bo_pin(bo, RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
  64. if (r) {
  65. radeon_bo_unreserve(bo);
  66. radeon_bo_unref(&bo);
  67. dev_err(rdev->dev, "(%d) semaphore pin failed\n", r);
  68. return r;
  69. }
  70. INIT_LIST_HEAD(&new_entrys);
  71. for (i = 0; i < bo_size/8; ++i) {
  72. struct radeon_semaphore *sem = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
  73. ttm_bo_reference(&bo->tbo);
  74. sem->robj = bo;
  75. sem->gpu_addr = gpu_addr;
  76. gpu_addr += 8;
  77. list_add_tail(&sem->list, &new_entrys);
  78. }
  79. radeon_bo_unreserve(bo);
  80. radeon_bo_unref(&bo);
  81. write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
  82. list_splice_tail(&new_entrys, &rdev->semaphore_drv.free);
  83. write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
  84. DRM_INFO("%d new semaphores allocated\n", (int)(bo_size/8));
  85. return 0;
  86. }
  87. int radeon_semaphore_create(struct radeon_device *rdev,
  88. struct radeon_semaphore **semaphore)
  89. {
  90. unsigned long irq_flags;
  91. write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
  92. if (list_empty(&rdev->semaphore_drv.free)) {
  93. int r;
  94. write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
  95. r = allocate_semaphores(rdev);
  96. if (r)
  97. return r;
  98. write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
  99. }
  100. *semaphore = list_first_entry(&rdev->semaphore_drv.free, struct radeon_semaphore, list);
  101. list_del(&(*semaphore)->list);
  102. write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
  103. return 0;
  104. }
  105. void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
  106. struct radeon_semaphore *semaphore)
  107. {
  108. radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, false);
  109. }
  110. void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
  111. struct radeon_semaphore *semaphore)
  112. {
  113. radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true);
  114. }
  115. void radeon_semaphore_free(struct radeon_device *rdev,
  116. struct radeon_semaphore *semaphore)
  117. {
  118. unsigned long irq_flags;
  119. write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
  120. list_add_tail(&semaphore->list, &rdev->semaphore_drv.free);
  121. write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
  122. }
  123. void radeon_semaphore_driver_fini(struct radeon_device *rdev)
  124. {
  125. struct radeon_semaphore *i, *n;
  126. struct list_head entrys;
  127. unsigned long irq_flags;
  128. INIT_LIST_HEAD(&entrys);
  129. write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags);
  130. if (!list_empty(&rdev->semaphore_drv.free)) {
  131. list_splice(&rdev->semaphore_drv.free, &entrys);
  132. }
  133. INIT_LIST_HEAD(&rdev->semaphore_drv.free);
  134. write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags);
  135. list_for_each_entry_safe(i, n, &entrys, list) {
  136. radeon_bo_unref(&i->robj);
  137. kfree(i);
  138. }
  139. }