amdgpu_ih.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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, &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 = kzalloc(adev->irq.ih.ring_size + 8, GFP_KERNEL);
  96. if (adev->irq.ih.ring == NULL)
  97. return -ENOMEM;
  98. adev->irq.ih.rb_dma_addr = pci_map_single(adev->pdev,
  99. (void *)adev->irq.ih.ring,
  100. adev->irq.ih.ring_size,
  101. PCI_DMA_BIDIRECTIONAL);
  102. if (pci_dma_mapping_error(adev->pdev, adev->irq.ih.rb_dma_addr)) {
  103. dev_err(&adev->pdev->dev, "Failed to DMA MAP the IH RB page\n");
  104. kfree((void *)adev->irq.ih.ring);
  105. return -ENOMEM;
  106. }
  107. adev->irq.ih.wptr_offs = (adev->irq.ih.ring_size / 4) + 0;
  108. adev->irq.ih.rptr_offs = (adev->irq.ih.ring_size / 4) + 1;
  109. }
  110. return 0;
  111. } else {
  112. r = amdgpu_wb_get(adev, &adev->irq.ih.wptr_offs);
  113. if (r) {
  114. dev_err(adev->dev, "(%d) ih wptr_offs wb alloc failed\n", r);
  115. return r;
  116. }
  117. r = amdgpu_wb_get(adev, &adev->irq.ih.rptr_offs);
  118. if (r) {
  119. amdgpu_wb_free(adev, adev->irq.ih.wptr_offs);
  120. dev_err(adev->dev, "(%d) ih rptr_offs wb alloc failed\n", r);
  121. return r;
  122. }
  123. return amdgpu_ih_ring_alloc(adev);
  124. }
  125. }
  126. /**
  127. * amdgpu_ih_ring_fini - tear down the IH state
  128. *
  129. * @adev: amdgpu_device pointer
  130. *
  131. * Tears down the IH state and frees buffer
  132. * used for the IH ring buffer.
  133. */
  134. void amdgpu_ih_ring_fini(struct amdgpu_device *adev)
  135. {
  136. int r;
  137. if (adev->irq.ih.use_bus_addr) {
  138. if (adev->irq.ih.ring) {
  139. /* add 8 bytes for the rptr/wptr shadows and
  140. * add them to the end of the ring allocation.
  141. */
  142. pci_unmap_single(adev->pdev, adev->irq.ih.rb_dma_addr,
  143. adev->irq.ih.ring_size + 8, PCI_DMA_BIDIRECTIONAL);
  144. kfree((void *)adev->irq.ih.ring);
  145. adev->irq.ih.ring = NULL;
  146. }
  147. } else {
  148. if (adev->irq.ih.ring_obj) {
  149. r = amdgpu_bo_reserve(adev->irq.ih.ring_obj, false);
  150. if (likely(r == 0)) {
  151. amdgpu_bo_kunmap(adev->irq.ih.ring_obj);
  152. amdgpu_bo_unpin(adev->irq.ih.ring_obj);
  153. amdgpu_bo_unreserve(adev->irq.ih.ring_obj);
  154. }
  155. amdgpu_bo_unref(&adev->irq.ih.ring_obj);
  156. adev->irq.ih.ring = NULL;
  157. adev->irq.ih.ring_obj = NULL;
  158. }
  159. amdgpu_wb_free(adev, adev->irq.ih.wptr_offs);
  160. amdgpu_wb_free(adev, adev->irq.ih.rptr_offs);
  161. }
  162. }
  163. /**
  164. * amdgpu_ih_process - interrupt handler
  165. *
  166. * @adev: amdgpu_device pointer
  167. *
  168. * Interrupt hander (VI), walk the IH ring.
  169. * Returns irq process return code.
  170. */
  171. int amdgpu_ih_process(struct amdgpu_device *adev)
  172. {
  173. struct amdgpu_iv_entry entry;
  174. u32 wptr;
  175. if (!adev->irq.ih.enabled || adev->shutdown)
  176. return IRQ_NONE;
  177. wptr = amdgpu_ih_get_wptr(adev);
  178. restart_ih:
  179. /* is somebody else already processing irqs? */
  180. if (atomic_xchg(&adev->irq.ih.lock, 1))
  181. return IRQ_NONE;
  182. DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, adev->irq.ih.rptr, wptr);
  183. /* Order reading of wptr vs. reading of IH ring data */
  184. rmb();
  185. while (adev->irq.ih.rptr != wptr) {
  186. u32 ring_index = adev->irq.ih.rptr >> 2;
  187. /* Before dispatching irq to IP blocks, send it to amdkfd */
  188. amdgpu_amdkfd_interrupt(adev,
  189. (const void *) &adev->irq.ih.ring[ring_index]);
  190. entry.iv_entry = (const uint32_t *)
  191. &adev->irq.ih.ring[ring_index];
  192. amdgpu_ih_decode_iv(adev, &entry);
  193. adev->irq.ih.rptr &= adev->irq.ih.ptr_mask;
  194. amdgpu_irq_dispatch(adev, &entry);
  195. }
  196. amdgpu_ih_set_rptr(adev);
  197. atomic_set(&adev->irq.ih.lock, 0);
  198. /* make sure wptr hasn't changed while processing */
  199. wptr = amdgpu_ih_get_wptr(adev);
  200. if (wptr != adev->irq.ih.rptr)
  201. goto restart_ih;
  202. return IRQ_HANDLED;
  203. }