amdgpu_ih.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <drm/drmP.h>
  24. #include "amdgpu.h"
  25. #include "amdgpu_ih.h"
  26. #include "amdgpu_amdkfd.h"
  27. /**
  28. * amdgpu_ih_ring_alloc - allocate memory for the IH ring
  29. *
  30. * @adev: amdgpu_device pointer
  31. *
  32. * Allocate a ring buffer for the interrupt controller.
  33. * Returns 0 for success, errors for failure.
  34. */
  35. static int amdgpu_ih_ring_alloc(struct amdgpu_device *adev)
  36. {
  37. int r;
  38. /* Allocate ring buffer */
  39. if (adev->irq.ih.ring_obj == NULL) {
  40. r = amdgpu_bo_create(adev, adev->irq.ih.ring_size,
  41. PAGE_SIZE, true,
  42. AMDGPU_GEM_DOMAIN_GTT, 0,
  43. NULL, NULL, &adev->irq.ih.ring_obj);
  44. if (r) {
  45. DRM_ERROR("amdgpu: failed to create ih ring buffer (%d).\n", r);
  46. return r;
  47. }
  48. r = amdgpu_bo_reserve(adev->irq.ih.ring_obj, false);
  49. if (unlikely(r != 0))
  50. return r;
  51. r = amdgpu_bo_pin(adev->irq.ih.ring_obj,
  52. AMDGPU_GEM_DOMAIN_GTT,
  53. &adev->irq.ih.gpu_addr);
  54. if (r) {
  55. amdgpu_bo_unreserve(adev->irq.ih.ring_obj);
  56. DRM_ERROR("amdgpu: failed to pin ih ring buffer (%d).\n", r);
  57. return r;
  58. }
  59. r = amdgpu_bo_kmap(adev->irq.ih.ring_obj,
  60. (void **)&adev->irq.ih.ring);
  61. amdgpu_bo_unreserve(adev->irq.ih.ring_obj);
  62. if (r) {
  63. DRM_ERROR("amdgpu: failed to map ih ring buffer (%d).\n", r);
  64. return r;
  65. }
  66. }
  67. return 0;
  68. }
  69. /**
  70. * amdgpu_ih_ring_init - initialize the IH state
  71. *
  72. * @adev: amdgpu_device pointer
  73. *
  74. * Initializes the IH state and allocates a buffer
  75. * for the IH ring buffer.
  76. * Returns 0 for success, errors for failure.
  77. */
  78. int amdgpu_ih_ring_init(struct amdgpu_device *adev, unsigned ring_size,
  79. bool use_bus_addr)
  80. {
  81. u32 rb_bufsz;
  82. int r;
  83. /* Align ring size */
  84. rb_bufsz = order_base_2(ring_size / 4);
  85. ring_size = (1 << rb_bufsz) * 4;
  86. adev->irq.ih.ring_size = ring_size;
  87. adev->irq.ih.ptr_mask = adev->irq.ih.ring_size - 1;
  88. adev->irq.ih.rptr = 0;
  89. adev->irq.ih.use_bus_addr = use_bus_addr;
  90. if (adev->irq.ih.use_bus_addr) {
  91. if (!adev->irq.ih.ring) {
  92. /* add 8 bytes for the rptr/wptr shadows and
  93. * add them to the end of the ring allocation.
  94. */
  95. adev->irq.ih.ring = pci_alloc_consistent(adev->pdev,
  96. adev->irq.ih.ring_size + 8,
  97. &adev->irq.ih.rb_dma_addr);
  98. if (adev->irq.ih.ring == NULL)
  99. return -ENOMEM;
  100. memset((void *)adev->irq.ih.ring, 0, adev->irq.ih.ring_size + 8);
  101. adev->irq.ih.wptr_offs = (adev->irq.ih.ring_size / 4) + 0;
  102. adev->irq.ih.rptr_offs = (adev->irq.ih.ring_size / 4) + 1;
  103. }
  104. return 0;
  105. } else {
  106. r = amdgpu_wb_get(adev, &adev->irq.ih.wptr_offs);
  107. if (r) {
  108. dev_err(adev->dev, "(%d) ih wptr_offs wb alloc failed\n", r);
  109. return r;
  110. }
  111. r = amdgpu_wb_get(adev, &adev->irq.ih.rptr_offs);
  112. if (r) {
  113. amdgpu_wb_free(adev, adev->irq.ih.wptr_offs);
  114. dev_err(adev->dev, "(%d) ih rptr_offs wb alloc failed\n", r);
  115. return r;
  116. }
  117. return amdgpu_ih_ring_alloc(adev);
  118. }
  119. }
  120. /**
  121. * amdgpu_ih_ring_fini - tear down the IH state
  122. *
  123. * @adev: amdgpu_device pointer
  124. *
  125. * Tears down the IH state and frees buffer
  126. * used for the IH ring buffer.
  127. */
  128. void amdgpu_ih_ring_fini(struct amdgpu_device *adev)
  129. {
  130. int r;
  131. if (adev->irq.ih.use_bus_addr) {
  132. if (adev->irq.ih.ring) {
  133. /* add 8 bytes for the rptr/wptr shadows and
  134. * add them to the end of the ring allocation.
  135. */
  136. pci_free_consistent(adev->pdev, adev->irq.ih.ring_size + 8,
  137. (void *)adev->irq.ih.ring,
  138. adev->irq.ih.rb_dma_addr);
  139. adev->irq.ih.ring = NULL;
  140. }
  141. } else {
  142. if (adev->irq.ih.ring_obj) {
  143. r = amdgpu_bo_reserve(adev->irq.ih.ring_obj, false);
  144. if (likely(r == 0)) {
  145. amdgpu_bo_kunmap(adev->irq.ih.ring_obj);
  146. amdgpu_bo_unpin(adev->irq.ih.ring_obj);
  147. amdgpu_bo_unreserve(adev->irq.ih.ring_obj);
  148. }
  149. amdgpu_bo_unref(&adev->irq.ih.ring_obj);
  150. adev->irq.ih.ring = NULL;
  151. adev->irq.ih.ring_obj = NULL;
  152. }
  153. amdgpu_wb_free(adev, adev->irq.ih.wptr_offs);
  154. amdgpu_wb_free(adev, adev->irq.ih.rptr_offs);
  155. }
  156. }
  157. /**
  158. * amdgpu_ih_process - interrupt handler
  159. *
  160. * @adev: amdgpu_device pointer
  161. *
  162. * Interrupt hander (VI), walk the IH ring.
  163. * Returns irq process return code.
  164. */
  165. int amdgpu_ih_process(struct amdgpu_device *adev)
  166. {
  167. struct amdgpu_iv_entry entry;
  168. u32 wptr;
  169. if (!adev->irq.ih.enabled || adev->shutdown)
  170. return IRQ_NONE;
  171. wptr = amdgpu_ih_get_wptr(adev);
  172. restart_ih:
  173. /* is somebody else already processing irqs? */
  174. if (atomic_xchg(&adev->irq.ih.lock, 1))
  175. return IRQ_NONE;
  176. DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, adev->irq.ih.rptr, wptr);
  177. /* Order reading of wptr vs. reading of IH ring data */
  178. rmb();
  179. while (adev->irq.ih.rptr != wptr) {
  180. u32 ring_index = adev->irq.ih.rptr >> 2;
  181. /* Before dispatching irq to IP blocks, send it to amdkfd */
  182. amdgpu_amdkfd_interrupt(adev,
  183. (const void *) &adev->irq.ih.ring[ring_index]);
  184. entry.iv_entry = (const uint32_t *)
  185. &adev->irq.ih.ring[ring_index];
  186. amdgpu_ih_decode_iv(adev, &entry);
  187. adev->irq.ih.rptr &= adev->irq.ih.ptr_mask;
  188. amdgpu_irq_dispatch(adev, &entry);
  189. }
  190. amdgpu_ih_set_rptr(adev);
  191. atomic_set(&adev->irq.ih.lock, 0);
  192. /* make sure wptr hasn't changed while processing */
  193. wptr = amdgpu_ih_get_wptr(adev);
  194. if (wptr != adev->irq.ih.rptr)
  195. goto restart_ih;
  196. return IRQ_HANDLED;
  197. }